diff --git a/README.md b/README.md index 776d746ef..76e9af825 100644 --- a/README.md +++ b/README.md @@ -85,7 +85,7 @@ * [Speech synthesis (experimental)](#speech-synthesis-experimental) ### Node -* [Write json to file](#write-json-to-file) +* [Write JSON to file](#write-json-to-file) ### Object * [Object from key value pairs](#object-from-key-value-pairs) @@ -967,7 +967,7 @@ const speak = message => { [⬆ back to top](#table-of-contents) ## Node -### Write a JSON to a file +### Write JSON to file Use `fs.writeFile()`, template literals and `JSON.stringify()` to write a `json` object to a `.json` file. diff --git a/snippets/3DigHexcode-to-6DigHexcode.md b/snippets/3DigHexcode-to-6DigHexcode.md new file mode 100644 index 000000000..2e9b9b8fc --- /dev/null +++ b/snippets/3DigHexcode-to-6DigHexcode.md @@ -0,0 +1,11 @@ +### 3DigHexcode to 6DigHexcode + +Use `Array.map()`, `split()` and `Array.join()` to join the mapped array for converting a three-digit RGB notated hexadecimal colorcode to the six-digit form. + +```js +const convertHex = shortHex => + shortHex[0] == '#' ? ('#' + shortHex.slice(1).split('').map(x => x+x).join('')) : + ('#' + shortHex.split('').map(x => x+x).join('')); +// convertHex('#03f') -> '#0033ff' +// convertHex('05a') -> '#0055aa' +``` diff --git a/snippets/array-concatenation.md b/snippets/array-concatenation.md index 40bf43c29..4c337c9e9 100644 --- a/snippets/array-concatenation.md +++ b/snippets/array-concatenation.md @@ -1,8 +1,8 @@ ### Array concatenation -Use `Array.concat()` to concatenate an array with any additional arrays and/or values, specified in `args`. +Use Array spread operators (`...`) to concatenate an array with any additional arrays and/or values, specified in `args`. ```js -const ArrayConcat = (arr, ...args) => [].concat(arr, ...args); -// ArrayConcat([1], [1, 2, 3, [4]]) -> [1, 2, 3, [4]] +const ArrayConcat = (arr, ...args) => [...arr,...args]; +// ArrayConcat([1], [1, 2, 3, [4]]) -> [1, 1, 2, 3, [4]] ``` diff --git a/snippets/array-without.md b/snippets/array-without.md index dd657fe2a..b5b41aee8 100644 --- a/snippets/array-without.md +++ b/snippets/array-without.md @@ -1,9 +1,9 @@ ### Array without -Use `Array.filter()` to create an array excluding all given values. +Use `Array.filter()` to create an array excluding(using `!Array.includes()`) all given values. ```js -const without = (arr, ...args) => arr.filter(v => args.indexOf(v) === -1); -// without[2, 1, 2, 3], 1, 2) -> [3] +const without = (arr, ...args) => arr.filter(v => !args.includes(v)); +// without([2, 1, 2, 3], 1, 2) -> [3] // without([2, 1, 2, 3, 4, 5, 5, 5, 3, 2, 7, 7], 3, 1, 5, 2) -> [ 4, 7, 7 ] ``` diff --git a/snippets/compose-functions.md b/snippets/compose-functions.md index 718755719..d61c9f3b6 100644 --- a/snippets/compose-functions.md +++ b/snippets/compose-functions.md @@ -1,6 +1,6 @@ ### Compose functions -Use `Array.reduce()` with the spread operator (`...`) to perform right-to-;eft function composition. +Use `Array.reduce()` to perform right-to-left function composition. The last (rightmost) function can accept one or more arguments; the remaining functions must be unary. ```js @@ -11,4 +11,4 @@ const multiply = (x, y) => x * y const multiplyAndAdd5 = compose(add5, multiply) multiplyAndAdd5(5, 2) -> 15 */ -``` +``` \ No newline at end of file diff --git a/snippets/initialize-array-with-range.md b/snippets/initialize-array-with-range.md index def23fb9c..a601ddbb1 100644 --- a/snippets/initialize-array-with-range.md +++ b/snippets/initialize-array-with-range.md @@ -5,6 +5,6 @@ You can omit `start` to use a default value of `0`. ```js const initializeArrayRange = (end, start = 0) => - Array.apply(null, Array(end - start)).map((v, i) => i + start); + Array.from({ length: end - start }).map((v, i) => i + start) // initializeArrayRange(5) -> [0,1,2,3,4] ``` diff --git a/snippets/read-file-to-array.md b/snippets/read-file-to-array.md new file mode 100644 index 000000000..451e2dd06 --- /dev/null +++ b/snippets/read-file-to-array.md @@ -0,0 +1,19 @@ +### Read a file to an Array + +Use `readFileSync` function in `fs` node package to create a `Buffer` from a file. +convert buffer to string using `toString(encoding)` function. +creating an array from contents of file by `split`ing file content line by line(each `\n`). + + ```js +const fs = require('fs'); +const readFileToArray = filename => fs.readFileSync(filename).toString('UTF8').split('\n'); +/* + contents of test.txt : + line1 + line2 + line3 + ___________________________ + let arr = readFileToArray('test.txt') + console.log(arr) // -> ['line1', 'line2', 'line3'] + */ +``` \ No newline at end of file diff --git a/snippets/take-every-nth-element.md b/snippets/take-every-nth-element.md new file mode 100644 index 000000000..639fb0e6c --- /dev/null +++ b/snippets/take-every-nth-element.md @@ -0,0 +1,8 @@ +### Take every nth element of an array + +Use `Array.filter()` to create a new array that contains every nth element of a given array. + +```js +const everynth = (arr, nth) => arr.filter((e, i) => i % nth === 0); +// everynth([1,2,3,4,5,6], 2) -> [ 1, 3, 5 ] +``` diff --git a/snippets/write-json-to-file.md b/snippets/write-json-to-file.md index 17de5a44e..587e5f0f1 100644 --- a/snippets/write-json-to-file.md +++ b/snippets/write-json-to-file.md @@ -1,4 +1,4 @@ -### Write a JSON to a file +### Write JSON to file Use `fs.writeFile()`, template literals and `JSON.stringify()` to write a `json` object to a `.json` file. diff --git a/tag_database b/tag_database index 3acb709f1..34efb5b8c 100644 --- a/tag_database +++ b/tag_database @@ -67,6 +67,7 @@ powerset:math promisify:function random-integer-in-range:utility random-number-in-range:utility +read-file-to-array:node redirect-to-URL:browser reverse-a-string:string RGB-to-hexadecimal:utility @@ -85,6 +86,7 @@ swap-values-of-two-variables:utility tail-of-list:array take-right:array take:array +take-every-nth-element:array truncate-a-string:string unique-values-of-array:array URL-parameters:utility @@ -92,4 +94,4 @@ UUID-generator:utility validate-email:utility validate-number:utility value-or-default:utility -write-json-to-file:node +write-JSON-to-file:node