From 05aeadf916e8d6ca0768ab736947ea5068ce7edd Mon Sep 17 00:00:00 2001 From: atomiks Date: Sun, 31 Dec 2017 03:40:22 +1100 Subject: [PATCH 1/4] Test readme gen --- README.md | 36 ++++++++++++++++++++++++------------ 1 file changed, 24 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index e036b03bd..d2303ccd5 100644 --- a/README.md +++ b/README.md @@ -262,7 +262,8 @@ -## Adapter +--- + ## Adapter ### call @@ -424,7 +425,8 @@ arrayMax([1, 2, 4]); // 4
[⬆ Back to top](#table-of-contents) -## Array +--- + ## Array ### chunk @@ -1437,7 +1439,8 @@ zipObject(['a', 'b'], [1, 2, 3]); // {a: 1, b: 2}
[⬆ Back to top](#table-of-contents) -## Browser +--- + ## Browser ### arrayToHtmlList @@ -1888,7 +1891,8 @@ UUIDGeneratorBrowser(); // '7982fcfe-5721-4632-bede-6000885be57d'
[⬆ Back to top](#table-of-contents) -## Date +--- + ## Date ### getDaysDiffBetweenDates @@ -1988,7 +1992,8 @@ tomorrow(); // 2017-12-27 (if current date is 2017-12-26)
[⬆ Back to top](#table-of-contents) -## Function +--- + ## Function ### chainAsync @@ -2151,7 +2156,8 @@ async function sleepyWork() {
[⬆ Back to top](#table-of-contents) -## Logic +--- + ## Logic ### negate @@ -2175,7 +2181,8 @@ negate(isOdd)(1); // false
[⬆ Back to top](#table-of-contents) -## Math +--- + ## Math ### average @@ -2880,7 +2887,8 @@ sum([1, 2, 3, 4]); // 10
[⬆ Back to top](#table-of-contents) -## Media +--- + ## Media ### speechSynthesis @@ -2910,7 +2918,8 @@ speechSynthesis('Hello, World'); // // plays the message
[⬆ Back to top](#table-of-contents) -## Node +--- + ## Node ### JSONToFile @@ -2999,7 +3008,8 @@ UUIDGeneratorNode(); // '79c7c136-60ee-40a2-beb2-856f1feabefc'
[⬆ Back to top](#table-of-contents) -## Object +--- + ## Object ### cleanObj @@ -3214,7 +3224,8 @@ truthCheckCollection([{ user: 'Tinky-Winky', sex: 'male' }, { user: 'Dipsy', sex
[⬆ Back to top](#table-of-contents) -## String +--- + ## String ### anagrams @@ -3738,7 +3749,8 @@ words('python, javaScript & coffee'); // ["python", "javaScript", "coffee"]
[⬆ Back to top](#table-of-contents) -## Utility +--- + ## Utility ### coalesce From 4a142715056959d8c448a72307df42ace27ac40a Mon Sep 17 00:00:00 2001 From: atomiks Date: Sun, 31 Dec 2017 03:55:27 +1100 Subject: [PATCH 2/4] Add emojis --- README.md | 48 +++++++++---------- scripts/build.js | 122 ++++++++++++++++++++++++++++------------------- 2 files changed, 98 insertions(+), 72 deletions(-) diff --git a/README.md b/README.md index d2303ccd5..16e26af95 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,7 @@ ## Table of Contents -### Adapter +### 🔌 Adapter
View contents @@ -28,7 +28,7 @@
-### Array +### 📚 Array
View contents @@ -76,7 +76,7 @@
-### Browser +### 🖥️ Browser
View contents @@ -102,7 +102,7 @@
-### Date +### ⏱️ Date
View contents @@ -114,7 +114,7 @@
-### Function +### 🎛️ Function
View contents @@ -128,7 +128,7 @@
-### Logic +### 🔮 Logic
View contents @@ -137,7 +137,7 @@
-### Math +### ➗ Math
View contents @@ -173,7 +173,7 @@
-### Media +### 📺 Media
View contents @@ -182,7 +182,7 @@
-### Node +### 📦 Node
View contents @@ -193,7 +193,7 @@
-### Object +### 🗃️ Object
View contents @@ -209,7 +209,7 @@
-### String +### 📜 String
View contents @@ -236,7 +236,7 @@
-### Utility +### 💎 Utility
View contents @@ -263,7 +263,7 @@
--- - ## Adapter + ## 🔌 Adapter ### call @@ -426,7 +426,7 @@ arrayMax([1, 2, 4]); // 4
[⬆ Back to top](#table-of-contents) --- - ## Array + ## 📚 Array ### chunk @@ -1440,7 +1440,7 @@ zipObject(['a', 'b'], [1, 2, 3]); // {a: 1, b: 2}
[⬆ Back to top](#table-of-contents) --- - ## Browser + ## 🖥️ Browser ### arrayToHtmlList @@ -1892,7 +1892,7 @@ UUIDGeneratorBrowser(); // '7982fcfe-5721-4632-bede-6000885be57d'
[⬆ Back to top](#table-of-contents) --- - ## Date + ## ⏱️ Date ### getDaysDiffBetweenDates @@ -1993,7 +1993,7 @@ tomorrow(); // 2017-12-27 (if current date is 2017-12-26)
[⬆ Back to top](#table-of-contents) --- - ## Function + ## 🎛️ Function ### chainAsync @@ -2157,7 +2157,7 @@ async function sleepyWork() {
[⬆ Back to top](#table-of-contents) --- - ## Logic + ## 🔮 Logic ### negate @@ -2182,7 +2182,7 @@ negate(isOdd)(1); // false
[⬆ Back to top](#table-of-contents) --- - ## Math + ## ➗ Math ### average @@ -2888,7 +2888,7 @@ sum([1, 2, 3, 4]); // 10
[⬆ Back to top](#table-of-contents) --- - ## Media + ## 📺 Media ### speechSynthesis @@ -2919,7 +2919,7 @@ speechSynthesis('Hello, World'); // // plays the message
[⬆ Back to top](#table-of-contents) --- - ## Node + ## 📦 Node ### JSONToFile @@ -3009,7 +3009,7 @@ UUIDGeneratorNode(); // '79c7c136-60ee-40a2-beb2-856f1feabefc'
[⬆ Back to top](#table-of-contents) --- - ## Object + ## 🗃️ Object ### cleanObj @@ -3225,7 +3225,7 @@ truthCheckCollection([{ user: 'Tinky-Winky', sex: 'male' }, { user: 'Dipsy', sex
[⬆ Back to top](#table-of-contents) --- - ## String + ## 📜 String ### anagrams @@ -3750,7 +3750,7 @@ words('python, javaScript & coffee'); // ["python", "javaScript", "coffee"]
[⬆ Back to top](#table-of-contents) --- - ## Utility + ## 💎 Utility ### coalesce diff --git a/scripts/build.js b/scripts/build.js index a6e6d607d..1f400a04e 100644 --- a/scripts/build.js +++ b/scripts/build.js @@ -3,51 +3,64 @@ Run using `npm run builder`. */ // Load modules -const fs = require('fs-extra'), - path = require('path'), - chalk = require('chalk'); -// Set variables for paths -const snippetsPath = './snippets', - staticPartsPath = './static-parts'; -// Set variables for script -let snippets = {}, - startPart = '', +const fs = require('fs-extra'); +const path = require('path'); +const chalk = require('chalk'); + +const SNIPPETS_PATH = './snippets'; +const STATIC_PARTS_PATH = './static-parts'; + +const snippets = {}; +const emojis = { + adapter: '🔌', + array: '📚', + browser: '🖥️', + date: '⏱️', + function: '🎛️', + logic: '🔮', + math: '➗', + media: '📺', + node: '📦', + object: '🗃️', + string: '📜', + utility: '💎' +}; + +let startPart = '', endPart = '', output = '', tagDbData = {}; + // Load helper functions (these are from existing snippets in 30 seconds of code!) const objectFromPairs = arr => arr.reduce((a, v) => ((a[v[0]] = v[1]), a), {}); const capitalize = (str, lowerRest = false) => str.slice(0, 1).toUpperCase() + (lowerRest ? str.slice(1).toLowerCase() : str.slice(1)); -// Start the timer of the script + console.time('Builder'); + // Synchronously read all snippets and sort them as necessary (case-insensitive) try { - let snippetFilenames = fs.readdirSync(snippetsPath); - snippetFilenames.sort((a, b) => { - a = a.toLowerCase(); - b = b.toLowerCase(); - if (a < b) return -1; - if (a > b) return 1; - return 0; - }); + const snippetFilenames = fs + .readdirSync(SNIPPETS_PATH) + .sort((a, b) => a.toLowerCase() - b.toLowerCase()); // Store the data read from each snippet in the appropriate object - for (let snippet of snippetFilenames) - snippets[snippet] = fs.readFileSync(path.join(snippetsPath, snippet), 'utf8'); + for (const name of snippetFilenames) { + snippets[name] = fs.readFileSync(path.join(SNIPPETS_PATH, name), 'utf8'); + } } catch (err) { - // Handle errors (hopefully not!) console.log(`${chalk.red('ERROR!')} During snippet loading: ${err}`); process.exit(1); } + // Load static parts for the README file try { - startPart = fs.readFileSync(path.join(staticPartsPath, 'README-start.md'), 'utf8'); - endPart = fs.readFileSync(path.join(staticPartsPath, 'README-end.md'), 'utf8'); + startPart = fs.readFileSync(path.join(STATIC_PARTS_PATH, 'README-start.md'), 'utf8'); + endPart = fs.readFileSync(path.join(STATIC_PARTS_PATH, 'README-end.md'), 'utf8'); } catch (err) { - // Handle errors (hopefully not!) console.log(`${chalk.red('ERROR!')} During static part loading: ${err}`); process.exit(1); } + // Load tag data from the database try { tagDbData = objectFromPairs( @@ -58,48 +71,62 @@ try { .map(v => v.split(':').slice(0, 2)) ); } catch (err) { - // Handle errors (hopefully not!) console.log(`${chalk.red('ERROR!')} During tag database loading: ${err}`); process.exit(1); } + // Create the output for the README file try { + const tags = [ + ...new Set( + Object.entries(tagDbData) + .map(t => t[1]) + .filter(v => v) + .sort((a, b) => a.localeCompare(b)) + ) + ]; + // Add the start static part output += `${startPart + '\n'}`; - // Loop over tags and snippets to create the table of contents let uncategorizedOutput = ''; - for (let tag of [...new Set(Object.entries(tagDbData).map(t => t[1]))] - .filter(v => v) - .sort((a, b) => a.localeCompare(b))) { - if (capitalize(tag, true) == 'Uncategorized') { - uncategorizedOutput += `### _${capitalize( - tag, - true - )}_\n\n
\nView contents\n\n`; - for (let taggedSnippet of Object.entries(tagDbData).filter(v => v[1] === tag)) + + // Loop over tags and snippets to create the table of contents + for (const tag of tags) { + const capitalizedTag = capitalize(tag, true); + + if (capitalizedTag === 'Uncategorized') { + uncategorizedOutput += `### _${capitalizedTag}_\n\n
\nView contents\n\n`; + for (const taggedSnippet of Object.entries(tagDbData).filter(v => v[1] === tag)) { uncategorizedOutput += `* [\`${taggedSnippet[0]}\`](#${taggedSnippet[0].toLowerCase()})\n`; + } uncategorizedOutput += '\n
\n\n'; } else { - output += `### ${capitalize(tag, true)}\n\n
\nView contents\n\n`; - for (let taggedSnippet of Object.entries(tagDbData).filter(v => v[1] === tag)) + output += `### ${ + emojis[tag] + } ${capitalizedTag}\n\n
\nView contents\n\n`; + for (const taggedSnippet of Object.entries(tagDbData).filter(v => v[1] === tag)) { output += `* [\`${taggedSnippet[0]}\`](#${taggedSnippet[0].toLowerCase()})\n`; + } output += '\n
\n\n'; } } + output += uncategorizedOutput; uncategorizedOutput = ''; + // Loop over tags and snippets to create the list of snippets - for (let tag of [...new Set(Object.entries(tagDbData).map(t => t[1]))] - .filter(v => v) - .sort((a, b) => a.localeCompare(b))) { - if (capitalize(tag, true) == 'Uncategorized') { - uncategorizedOutput += `## _${capitalize(tag, true)}_\n`; - for (let taggedSnippet of Object.entries(tagDbData).filter(v => v[1] === tag)) + for (const tag of tags) { + const capitalizedTag = capitalize(tag, true); + + if (capitalizedTag == 'Uncategorized') { + uncategorizedOutput += `---\n ## _${capitalizedTag}_\n`; + for (const taggedSnippet of Object.entries(tagDbData).filter(v => v[1] === tag)) { uncategorizedOutput += `\n${snippets[taggedSnippet[0] + '.md'] + '\n
[⬆ back to top](#table-of-contents)\n\n'}`; + } } else { - output += `## ${capitalize(tag, true)}\n`; - for (let taggedSnippet of Object.entries(tagDbData).filter(v => v[1] === tag)) { + output += `---\n ## ${emojis[tag]} ${capitalizedTag}\n`; + for (const taggedSnippet of Object.entries(tagDbData).filter(v => v[1] === tag)) { let data = snippets[taggedSnippet[0] + '.md']; data = data.slice(0, data.lastIndexOf('```js')) + @@ -111,17 +138,16 @@ try { } } } + output += uncategorizedOutput; // Add the ending static part output += `\n${endPart + '\n'}`; // Write to the README file fs.writeFileSync('README.md', output); } catch (err) { - // Handle errors (hopefully not!) console.log(`${chalk.red('ERROR!')} During README generation: ${err}`); process.exit(1); } -// Log a success message + console.log(`${chalk.green('SUCCESS!')} README file generated!`); -// Log the time taken console.timeEnd('Builder'); From 418406563b38f715a06292254315ae319234c3f7 Mon Sep 17 00:00:00 2001 From: atomiks Date: Sun, 31 Dec 2017 03:57:00 +1100 Subject: [PATCH 3/4] revert README back --- README.md | 139 +++++++++++++++++++++++++----------------------------- 1 file changed, 63 insertions(+), 76 deletions(-) diff --git a/README.md b/README.md index 16e26af95..d3467da01 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,7 @@ ## Table of Contents -### 🔌 Adapter +### Adapter
View contents @@ -28,7 +28,7 @@
-### 📚 Array +### Array
View contents @@ -76,7 +76,7 @@
-### 🖥️ Browser +### Browser
View contents @@ -102,7 +102,7 @@
-### ⏱️ Date +### Date
View contents @@ -114,7 +114,7 @@
-### 🎛️ Function +### Function
View contents @@ -128,7 +128,7 @@
-### 🔮 Logic +### Logic
View contents @@ -137,7 +137,7 @@
-### ➗ Math +### Math
View contents @@ -173,7 +173,7 @@
-### 📺 Media +### Media
View contents @@ -182,7 +182,7 @@
-### 📦 Node +### Node
View contents @@ -193,7 +193,7 @@
-### 🗃️ Object +### Object
View contents @@ -209,7 +209,7 @@
-### 📜 String +### String
View contents @@ -236,7 +236,7 @@
-### 💎 Utility +### Utility
View contents @@ -262,19 +262,18 @@
---- - ## 🔌 Adapter +## Adapter + +### call + +Given a key and a set of arguments, call them when given a context. Primarily useful in composition. + +Use a closure to call a stored key with stored arguments. -### call - -Given a key and a set of arguments, call them when given a context. Primarily useful in composition. - -Use a closure to call a stored key with stored arguments. - ```js const call = (key, ...args) => context => context[key](...args); -``` - +``` +
Examples @@ -286,23 +285,23 @@ const map = call.bind(null, 'map'); Promise.resolve([1, 2, 3]) .then(map(x => 2 * x)) .then(console.log); //[ 2, 4, 6 ] -``` +```

[⬆ Back to top](#table-of-contents) -### collectInto - -Changes a function that accepts an array into a variadic function. - -Given a function, return a closure that collects all inputs into an array-accepting function. - +### collectInto + +Changes a function that accepts an array into a variadic function. + +Given a function, return a closure that collects all inputs into an array-accepting function. + ```js const collectInto = fn => (...args) => fn(args); -``` - +``` +
Examples @@ -312,23 +311,23 @@ let p1 = Promise.resolve(1); let p2 = Promise.resolve(2); let p3 = new Promise(resolve => setTimeout(resolve, 2000, 3)); Pall(p1, p2, p3).then(console.log); -``` +```

[⬆ Back to top](#table-of-contents) -### flip - -Flip takes a function as an argument, then makes the first argument the last - -Return a closure that takes variadic inputs, and splices the last argument to make it the first argument before applying the rest. - +### flip + +Flip takes a function as an argument, then makes the first argument the last + +Return a closure that takes variadic inputs, and splices the last argument to make it the first argument before applying the rest. + ```js const flip = fn => (...args) => fn(args.pop(), ...args); -``` - +``` +
Examples @@ -340,7 +339,7 @@ let mergePerson = mergeFrom.bind(null, a); mergePerson(b); // == b b = {}; Object.assign(b, a); // == b -``` +```
@@ -402,16 +401,16 @@ delay(2000).then(() => console.log('Hi!')); // // Promise resolves after 2s
[⬆ Back to top](#table-of-contents) -### spreadOver - -Takes a variadic function and returns a closure that accepts an array of arguments to map to the inputs of the function. - -Use closures and the spread operator (`...`) to map the array of arguments to the inputs of the function. - +### spreadOver + +Takes a variadic function and returns a closure that accepts an array of arguments to map to the inputs of the function. + +Use closures and the spread operator (`...`) to map the array of arguments to the inputs of the function. + ```js const spreadOver = fn => argsArr => fn(...argsArr); -``` - +``` +
Examples @@ -419,14 +418,13 @@ const spreadOver = fn => argsArr => fn(...argsArr); const arrayMax = spreadOver(Math.max); arrayMax([1, 2, 3]); // 3 arrayMax([1, 2, 4]); // 4 -``` +```

[⬆ Back to top](#table-of-contents) ---- - ## 📚 Array +## Array ### chunk @@ -1112,8 +1110,8 @@ console.log(pulled); // [ 'b', 'd' ] QuickSort an Array (ascending sort by default). -Use recursion. -Use `Array.filter` and spread operator (`...`) to create an array that all elements with values less than the pivot come before the pivot, and all elements with values greater than the pivot come after it. +Use recursion. +Use `Array.filter` and spread operator (`...`) to create an array that all elements with values less than the pivot come before the pivot, and all elements with values greater than the pivot come after it. If the parameter `desc` is truthy, return array sorts in descending order. ```js @@ -1439,8 +1437,7 @@ zipObject(['a', 'b'], [1, 2, 3]); // {a: 1, b: 2}
[⬆ Back to top](#table-of-contents) ---- - ## 🖥️ Browser +## Browser ### arrayToHtmlList @@ -1713,7 +1710,7 @@ const httpsRedirect = () => { Run the callback whenever the user input type changes (`mouse` or `touch`). Useful for enabling/disabling code depending on the input device. This process is dynamic and works with hybrid devices (e.g. touchscreen laptops). -Use two event listeners. Assume `mouse` input initially and bind a `touchstart` event listener to the document. +Use two event listeners. Assume `mouse` input initially and bind a `touchstart` event listener to the document. On `touchstart`, add a `mousemove` event listener to listen for two consecutive `mousemove` events firing within 20ms, using `performance.now()`. Run the callback with the input type as an argument in either of these situations. @@ -1891,8 +1888,7 @@ UUIDGeneratorBrowser(); // '7982fcfe-5721-4632-bede-6000885be57d'
[⬆ Back to top](#table-of-contents) ---- - ## ⏱️ Date +## Date ### getDaysDiffBetweenDates @@ -1992,8 +1988,7 @@ tomorrow(); // 2017-12-27 (if current date is 2017-12-26)
[⬆ Back to top](#table-of-contents) ---- - ## 🎛️ Function +## Function ### chainAsync @@ -2156,8 +2151,7 @@ async function sleepyWork() {
[⬆ Back to top](#table-of-contents) ---- - ## 🔮 Logic +## Logic ### negate @@ -2181,8 +2175,7 @@ negate(isOdd)(1); // false
[⬆ Back to top](#table-of-contents) ---- - ## ➗ Math +## Math ### average @@ -2887,8 +2880,7 @@ sum([1, 2, 3, 4]); // 10
[⬆ Back to top](#table-of-contents) ---- - ## 📺 Media +## Media ### speechSynthesis @@ -2918,8 +2910,7 @@ speechSynthesis('Hello, World'); // // plays the message
[⬆ Back to top](#table-of-contents) ---- - ## 📦 Node +## Node ### JSONToFile @@ -3008,8 +2999,7 @@ UUIDGeneratorNode(); // '79c7c136-60ee-40a2-beb2-856f1feabefc'
[⬆ Back to top](#table-of-contents) ---- - ## 🗃️ Object +## Object ### cleanObj @@ -3224,8 +3214,7 @@ truthCheckCollection([{ user: 'Tinky-Winky', sex: 'male' }, { user: 'Dipsy', sex
[⬆ Back to top](#table-of-contents) ---- - ## 📜 String +## String ### anagrams @@ -3749,8 +3738,7 @@ words('python, javaScript & coffee'); // ["python", "javaScript", "coffee"]
[⬆ Back to top](#table-of-contents) ---- - ## 💎 Utility +## Utility ### coalesce @@ -4215,4 +4203,3 @@ validateNumber('10'); // true ## Credits *Icons made by [Smashicons](https://www.flaticon.com/authors/smashicons) from [www.flaticon.com](https://www.flaticon.com/) is licensed by [CC 3.0 BY](http://creativecommons.org/licenses/by/3.0/).* - From 1502d75d1379e892cf243d003fd607c0d10f45ee Mon Sep 17 00:00:00 2001 From: atomiks Date: Sun, 31 Dec 2017 04:02:28 +1100 Subject: [PATCH 4/4] handle new categories --- scripts/build.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/scripts/build.js b/scripts/build.js index 1f400a04e..b1e49a0a5 100644 --- a/scripts/build.js +++ b/scripts/build.js @@ -102,7 +102,7 @@ try { uncategorizedOutput += '\n
\n\n'; } else { output += `### ${ - emojis[tag] + emojis[tag] || '' } ${capitalizedTag}\n\n
\nView contents\n\n`; for (const taggedSnippet of Object.entries(tagDbData).filter(v => v[1] === tag)) { output += `* [\`${taggedSnippet[0]}\`](#${taggedSnippet[0].toLowerCase()})\n`; @@ -125,7 +125,7 @@ try { '\n
[⬆ back to top](#table-of-contents)\n\n'}`; } } else { - output += `---\n ## ${emojis[tag]} ${capitalizedTag}\n`; + output += `---\n ## ${emojis[tag] || ''} ${capitalizedTag}\n`; for (const taggedSnippet of Object.entries(tagDbData).filter(v => v[1] === tag)) { let data = snippets[taggedSnippet[0] + '.md']; data =