Updated examples
This commit is contained in:
committed by
Agamemnon Zorbas
parent
99a07d22dd
commit
97c250bcfb
@ -12,5 +12,5 @@ const JSONToDate = arr => {
|
||||
```
|
||||
|
||||
```js
|
||||
JSONToDate(/Date(1489525200000)/) -> "14/3/2017"
|
||||
JSONToDate(/Date(1489525200000)/) // "14/3/2017"
|
||||
```
|
||||
|
||||
@ -9,5 +9,5 @@ const RGBToHex = (r, g, b) => ((r << 16) + (g << 8) + b).toString(16).padStart(6
|
||||
```
|
||||
|
||||
```js
|
||||
RGBToHex(255, 165, 1) -> 'ffa501'
|
||||
RGBToHex(255, 165, 1) // 'ffa501'
|
||||
```
|
||||
|
||||
@ -12,5 +12,5 @@ const UUIDGenerator = () =>
|
||||
```
|
||||
|
||||
```js
|
||||
UUIDGenerator() -> '7982fcfe-5721-4632-bede-6000885be57d'
|
||||
UUIDGenerator() // '7982fcfe-5721-4632-bede-6000885be57d'
|
||||
```
|
||||
|
||||
@ -16,5 +16,5 @@ const anagrams = str => {
|
||||
```
|
||||
|
||||
```js
|
||||
anagrams('abc') -> ['abc','acb','bac','bca','cab','cba']
|
||||
anagrams('abc') // ['abc','acb','bac','bca','cab','cba']
|
||||
```
|
||||
|
||||
@ -9,5 +9,5 @@ const arrayAverage = arr => arr.reduce((acc, val) => acc + val, 0) / arr.length;
|
||||
```
|
||||
|
||||
```js
|
||||
arrayAverage([1,2,3]) -> 2
|
||||
arrayAverage([1,2,3]) // 2
|
||||
```
|
||||
|
||||
@ -12,6 +12,6 @@ const arrayGcd = arr => {
|
||||
```
|
||||
|
||||
```js
|
||||
arrayGcd([1,2,3,4,5]) -> 1
|
||||
arrayGcd([4,8,12]) -> 4
|
||||
arrayGcd([1,2,3,4,5]) // 1
|
||||
arrayGcd([4,8,12]) // 4
|
||||
```
|
||||
|
||||
@ -13,6 +13,6 @@ const arrayLcm = arr => {
|
||||
```
|
||||
|
||||
```js
|
||||
arrayLcm([1,2,3,4,5]) -> 60
|
||||
arrayLcm([4,8,12]) -> 24
|
||||
arrayLcm([1,2,3,4,5]) // 60
|
||||
arrayLcm([4,8,12]) // 24
|
||||
```
|
||||
|
||||
@ -9,5 +9,5 @@ const arrayMax = arr => Math.max(...arr);
|
||||
```
|
||||
|
||||
```js
|
||||
arrayMax([10, 1, 5]) -> 10
|
||||
arrayMax([10, 1, 5]) // 10
|
||||
```
|
||||
|
||||
@ -9,5 +9,5 @@ const arrayMin = arr => Math.min(...arr);
|
||||
```
|
||||
|
||||
```js
|
||||
arrayMin([10, 1, 5]) -> 1
|
||||
arrayMin([10, 1, 5]) // 1
|
||||
```
|
||||
|
||||
@ -9,5 +9,5 @@ const arraySum = arr => arr.reduce((acc, val) => acc + val, 0);
|
||||
```
|
||||
|
||||
```js
|
||||
arraySum([1,2,3,4]) -> 10
|
||||
arraySum([1,2,3,4]) // 10
|
||||
```
|
||||
|
||||
@ -10,5 +10,5 @@ const bottomVisible = () =>
|
||||
```
|
||||
|
||||
```js
|
||||
// bottomVisible() -> true
|
||||
// bottomVisible() // true
|
||||
```
|
||||
|
||||
@ -11,6 +11,6 @@ const capitalize = ([first, ...rest], lowerRest = false) =>
|
||||
```
|
||||
|
||||
```js
|
||||
capitalize('fooBar') -> 'FooBar'
|
||||
capitalize('fooBar', true) -> 'Foobar'
|
||||
capitalize('fooBar') // 'FooBar'
|
||||
capitalize('fooBar', true) // 'Foobar'
|
||||
```
|
||||
|
||||
@ -9,5 +9,5 @@ const capitalizeEveryWord = str => str.replace(/\b[a-z]/g, char => char.toUpperC
|
||||
```
|
||||
|
||||
```js
|
||||
capitalizeEveryWord('hello world!') -> 'Hello World!'
|
||||
capitalizeEveryWord('hello world!') // 'Hello World!'
|
||||
```
|
||||
|
||||
@ -12,5 +12,5 @@ const chunk = (arr, size) =>
|
||||
```
|
||||
|
||||
```js
|
||||
chunk([1,2,3,4,5], 2) -> [[1,2],[3,4],[5]]
|
||||
chunk([1,2,3,4,5], 2) // [[1,2],[3,4],[5]]
|
||||
```
|
||||
|
||||
@ -9,6 +9,6 @@ const collatz = n => (n % 2 == 0) ? (n / 2) : (3 * n + 1);
|
||||
```
|
||||
|
||||
```js
|
||||
collatz(8) -> 4
|
||||
collatz(5) -> 16
|
||||
collatz(8) // 4
|
||||
collatz(5) // 16
|
||||
```
|
||||
|
||||
@ -9,5 +9,5 @@ const compact = arr => arr.filter(Boolean);
|
||||
```
|
||||
|
||||
```js
|
||||
compact([0, 1, false, 2, '', 3, 'a', 'e'*23, NaN, 's', 34]) -> [ 1, 2, 3, 'a', 's', 34 ]
|
||||
compact([0, 1, false, 2, '', 3, 'a', 'e'*23, NaN, 's', 34]) // [ 1, 2, 3, 'a', 's', 34 ]
|
||||
```
|
||||
|
||||
@ -13,5 +13,5 @@ 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
|
||||
multiplyAndAdd5(5, 2) // 15
|
||||
```
|
||||
|
||||
@ -9,5 +9,5 @@ const countOccurrences = (arr, value) => arr.reduce((a, v) => v === value ? a +
|
||||
```
|
||||
|
||||
```js
|
||||
countOccurrences([1,1,2,1,2,3], 1) -> 3
|
||||
countOccurrences([1,1,2,1,2,3], 1) // 3
|
||||
```
|
||||
|
||||
@ -9,6 +9,6 @@ const countVowels = str => (str.match(/[aeiou]/ig) || []).length;
|
||||
```
|
||||
|
||||
```js
|
||||
countVowels('foobar') -> 3
|
||||
countVowels('gym') -> 0
|
||||
countVowels('foobar') // 3
|
||||
countVowels('gym') // 0
|
||||
```
|
||||
|
||||
@ -9,5 +9,5 @@ const currentURL = () => window.location.href;
|
||||
```
|
||||
|
||||
```js
|
||||
currentUrl() -> 'https://google.com'
|
||||
currentUrl() // 'https://google.com'
|
||||
```
|
||||
|
||||
@ -15,6 +15,6 @@ const curry = (fn, arity = fn.length, ...args) =>
|
||||
```
|
||||
|
||||
```js
|
||||
curry(Math.pow)(2)(10) -> 1024
|
||||
curry(Math.min, 3)(10)(50)(2) -> 2
|
||||
curry(Math.pow)(2)(10) // 1024
|
||||
curry(Math.min, 3)(10)(50)(2) // 2
|
||||
```
|
||||
|
||||
@ -11,5 +11,5 @@ const deepFlatten = arr => [].concat(...arr.map(v => Array.isArray(v) ? deepFlat
|
||||
```
|
||||
|
||||
```js
|
||||
deepFlatten([1,[2],[[3],4],5]) -> [1,2,3,4,5]
|
||||
deepFlatten([1,[2],[[3],4],5]) // [1,2,3,4,5]
|
||||
```
|
||||
|
||||
@ -9,5 +9,5 @@ const difference = (a, b) => { const s = new Set(b); return a.filter(x => !s.has
|
||||
```
|
||||
|
||||
```js
|
||||
difference([1,2,3], [1,2,4]) -> [3]
|
||||
difference([1,2,3], [1,2,4]) // [3]
|
||||
```
|
||||
|
||||
@ -9,5 +9,5 @@ const distance = (x0, y0, x1, y1) => Math.hypot(x1 - x0, y1 - y0);
|
||||
```
|
||||
|
||||
```js
|
||||
distance(1,1, 2,3) -> 2.23606797749979
|
||||
distance(1,1, 2,3) // 2.23606797749979
|
||||
```
|
||||
|
||||
@ -9,5 +9,5 @@ const distinctValuesOfArray = arr => [...new Set(arr)];
|
||||
```
|
||||
|
||||
```js
|
||||
distinctValuesOfArray([1,2,2,3,4,4,5]) -> [1,2,3,4,5]
|
||||
distinctValuesOfArray([1,2,2,3,4,4,5]) // [1,2,3,4,5]
|
||||
```
|
||||
|
||||
@ -13,5 +13,5 @@ const dropElements = (arr, func) => {
|
||||
```
|
||||
|
||||
```js
|
||||
dropElements([1, 2, 3, 4], n => n >= 3) -> [3,4]
|
||||
dropElements([1, 2, 3, 4], n => n >= 3) // [3,4]
|
||||
```
|
||||
|
||||
@ -20,6 +20,6 @@ const elementIsVisibleInViewport = (el, partiallyVisible = false) => {
|
||||
|
||||
```js
|
||||
// e.g. 100x100 viewport and a 10x10px element at position {top: -1, left: 0, bottom: 9, right: 10}
|
||||
elementIsVisibleInViewport(el) -> false // (not fully visible)
|
||||
elementIsVisibleInViewport(el, true) -> true // (partially visible)
|
||||
elementIsVisibleInViewport(el) // false // (not fully visible)
|
||||
elementIsVisibleInViewport(el, true) // true // (partially visible)
|
||||
```
|
||||
|
||||
@ -9,5 +9,5 @@ const escapeRegExp = str => str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
||||
```
|
||||
|
||||
```js
|
||||
escapeRegExp('(test)') -> \\(test\\)
|
||||
escapeRegExp('(test)') // \\(test\\)
|
||||
```
|
||||
|
||||
@ -9,5 +9,5 @@ const everyNth = (arr, nth) => arr.filter((e, i) => i % nth === nth - 1);
|
||||
```
|
||||
|
||||
```js
|
||||
everyNth([1,2,3,4,5,6], 2) -> [ 2, 4, 6 ]
|
||||
everyNth([1,2,3,4,5,6], 2) // [ 2, 4, 6 ]
|
||||
```
|
||||
|
||||
@ -14,5 +14,5 @@ const factorial = n =>
|
||||
```
|
||||
|
||||
```js
|
||||
factorial(6) -> 720
|
||||
factorial(6) // 720
|
||||
```
|
||||
|
||||
@ -11,5 +11,5 @@ const fibonacci = n =>
|
||||
```
|
||||
|
||||
```js
|
||||
factorial(6) -> 720
|
||||
factorial(6) // 720
|
||||
```
|
||||
|
||||
@ -10,5 +10,5 @@ const fibonacciCountUntilNum = num =>
|
||||
```
|
||||
|
||||
```js
|
||||
fibonacciCountUntilNum(10) -> 7
|
||||
fibonacciCountUntilNum(10) // 7
|
||||
```
|
||||
|
||||
@ -9,5 +9,5 @@ const filterNonUnique = arr => arr.filter(i => arr.indexOf(i) === arr.lastIndexO
|
||||
```
|
||||
|
||||
```js
|
||||
filterNonUnique([1,2,2,3,4,4,5]) -> [1,3,5]
|
||||
filterNonUnique([1,2,2,3,4,4,5]) // [1,3,5]
|
||||
```
|
||||
|
||||
@ -14,5 +14,5 @@ const flattenDepth = (arr, depth = 1) =>
|
||||
```
|
||||
|
||||
```js
|
||||
flatten([1,[2],3,4]) -> [1,2,3,4]
|
||||
flatten([1,[2],3,4]) // [1,2,3,4]
|
||||
```
|
||||
|
||||
@ -12,7 +12,7 @@ const fromCamelCase = (str, separator = '_') =>
|
||||
```
|
||||
|
||||
```js
|
||||
fromCamelCase('someDatabaseFieldName', ' ') -> 'some database field name'
|
||||
fromCamelCase('someLabelThatNeedsToBeCamelized', '-') -> 'some-label-that-needs-to-be-camelized'
|
||||
fromCamelCase('someJavascriptProperty', '_') -> 'some_javascript_property'
|
||||
fromCamelCase('someDatabaseFieldName', ' ') // 'some database field name'
|
||||
fromCamelCase('someLabelThatNeedsToBeCamelized', '-') // 'some-label-that-needs-to-be-camelized'
|
||||
fromCamelCase('someJavascriptProperty', '_') // 'some_javascript_property'
|
||||
```
|
||||
|
||||
@ -9,5 +9,5 @@ const functionName = fn => (console.debug(fn.name), fn);
|
||||
```
|
||||
|
||||
```js
|
||||
functionName(Math.max) -> max (logged in debug channel of console)
|
||||
functionName(Math.max) // max (logged in debug channel of console)
|
||||
```
|
||||
|
||||
@ -11,5 +11,5 @@ const gcd = (x, y) => !y ? x : gcd(y, x % y);
|
||||
```
|
||||
|
||||
```js
|
||||
gcd (8, 36) -> 4
|
||||
gcd (8, 36) // 4
|
||||
```
|
||||
|
||||
@ -9,5 +9,5 @@ const getDaysDiffBetweenDates = (dateInitial, dateFinal) => (dateFinal - dateIni
|
||||
```
|
||||
|
||||
```js
|
||||
getDaysDiffBetweenDates(new Date("2017-12-13"), new Date("2017-12-22")) -> 9
|
||||
getDaysDiffBetweenDates(new Date("2017-12-13"), new Date("2017-12-22")) // 9
|
||||
```
|
||||
|
||||
@ -12,5 +12,5 @@ const getScrollPosition = (el = window) =>
|
||||
```
|
||||
|
||||
```js
|
||||
getScrollPosition() -> {x: 0, y: 200}
|
||||
getScrollPosition() // {x: 0, y: 200}
|
||||
```
|
||||
|
||||
@ -10,5 +10,5 @@ const getType = v =>
|
||||
```
|
||||
|
||||
```js
|
||||
getType(new Set([1,2,3])) -> "set"
|
||||
getType(new Set([1,2,3])) // "set"
|
||||
```
|
||||
|
||||
@ -13,5 +13,5 @@ const getURLParameters = url =>
|
||||
```
|
||||
|
||||
```js
|
||||
getURLParameters('http://url.com/page?name=Adam&surname=Smith') -> {name: 'Adam', surname: 'Smith'}
|
||||
getURLParameters('http://url.com/page?name=Adam&surname=Smith') // {name: 'Adam', surname: 'Smith'}
|
||||
```
|
||||
|
||||
@ -12,6 +12,6 @@ const groupBy = (arr, func) =>
|
||||
```
|
||||
|
||||
```js
|
||||
groupBy([6.1, 4.2, 6.3], Math.floor) -> {4: [4.2], 6: [6.1, 6.3]}
|
||||
groupBy(['one', 'two', 'three'], 'length') -> {3: ['one', 'two'], 5: ['three']}
|
||||
groupBy([6.1, 4.2, 6.3], Math.floor) // {4: [4.2], 6: [6.1, 6.3]}
|
||||
groupBy(['one', 'two', 'three'], 'length') // {3: ['one', 'two'], 5: ['three']}
|
||||
```
|
||||
|
||||
@ -11,5 +11,5 @@ const hammingDistance = (num1, num2) =>
|
||||
```
|
||||
|
||||
```js
|
||||
hammingDistance(2,3) -> 1
|
||||
hammingDistance(2,3) // 1
|
||||
```
|
||||
|
||||
@ -9,5 +9,5 @@ const head = arr => arr[0];
|
||||
```
|
||||
|
||||
```js
|
||||
head([1,2,3]) -> 1
|
||||
head([1,2,3]) // 1
|
||||
```
|
||||
|
||||
@ -9,5 +9,5 @@ const initial = arr => arr.slice(0, -1);
|
||||
```
|
||||
|
||||
```js
|
||||
initial([1,2,3]) -> [1,2]
|
||||
initial([1,2,3]) // [1,2]
|
||||
```
|
||||
|
||||
@ -9,5 +9,5 @@ const initialize2DArray = (w, h, val = null) => Array(h).fill().map(() => Array(
|
||||
```
|
||||
|
||||
```js
|
||||
initialize2DArray(2, 2, 0) -> [[0,0], [0,0]]
|
||||
initialize2DArray(2, 2, 0) // [[0,0], [0,0]]
|
||||
```
|
||||
|
||||
@ -11,6 +11,6 @@ const initializeArrayWithRange = (end, start = 0) =>
|
||||
```
|
||||
|
||||
```js
|
||||
initializeArrayWithRange(5) -> [0,1,2,3,4,5]
|
||||
initializeArrayWithRange(7, 3) -> [3,4,5,6,7]
|
||||
initializeArrayWithRange(5) // [0,1,2,3,4,5]
|
||||
initializeArrayWithRange(7, 3) // [3,4,5,6,7]
|
||||
```
|
||||
|
||||
@ -10,5 +10,5 @@ const initializeArrayWithValues = (n, value = 0) => Array(n).fill(value);
|
||||
```
|
||||
|
||||
```js
|
||||
initializeArrayWithValues(5, 2) -> [2,2,2,2,2]
|
||||
initializeArrayWithValues(5, 2) // [2,2,2,2,2]
|
||||
```
|
||||
|
||||
@ -9,5 +9,5 @@ const intersection = (a, b) => { const s = new Set(b); return a.filter(x => s.ha
|
||||
```
|
||||
|
||||
```js
|
||||
intersection([1,2,3], [4,3,2]) -> [2,3]
|
||||
intersection([1,2,3], [4,3,2]) // [2,3]
|
||||
```
|
||||
|
||||
@ -9,6 +9,6 @@ const isArray = val => !!val && Array.isArray(val);
|
||||
```
|
||||
|
||||
```js
|
||||
isArray(null) -> false
|
||||
isArray([1]) -> true
|
||||
isArray(null) // false
|
||||
isArray([1]) // true
|
||||
```
|
||||
|
||||
@ -9,6 +9,6 @@ const isBoolean = val => typeof val === 'boolean';
|
||||
```
|
||||
|
||||
```js
|
||||
isBoolean(null) -> false
|
||||
isBoolean(false) -> true
|
||||
isBoolean(null) // false
|
||||
isBoolean(false) // true
|
||||
```
|
||||
|
||||
@ -9,5 +9,5 @@ const isDivisible = (dividend, divisor) => dividend % divisor === 0;
|
||||
```
|
||||
|
||||
```js
|
||||
isDivisible(6,3) -> true
|
||||
isDivisible(6,3) // true
|
||||
```
|
||||
|
||||
@ -10,5 +10,5 @@ const isEven = num => num % 2 === 0;
|
||||
```
|
||||
|
||||
```js
|
||||
isEven(3) -> false
|
||||
isEven(3) // false
|
||||
```
|
||||
|
||||
@ -9,6 +9,6 @@ const isFunction = val => val && typeof val === 'function';
|
||||
```
|
||||
|
||||
```js
|
||||
isFunction('x') -> false
|
||||
isFunction(x => x) -> true
|
||||
isFunction('x') // false
|
||||
isFunction(x => x) // true
|
||||
```
|
||||
|
||||
@ -9,6 +9,6 @@ const isNumber = val => typeof val === 'number';
|
||||
```
|
||||
|
||||
```js
|
||||
isNumber('1') -> false
|
||||
isNumber(1) -> true
|
||||
isNumber('1') // false
|
||||
isNumber(1) // true
|
||||
```
|
||||
@ -14,6 +14,6 @@ const isPrime = num => {
|
||||
```
|
||||
|
||||
```js
|
||||
isPrime(11) -> true
|
||||
isPrime(12) -> false
|
||||
isPrime(11) // true
|
||||
isPrime(12) // false
|
||||
```
|
||||
|
||||
@ -9,6 +9,6 @@ const isString = val => typeof val === 'string';
|
||||
```
|
||||
|
||||
```js
|
||||
isString(10) -> false
|
||||
isString('10') -> true
|
||||
isString(10) // false
|
||||
isString('10') // true
|
||||
```
|
||||
|
||||
@ -9,6 +9,6 @@ const isSymbol = val => typeof val === 'symbol';
|
||||
```
|
||||
|
||||
```js
|
||||
isSymbol('x') -> false
|
||||
isSymbol(Symbol('x')) -> true
|
||||
isSymbol('x') // false
|
||||
isSymbol(Symbol('x')) // true
|
||||
```
|
||||
|
||||
@ -9,5 +9,5 @@ const last = arr => arr[arr.length - 1];
|
||||
```
|
||||
|
||||
```js
|
||||
last([1,2,3]) -> 3
|
||||
last([1,2,3]) // 3
|
||||
```
|
||||
|
||||
@ -13,5 +13,5 @@ const lcm = (x, y) => {
|
||||
```
|
||||
|
||||
```js
|
||||
lcm(12,7) -> 84
|
||||
lcm(12,7) // 84
|
||||
```
|
||||
|
||||
@ -13,6 +13,6 @@ const median = arr => {
|
||||
```
|
||||
|
||||
```js
|
||||
median([5,6,50,1,-5]) -> 5
|
||||
median([0,10,-2,7]) -> 3.5
|
||||
median([5,6,50,1,-5]) // 5
|
||||
median([0,10,-2,7]) // 3.5
|
||||
```
|
||||
|
||||
@ -9,6 +9,6 @@ const negate = func => (...args) => !func(...args);
|
||||
```
|
||||
|
||||
```js
|
||||
filter([1, 2, 3, 4, 5, 6], negate(isEven)) -> [1, 3, 5]
|
||||
negate(isOdd)(1) -> false
|
||||
filter([1, 2, 3, 4, 5, 6], negate(isEven)) // [1, 3, 5]
|
||||
negate(isOdd)(1) // false
|
||||
```
|
||||
|
||||
@ -9,5 +9,5 @@ const objectFromPairs = arr => arr.reduce((a, v) => (a[v[0]] = v[1], a), {});
|
||||
```
|
||||
|
||||
```js
|
||||
objectFromPairs([['a',1],['b',2]]) -> {a: 1, b: 2}
|
||||
objectFromPairs([['a',1],['b',2]]) // {a: 1, b: 2}
|
||||
```
|
||||
|
||||
@ -9,5 +9,5 @@ const objectToPairs = obj => Object.keys(obj).map(k => [k, obj[k]]);
|
||||
```
|
||||
|
||||
```js
|
||||
objectToPairs({a: 1, b: 2}) -> [['a',1],['b',2]])
|
||||
objectToPairs({a: 1, b: 2}) // [['a',1],['b',2]])
|
||||
```
|
||||
|
||||
@ -21,6 +21,6 @@ const orderBy = (arr, props, orders) =>
|
||||
```js
|
||||
const users = [{ 'name': 'fred', 'age': 48 },{ 'name': 'barney', 'age': 36 },
|
||||
{ 'name': 'fred', 'age': 40 },{ 'name': 'barney', 'age': 34 }];
|
||||
orderby(users, ['name', 'age'], ['asc', 'desc']) -> [{name: 'barney', age: 36}, {name: 'barney', age: 34}, {name: 'fred', age: 48}, {name: 'fred', age: 40}]
|
||||
orderby(users, ['name', 'age']) -> [{name: 'barney', age: 34}, {name: 'barney', age: 36}, {name: 'fred', age: 40}, {name: 'fred', age: 48}]
|
||||
orderby(users, ['name', 'age'], ['asc', 'desc']) // [{name: 'barney', age: 36}, {name: 'barney', age: 34}, {name: 'fred', age: 48}, {name: 'fred', age: 40}]
|
||||
orderby(users, ['name', 'age']) // [{name: 'barney', age: 34}, {name: 'barney', age: 36}, {name: 'fred', age: 40}, {name: 'fred', age: 48}]
|
||||
```
|
||||
|
||||
@ -13,5 +13,5 @@ const palindrome = str => {
|
||||
```
|
||||
|
||||
```js
|
||||
palindrome('taco cat') -> true
|
||||
palindrome('taco cat') // true
|
||||
```
|
||||
|
||||
@ -10,5 +10,5 @@ const percentile = (arr, val) =>
|
||||
```
|
||||
|
||||
```js
|
||||
percentile([1,2,3,4,5,6,7,8,9,10], 6) -> 55
|
||||
percentile([1,2,3,4,5,6,7,8,9,10], 6) // 55
|
||||
```
|
||||
|
||||
@ -10,5 +10,5 @@ const pick = (obj, arr) =>
|
||||
```
|
||||
|
||||
```js
|
||||
pick({ 'a': 1, 'b': '2', 'c': 3 }, ['a', 'c']) -> { 'a': 1, 'c': 3 }
|
||||
pick({ 'a': 1, 'b': '2', 'c': 3 }, ['a', 'c']) // { 'a': 1, 'c': 3 }
|
||||
```
|
||||
|
||||
@ -13,5 +13,5 @@ const pipeFunctions = (...fns) => fns.reduce((f, g) => (...args) => g(f(...args)
|
||||
const add5 = x => x + 5
|
||||
const multiply = (x, y) => x * y
|
||||
const multiplyAndAdd5 = pipeFunctions(multiply, add5)
|
||||
multiplyAndAdd5(5, 2) -> 15
|
||||
multiplyAndAdd5(5, 2) // 15
|
||||
```
|
||||
|
||||
@ -10,5 +10,5 @@ const powerset = arr =>
|
||||
```
|
||||
|
||||
```js
|
||||
powerset([1,2]) -> [[], [1], [2], [2,1]]
|
||||
powerset([1,2]) // [[], [1], [2], [2,1]]
|
||||
```
|
||||
|
||||
@ -18,5 +18,5 @@ const promisify = func =>
|
||||
|
||||
```js
|
||||
const delay = promisify((d, cb) => setTimeout(cb, d))
|
||||
delay(2000).then(() => console.log('Hi!')) -> // Promise resolves after 2s
|
||||
delay(2000).then(() => console.log('Hi!')) // // Promise resolves after 2s
|
||||
```
|
||||
|
||||
@ -19,9 +19,9 @@ const pull = (arr, ...args) => {
|
||||
```js
|
||||
let myArray1 = ['a', 'b', 'c', 'a', 'b', 'c'];
|
||||
pull(myArray1, 'a', 'c');
|
||||
console.log(myArray1) -> [ 'b', 'b' ]
|
||||
console.log(myArray1) // [ 'b', 'b' ]
|
||||
|
||||
let myArray2 = ['a', 'b', 'c', 'a', 'b', 'c'];
|
||||
pull(myArray2, ['a', 'c']);
|
||||
console.log(myArray2) -> [ 'b', 'b' ]
|
||||
console.log(myArray2) // [ 'b', 'b' ]
|
||||
```
|
||||
|
||||
15
snippets/randomHexColorCode.md
Normal file
15
snippets/randomHexColorCode.md
Normal file
@ -0,0 +1,15 @@
|
||||
### randomHexColorCode
|
||||
|
||||
Generates a random hexadecimal color code.
|
||||
|
||||
Use `Math.random` to generate a random 24-bit(6x4bits) hexadecimal number. Use bit shifting and then convert it to an hexadecimal String using `toString(16)`.
|
||||
|
||||
```js
|
||||
const randomHexColorCode = () => '#'+(Math.random()*0xFFFFFF<<0).toString(16);
|
||||
```
|
||||
|
||||
```js
|
||||
randomHexColorCode() // "#e34155"
|
||||
randomHexColorCode() // "#fd73a6"
|
||||
randomHexColorCode() // "#4144c6"
|
||||
```
|
||||
@ -9,5 +9,5 @@ const randomIntegerInRange = (min, max) => Math.floor(Math.random() * (max - min
|
||||
```
|
||||
|
||||
```js
|
||||
randomIntegerInRange(0, 5) -> 2
|
||||
randomIntegerInRange(0, 5) // 2
|
||||
```
|
||||
|
||||
@ -9,5 +9,5 @@ const randomNumberInRange = (min, max) => Math.random() * (max - min) + min;
|
||||
```
|
||||
|
||||
```js
|
||||
randomNumberInRange(2,10) -> 6.0211363285087005
|
||||
randomNumberInRange(2,10) // 6.0211363285087005
|
||||
```
|
||||
|
||||
@ -14,5 +14,5 @@ const remove = (arr, func) =>
|
||||
```
|
||||
|
||||
```js
|
||||
remove([1, 2, 3, 4], n => n % 2 == 0) -> [2, 4]
|
||||
remove([1, 2, 3, 4], n => n % 2 == 0) // [2, 4]
|
||||
```
|
||||
|
||||
@ -10,5 +10,5 @@ const reverseString = str => str.split('').reverse().join('');
|
||||
```
|
||||
|
||||
```js
|
||||
reverseString('foobar') -> 'raboof'
|
||||
reverseString('foobar') // 'raboof'
|
||||
```
|
||||
|
||||
@ -10,5 +10,5 @@ const runPromisesInSeries = ps => ps.reduce((p, next) => p.then(next), Promise.r
|
||||
|
||||
```js
|
||||
const delay = (d) => new Promise(r => setTimeout(r, d))
|
||||
runPromisesInSeries([() => delay(1000), () => delay(2000)]) -> //executes each promise sequentially, taking a total of 3 seconds to complete
|
||||
runPromisesInSeries([() => delay(1000), () => delay(2000)]) // //executes each promise sequentially, taking a total of 3 seconds to complete
|
||||
```
|
||||
|
||||
@ -10,5 +10,5 @@ const sample = arr => arr[Math.floor(Math.random() * arr.length)];
|
||||
```
|
||||
|
||||
```js
|
||||
sample([3, 7, 9, 11]) -> 9
|
||||
sample([3, 7, 9, 11]) // 9
|
||||
```
|
||||
|
||||
@ -11,5 +11,5 @@ const select = (from, selector) =>
|
||||
|
||||
```js
|
||||
const obj = {selector: {to: {val: 'val to select'}}};
|
||||
select(obj, 'selector.to.val'); -> 'val to select'
|
||||
select(obj, 'selector.to.val'); // 'val to select'
|
||||
```
|
||||
|
||||
@ -11,5 +11,5 @@ const shallowClone = obj => Object.assign({}, obj);
|
||||
```js
|
||||
const a = { x: true, y: 1 };
|
||||
const b = shallowClone(a);
|
||||
a === b -> false
|
||||
a === b // false
|
||||
```
|
||||
|
||||
@ -9,5 +9,5 @@ const similarity = (arr, values) => arr.filter(v => values.includes(v));
|
||||
```
|
||||
|
||||
```js
|
||||
similarity([1,2,3], [1,2,4]) -> [1,2]
|
||||
similarity([1,2,3], [1,2,4]) // [1,2]
|
||||
```
|
||||
|
||||
@ -10,5 +10,5 @@ const sortCharactersInString = str =>
|
||||
```
|
||||
|
||||
```js
|
||||
sortCharactersInString('cabbage') -> 'aabbceg'
|
||||
sortCharactersInString('cabbage') // 'aabbceg'
|
||||
```
|
||||
|
||||
@ -16,5 +16,5 @@ const speechSynthesis = message => {
|
||||
```
|
||||
|
||||
```js
|
||||
speechSynthesis('Hello, World') -> // plays the message
|
||||
speechSynthesis('Hello, World') // // plays the message
|
||||
```
|
||||
|
||||
@ -10,6 +10,6 @@ const spreadOver = fn => argsArr => fn(...argsArr);
|
||||
|
||||
```js
|
||||
const arrayMax = spreadOver(Math.max)
|
||||
arrayMax([1,2,3]) -> 3
|
||||
arrayMax([1,2,4]) -> 4
|
||||
arrayMax([1,2,3]) // 3
|
||||
arrayMax([1,2,4]) // 4
|
||||
```
|
||||
|
||||
@ -17,6 +17,6 @@ const standardDeviation = (arr, usePopulation = false) => {
|
||||
```
|
||||
|
||||
```js
|
||||
standardDeviation([10,2,38,23,38,23,21]) -> 13.284434142114991 (sample)
|
||||
standardDeviation([10,2,38,23,38,23,21], true) -> 12.29899614287479 (population)
|
||||
standardDeviation([10,2,38,23,38,23,21]) // 13.284434142114991 (sample)
|
||||
standardDeviation([10,2,38,23,38,23,21], true) // 12.29899614287479 (population)
|
||||
```
|
||||
|
||||
@ -12,5 +12,5 @@ const symmetricDifference = (a, b) => {
|
||||
```
|
||||
|
||||
```js
|
||||
symmetricDifference([1,2,3], [1,2,4]) -> [3,4]
|
||||
symmetricDifference([1,2,3], [1,2,4]) // [3,4]
|
||||
```
|
||||
|
||||
@ -9,6 +9,6 @@ const tail = arr => arr.length > 1 ? arr.slice(1) : arr;
|
||||
```
|
||||
|
||||
```js
|
||||
tail([1,2,3]) -> [2,3]
|
||||
tail([1]) -> [1]
|
||||
tail([1,2,3]) // [2,3]
|
||||
tail([1]) // [1]
|
||||
```
|
||||
|
||||
@ -9,6 +9,6 @@ const take = (arr, n = 1) => arr.slice(0, n);
|
||||
```
|
||||
|
||||
```js
|
||||
take([1, 2, 3], 5) -> [1, 2, 3]
|
||||
take([1, 2, 3], 0) -> []
|
||||
take([1, 2, 3], 5) // [1, 2, 3]
|
||||
take([1, 2, 3], 0) // []
|
||||
```
|
||||
|
||||
@ -9,6 +9,6 @@ const takeRight = (arr, n = 1) => arr.slice(arr.length - n, arr.length);
|
||||
```
|
||||
|
||||
```js
|
||||
takeRight([1, 2, 3], 2) -> [ 2, 3 ]
|
||||
takeRight([1, 2, 3]) -> [3]
|
||||
takeRight([1, 2, 3], 2) // [ 2, 3 ]
|
||||
takeRight([1, 2, 3]) // [3]
|
||||
```
|
||||
|
||||
@ -12,6 +12,6 @@ const timeTaken = callback => {
|
||||
```
|
||||
|
||||
```js
|
||||
timeTaken(() => Math.pow(2, 10)) -> 1024
|
||||
timeTaken(() => Math.pow(2, 10)) // 1024
|
||||
(logged): timeTaken: 0.02099609375ms
|
||||
```
|
||||
|
||||
@ -15,8 +15,8 @@ const toCamelCase = str => {
|
||||
```
|
||||
|
||||
```js
|
||||
toCamelCase("some_database_field_name") -> 'someDatabaseFieldName'
|
||||
toCamelCase("Some label that needs to be camelized") -> 'someLabelThatNeedsToBeCamelized'
|
||||
toCamelCase("some-javascript-property") -> 'someJavascriptProperty'
|
||||
toCamelCase("some-mixed_string with spaces_underscores-and-hyphens") -> 'someMixedStringWithSpacesUnderscoresAndHyphens'
|
||||
toCamelCase("some_database_field_name") // 'someDatabaseFieldName'
|
||||
toCamelCase("Some label that needs to be camelized") // 'someLabelThatNeedsToBeCamelized'
|
||||
toCamelCase("some-javascript-property") // 'someJavascriptProperty'
|
||||
toCamelCase("some-mixed_string with spaces_underscores-and-hyphens") // 'someMixedStringWithSpacesUnderscoresAndHyphens'
|
||||
```
|
||||
|
||||
@ -13,9 +13,9 @@ const toKebabCase = str =>
|
||||
```
|
||||
|
||||
```js
|
||||
toKebabCase("camelCase") -> 'camel-case'
|
||||
toKebabCase("some text") -> 'some-text'
|
||||
toKebabCase("some-mixed_string With spaces_underscores-and-hyphens") -> 'some-mixed-string-with-spaces-underscores-and-hyphens'
|
||||
toKebabCase("AllThe-small Things") -> "all-the-small-things"
|
||||
toKebabCase('IAmListeningToFMWhileLoadingDifferentURLOnMyBrowserAndAlsoEditingSomeXMLAndHTML') -> "i-am-listening-to-fm-while-loading-different-url-on-my-browser-and-also-editing-xml-and-html"
|
||||
toKebabCase("camelCase") // 'camel-case'
|
||||
toKebabCase("some text") // 'some-text'
|
||||
toKebabCase("some-mixed_string With spaces_underscores-and-hyphens") // 'some-mixed-string-with-spaces-underscores-and-hyphens'
|
||||
toKebabCase("AllThe-small Things") // "all-the-small-things"
|
||||
toKebabCase('IAmListeningToFMWhileLoadingDifferentURLOnMyBrowserAndAlsoEditingSomeXMLAndHTML') // "i-am-listening-to-fm-while-loading-different-url-on-my-browser-and-also-editing-xml-and-html"
|
||||
```
|
||||
|
||||
@ -16,5 +16,5 @@ const toOrdinalSuffix = num => {
|
||||
```
|
||||
|
||||
```js
|
||||
toOrdinalSuffix("123") -> "123rd"
|
||||
toOrdinalSuffix("123") // "123rd"
|
||||
```
|
||||
|
||||
@ -14,10 +14,10 @@ const toSnakeCase = str => {
|
||||
```
|
||||
|
||||
```js
|
||||
toSnakeCase("camelCase") -> 'camel_case'
|
||||
toSnakeCase("some text") -> 'some_text'
|
||||
toSnakeCase("some-javascript-property") -> 'some_javascript_property'
|
||||
toSnakeCase("some-mixed_string With spaces_underscores-and-hyphens") -> 'some_mixed_string_with_spaces_underscores_and_hyphens'
|
||||
toSnakeCase("AllThe-small Things") -> "all_the_smal_things"
|
||||
toSnakeCase('IAmListeningToFMWhileLoadingDifferentURLOnMyBrowserAndAlsoEditingSomeXMLAndHTML') -> "i_am_listening_to_fm_while_loading_different_url_on_my_browser_and_also_editing_some_xml_and_html"
|
||||
toSnakeCase("camelCase") // 'camel_case'
|
||||
toSnakeCase("some text") // 'some_text'
|
||||
toSnakeCase("some-javascript-property") // 'some_javascript_property'
|
||||
toSnakeCase("some-mixed_string With spaces_underscores-and-hyphens") // 'some_mixed_string_with_spaces_underscores_and_hyphens'
|
||||
toSnakeCase("AllThe-small Things") // "all_the_smal_things"
|
||||
toSnakeCase('IAmListeningToFMWhileLoadingDifferentURLOnMyBrowserAndAlsoEditingSomeXMLAndHTML') // "i_am_listening_to_fm_while_loading_different_url_on_my_browser_and_also_editing_some_xml_and_html"
|
||||
```
|
||||
|
||||
@ -11,5 +11,5 @@ const truncateString = (str, num) =>
|
||||
```
|
||||
|
||||
```js
|
||||
truncateString('boomerang', 7) -> 'boom...'
|
||||
truncateString('boomerang', 7) // 'boom...'
|
||||
```
|
||||
|
||||
@ -9,5 +9,5 @@ const truthCheckCollection = (collection, pre) => (collection.every(obj => obj[p
|
||||
```
|
||||
|
||||
```js
|
||||
truthCheckCollection([{"user": "Tinky-Winky", "sex": "male"}, {"user": "Dipsy", "sex": "male"}], "sex") -> true
|
||||
truthCheckCollection([{"user": "Tinky-Winky", "sex": "male"}, {"user": "Dipsy", "sex": "male"}], "sex") // true
|
||||
```
|
||||
|
||||
@ -9,5 +9,5 @@ const union = (a, b) => Array.from(new Set([...a, ...b]));
|
||||
```
|
||||
|
||||
```js
|
||||
union([1,2,3], [4,3,2]) -> [1,2,3,4]
|
||||
union([1,2,3], [4,3,2]) // [1,2,3,4]
|
||||
```
|
||||
|
||||
@ -11,5 +11,5 @@ const validateNumber = n => !isNaN(parseFloat(n)) && isFinite(n) && Number(n) ==
|
||||
```
|
||||
|
||||
```js
|
||||
validateNumber('10') -> true
|
||||
validateNumber('10') // true
|
||||
```
|
||||
|
||||
@ -11,5 +11,5 @@ const without = (arr, ...args) => arr.filter(v => !args.includes(v));
|
||||
```
|
||||
|
||||
```js
|
||||
without([2, 1, 2, 3], 1, 2) -> [3]
|
||||
without([2, 1, 2, 3], 1, 2) // [3]
|
||||
```
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user