From 6791be0dfee0dc412d733cc3c58c95b13b77a362 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefan=20Feje=C5=A1?= Date: Sun, 17 Dec 2017 22:43:08 +0100 Subject: [PATCH] Update readme and index --- README.md | 42 +++++++++++++++++++-------------------- docs/index.html | 53 +++++++++++++++++++++++++------------------------ 2 files changed, 48 insertions(+), 47 deletions(-) diff --git a/README.md b/README.md index 4cd0838cd..c5ab3f8ba 100644 --- a/README.md +++ b/README.md @@ -238,7 +238,7 @@ Use ES6 `Set` and the `...rest` operator to discard all duplicated values. ```js const distinctValuesOfArray = arr => [...new Set(arr)]; -// unique([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] ``` [⬆ back to top](#table-of-contents) @@ -268,7 +268,7 @@ Use `Array.filter()` to create a new array that contains every nth element of a ```js const everyNth = (arr, nth) => arr.filter((e, i) => i % nth === 0); -// everynth([1,2,3,4,5,6], 2) -> [ 1, 3, 5 ] +// everyNth([1,2,3,4,5,6], 2) -> [ 1, 3, 5 ] ``` [⬆ back to top](#table-of-contents) @@ -370,7 +370,7 @@ You can omit `start` to use a default value of `0`. ```js const initializeArrayWithRange = (end, start = 0) => Array.from({ length: end - start }).map((v, i) => i + start); -// initializeArrayRange(5) -> [0,1,2,3,4] +// initializeArrayWithRange(5) -> [0,1,2,3,4] ``` [⬆ back to top](#table-of-contents) @@ -384,7 +384,7 @@ You can omit `value` to use a default value of `0`. ```js const initializeArrayWithValues = (n, value = 0) => Array(n).fill(value); -// initializeArray(5, 2) -> [2,2,2,2,2] +// initializeArrayWithValues(5, 2) -> [2,2,2,2,2] ``` [⬆ back to top](#table-of-contents) @@ -690,7 +690,7 @@ You can omit `el` to use a default value of `window`. const getScrollPosition = (el = window) => ({x: (el.pageXOffset !== undefined) ? el.pageXOffset : el.scrollLeft, y: (el.pageYOffset !== undefined) ? el.pageYOffset : el.scrollTop}); -// getScrollPos() -> {x: 0, y: 200} +// getScrollPosition() -> {x: 0, y: 200} ``` [⬆ back to top](#table-of-contents) @@ -707,7 +707,7 @@ const getURLParameters = url => url.match(/([^?=&]+)(=([^&]*))/g).reduce( (a, v) => (a[v.slice(0, v.indexOf('='))] = v.slice(v.indexOf('=') + 1), a), {} ); -// 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'} ``` [⬆ back to top](#table-of-contents) @@ -772,7 +772,7 @@ const JSONToDate = arr => { const dt = new Date(parseInt(arr.toString().substr(6))); return `${ dt.getDate() }/${ dt.getMonth() + 1 }/${ dt.getFullYear() }` }; -// jsonToDate(/Date(1489525200000)/) -> "14/3/2017" +// JSONToDate(/Date(1489525200000)/) -> "14/3/2017" ``` [⬆ back to top](#table-of-contents) @@ -876,7 +876,7 @@ const pipeFunctions = (...fns) => fns.reduce((f, g) => (...args) => g(f(...args) /* const add5 = x => x + 5 const multiply = (x, y) => x * y -const multiplyAndAdd5 = pipe(multiply, add5) +const multiplyAndAdd5 = pipeFunctions(multiply, add5) multiplyAndAdd5(5, 2) -> 15 */ ``` @@ -914,7 +914,7 @@ Use `Array.reduce()` to create a promise chain, where each promise returns the n ```js const runPromisesInSeries = ps => ps.reduce((p, next) => p.then(next), Promise.resolve()); // const delay = (d) => new Promise(r => setTimeout(r, d)) -// series([() => 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 ``` [⬆ back to top](#table-of-contents) @@ -947,7 +947,7 @@ Use `Array.reduce()` to add each value to an accumulator, initialized with a val ```js const arrayAverage = arr => arr.reduce((acc, val) => acc + val, 0) / arr.length; -// average([1,2,3]) -> 2 +// arrayAverage([1,2,3]) -> 2 ``` [⬆ back to top](#table-of-contents) @@ -960,7 +960,7 @@ Use `Array.reduce()` to add each value to an accumulator, initialized with a val ```js const arraySum = arr => arr.reduce((acc, val) => acc + val, 0); -// sum([1,2,3,4]) -> 10 +// arraySum([1,2,3,4]) -> 10 ``` [⬆ back to top](#table-of-contents) @@ -1197,7 +1197,7 @@ Use `Math.random()` to generate a random value, map it to the desired range usin ```js const randomNumberInRange = (min, max) => Math.random() * (max - min) + min; -// randomInRange(2,10) -> 6.0211363285087005 +// randomNumberInRange(2,10) -> 6.0211363285087005 ``` [⬆ back to top](#table-of-contents) @@ -1254,7 +1254,7 @@ const speechSynthesis = message => { msg.voice = window.speechSynthesis.getVoices()[0]; window.speechSynthesis.speak(msg); }; -// speak('Hello, World') -> plays the message +// speechSynthesis('Hello, World') -> plays the message ``` [⬆ back to top](#table-of-contents) @@ -1269,7 +1269,7 @@ Use `fs.writeFile()`, template literals and `JSON.stringify()` to write a `json` ```js const fs = require('fs'); const JSONToFile = (obj, filename) => fs.writeFile(`${filename}.json`, JSON.stringify(obj, null, 2)) -// jsonToFile({test: "is passed"}, 'testJsonFile') -> writes the object to 'testJsonFile.json' +// JSONToFile({test: "is passed"}, 'testJsonFile') -> writes the object to 'testJsonFile.json' ``` [⬆ back to top](#table-of-contents) @@ -1291,7 +1291,7 @@ const readFileLines = filename => fs.readFileSync(filename).toString('UTF8').spl line2 line3 ___________________________ - let arr = readFileToArray('test.txt') + let arr = readFileLines('test.txt') console.log(arr) // -> ['line1', 'line2', 'line3'] */ ``` @@ -1317,7 +1317,7 @@ const cleanObj = (obj, keysToKeep = [], childIndicator) => { }) } /* - testObj = {a: 1, b: 2, children: {a: 1, b: 2}} + const testObj = {a: 1, b: 2, children: {a: 1, b: 2}} cleanObj(testObj, ["a"],"children") console.log(testObj)// { a: 1, children : { a: 1}} */ @@ -1504,7 +1504,7 @@ Return the string truncated to the desired length, with `...` appended to the en ```js const truncateString = (str, num) => str.length > num ? str.slice(0, num > 3 ? num - 3 : num) + '...' : str; -// truncate('boomerang', 7) -> 'boom...' +// truncateString('boomerang', 7) -> 'boom...' ``` [⬆ back to top](#table-of-contents) @@ -1519,8 +1519,8 @@ Use `Array.map()`, `split()` and `Array.join()` to join the mapped array for con ```js const extendHex = shortHex => '#' + shortHex.slice(shortHex.startsWith('#') ? 1 : 0).split('').map(x => x+x).join('') -// convertHex('#03f') -> '#0033ff' -// convertHex('05a') -> '#0055aa' +// extendHex('#03f') -> '#0033ff' +// extendHex('05a') -> '#0055aa' ``` [⬆ back to top](#table-of-contents) @@ -1651,7 +1651,7 @@ Convert given RGB parameters to hexadecimal string using bitwise left-shift oper ```js const RGBToHex = (r, g, b) => ((r << 16) + (g << 8) + b).toString(16).padStart(6, '0'); -// rgbToHex(255, 165, 1) -> 'ffa501' +// RGBToHex(255, 165, 1) -> 'ffa501' ``` [⬆ back to top](#table-of-contents) @@ -1704,7 +1704,7 @@ const UUIDGenerator = () => ([1e7] + -1e3 + -4e3 + -8e3 + -1e11).replace(/[018]/g, c => (c ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> c / 4).toString(16) ); -// uuid() -> '7982fcfe-5721-4632-bede-6000885be57d' +// UUIDGenerator() -> '7982fcfe-5721-4632-bede-6000885be57d' ``` [⬆ back to top](#table-of-contents) diff --git a/docs/index.html b/docs/index.html index e11294cd0..2383f3c8c 100644 --- a/docs/index.html +++ b/docs/index.html @@ -23,7 +23,7 @@
-

 30 seconds of code

+

 30 seconds of code

@@ -207,7 +207,7 @@ Recursively flatten each element that is an array.

Returns all the distinct values of an array.

Use ES6 Set and the ...rest operator to discard all duplicated values.

const distinctValuesOfArray = arr => [...new Set(arr)];
-// unique([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]
 

dropElements

Removes elements in an array until the passed function returns true. Returns the remaining elements in the array.

@@ -223,7 +223,7 @@ Returns the remaining elements.

Returns every nth element in an array.

Use Array.filter() to create a new array that contains every nth element of a given array.

const everyNth = (arr, nth) => arr.filter((e, i) => i % nth === 0);
-// everynth([1,2,3,4,5,6], 2) -> [ 1, 3, 5 ]
+// everyNth([1,2,3,4,5,6], 2) -> [ 1, 3, 5 ]
 

filterNonUnique

Filters out the non-unique values in an array.

@@ -276,14 +276,14 @@ Use Array.reduce() to create an object, where the keys are produced You can omit start to use a default value of 0.

const initializeArrayWithRange = (end, start = 0) =>
   Array.from({ length: end - start }).map((v, i) => i + start);
-// initializeArrayRange(5) -> [0,1,2,3,4]
+// initializeArrayWithRange(5) -> [0,1,2,3,4]
 

initializeArrayWithValues

Initializes and fills an array with the specified values.

Use Array(n) to create an array of the desired length, fill(v) to fill it with the desired values. You can omit value to use a default value of 0.

const initializeArrayWithValues = (n, value = 0) => Array(n).fill(value);
-// initializeArray(5, 2) -> [2,2,2,2,2]
+// initializeArrayWithValues(5, 2) -> [2,2,2,2,2]
 

intersection

Returns a list of elements that exist in both arrays.

@@ -449,7 +449,7 @@ You can omit el to use a default value of window.

const getScrollPosition = (el = window) =>
   ({x: (el.pageXOffset !== undefined) ? el.pageXOffset : el.scrollLeft,
     y: (el.pageYOffset !== undefined) ? el.pageYOffset : el.scrollTop});
-// getScrollPos() -> {x: 0, y: 200}
+// getScrollPosition() -> {x: 0, y: 200}
 

getURLParameters

Returns an object containing the parameters of the current URL.

@@ -459,7 +459,7 @@ Pass location.search as the argument to apply to the current url.match(/([^?=&]+)(=([^&]*))/g).reduce( (a, v) => (a[v.slice(0, v.indexOf('='))] = v.slice(v.indexOf('=') + 1), a), {} ); -// 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'}

redirect

Redirects to a specified URL.

@@ -496,7 +496,7 @@ Scroll by a fraction of the distance from top. Use window.requestAnimation const dt = new Date(parseInt(arr.toString().substr(6))); return `${ dt.getDate() }/${ dt.getMonth() + 1 }/${ dt.getFullYear() }` }; -// jsonToDate(/Date(1489525200000)/) -> "14/3/2017" +// JSONToDate(/Date(1489525200000)/) -> "14/3/2017"

