Housekeeping, tag and build
This commit is contained in:
89
README.md
89
README.md
@ -44,6 +44,7 @@
|
|||||||
* [Similarity between arrays](#similarity-between-arrays)
|
* [Similarity between arrays](#similarity-between-arrays)
|
||||||
* [Sum of array of numbers](#sum-of-array-of-numbers)
|
* [Sum of array of numbers](#sum-of-array-of-numbers)
|
||||||
* [Tail of list](#tail-of-list)
|
* [Tail of list](#tail-of-list)
|
||||||
|
* [Take every nth element](#take-every-nth-element)
|
||||||
* [Take right](#take-right)
|
* [Take right](#take-right)
|
||||||
* [Take](#take)
|
* [Take](#take)
|
||||||
* [Unique values of array](#unique-values-of-array)
|
* [Unique values of array](#unique-values-of-array)
|
||||||
@ -61,8 +62,9 @@
|
|||||||
|
|
||||||
### Function
|
### Function
|
||||||
* [Chain asynchronous functions](#chain-asynchronous-functions)
|
* [Chain asynchronous functions](#chain-asynchronous-functions)
|
||||||
|
* [Compose functions](#compose-functions)
|
||||||
* [Curry](#curry)
|
* [Curry](#curry)
|
||||||
* [Pipe](#pipe)
|
* [Pipe functions](#pipe-functions)
|
||||||
* [Promisify](#promisify)
|
* [Promisify](#promisify)
|
||||||
* [Run promises in series](#run-promises-in-series)
|
* [Run promises in series](#run-promises-in-series)
|
||||||
* [Sleep](#sleep)
|
* [Sleep](#sleep)
|
||||||
@ -85,6 +87,7 @@
|
|||||||
* [Speech synthesis (experimental)](#speech-synthesis-experimental)
|
* [Speech synthesis (experimental)](#speech-synthesis-experimental)
|
||||||
|
|
||||||
### Node
|
### Node
|
||||||
|
* [Read file as array of lines](#read-file-as-array-of-lines)
|
||||||
* [Write JSON to file](#write-json-to-file)
|
* [Write JSON to file](#write-json-to-file)
|
||||||
|
|
||||||
### Object
|
### Object
|
||||||
@ -102,6 +105,7 @@
|
|||||||
* [Truncate a string](#truncate-a-string)
|
* [Truncate a string](#truncate-a-string)
|
||||||
|
|
||||||
### Utility
|
### Utility
|
||||||
|
* [3 digit hexcode to 6 digit hexcode](#3-digit-hexcode-to-6-digit-hexcode)
|
||||||
* [Escape regular expression](#escape-regular-expression)
|
* [Escape regular expression](#escape-regular-expression)
|
||||||
* [Get native type of value](#get-native-type-of-value)
|
* [Get native type of value](#get-native-type-of-value)
|
||||||
* [Hexcode to RGB](#hexcode-to-rgb)
|
* [Hexcode to RGB](#hexcode-to-rgb)
|
||||||
@ -128,11 +132,11 @@
|
|||||||
|
|
||||||
### Array concatenation
|
### 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
|
```js
|
||||||
const ArrayConcat = (arr, ...args) => [].concat(arr, ...args);
|
const ArrayConcat = (arr, ...args) => [...arr,...args];
|
||||||
// ArrayConcat([1], [1, 2, 3, [4]]) -> [1, 2, 3, [4]]
|
// ArrayConcat([1], [1, 2, 3, [4]]) -> [1, 1, 2, 3, [4]]
|
||||||
```
|
```
|
||||||
|
|
||||||
[⬆ back to top](#table-of-contents)
|
[⬆ back to top](#table-of-contents)
|
||||||
@ -213,11 +217,11 @@ const union = (a, b) => Array.from(new Set([...a, ...b]));
|
|||||||
|
|
||||||
### Array without
|
### 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
|
```js
|
||||||
const without = (arr, ...args) => arr.filter(v => args.indexOf(v) === -1);
|
const without = (arr, ...args) => arr.filter(v => !args.includes(v));
|
||||||
// without[2, 1, 2, 3], 1, 2) -> [3]
|
// 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 ]
|
// without([2, 1, 2, 3, 4, 5, 5, 5, 3, 2, 7, 7], 3, 1, 5, 2) -> [ 4, 7, 7 ]
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -231,7 +235,7 @@ If lengths of the argument-arrays vary, `undefined` is used where no value could
|
|||||||
|
|
||||||
```js
|
```js
|
||||||
const zip = (...arrays) => {
|
const zip = (...arrays) => {
|
||||||
const maxLength = Math.max.apply(null, arrays.map(a => a.length));
|
const maxLength = Math.max(...arrays.map(x => x.length));
|
||||||
return Array.from({length: maxLength}).map((_, i) => {
|
return Array.from({length: maxLength}).map((_, i) => {
|
||||||
return Array.from({length: arrays.length}, (_, k) => arrays[k][i]);
|
return Array.from({length: arrays.length}, (_, k) => arrays[k][i]);
|
||||||
})
|
})
|
||||||
@ -434,7 +438,7 @@ You can omit `start` to use a default value of `0`.
|
|||||||
|
|
||||||
```js
|
```js
|
||||||
const initializeArrayRange = (end, start = 0) =>
|
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]
|
// initializeArrayRange(5) -> [0,1,2,3,4]
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -551,6 +555,17 @@ const tail = arr => arr.length > 1 ? arr.slice(1) : arr;
|
|||||||
|
|
||||||
[⬆ back to top](#table-of-contents)
|
[⬆ back to top](#table-of-contents)
|
||||||
|
|
||||||
|
### Take every nth element
|
||||||
|
|
||||||
|
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 ]
|
||||||
|
```
|
||||||
|
|
||||||
|
[⬆ back to top](#table-of-contents)
|
||||||
|
|
||||||
### Take right
|
### Take right
|
||||||
|
|
||||||
Use `Array.slice()` to create a slice of the array with `n` elements taken from the end.
|
Use `Array.slice()` to create a slice of the array with `n` elements taken from the end.
|
||||||
@ -707,6 +722,22 @@ chainAsync([
|
|||||||
|
|
||||||
[⬆ back to top](#table-of-contents)
|
[⬆ back to top](#table-of-contents)
|
||||||
|
|
||||||
|
### Compose functions
|
||||||
|
|
||||||
|
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
|
||||||
|
const compose = (...fns) => fns.reduce((f, g) => (...args) => f(g(...args)));
|
||||||
|
/*
|
||||||
|
const add5 = x => x + 5
|
||||||
|
const multiply = (x, y) => x * y
|
||||||
|
const multiplyAndAdd5 = compose(add5, multiply)
|
||||||
|
multiplyAndAdd5(5, 2) -> 15
|
||||||
|
*/
|
||||||
|
```
|
||||||
|
[⬆ back to top](#table-of-contents)
|
||||||
|
|
||||||
### Curry
|
### Curry
|
||||||
|
|
||||||
Use recursion.
|
Use recursion.
|
||||||
@ -725,9 +756,9 @@ const curry = (fn, arity = fn.length, ...args) =>
|
|||||||
|
|
||||||
[⬆ back to top](#table-of-contents)
|
[⬆ back to top](#table-of-contents)
|
||||||
|
|
||||||
### Pipe
|
### Pipe functions
|
||||||
|
|
||||||
Use `Array.reduce()` to perform left-to-right function composition.
|
Use `Array.reduce()` with the spread operator (`...`) to perform left-to-right function composition.
|
||||||
The first (leftmost) function can accept one or more arguments; the remaining functions must be unary.
|
The first (leftmost) function can accept one or more arguments; the remaining functions must be unary.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
@ -967,6 +998,28 @@ const speak = message => {
|
|||||||
[⬆ back to top](#table-of-contents)
|
[⬆ back to top](#table-of-contents)
|
||||||
## Node
|
## Node
|
||||||
|
|
||||||
|
### Read file as array of lines
|
||||||
|
|
||||||
|
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']
|
||||||
|
*/
|
||||||
|
```
|
||||||
|
|
||||||
|
[⬆ back to top](#table-of-contents)
|
||||||
|
|
||||||
### Write JSON to file
|
### Write JSON to file
|
||||||
|
|
||||||
Use `fs.writeFile()`, template literals and `JSON.stringify()` to write a `json` object to a `.json` file.
|
Use `fs.writeFile()`, template literals and `JSON.stringify()` to write a `json` object to a `.json` file.
|
||||||
@ -1114,6 +1167,20 @@ const truncate = (str, num) =>
|
|||||||
[⬆ back to top](#table-of-contents)
|
[⬆ back to top](#table-of-contents)
|
||||||
## Utility
|
## Utility
|
||||||
|
|
||||||
|
### 3-digit hexcode to 6-digit hexcode
|
||||||
|
|
||||||
|
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'
|
||||||
|
```
|
||||||
|
|
||||||
|
[⬆ back to top](#table-of-contents)
|
||||||
|
|
||||||
### Escape regular expression
|
### Escape regular expression
|
||||||
|
|
||||||
Use `replace()` to escape special characters.
|
Use `replace()` to escape special characters.
|
||||||
|
|||||||
@ -1,10 +1,10 @@
|
|||||||
### 3DigHexcode to 6DigHexcode
|
### 3-digit hexcode to 6-digit hexcode
|
||||||
|
|
||||||
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.
|
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
|
```js
|
||||||
const convertHex = shortHex =>
|
const convertHex = shortHex =>
|
||||||
shortHex[0] == '#' ? ('#' + shortHex.slice(1).split('').map(x => x+x).join('')) :
|
shortHex[0] == '#' ? ('#' + shortHex.slice(1).split('').map(x => x+x).join('')) :
|
||||||
('#' + shortHex.split('').map(x => x+x).join(''));
|
('#' + shortHex.split('').map(x => x+x).join(''));
|
||||||
// convertHex('#03f') -> '#0033ff'
|
// convertHex('#03f') -> '#0033ff'
|
||||||
// convertHex('05a') -> '#0055aa'
|
// convertHex('05a') -> '#0055aa'
|
||||||
@ -5,6 +5,6 @@ You can omit `start` to use a default value of `0`.
|
|||||||
|
|
||||||
```js
|
```js
|
||||||
const initializeArrayRange = (end, start = 0) =>
|
const initializeArrayRange = (end, start = 0) =>
|
||||||
Array.from({ length: end - start }).map((v, i) => i + start)
|
Array.from({ length: end - start }).map((v, i) => i + start);
|
||||||
// initializeArrayRange(5) -> [0,1,2,3,4]
|
// initializeArrayRange(5) -> [0,1,2,3,4]
|
||||||
```
|
```
|
||||||
|
|||||||
@ -1,4 +1,4 @@
|
|||||||
### Pipe
|
### Pipe functions
|
||||||
|
|
||||||
Use `Array.reduce()` with the spread operator (`...`) to perform left-to-right function composition.
|
Use `Array.reduce()` with the spread operator (`...`) to perform left-to-right function composition.
|
||||||
The first (leftmost) function can accept one or more arguments; the remaining functions must be unary.
|
The first (leftmost) function can accept one or more arguments; the remaining functions must be unary.
|
||||||
@ -1,4 +1,4 @@
|
|||||||
### Read a file to an Array
|
### Read file as array of lines
|
||||||
|
|
||||||
Use `readFileSync` function in `fs` node package to create a `Buffer` from a file.
|
Use `readFileSync` function in `fs` node package to create a `Buffer` from a file.
|
||||||
convert buffer to string using `toString(encoding)` function.
|
convert buffer to string using `toString(encoding)` function.
|
||||||
@ -16,4 +16,4 @@ const readFileToArray = filename => fs.readFileSync(filename).toString('UTF8').s
|
|||||||
let arr = readFileToArray('test.txt')
|
let arr = readFileToArray('test.txt')
|
||||||
console.log(arr) // -> ['line1', 'line2', 'line3']
|
console.log(arr) // -> ['line1', 'line2', 'line3']
|
||||||
*/
|
*/
|
||||||
```
|
```
|
||||||
@ -1,4 +1,4 @@
|
|||||||
### Take every nth element of an array
|
### Take every nth element
|
||||||
|
|
||||||
Use `Array.filter()` to create a new array that contains every nth element of a given array.
|
Use `Array.filter()` to create a new array that contains every nth element of a given array.
|
||||||
|
|
||||||
|
|||||||
@ -1,3 +1,4 @@
|
|||||||
|
3-digit-hexcode-to-6-digit-hexcode:utility
|
||||||
anagrams-of-string-(with-duplicates):string
|
anagrams-of-string-(with-duplicates):string
|
||||||
array-concatenation:array
|
array-concatenation:array
|
||||||
array-difference:array
|
array-difference:array
|
||||||
@ -17,6 +18,7 @@ check-for-palindrome:string
|
|||||||
chunk-array:array
|
chunk-array:array
|
||||||
collatz-algorithm:math
|
collatz-algorithm:math
|
||||||
compact:array
|
compact:array
|
||||||
|
compose-functions:function
|
||||||
count-occurrences-of-a-value-in-array:array
|
count-occurrences-of-a-value-in-array:array
|
||||||
current-URL:browser
|
current-URL:browser
|
||||||
curry:function
|
curry:function
|
||||||
@ -62,12 +64,12 @@ object-to-key-value-pairs:object
|
|||||||
ordinal-suffix-of-number:utility
|
ordinal-suffix-of-number:utility
|
||||||
percentile:math
|
percentile:math
|
||||||
pick:array
|
pick:array
|
||||||
pipe:function
|
pipe-functions:function
|
||||||
powerset:math
|
powerset:math
|
||||||
promisify:function
|
promisify:function
|
||||||
random-integer-in-range:utility
|
random-integer-in-range:utility
|
||||||
random-number-in-range:utility
|
random-number-in-range:utility
|
||||||
read-file-to-array:node
|
read-file-as-array-of-lines:node
|
||||||
redirect-to-URL:browser
|
redirect-to-URL:browser
|
||||||
reverse-a-string:string
|
reverse-a-string:string
|
||||||
RGB-to-hexadecimal:utility
|
RGB-to-hexadecimal:utility
|
||||||
@ -84,9 +86,9 @@ standard-deviation:math
|
|||||||
sum-of-array-of-numbers:array
|
sum-of-array-of-numbers:array
|
||||||
swap-values-of-two-variables:utility
|
swap-values-of-two-variables:utility
|
||||||
tail-of-list:array
|
tail-of-list:array
|
||||||
|
take-every-nth-element:array
|
||||||
take-right:array
|
take-right:array
|
||||||
take:array
|
take:array
|
||||||
take-every-nth-element:array
|
|
||||||
truncate-a-string:string
|
truncate-a-string:string
|
||||||
unique-values-of-array:array
|
unique-values-of-array:array
|
||||||
URL-parameters:utility
|
URL-parameters:utility
|
||||||
|
|||||||
Reference in New Issue
Block a user