-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJavaScript 4 Dummies.xml
407 lines (407 loc) · 44.8 KB
/
JavaScript 4 Dummies.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
<templateSet group="JavaScript 4 Dummies">
<template name="jsd_all" value="/*This snippet returns true if the predicate function returns true for all elements in a collection and false otherwise. You can omit the second argument fnif you want to use Booleanas a default value.*/ const all = (arr, fn = Boolean) => arr.every(fn); /* all([4, 2, 3], x => x > 1); // true all([1, 2, 3]); // true */" description="This snippet returns true if the predicate function returns true for all elements in a collection and false otherwise. You can omit the second argument fnif you want to use Booleanas a default value." toReformat="false" toShortenFQNames="true">
<context>
<option name="JAVA_SCRIPT" value="true" />
</context>
</template>
<template name="jsd_allEqual" value="/*This snippet checks whether all elements of the array are equal.*/ const allEqual = arr => arr.every(val => val === arr[0]); /* allEqual([1, 2, 3, 4, 5, 6]); // false allEqual([1, 1, 1, 1]); // true */" description="This snippet checks whether all elements of the array are equal." toReformat="false" toShortenFQNames="true">
<context>
<option name="JAVA_SCRIPT" value="true" />
</context>
</template>
<template name="jsd_aprxEqual" value="/*This snippet checks whether two numbers are approximately equal to each other, with a small difference.*/ const approximatelyEqual = (v1, v2, epsilon = 0.001) => Math.abs(v1 - v2) < epsilon; /* approximatelyEqual(Math.PI / 2.0, 1.5708); // true */" description="This snippet checks whether two numbers are approximately equal to each other, with a small difference." toReformat="false" toShortenFQNames="true">
<context>
<option name="JAVA_SCRIPT" value="true" />
</context>
</template>
<template name="jsd_arrayToCSV" value="/*This snippet converts the elements that don’t have commas or double quotes to strings with comma-separated values.*/ const arrayToCSV = (arr, delimiter = ',') => arr.map(v => v.map(x => `"${x}"`).join(delimiter)).join('\n'); /* arrayToCSV([['a', 'b'], ['c', 'd']]); // '"a","b"\n"c","d"' arrayToCSV([['a', 'b'], ['c', 'd']], ';'); // '"a";"b"\n"c";"d"' */" description="This snippet converts the elements that don’t have commas or double quotes to strings with comma-separated values." toReformat="false" toShortenFQNames="true">
<context>
<option name="JAVA_SCRIPT" value="true" />
</context>
</template>
<template name="jsd_average" value="/*This snippet returns the average of two or more numerical values.*/ const average = (...nums) => nums.reduce((acc, val) => acc + val, 0) / nums.length; /* average(...[1, 2, 3]); // 2 average(1, 2, 3); // 2 */" description="This snippet returns the average of two or more numerical values." toReformat="false" toShortenFQNames="true">
<context>
<option name="JAVA_SCRIPT" value="true" />
</context>
</template>
<template name="jsd_averageBy" value="/*This snippet returns the average of an array after initially doing the mapping of each element to a value using a given function.*/ const averageBy = (arr, fn) => arr.map(typeof fn === 'function' ? fn : val => val[fn]).reduce((acc, val) => acc + val, 0) / arr.length; /* averageBy([{ n: 4 }, { n: 2 }, { n: 8 }, { n: 6 }], o => o.n); // 5 averageBy([{ n: 4 }, { n: 2 }, { n: 8 }, { n: 6 }], 'n'); // 5 */" description="This snippet returns the average of an array after initially doing the mapping of each element to a value using a given function." toReformat="false" toShortenFQNames="true">
<context>
<option name="JAVA_SCRIPT" value="true" />
</context>
</template>
<template name="jsd_bifurcate" value="/*This snippet splits values into two groups and then puts a truthy element of filterin the first group, and in the second group otherwise. You can use Array.prototype.reduce()and Array.prototype.push()to add elements to groups based on filter.*/ const bifurcate = (arr, filter) => arr.reduce((acc, val, i) => (acc[filter[i] ? 0 : 1].push(val), acc), [[], []]); /* bifurcate(['beep', 'boop', 'foo', 'bar'], [true, true, false, true]); // [ ['beep', 'boop', 'bar'], ['foo'] ] */" description="This snippet splits values into two groups and then puts a truthy element of filterin the first group, and in the second group otherwise." toReformat="false" toShortenFQNames="true">
<context>
<option name="JAVA_SCRIPT" value="true" />
</context>
</template>
<template name="jsd_bifurcateBy" value="/*This snippet splits values into two groups, based on a predicate function. If the predicate function returns a truthy value, the element will be placed in the first group. Otherwise, it will be placed in the second group. You can use Array.prototype.reduce()and Array.prototype.push()to add elements to groups, based on the value returned by fnfor each element.*/ const bifurcateBy = (arr, fn) => arr.reduce((acc, val, i) => (acc[fn(val, i) ? 0 : 1].push(val), acc), [[], []]); /* bifurcateBy(['beep', 'boop', 'foo', 'bar'], x => x[0] === 'b'); // [ ['beep', 'boop', 'bar'], ['foo'] ] */" description="This snippet splits values into two groups, based on a predicate function. If the predicate function returns a truthy value, the element will be placed in the first group. Otherwise, it will be placed in the second group." toReformat="false" toShortenFQNames="true">
<context>
<option name="JAVA_SCRIPT" value="true" />
</context>
</template>
<template name="jsd_byteSize" value="/*This snippet returns the length of a string in bytes.*/ const byteSize = str => new Blob([str]).size; /* byteSize('😀'); // 4 byteSize('Hello World'); // 11 */" description="This snippet returns the length of a string in bytes." toReformat="false" toShortenFQNames="true">
<context>
<option name="JAVA_SCRIPT" value="true" />
</context>
</template>
<template name="jsd_capitalize" value="/*This snippet capitalizes the first letter of a string.*/ const capitalize = ([first, ...rest]) => first.toUpperCase() + rest.join(''); /* capitalize('fooBar'); // 'FooBar' capitalize('fooBar', true); // 'FooBar' */" description="This snippet capitalizes the first letter of a string." toReformat="false" toShortenFQNames="true">
<context>
<option name="JAVA_SCRIPT" value="true" />
</context>
</template>
<template name="jsd_capitalizeEveryWord" value="/*This snippet capitalizes the first letter of every word in a given string.*/ const capitalizeEveryWord = str => str.replace(/\b[a-z]/g, char => char.toUpperCase()); /* capitalizeEveryWord('hello world!'); // 'Hello World!' */" description="This snippet capitalizes the first letter of every word in a given string." toReformat="false" toShortenFQNames="true">
<context>
<option name="JAVA_SCRIPT" value="true" />
</context>
</template>
<template name="jsd_castArray" value="/*This snippet converts a non-array value into array.*/ const castArray = val => (Array.isArray(val) ? val : [val]); /* castArray('foo'); // ['foo'] castArray([1]); // [1] */" description="This snippet converts a non-array value into array." toReformat="false" toShortenFQNames="true">
<context>
<option name="JAVA_SCRIPT" value="true" />
</context>
</template>
<template name="jsd_compact" value="/*This snippet removes false values from an array.*/ const compact = arr => arr.filter(Boolean); /* compact([0, 1, false, 2, '', 3, 'a', 'e' * 23, NaN, 's', 34]); // [ 1, 2, 3, 'a', 's', 34 ] */" description="This snippet removes false values from an array." toReformat="false" toShortenFQNames="true">
<context>
<option name="JAVA_SCRIPT" value="true" />
</context>
</template>
<template name="jsd_countOccurrences" value="/*This snippet counts the occurrences of a value in an array.*/ const countOccurrences = (arr, val) => arr.reduce((a, v) => (v === val ? a + 1 : a), 0); /* countOccurrences([1, 1, 2, 1, 2, 3], 1); // 3 */" description="This snippet counts the occurrences of a value in an array." toReformat="false" toShortenFQNames="true">
<context>
<option name="JAVA_SCRIPT" value="true" />
</context>
</template>
<template name="jsd_createDirectory" value="/*This snippet uses existsSync() to check whether a directory exists and then mkdirSync() to create it if it doesn’t.*/ const fs = require('fs'); const createDirIfNotExists = dir => (!fs.existsSync(dir) ? fs.mkdirSync(dir) : undefined); createDirIfNotExists('test'); // creates the directory 'test', if it doesn't exist" description="This snippet uses existsSync() to check whether a directory exists and then mkdirSync() to create it if it doesn’t." toReformat="false" toShortenFQNames="true">
<context>
<option name="JAVA_SCRIPT" value="true" />
</context>
</template>
<template name="jsd_dayOfYear" value="/*This snippet gets the day of the year from a Dateobject.*/ const dayOfYear = date => Math.floor((date - new Date(date.getFullYear(), 0, 0)) / 1000 / 60 / 60 / 24); /* dayOfYear(new Date()); // 272 */" description="This snippet gets the day of the year from a Dateobject." toReformat="false" toShortenFQNames="true">
<context>
<option name="JAVA_SCRIPT" value="true" />
</context>
</template>
<template name="jsd_decapitalize" value="/*This snippet turns the first letter of a string into lowercase.*/ const decapitalize = ([first, ...rest]) => first.toLowerCase() + rest.join('') /* decapitalize('FooBar'); // 'fooBar' decapitalize('FooBar'); // 'fooBar */" description="This snippet turns the first letter of a string into lowercase." toReformat="false" toShortenFQNames="true">
<context>
<option name="JAVA_SCRIPT" value="true" />
</context>
</template>
<template name="jsd_deepFlatten" value="/*This snippet flattens an array recursively.*/ const deepFlatten = arr => [].concat(...arr.map(v => (Array.isArray(v) ? deepFlatten(v) : v))); /* deepFlatten([1, [2], [[3], 4], 5]); // [1,2,3,4,5] */" description="This snippet flattens an array recursively." toReformat="false" toShortenFQNames="true">
<context>
<option name="JAVA_SCRIPT" value="true" />
</context>
</template>
<template name="jsd_default" value="/*This snippet assigns default values for all properties in an object that are undefined.*/ const defaults = (obj, ...defs) => Object.assign({}, obj, ...defs.reverse(), obj); /* defaults({ a: 1 }, { b: 2 }, { b: 6 }, { a: 3 }); // { a: 1, b: 2 } */" description="This snippet assigns default values for all properties in an object that are undefined." toReformat="false" toShortenFQNames="true">
<context>
<option name="JAVA_SCRIPT" value="true" />
</context>
</template>
<template name="jsd_defer" value="/*This snippet delays the execution of a function until the current call stack is cleared.*/ const defer = (fn, ...args) => setTimeout(fn, 1, ...args); /* defer(console.log, 'a'), console.log('b'); // logs 'b' then 'a' */" description="This snippet delays the execution of a function until the current call stack is cleared." toReformat="false" toShortenFQNames="true">
<context>
<option name="JAVA_SCRIPT" value="true" />
</context>
</template>
<template name="jsd_degToRads" value="/*This code snippet can be used to convert a value from degrees to radians.*/ const degreesToRads = deg => (deg * Math.PI) / 180.0; /* degreesToRads(90.0); // ~1.5708 */" description="This code snippet can be used to convert a value from degrees to radians." toReformat="false" toShortenFQNames="true">
<context>
<option name="JAVA_SCRIPT" value="true" />
</context>
</template>
<template name="jsd_difference" value="/*This snippet finds the difference between two arrays.*/ const difference = (a, b) => { const s = new Set(b); return a.filter(x => !s.has(x)); }; /* difference([1, 2, 3], [1, 2, 4]); // [3] */" description="This snippet finds the difference between two arrays." toReformat="false" toShortenFQNames="true">
<context>
<option name="JAVA_SCRIPT" value="true" />
</context>
</template>
<template name="jsd_differenceBy" value="/*This method returns the difference between two arrays, after applying a given function to each element of both lists.*/ const differenceBy = (a, b, fn) => { const s = new Set(b.map(fn)); return a.filter(x => !s.has(fn(x))); }; /* differenceBy([2.1, 1.2], [2.3, 3.4], Math.floor); // [1.2] differenceBy([{ x: 2 }, { x: 1 }], [{ x: 1 }], v => v.x); // [ { x: 2 } ] */" description="This method returns the difference between two arrays, after applying a given function to each element of both lists." toReformat="false" toShortenFQNames="true">
<context>
<option name="JAVA_SCRIPT" value="true" />
</context>
</template>
<template name="jsd_differenceWith" value="/*This snippet removes the values for which the comparator function returns false.*/ const differenceWith = (arr, val, comp) => arr.filter(a => val.findIndex(b => comp(a, b)) === -1); /* differenceWith([1, 1.2, 1.5, 3, 0], [1.9, 3, 0], (a, b) => Math.round(a) === Math.round(b)); // [1, 1.2] */" description="This snippet removes the values for which the comparator function returns false." toReformat="false" toShortenFQNames="true">
<context>
<option name="JAVA_SCRIPT" value="true" />
</context>
</template>
<template name="jsd_digitize" value="/*This snippet gets a number as input and returns an array of its digits.*/ const digitize = n => [...`${n}`].map(i => parseInt(i)); /* digitize(431); // [4, 3, 1] */" description="This snippet gets a number as input and returns an array of its digits." toReformat="false" toShortenFQNames="true">
<context>
<option name="JAVA_SCRIPT" value="true" />
</context>
</template>
<template name="jsd_distance" value="/*This snippet returns the distance between two points by calculating the Euclidean distance.*/ const distance = (x0, y0, x1, y1) => Math.hypot(x1 - x0, y1 - y0); /* distance(1, 1, 2, 3); // 2.23606797749979 */" description="This snippet returns the distance between two points by calculating the Euclidean distance." toReformat="false" toShortenFQNames="true">
<context>
<option name="JAVA_SCRIPT" value="true" />
</context>
</template>
<template name="jsd_dropElements" value="/*This snippet returns a new array with n elements removed from the left.*/ const drop = (arr, n = 1) => arr.slice(n); /* drop([1, 2, 3]); // [2,3] drop([1, 2, 3], 2); // [3] drop([1, 2, 3], 42); // [] */" description="This snippet returns a new array with n elements removed from the left." toReformat="false" toShortenFQNames="true">
<context>
<option name="JAVA_SCRIPT" value="true" />
</context>
</template>
<template name="jsd_dropRight" value="/*This snippet returns a new array with n elements removed from the right.*/ const dropRight = (arr, n = 1) => arr.slice(0, -n); /* dropRight([1, 2, 3]); // [1,2] dropRight([1, 2, 3], 2); // [1] dropRight([1, 2, 3], 42); // [] */" description="This snippet returns a new array with n elements removed from the right." toReformat="false" toShortenFQNames="true">
<context>
<option name="JAVA_SCRIPT" value="true" />
</context>
</template>
<template name="jsd_dropRightWhile" value="/*This snippet removes elements from the right side of an array until the passed function returns true.*/ const dropRightWhile = (arr, func) => { while (arr.length > 0 && !func(arr[arr.length - 1])) arr = arr.slice(0, -1); return arr; }; /* dropRightWhile([1, 2, 3, 4], n => n < 3); // [1, 2] */" description="This snippet removes elements from the right side of an array until the passed function returns true." toReformat="false" toShortenFQNames="true">
<context>
<option name="JAVA_SCRIPT" value="true" />
</context>
</template>
<template name="jsd_dropWhile" value="/*This snippet removes elements from an array until the passed function returns true.*/ const dropWhile = (arr, func) => { while (arr.length > 0 && !func(arr[0])) arr = arr.slice(1); return arr; }; /* dropWhile([1, 2, 3, 4], n => n >= 3); // [3,4] */" description="This snippet removes elements from an array until the passed function returns true." toReformat="false" toShortenFQNames="true">
<context>
<option name="JAVA_SCRIPT" value="true" />
</context>
</template>
<template name="jsd_filterDupElm" value="/*This snippet removes duplicate values in an array.*/ const filterNonUnique = arr => [ …new Set(arr)]; /* filterNonUnique([1, 2, 2, 3, 4, 4, 5]); // [1, 2, 3, 4, 5] */" description="This snippet removes duplicate values in an array." toReformat="false" toShortenFQNames="true">
<context>
<option name="JAVA_SCRIPT" value="true" />
</context>
</template>
<template name="jsd_findKey" value="/*This snippet returns the first key that satisfies a given function.*/ const findKey = (obj, fn) => Object.keys(obj).find(key => fn(obj[key], key, obj)); /* findKey( { barney: { age: 36, active: true }, fred: { age: 40, active: false }, pebbles: { age: 1, active: true } }, o => o['active'] ); // 'barney' */" description="This snippet returns the first key that satisfies a given function." toReformat="false" toShortenFQNames="true">
<context>
<option name="JAVA_SCRIPT" value="true" />
</context>
</template>
<template name="jsd_findLast" value="/*This snippet returns the last element for which a given function returns a truthy value.*/ const findLast = (arr, fn) => arr.filter(fn).pop(); /* findLast([1, 2, 3, 4], n => n % 2 === 1); // 3 */" description="This snippet returns the last element for which a given function returns a truthy value." toReformat="false" toShortenFQNames="true">
<context>
<option name="JAVA_SCRIPT" value="true" />
</context>
</template>
<template name="jsd_flatten" value="/*This snippet flattens an array up to a specified depth using recursion.*/ const flatten = (arr, depth = 1) => arr.reduce((a, v) => a.concat(depth > 1 && Array.isArray(v) ? flatten(v, depth - 1) : v), []); /* flatten([1, [2], 3, 4]); // [1, 2, 3, 4] flatten([1, [2, [3, [4, 5], 6], 7], 8], 2); // [1, 2, 3, [4, 5], 6, 7, 8] */" description="This snippet flattens an array up to a specified depth using recursion." toReformat="false" toShortenFQNames="true">
<context>
<option name="JAVA_SCRIPT" value="true" />
</context>
</template>
<template name="jsd_forEachRight" value="/*This snippet executes a function for each element of an array starting from the array’s last element.*/ const forEachRight = (arr, callback) => arr .slice(0) .reverse() .forEach(callback); /* forEachRight([1, 2, 3, 4], val => console.log(val)); // '4', '3', '2', '1' */" description="This snippet executes a function for each element of an array starting from the array’s last element." toReformat="false" toShortenFQNames="true">
<context>
<option name="JAVA_SCRIPT" value="true" />
</context>
</template>
<template name="jsd_forOwn" value="/*This snippet iterates on each property of an object and iterates a callback for each one respectively.*/ const forOwn = (obj, fn) => Object.keys(obj).forEach(key => fn(obj[key], key, obj)); /* forOwn({ foo: 'bar', a: 1 }, v => console.log(v)); // 'bar', 1 */" description="This snippet iterates on each property of an object and iterates a callback for each one respectively." toReformat="false" toShortenFQNames="true">
<context>
<option name="JAVA_SCRIPT" value="true" />
</context>
</template>
<template name="jsd_funcName" value="/*This snippet prints the name of a function into the console.*/ const functionName = fn => (console.debug(fn.name), fn); /* functionName(Math.max); // max (logged in debug channel of console) */" description="This snippet prints the name of a function into the console." toReformat="false" toShortenFQNames="true">
<context>
<option name="JAVA_SCRIPT" value="true" />
</context>
</template>
<template name="jsd_getTime" value="/*This snippet can be used to get the time from a Dateobject as a string.*/ const getColonTimeFromDate = date => date.toTimeString().slice(0, 8); /* getColonTimeFromDate(new Date()); // "08:38:00" */" description="This snippet can be used to get the time from a Dateobject as a string." toReformat="false" toShortenFQNames="true">
<context>
<option name="JAVA_SCRIPT" value="true" />
</context>
</template>
<template name="jsd_getDayBeetwenDates" value="/*This snippet can be used to find the difference in days between two dates.*/ const getDaysDiffBetweenDates = (dateInitial, dateFinal) => (dateFinal - dateInitial) / (1000 * 3600 * 24); /* getDaysDiffBetweenDates(new Date('2019-01-13'), new Date('2019-01-15')); // 2 */" description="This snippet can be used to find the difference in days between two dates." toReformat="false" toShortenFQNames="true">
<context>
<option name="JAVA_SCRIPT" value="true" />
</context>
</template>
<template name="jsd_getType" value="/*This snippet can be used to get the type of a value.*/ const getType = v => v === undefined ? 'undefined' : v === null ? 'null' : v.constructor.name.toLowerCase(); /* getType(new Set([1, 2, 3])); // 'set' */" description="This snippet can be used to get the type of a value." toReformat="false" toShortenFQNames="true">
<context>
<option name="JAVA_SCRIPT" value="true" />
</context>
</template>
<template name="jsd_headOfList" value="/*This snippet returns the head of a list.*/ const head = arr => arr[0]; /* head([1, 2, 3]); // 1 */" description="This snippet returns the head of a list." toReformat="false" toShortenFQNames="true">
<context>
<option name="JAVA_SCRIPT" value="true" />
</context>
</template>
<template name="jsd_indexOfAll" value="/*This snippet can be used to get all indexes of a value in an array, which returns an empty array, in case this value is not included in it.*/ const indexOfAll = (arr, val) => arr.reduce((acc, el, i) => (el === val ? [...acc, i] : acc), []); /* indexOfAll([1, 2, 3, 1, 2, 3], 1); // [0,3] indexOfAll([1, 2, 3], 4); // [] */" description="This snippet can be used to get all indexes of a value in an array, which returns an empty array, in case this value is not included in it." toReformat="false" toShortenFQNames="true">
<context>
<option name="JAVA_SCRIPT" value="true" />
</context>
</template>
<template name="jsd_initial" value="/*This snippet returns all elements of an array except the last one.*/ const initial = arr => arr.slice(0, -1); /* initial([1, 2, 3]); // [1,2]const initial = arr => arr.slice(0, -1); initial([1, 2, 3]); // [1,2] */" description="This snippet returns all elements of an array except the last one." toReformat="false" toShortenFQNames="true">
<context>
<option name="JAVA_SCRIPT" value="true" />
</context>
</template>
<template name="jsd_intersection" value="/*This snippet can be used to get an array with elements that are included in two other arrays.*/ const intersection = (a, b) => { const s = new Set(b); return a.filter(x => s.has(x)); }; /* intersection([1, 2, 3], [4, 3, 2]); // [2, 3] */" description="This snippet can be used to get an array with elements that are included in two other arrays." toReformat="false" toShortenFQNames="true">
<context>
<option name="JAVA_SCRIPT" value="true" />
</context>
</template>
<template name="jsd_intersectionBy" value="/*This snippet can be used to return a list of elements that exist in both arrays, after a particular function has been executed to each element of both arrays.*/ const intersectionBy = (a, b, fn) => { const s = new Set(b.map(fn)); return a.filter(x => s.has(fn(x))); }; /* intersectionBy([2.1, 1.2], [2.3, 3.4], Math.floor); // [2.1] */" description="This snippet can be used to return a list of elements that exist in both arrays, after a particular function has been executed to each element of both arrays." toReformat="false" toShortenFQNames="true">
<context>
<option name="JAVA_SCRIPT" value="true" />
</context>
</template>
<template name="jsd_is" value="/*This snippet can be used to check if a value is of a particular type.*/ const is = (type, val) => ![, null].includes(val) && val.constructor === type; /* is(Array, [1]); // true is(ArrayBuffer, new ArrayBuffer()); // true is(Map, new Map()); // true is(RegExp, /./g); // true is(Set, new Set()); // true is(WeakMap, new WeakMap()); // true is(WeakSet, new WeakSet()); // true is(String, ''); // true is(String, new String('')); // true is(Number, 1); // true is(Number, new Number(1)); // true is(Boolean, true); // true is(Boolean, new Boolean(true)); // true */" description="This snippet can be used to check if a value is of a particular type." toReformat="false" toShortenFQNames="true">
<context>
<option name="JAVA_SCRIPT" value="true" />
</context>
</template>
<template name="jsd_isAfterDate" value="/*This snippet can be used to check whether a date is after another date.*/ const isAfterDate = (dateA, dateB) => dateA > dateB; /* isAfterDate(new Date(2010, 10, 21), new Date(2010, 10, 20)); // true */" description="This snippet can be used to check whether a date is after another date." toReformat="false" toShortenFQNames="true">
<context>
<option name="JAVA_SCRIPT" value="true" />
</context>
</template>
<template name="jsd_isAnagram" value="/*This snippet can be used to check whether a particular string is an anagram with another string.*/ const isAnagram = (str1, str2) => { const normalize = str => str .toLowerCase() .replace(/[^a-z0-9]/gi, '') .split('') .sort() .join(''); return normalize(str1) === normalize(str2); }; /* isAnagram('iceman', 'cinema'); // true */" description="This snippet can be used to check whether a particular string is an anagram with another string." toReformat="false" toShortenFQNames="true">
<context>
<option name="JAVA_SCRIPT" value="true" />
</context>
</template>
<template name="jsd_isArrayLike" value="/*This snippet can be used to check if a provided argument is iterable like an array.*/ const isArrayLike = obj => obj != null && typeof obj[Symbol.iterator] === 'function'; /* isArrayLike(document.querySelectorAll('.className')); // true isArrayLike('abc'); // true isArrayLike(null); // false */" description="This snippet can be used to check if a provided argument is iterable like an array." toReformat="false" toShortenFQNames="true">
<context>
<option name="JAVA_SCRIPT" value="true" />
</context>
</template>
<template name="jsd_isBeforeDate" value="/*This snippet can be used to check whether a date is before another date.*/ const isBeforeDate = (dateA, dateB) => dateA < dateB; /* isBeforeDate(new Date(2010, 10, 20), new Date(2010, 10, 21)); // true */" description="This snippet can be used to check whether a date is before another date." toReformat="false" toShortenFQNames="true">
<context>
<option name="JAVA_SCRIPT" value="true" />
</context>
</template>
<template name="jsd_isBoolean" value="/*This snippet can be used to check whether an argument is a boolean.*/ const isBoolean = val => typeof val === 'boolean'; /* isBoolean(null); // false isBoolean(false); // true */" description="This snippet can be used to check whether an argument is a boolean." toReformat="false" toShortenFQNames="true">
<context>
<option name="JAVA_SCRIPT" value="true" />
</context>
</template>
<template name="jsd_isLowerCase" value="/*This snippet can be used to determine whether a string is lower case.*/ const isLowerCase = str => str === str.toLowerCase(); /* isLowerCase('abc'); // true isLowerCase('a3@$'); // true isLowerCase('Ab4'); // false */" description="This snippet can be used to determine whether a string is lower case." toReformat="false" toShortenFQNames="true">
<context>
<option name="JAVA_SCRIPT" value="true" />
</context>
</template>
<template name="jsd_isNil" value="/*This snippet can be used to check whether a value is null or undefined.*/ const isNil = val => val === undefined || val === null; /* isNil(null); // true isNil(undefined); // true */" description="This snippet can be used to check whether a value is null or undefined." toReformat="false" toShortenFQNames="true">
<context>
<option name="JAVA_SCRIPT" value="true" />
</context>
</template>
<template name="jsd_isNull" value="/*This snippet can be used to check whether a value is null.*/ const isNull = val => val === null; /* isNull(null); // true */" description="This snippet can be used to check whether a value is null." toReformat="false" toShortenFQNames="true">
<context>
<option name="JAVA_SCRIPT" value="true" />
</context>
</template>
<template name="jsd_isNumber" value="/*This snippet can be used to check whether a provided value is a number.*/ function isNumber(n) { return !isNaN(parseFloat(n)) && isFinite(n); } /* isNumber('1'); // false isNumber(1); // true */" description="This snippet can be used to check whether a provided value is a number." toReformat="false" toShortenFQNames="true">
<context>
<option name="JAVA_SCRIPT" value="true" />
</context>
</template>
<template name="jsd_isObject" value="/*This snippet can be used to check whether a provided value is an object. It uses the Object constructor to create an object wrapper for the given value. If it is already an object, then an object type that corresponds to the given value will be returned. Otherwise, a new object will be returned.*/ const isObject = obj => obj === Object(obj); /* isObject([1, 2, 3, 4]); // true isObject([]); // true isObject(['Hello!']); // true isObject({ a: 1 }); // true isObject({}); // true isObject(true); // false */" description="This snippet can be used to check whether a provided value is an object. It uses the Object constructor to create an object wrapper for the given value." toReformat="false" toShortenFQNames="true">
<context>
<option name="JAVA_SCRIPT" value="true" />
</context>
</template>
<template name="jsd_isObjectLike" value="/*This snippet can be used to check if a value is not nulland that its typeof is “object”.*/ const isObjectLike = val => val !== null && typeof val === 'object'; /* isObjectLike({}); // true isObjectLike([1, 2, 3]); // true isObjectLike(x => x); // false isObjectLike(null); // false */" description="This snippet can be used to check if a value is not nulland that its typeof is “object”." toReformat="false" toShortenFQNames="true">
<context>
<option name="JAVA_SCRIPT" value="true" />
</context>
</template>
<template name="jsd_isPlainObject" value="/*This snippet checks whether a value is an object created by the Object constructor.*/ const isPlainObject = val => !!val && typeof val === 'object' && val.constructor === Object; /* isPlainObject({ a: 1 }); // true isPlainObject(new Map()); // false */" description="This snippet checks whether a value is an object created by the Object constructor." toReformat="false" toShortenFQNames="true">
<context>
<option name="JAVA_SCRIPT" value="true" />
</context>
</template>
<template name="jsd_isPromiseLike" value="/*This snippet checks whether an object looks like a Promise.*/ const isPromiseLike = obj => obj !== null && (typeof obj === 'object' || typeof obj === 'function') && typeof obj.then === 'function'; isPromiseLike({ then: function() { return ''; } }); // true /* isPromiseLike(null); // false isPromiseLike({}); // false */" description="This snippet checks whether an object looks like a Promise." toReformat="false" toShortenFQNames="true">
<context>
<option name="JAVA_SCRIPT" value="true" />
</context>
</template>
<template name="jsd_isSameDate" value="/*This snippet can be used to check whether two dates are equal.*/ const isSameDate = (dateA, dateB) => dateA.toISOString() === dateB.toISOString(); /* isSameDate(new Date(2010, 10, 20), new Date(2010, 10, 20)); // true */" description="This snippet can be used to check whether two dates are equal." toReformat="false" toShortenFQNames="true">
<context>
<option name="JAVA_SCRIPT" value="true" />
</context>
</template>
<template name="jsd_isString" value="/*This snippet can be used to check whether an argument is a string.*/ const isSymbol = val => typeof val === 'symbol'; /* isSymbol(Symbol('x')); // true */" description="This snippet can be used to check whether an argument is a string." toReformat="false" toShortenFQNames="true">
<context>
<option name="JAVA_SCRIPT" value="true" />
</context>
</template>
<template name="jsd_isUndefined" value="/*This snippet can be used to check whether a value is undefined.*/ const isUndefined = val => val === undefined; /* isUndefined(undefined); // true */" description="This snippet can be used to check whether a value is undefined." toReformat="false" toShortenFQNames="true">
<context>
<option name="JAVA_SCRIPT" value="true" />
</context>
</template>
<template name="jsd_isUpperCase" value="/*This snippet can be used to check whether a string is upper case.*/ const isUpperCase = str => str === str.toUpperCase(); /* isUpperCase('ABC'); // true isLowerCase('A3@$'); // true isLowerCase('aB4'); // false */" description="This snippet can be used to check whether a string is upper case." toReformat="false" toShortenFQNames="true">
<context>
<option name="JAVA_SCRIPT" value="true" />
</context>
</template>
<template name="jsd_isValidJSON" value="/*This snippet can be used to check whether a string is a valid JSON.*/ const isValidJSON = str => { try { JSON.parse(str); return true; } catch (e) { return false; } }; /* isValidJSON('{"name":"Adam","age":20}'); // true isValidJSON('{"name":"Adam",age:"20"}'); // false isValidJSON(null); // true */" description="This snippet can be used to check whether a string is a valid JSON." toReformat="false" toShortenFQNames="true">
<context>
<option name="JAVA_SCRIPT" value="true" />
</context>
</template>
<template name="jsd_last" value="/*This snippet returns the last element of an array.*/ const last = arr => arr[arr.length - 1]; /* last([1, 2, 3]); // 3 */" description="This snippet returns the last element of an array." toReformat="false" toShortenFQNames="true">
<context>
<option name="JAVA_SCRIPT" value="true" />
</context>
</template>
<template name="jsd_matchesObj" value="/*This snippet compares two objects to determine if the first one contains the same property values as the second one.*/ const matches = (obj, source) => Object.keys(source).every(key => obj.hasOwnProperty(key) && obj[key] === source[key]); /* matches({ age: 25, hair: 'long', beard: true }, { hair: 'long', beard: true }); // true matches({ hair: 'long', beard: true }, { age: 25, hair: 'long', beard: true }); // false */" description="This snippet compares two objects to determine if the first one contains the same property values as the second one." toReformat="false" toShortenFQNames="true">
<context>
<option name="JAVA_SCRIPT" value="true" />
</context>
</template>
<template name="jsd_maxDate" value="/*This snippet can be used to get the latest date.*/ const maxDate = (...dates) => new Date(Math.max.apply(null, ...dates)); /* const array = [ new Date(2017, 4, 13), new Date(2018, 2, 12), new Date(2016, 0, 10), new Date(2016, 0, 9) ]; maxDate(array); // 2018-03-11T22:00:00.000Z */" description="This snippet can be used to get the latest date." toReformat="false" toShortenFQNames="true">
<context>
<option name="JAVA_SCRIPT" value="true" />
</context>
</template>
<template name="jsd_maxN" value="/*This snippet returns the n largest elements from a list. If nis greater than or equal to the list’s length, then it will return the original list (sorted in descending order).*/ const maxN = (arr, n = 1) => [...arr].sort((a, b) => b - a).slice(0, n); /* maxN([1, 2, 3]); // [3] maxN([1, 2, 3], 2); // [3,2] */" description="This snippet returns the n largest elements from a list. If nis greater than or equal to the list’s length, then it will return the original list (sorted in descending order)." toReformat="false" toShortenFQNames="true">
<context>
<option name="JAVA_SCRIPT" value="true" />
</context>
</template>
<template name="jsd_minDate" value="/*This snippet can be used to get the earliest date.*/ const minDate = (...dates) => new Date(Math.min.apply(null, ...dates)); /* const array = [ new Date(2017, 4, 13), new Date(2018, 2, 12), new Date(2016, 0, 10), new Date(2016, 0, 9) ]; minDate(array); // 2016-01-08T22:00:00.000Z */" description="This snippet can be used to get the earliest date." toReformat="false" toShortenFQNames="true">
<context>
<option name="JAVA_SCRIPT" value="true" />
</context>
</template>
<template name="jsd_minN" value="/*This snippet returns the n smallest elements from a list. If nis greater than or equal to the list’s length, then it will return the original list (sorted in ascending order).*/ const minN = (arr, n = 1) => [...arr].sort((a, b) => a - b).slice(0, n); /* minN([1, 2, 3]); // [1] minN([1, 2, 3], 2); // [1,2] */" description="This snippet returns the n smallest elements from a list. If nis greater than or equal to the list’s length, then it will return the original list (sorted in ascending order)." toReformat="false" toShortenFQNames="true">
<context>
<option name="JAVA_SCRIPT" value="true" />
</context>
</template>
<template name="jsd_negate" value="/*This snippet can be used to apply the not operator (!) to a predicate function with its arguments.*/ const negate = func => (...args) => !func(...args); /* [1, 2, 3, 4, 5, 6].filter(negate(n => n % 2 === 0)); // [ 1, 3, 5 ] */" description="This snippet can be used to apply the not operator (!) to a predicate function with its arguments." toReformat="false" toShortenFQNames="true">
<context>
<option name="JAVA_SCRIPT" value="true" />
</context>
</template>
<template name="jsd_radsToDegrees" value="/*This snippet can be used to convert an angle from radians to degrees.*/ const radsToDegrees = rad => (rad * 180.0) / Math.PI; /* radsToDegrees(Math.PI / 2); // 90 */" description="This snippet can be used to convert an angle from radians to degrees." toReformat="false" toShortenFQNames="true">
<context>
<option name="JAVA_SCRIPT" value="true" />
</context>
</template>
<template name="jsd_randomHexColor" value="/*This snippet can be used to generate a random hexadecimal color code.*/ const randomHexColorCode = () => { let n = (Math.random() * 0xfffff * 1000000).toString(16); return '#' + n.slice(0, 6); }; /* randomHexColorCode(); // "#e34155" */" description="This snippet can be used to generate a random hexadecimal color code." toReformat="false" toShortenFQNames="true">
<context>
<option name="JAVA_SCRIPT" value="true" />
</context>
</template>
<template name="jsd_randomIntArrayInRange" value="/*This snippet can be used to generate an array with n random integers in a specified range.*/ const randomIntArrayInRange = (min, max, n = 1) => Array.from({ length: n }, () => Math.floor(Math.random() * (max - min + 1)) + min); /* randomIntArrayInRange(12, 35, 10); // [ 34, 14, 27, 17, 30, 27, 20, 26, 21, 14 ] */" description="This snippet can be used to generate an array with n random integers in a specified range." toReformat="false" toShortenFQNames="true">
<context>
<option name="JAVA_SCRIPT" value="true" />
</context>
</template>
<template name="jsd_randomNumberInRange" value="/*This snippet can be used to return a random number in a specified range.*/ const randomNumberInRange = (min, max) => Math.random() * (max - min) + min; /* randomNumberInRange(2, 10); // 6.0211363285087005 */" description="This snippet can be used to return a random number in a specified range." toReformat="false" toShortenFQNames="true">
<context>
<option name="JAVA_SCRIPT" value="true" />
</context>
</template>
<template name="jsd_readFileLines" value="/*This snippet can be used to read a file by getting an array of lines from a file.*/ const fs = require('fs'); const readFileLines = filename => fs .readFileSync(filename) .toString('UTF8') .split('\n'); let arr = readFileLines('test.txt'); console.log(arr); // ['line1', 'line2', 'line3']" description="This snippet can be used to read a file by getting an array of lines from a file." toReformat="false" toShortenFQNames="true">
<context>
<option name="JAVA_SCRIPT" value="true" />
</context>
</template>
<template name="jsd_reverse" value="/*This snippet can be used to reverse a string.*/ const reverseString = str => [...str].reverse().join(''); /* reverseString('foobar'); // 'raboof' */" description="This snippet can be used to reverse a string." toReformat="false" toShortenFQNames="true">
<context>
<option name="JAVA_SCRIPT" value="true" />
</context>
</template>
<template name="jsd_round" value="/*This snippet can be used to round a number to a specified number of digits.*/ const round = (n, decimals = 0) => Number(`${Math.round(`${n}e${decimals}`)}e-${decimals}`); /* round(1.005, 2); // 1.01 */" description="This snippet can be used to round a number to a specified number of digits." toReformat="false" toShortenFQNames="true">
<context>
<option name="JAVA_SCRIPT" value="true" />
</context>
</template>
<template name="jsd_rndGetFromArray" value="/*This snippet can be used to get a random number from an array.*/ const sample = arr => arr[Math.floor(Math.random() * arr.length)]; /* sample([3, 7, 9, 11]); // 9 */" description="This snippet can be used to get a random number from an array." toReformat="false" toShortenFQNames="true">
<context>
<option name="JAVA_SCRIPT" value="true" />
</context>
</template>
<template name="jsd_sampleSizeArray" value="/*This snippet can be used to get nrandom elements from unique positions from an array up to the size of the array. Elements in the array are shuffled using the Fisher-Yates algorithm*/ const sampleSize = ([...arr], n = 1) => { let m = arr.length; while (m) { const i = Math.floor(Math.random() * m--); [arr[m], arr[i]] = [arr[i], arr[m]]; } return arr.slice(0, n); }; /* sampleSize([1, 2, 3], 2); // [3,1] sampleSize([1, 2, 3], 4); // [2,3,1] */" description="This snippet can be used to get nrandom elements from unique positions from an array up to the size of the array. Elements in the array are shuffled using the Fisher-Yates algorithm" toReformat="false" toShortenFQNames="true">
<context>
<option name="JAVA_SCRIPT" value="true" />
</context>
</template>
<template name="jsd_consoleLog" value="console.log($END$);" description="This snippet for fast add console.log" toReformat="false" toShortenFQNames="true">
<context>
<option name="JAVA_SCRIPT" value="true" />
</context>
</template>
</templateSet>