toEnglishDate

Converts a date from American format to English format.

@@ -558,7 +558,7 @@ The first (leftmost) function can accept one or more arguments; the remaining fu /* const add5 = x => x + 5 const multiply = (x, y) => x * y -const multiplyAndAdd5 = pipe(multiply, add5) +const multiplyAndAdd5 = pipeFunctions(multiply, add5) multiplyAndAdd5(5, 2) -> 15 */ @@ -581,7 +581,7 @@ Use the ...rest operator to pass in all the parameters.

Use Array.reduce() to create a promise chain, where each promise returns the next promise when resolved.

const runPromisesInSeries = ps => ps.reduce((p, next) => p.then(next), Promise.resolve());
 // const delay = (d) => new Promise(r => setTimeout(r, d))
-// series([() => 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
 

sleep

Delays the execution of an asynchronous function.

@@ -600,13 +600,13 @@ async function sleepyWork() {

Returns the average of an array of numbers.

Use Array.reduce() to add each value to an accumulator, initialized with a value of 0, divide by the length of the array.

const arrayAverage = arr => arr.reduce((acc, val) => acc + val, 0) / arr.length;
-// average([1,2,3]) -> 2
+// arrayAverage([1,2,3]) -> 2
 

arraySum

Returns the sum of an array of numbers.

Use Array.reduce() to add each value to an accumulator, initialized with a value of 0.

const arraySum = arr => arr.reduce((acc, val) => acc + val, 0);
-// sum([1,2,3,4]) -> 10
+// arraySum([1,2,3,4]) -> 10
 

collatz

Applies the Collatz algorithm.

@@ -731,7 +731,7 @@ Then, split('') into individual characters, reverse(),

Returns a random number in the specified range.

Use Math.random() to generate a random value, map it to the desired range using multiplication.

const randomNumberInRange = (min, max) => Math.random() * (max - min) + min;
-// randomInRange(2,10) -> 6.0211363285087005
+// randomNumberInRange(2,10) -> 6.0211363285087005
 

round

Rounds a number to a specified amount of digits.

@@ -766,7 +766,7 @@ Use window.speechSynthesis.speak() to play the message.

msg.voice = window.speechSynthesis.getVoices()[0]; window.speechSynthesis.speak(msg); }; -// speak('Hello, World') -> plays the message +// speechSynthesis('Hello, World') -> plays the message

