Added tagger script, tagged snippets retroactively
This commit is contained in:
65
package-lock.json
generated
65
package-lock.json
generated
@ -65,16 +65,6 @@
|
|||||||
"resolved": "https://registry.npmjs.org/ansi-escapes/-/ansi-escapes-1.4.0.tgz",
|
"resolved": "https://registry.npmjs.org/ansi-escapes/-/ansi-escapes-1.4.0.tgz",
|
||||||
"integrity": "sha1-06ioOzGapneTZisT52HHkRQiMG4="
|
"integrity": "sha1-06ioOzGapneTZisT52HHkRQiMG4="
|
||||||
},
|
},
|
||||||
"ansi-regex": {
|
|
||||||
"version": "0.2.1",
|
|
||||||
"resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-0.2.1.tgz",
|
|
||||||
"integrity": "sha1-DY6UaWej2BQ/k+JOKYUl/BsiNfk="
|
|
||||||
},
|
|
||||||
"ansi-styles": {
|
|
||||||
"version": "1.1.0",
|
|
||||||
"resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-1.1.0.tgz",
|
|
||||||
"integrity": "sha1-6uy/Zs1waIJ2Cy9GkVgrj1XXp94="
|
|
||||||
},
|
|
||||||
"anymatch": {
|
"anymatch": {
|
||||||
"version": "1.3.2",
|
"version": "1.3.2",
|
||||||
"resolved": "https://registry.npmjs.org/anymatch/-/anymatch-1.3.2.tgz",
|
"resolved": "https://registry.npmjs.org/anymatch/-/anymatch-1.3.2.tgz",
|
||||||
@ -333,21 +323,39 @@
|
|||||||
"integrity": "sha1-Sm+gc5nCa7pH8LJJa00PtAjFVQ0="
|
"integrity": "sha1-Sm+gc5nCa7pH8LJJa00PtAjFVQ0="
|
||||||
},
|
},
|
||||||
"chalk": {
|
"chalk": {
|
||||||
"version": "0.5.1",
|
"version": "2.3.0",
|
||||||
"resolved": "https://registry.npmjs.org/chalk/-/chalk-0.5.1.tgz",
|
"resolved": "https://registry.npmjs.org/chalk/-/chalk-2.3.0.tgz",
|
||||||
"integrity": "sha1-Zjs6ZItotV0EaQ1JFnqoN4WPIXQ=",
|
"integrity": "sha512-Az5zJR2CBujap2rqXGaJKaPHyJ0IrUimvYNX+ncCy8PJP4ltOGTrHUIo097ZaL2zMeKYpiCdqDvS6zdrTFok3Q==",
|
||||||
|
"dev": true,
|
||||||
"requires": {
|
"requires": {
|
||||||
"ansi-styles": "1.1.0",
|
"ansi-styles": "3.2.0",
|
||||||
"escape-string-regexp": "1.0.5",
|
"escape-string-regexp": "1.0.5",
|
||||||
"has-ansi": "0.1.0",
|
"supports-color": "4.5.0"
|
||||||
"strip-ansi": "0.3.0",
|
|
||||||
"supports-color": "0.2.0"
|
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
"ansi-styles": {
|
||||||
|
"version": "3.2.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-3.2.0.tgz",
|
||||||
|
"integrity": "sha512-NnSOmMEYtVR2JVMIGTzynRkkaxtiq1xnFBcdQD/DnNCYPoEPsVJhM98BDyaoNOQIi7p4okdi3E27eN7GQbsUug==",
|
||||||
|
"dev": true,
|
||||||
|
"requires": {
|
||||||
|
"color-convert": "1.9.1"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"has-flag": {
|
||||||
|
"version": "2.0.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/has-flag/-/has-flag-2.0.0.tgz",
|
||||||
|
"integrity": "sha1-6CB68cx7MNRGzHC3NLXovhj4jVE=",
|
||||||
|
"dev": true
|
||||||
|
},
|
||||||
"supports-color": {
|
"supports-color": {
|
||||||
"version": "0.2.0",
|
"version": "4.5.0",
|
||||||
"resolved": "https://registry.npmjs.org/supports-color/-/supports-color-0.2.0.tgz",
|
"resolved": "https://registry.npmjs.org/supports-color/-/supports-color-4.5.0.tgz",
|
||||||
"integrity": "sha1-2S3iaU6z9nMjlz1649i1W0wiGQo="
|
"integrity": "sha1-vnoN5ITexcXN34s9WRJQRJEvY1s=",
|
||||||
|
"dev": true,
|
||||||
|
"requires": {
|
||||||
|
"has-flag": "2.0.0"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
@ -442,7 +450,6 @@
|
|||||||
"resolved": "https://registry.npmjs.org/concurrently/-/concurrently-3.5.1.tgz",
|
"resolved": "https://registry.npmjs.org/concurrently/-/concurrently-3.5.1.tgz",
|
||||||
"integrity": "sha512-689HrwGw8Rbk1xtV9C4dY6TPJAvIYZbRbnKSAtfJ7tHqICFGoZ0PCWYjxfmerRyxBG0o3sbG3pe7N8vqPwIHuQ==",
|
"integrity": "sha512-689HrwGw8Rbk1xtV9C4dY6TPJAvIYZbRbnKSAtfJ7tHqICFGoZ0PCWYjxfmerRyxBG0o3sbG3pe7N8vqPwIHuQ==",
|
||||||
"requires": {
|
"requires": {
|
||||||
"chalk": "0.5.1",
|
|
||||||
"commander": "2.6.0",
|
"commander": "2.6.0",
|
||||||
"date-fns": "1.29.0",
|
"date-fns": "1.29.0",
|
||||||
"lodash": "4.17.4",
|
"lodash": "4.17.4",
|
||||||
@ -1346,14 +1353,6 @@
|
|||||||
"function-bind": "1.1.1"
|
"function-bind": "1.1.1"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"has-ansi": {
|
|
||||||
"version": "0.1.0",
|
|
||||||
"resolved": "https://registry.npmjs.org/has-ansi/-/has-ansi-0.1.0.tgz",
|
|
||||||
"integrity": "sha1-hPJlqujA5qiKEtcCKJS3VoiUxi4=",
|
|
||||||
"requires": {
|
|
||||||
"ansi-regex": "0.2.1"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"has-flag": {
|
"has-flag": {
|
||||||
"version": "1.0.0",
|
"version": "1.0.0",
|
||||||
"resolved": "https://registry.npmjs.org/has-flag/-/has-flag-1.0.0.tgz",
|
"resolved": "https://registry.npmjs.org/has-flag/-/has-flag-1.0.0.tgz",
|
||||||
@ -2855,14 +2854,6 @@
|
|||||||
"safe-buffer": "5.1.1"
|
"safe-buffer": "5.1.1"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"strip-ansi": {
|
|
||||||
"version": "0.3.0",
|
|
||||||
"resolved": "https://registry.npmjs.org/strip-ansi/-/strip-ansi-0.3.0.tgz",
|
|
||||||
"integrity": "sha1-JfSOoiynkYfzF0pNuHWTR7sSYiA=",
|
|
||||||
"requires": {
|
|
||||||
"ansi-regex": "0.2.1"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"strip-bom": {
|
"strip-bom": {
|
||||||
"version": "3.0.0",
|
"version": "3.0.0",
|
||||||
"resolved": "https://registry.npmjs.org/strip-bom/-/strip-bom-3.0.0.tgz",
|
"resolved": "https://registry.npmjs.org/strip-bom/-/strip-bom-3.0.0.tgz",
|
||||||
|
|||||||
@ -11,10 +11,13 @@
|
|||||||
"description": "A collection of useful Javascript snippets.",
|
"description": "A collection of useful Javascript snippets.",
|
||||||
"version": "1.0.0",
|
"version": "1.0.0",
|
||||||
"main": "index.js",
|
"main": "index.js",
|
||||||
"devDependencies": {},
|
"devDependencies": {
|
||||||
|
"chalk": "^2.3.0"
|
||||||
|
},
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"build-list": "node ./scripts/builder.js",
|
"build-list": "node ./scripts/builder.js",
|
||||||
"lint": "node ./scripts/lintSnippet.js",
|
"lint": "node ./scripts/lintSnippet.js",
|
||||||
|
"tag": "node ./scripts/tagger.js",
|
||||||
"start": "concurrently --kill-others \"nodemon -e js,md -i README.md -x \\\"npm run build-list\\\"\" \"live-server ./build\""
|
"start": "concurrently --kill-others \"nodemon -e js,md -i README.md -x \\\"npm run build-list\\\"\" \"live-server ./build\""
|
||||||
},
|
},
|
||||||
"repository": {
|
"repository": {
|
||||||
|
|||||||
70
scripts/tagger.js
Normal file
70
scripts/tagger.js
Normal file
@ -0,0 +1,70 @@
|
|||||||
|
var fs = require('fs-extra');
|
||||||
|
var path = require('path');
|
||||||
|
var chalk = require('chalk');
|
||||||
|
|
||||||
|
var snippetsPath = './snippets';
|
||||||
|
|
||||||
|
var snippets = {}, output = '', tagDbData = {}, missingTags = 0, tagDbStats = {};
|
||||||
|
|
||||||
|
const objectFromPairs = arr => arr.reduce((a, v) => (a[v[0]] = v[1], a), {});
|
||||||
|
const countOccurrences = (arr, value) => arr.reduce((a, v) => v === value ? a + 1 : a + 0, 0);
|
||||||
|
|
||||||
|
|
||||||
|
console.time('Tagger');
|
||||||
|
|
||||||
|
try {
|
||||||
|
var 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;
|
||||||
|
});
|
||||||
|
for(var snippet of snippetFilenames){
|
||||||
|
snippets[snippet] = fs.readFileSync(path.join(snippetsPath,snippet),'utf8');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (err){
|
||||||
|
console.log('Error during snippet loading: '+err);
|
||||||
|
process.exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
tagDbData = objectFromPairs(fs.readFileSync('tag_database','utf8').split('\n').map(v => v.split(':').slice(0,2)));
|
||||||
|
// for(var tag of [...new Set(Object.entries(tagDbData).map(x => x[1]))])
|
||||||
|
// tagDbStats[tag] = Object.values(tagDbData).filter(v => v === tag);
|
||||||
|
// console.log(tagDbStats);
|
||||||
|
tagDbStats = Object.entries(tagDbData).reduce((acc, val) => {acc.hasOwnProperty(val[1]) ? acc[val[1]]++ : acc[val[1]] = 1; return acc;}, {});
|
||||||
|
}
|
||||||
|
catch (err){
|
||||||
|
console.log('Error during tag database loading: '+err);
|
||||||
|
process.exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
for(var snippet of Object.entries(snippets))
|
||||||
|
if(tagDbData.hasOwnProperty(snippet[0].slice(0,-3)) && tagDbData[snippet[0].slice(0,-3)].trim())
|
||||||
|
output += `${snippet[0].slice(0,-3)}:${tagDbData[snippet[0].slice(0,-3)].trim()}\n`;
|
||||||
|
else {
|
||||||
|
output += `${snippet[0].slice(0,-3)}:\n`;
|
||||||
|
missingTags++;
|
||||||
|
console.log(`${chalk.red('Tag missing:')} ${snippet[0].slice(0,-3)}`);
|
||||||
|
}
|
||||||
|
fs.writeFileSync('tag_database', output);
|
||||||
|
}
|
||||||
|
catch (err){
|
||||||
|
console.log('Error during README generation: '+err);
|
||||||
|
process.exit(1);
|
||||||
|
}
|
||||||
|
console.log(`\n===Tag database statistics===`)
|
||||||
|
for(var tagData of Object.entries(tagDbStats).filter(v => v[0] !== 'undefined')){
|
||||||
|
console.log(`${chalk.green(tagData[0])}: ${tagData[1]} snippets`);
|
||||||
|
}
|
||||||
|
console.log(`${chalk.blue('Untagged snippets:')} ${missingTags}\n`);
|
||||||
|
|
||||||
|
console.timeEnd('Tagger');
|
||||||
@ -4,7 +4,6 @@ Use `Array.slice()` to create a slice of the array with `n` elements taken from
|
|||||||
|
|
||||||
```js
|
```js
|
||||||
const take = (arr, n = 1) => arr.slice(0, n);
|
const take = (arr, n = 1) => arr.slice(0, n);
|
||||||
|
|
||||||
// take([1, 2, 3], 5) -> [1, 2, 3]
|
// take([1, 2, 3], 5) -> [1, 2, 3]
|
||||||
// take([1, 2, 3], 0) -> []
|
// take([1, 2, 3], 0) -> []
|
||||||
```
|
```
|
||||||
|
|||||||
81
tag_database
Normal file
81
tag_database
Normal file
@ -0,0 +1,81 @@
|
|||||||
|
anagrams-of-string-(with-duplicates):string
|
||||||
|
array-concatenation:array
|
||||||
|
array-difference:array
|
||||||
|
array-intersection:array
|
||||||
|
array-union:array
|
||||||
|
average-of-array-of-numbers:array
|
||||||
|
bottom-visible:browser
|
||||||
|
capitalize-first-letter-of-every-word:string
|
||||||
|
capitalize-first-letter:string
|
||||||
|
chain-asynchronous-functions:function
|
||||||
|
check-for-palindrome:string
|
||||||
|
chunk-array:array
|
||||||
|
collatz-algorithm:math
|
||||||
|
compact:array
|
||||||
|
count-occurrences-of-a-value-in-array:array
|
||||||
|
current-URL:browser
|
||||||
|
curry:function
|
||||||
|
deep-flatten-array:array
|
||||||
|
distance-between-two-points:math
|
||||||
|
divisible-by-number:math
|
||||||
|
drop-elements-in-array:array
|
||||||
|
element-is-visible-in-viewport:browser
|
||||||
|
escape-regular-expression:utility
|
||||||
|
even-or-odd-number:math
|
||||||
|
factorial:math
|
||||||
|
fibonacci-array-generator:math
|
||||||
|
fill-array:array
|
||||||
|
filter-out-non-unique-values-in-an-array:array
|
||||||
|
flatten-array-up-to-depth:array
|
||||||
|
flatten-array:array
|
||||||
|
get-max-value-from-array:array
|
||||||
|
get-min-value-from-array:array
|
||||||
|
get-native-type-of-value:utility
|
||||||
|
get-scroll-position:browser
|
||||||
|
greatest-common-divisor-(GCD):math
|
||||||
|
group-by:array
|
||||||
|
hamming-distance:math
|
||||||
|
head-of-list:array
|
||||||
|
initial-of-list:array
|
||||||
|
initialize-array-with-range:array
|
||||||
|
initialize-array-with-values:array
|
||||||
|
is-array:utility
|
||||||
|
is-boolean:utility
|
||||||
|
is-function:utility
|
||||||
|
is-number:utility
|
||||||
|
is-string:utility
|
||||||
|
is-symbol:utility
|
||||||
|
last-of-list:array
|
||||||
|
measure-time-taken-by-function:utility
|
||||||
|
median-of-array-of-numbers:array
|
||||||
|
object-from-key-value-pairs:object
|
||||||
|
object-to-key-value-pairs:object
|
||||||
|
ordinal-suffix-of-number:utility
|
||||||
|
percentile:math
|
||||||
|
pick:array
|
||||||
|
pipe:function
|
||||||
|
powerset:math
|
||||||
|
promisify:function
|
||||||
|
random-integer-in-range:utility
|
||||||
|
random-number-in-range:utility
|
||||||
|
redirect-to-URL:browser
|
||||||
|
reverse-a-string:string
|
||||||
|
RGB-to-hexadecimal:utility
|
||||||
|
run-promises-in-series:function
|
||||||
|
scroll-to-top:browser
|
||||||
|
shuffle-array:array
|
||||||
|
similarity-between-arrays:array
|
||||||
|
sleep:function
|
||||||
|
sort-characters-in-string-(alphabetical):string
|
||||||
|
standard-deviation:math
|
||||||
|
sum-of-array-of-numbers:array
|
||||||
|
swap-values-of-two-variables:utility
|
||||||
|
tail-of-list:array
|
||||||
|
take:array
|
||||||
|
truncate-a-string:string
|
||||||
|
unique-values-of-array:array
|
||||||
|
URL-parameters:utility
|
||||||
|
UUID-generator:utility
|
||||||
|
validate-email:utility
|
||||||
|
validate-number:utility
|
||||||
|
value-or-default:utility
|
||||||
Reference in New Issue
Block a user