Node

JSONToFile

@@ -774,7 +774,7 @@ Use window.speechSynthesis.speak() to play the message.

Use fs.writeFile(), template literals and JSON.stringify() to write a json object to a .json file.

const fs = require('fs');
 const JSONToFile = (obj, filename) => fs.writeFile(`${filename}.json`, JSON.stringify(obj, null, 2))
-// jsonToFile({test: "is passed"}, 'testJsonFile') -> writes the object to 'testJsonFile.json'
+// JSONToFile({test: "is passed"}, 'testJsonFile') -> writes the object to 'testJsonFile.json'
 

readFileLines

Returns an array of lines from the specified file.

@@ -789,7 +789,7 @@ contents of test.txt : line2 line3 ___________________________ -let arr = readFileToArray('test.txt') +let arr = readFileLines('test.txt') console.log(arr) // -> ['line1', 'line2', 'line3'] */ @@ -808,7 +808,7 @@ also if you give it a special key(childIndicator) it will search de }) } /* - testObj = {a: 1, b: 2, children: {a: 1, b: 2}} + const testObj = {a: 1, b: 2, children: {a: 1, b: 2}} cleanObj(testObj, ["a"],"children") console.log(testObj)// { a: 1, children : { a: 1}} */ @@ -911,7 +911,7 @@ Combine characters to get a string using join('').

Return the string truncated to the desired length, with ... appended to the end or the original string.

const truncateString = (str, num) =>
   str.length > num ? str.slice(0, num > 3 ? num - 3 : num) + '...' : str;
-// truncate('boomerang', 7) -> 'boom...'
+// truncateString('boomerang', 7) -> 'boom...'
 

Utility

extendHex

@@ -920,8 +920,8 @@ Return the string truncated to the desired length, with ... appende Array.slice() is used to remove # from string start since it's added once.

const extendHex = shortHex =>
   '#' + shortHex.slice(shortHex.startsWith('#') ? 1 : 0).split('').map(x => x+x).join('')
-// convertHex('#03f') -> '#0033ff'
-// convertHex('05a') -> '#0055aa'
+// extendHex('#03f') -> '#0033ff'
+// extendHex('05a') -> '#0055aa'
 

getType

Returns the native type of a value.

@@ -934,12 +934,12 @@ Return the string truncated to the desired length, with ... appende

Converts a colorcode to a rgb() string.

Use bitwise right-shift operator and mask bits with & (and) operator to convert a hexadecimal color code (prefixed with #) to a string with the RGB values. In case it's a 3-digit-colorcode, do the same with the 6-digit-colorcode extended by the extendHex() function (ref. extendHex snippet)

const hexToRgb = hex => {
-  const extendHex = shortHex =>
+  const extendHex = shortHex => 
     '#' + shortHex.slice(shortHex.startsWith('#') ? 1 : 0).split('').map(x => x+x).join('');
-  return hex.slice(1).length==3 ?
+  return hex.slice(1).length==3 ? 
   `rgb(${parseInt(extendHex(hex).slice(1), 16) >> 16}, ${(parseInt(extendHex(hex).slice(1), 16) & 0x00ff00) >> 8}, ${parseInt(extendHex(hex).slice(1), 16) & 0x0000ff})`:
   `rgb(${parseInt(hex.slice(1), 16) >> 16}, ${(parseInt(hex.slice(1), 16) & 0x00ff00) >> 8}, ${parseInt(hex.slice(1), 16) & 0x0000ff})`;
-}
+}   
 // hexToRgb('#27ae60') -> 'rgb(39, 174, 96)'
 // hexToRgb('#acd') -> 'rgb(170, 204, 221)'
 
@@ -989,7 +989,7 @@ Return the string truncated to the desired length, with ... appende

Converts the values of RGB components to a colorcode.

Convert given RGB parameters to hexadecimal string using bitwise left-shift operator (<<) and toString(16), then padStart(6,'0') to get a 6-digit hexadecimal value.

const RGBToHex = (r, g, b) => ((r << 16) + (g << 8) + b).toString(16).padStart(6, '0');
-// rgbToHex(255, 165, 1) -> 'ffa501'
+// RGBToHex(255, 165, 1) -> 'ffa501'
 

timeTaken

Measures the time taken by a function to execute.

@@ -1021,7 +1021,7 @@ If digit is found in teens pattern, use teens ordinal.

([1e7] + -1e3 + -4e3 + -8e3 + -1e11).replace(/[018]/g, c => (c ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> c / 4).toString(16) ); -// uuid() -> '7982fcfe-5721-4632-bede-6000885be57d' +// UUIDGenerator() -> '7982fcfe-5721-4632-bede-6000885be57d'

validateEmail

Returns true if the given string is a valid email, false otherwise.

@@ -1042,7 +1042,8 @@ Use Number() to check if the coercion holds.


- + +