Updated tools, proper linting, build README

This commit is contained in:
Angelos Chalaris
2017-12-14 19:38:11 +02:00
parent 0a2175e00e
commit e0693339b4
13 changed files with 74 additions and 61 deletions

View File

@ -7,8 +7,8 @@ Here's what you can do to help:
- [Open issues](https://github.com/Chalarangelo/30-seconds-of-code/issues/new) for things you want to see added or modified. - [Open issues](https://github.com/Chalarangelo/30-seconds-of-code/issues/new) for things you want to see added or modified.
- Be part of the discussion by helping out with [existing issues](https://github.com/Chalarangelo/30-seconds-of-code/issues) or talking on our [gitter channel](https://gitter.im/30-seconds-of-code/Lobby). - Be part of the discussion by helping out with [existing issues](https://github.com/Chalarangelo/30-seconds-of-code/issues) or talking on our [gitter channel](https://gitter.im/30-seconds-of-code/Lobby).
- Submit [pull requests](https://github.com/Chalarangelo/30-seconds-of-code/pulls) with snippets you have created (see below for guidelines). - Submit [pull requests](https://github.com/Chalarangelo/30-seconds-of-code/pulls) with snippets you have created (see below for guidelines).
- Fix typos in existing snippets or run `npm run linter "snippet-name.md"` on unlinted snippets (yes, this is something we actually want help with).
- Tag untagged snippets by running `npm run tagger` and adding the appropriate tag next to the script name in `tag_database`. - Tag untagged snippets by running `npm run tagger` and adding the appropriate tag next to the script name in `tag_database`.
- Fix typos in existing snippets or run `npm run linter` to lint unlinted snippets (yes, this is something we actually want help with, as this can take quite a while to run).
### Snippet submission and Pull request guidelines ### Snippet submission and Pull request guidelines

View File

@ -122,7 +122,7 @@ const arrayConcat = (arr, ...args) => arr.concat(...args);
Create a `Set` from `b`, then use `Array.filter()` on `a` to only keep values not contained in `b`. Create a `Set` from `b`, then use `Array.filter()` on `a` to only keep values not contained in `b`.
```js ```js
const difference = (a, b) => { const s = new Set(b); return a.filter(x => !s.has(x)); } const difference = (a, b) => { const s = new Set(b); return a.filter(x => !s.has(x)); };
// difference([1,2,3], [1,2]) -> [3] // difference([1,2,3], [1,2]) -> [3]
``` ```
@ -133,7 +133,7 @@ const difference = (a, b) => { const s = new Set(b); return a.filter(x => !s.has
Create a `Set` from `b`, then use `Array.filter()` on `a` to only keep values contained in `b`. Create a `Set` from `b`, then use `Array.filter()` on `a` to only keep values contained in `b`.
```js ```js
const intersection = (a, b) => { const s = new Set(b); return a.filter(x => s.has(x)); } const intersection = (a, b) => { const s = new Set(b); return a.filter(x => s.has(x)); };
// intersection([1,2,3], [4,3,2]) -> [2,3] // intersection([1,2,3], [4,3,2]) -> [2,3]
``` ```
@ -144,7 +144,7 @@ const intersection = (a, b) => { const s = new Set(b); return a.filter(x => s.ha
Create a `Set` with all values of `a` and `b` and convert to an array. Create a `Set` with all values of `a` and `b` and convert to an array.
```js ```js
const union = (a, b) => Array.from(new Set([...a, ...b])) const union = (a, b) => Array.from(new Set([...a, ...b]));
// union([1,2,3], [4,3,2]) -> [1,2,3,4] // union([1,2,3], [4,3,2]) -> [1,2,3,4]
``` ```
@ -216,10 +216,10 @@ Loop through the array, using `Array.shift()` to drop the first element of the a
Returns the remaining elements. Returns the remaining elements.
```js ```js
const dropElements = (arr,func) => { const dropElements = (arr, func) => {
while(arr.length > 0 && !func(arr[0])) arr.shift(); while (arr.length > 0 && !func(arr[0])) arr.shift();
return arr; return arr;
} };
// dropElements([1, 2, 3, 4], n => n >= 3) -> [3,4] // dropElements([1, 2, 3, 4], n => n >= 3) -> [3,4]
``` ```
@ -232,7 +232,7 @@ Omit `start` to start at the first element and/or `end` to finish at the last.
```js ```js
const fillArray = (arr, value, start = 0, end = arr.length) => const fillArray = (arr, value, start = 0, end = arr.length) =>
arr.map((v,i) => i>=start && i<end ? value : v); arr.map((v, i) => i >= start && i < end ? value : v);
// fillArray([1,2,3,4],'8',1,3) -> [1,'8','8',4] // fillArray([1,2,3,4],'8',1,3) -> [1,'8','8',4]
``` ```
@ -258,8 +258,8 @@ Omit the second element, `depth` to flatten only to a depth of `1` (single flatt
```js ```js
const flattenDepth = (arr, depth = 1) => const flattenDepth = (arr, depth = 1) =>
depth != 1 ? arr.reduce((a, v) => a.concat(Array.isArray(v) ? flattenDepth (v, depth-1) : v), []) depth != 1 ? arr.reduce((a, v) => a.concat(Array.isArray(v) ? flattenDepth(v, depth - 1) : v), [])
: arr.reduce((a,v) => a.concat(v),[]); : arr.reduce((a, v) => a.concat(v), []);
// flattenDepth([1,[2],[[[3],4],5]], 2) -> [1,2,[3],4,5] // flattenDepth([1,[2],[[[3],4],5]], 2) -> [1,2,[3],4,5]
``` ```
@ -506,7 +506,7 @@ const elementIsVisibleInViewport = (el, partiallyVisible = false) => {
? ((top > 0 && top < innerHeight) || (bottom > 0 && bottom < innerHeight)) && ? ((top > 0 && top < innerHeight) || (bottom > 0 && bottom < innerHeight)) &&
((left > 0 && left < innerWidth) || (right > 0 && right < innerWidth)) ((left > 0 && left < innerWidth) || (right > 0 && right < innerWidth))
: top >= 0 && left >= 0 && bottom <= innerHeight && right <= innerWidth; : top >= 0 && left >= 0 && bottom <= innerHeight && right <= innerWidth;
} };
// e.g. 100x100 viewport and a 10x10px element at position {top: -1, left: 0, bottom: 9, right: 10} // 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) -> false (not fully visible)
// elementIsVisibleInViewport(el, true) -> true (partially visible) // elementIsVisibleInViewport(el, true) -> true (partially visible)
@ -664,7 +664,7 @@ async function sleepyWork() {
If `n` is even, return `n/2`. Otherwise return `3n+1`. If `n` is even, return `n/2`. Otherwise return `3n+1`.
```js ```js
const collatz = n => (n % 2 == 0) ? (n/2) : (3*n+1); const collatz = n => (n % 2 == 0) ? (n / 2) : (3 * n + 1);
// collatz(8) --> 4 // collatz(8) --> 4
// collatz(5) --> 16 // collatz(5) --> 16
``` ```
@ -795,7 +795,7 @@ const standardDeviation = (arr, usePopulation = false) => {
arr.reduce((acc, val) => acc.concat(Math.pow(val - mean, 2)), []) arr.reduce((acc, val) => acc.concat(Math.pow(val - mean, 2)), [])
.reduce((acc, val) => acc + val, 0) / (arr.length - (usePopulation ? 0 : 1)) .reduce((acc, val) => acc + val, 0) / (arr.length - (usePopulation ? 0 : 1))
); );
} };
// standardDeviation([10,2,38,23,38,23,21]) -> 13.284434142114991 (sample) // 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], true) -> 12.29899614287479 (population)
``` ```
@ -1041,10 +1041,10 @@ If digit is found in teens pattern, use teens ordinal.
```js ```js
const toOrdinalSuffix = num => { const toOrdinalSuffix = num => {
const int = parseInt(num), digits = [(int % 10), (int % 100)], const int = parseInt(num), digits = [(int % 10), (int % 100)],
ordinals = ["st", "nd", "rd", "th"], oPattern = [1,2,3,4], ordinals = ['st', 'nd', 'rd', 'th'], oPattern = [1, 2, 3, 4],
tPattern = [11, 12, 13, 14, 15, 16, 17, 18, 19] tPattern = [11, 12, 13, 14, 15, 16, 17, 18, 19];
return oPattern.includes(digits[0]) && !tPattern.includes(digits[1]) ? int + ordinals[digits[0]-1] : int + ordinals[3]; return oPattern.includes(digits[0]) && !tPattern.includes(digits[1]) ? int + ordinals[digits[0] - 1] : int + ordinals[3];
} };
// toOrdinalSuffix("123") -> "123rd" // toOrdinalSuffix("123") -> "123rd"
``` ```

View File

@ -1,31 +1,44 @@
var fs = require('fs-extra'); /*
var cp = require('child_process'); This is the linter script that lints snippets.
var path = require('path'); Run using `npm run linter`.
*/
// Load modules
const fs = require('fs-extra'), cp = require('child_process'), path = require('path');
// Set variables for paths
var snippetsPath = './snippets'; var snippetsPath = './snippets';
var snippetFilename = ''; // Read files, lint each one individually and update
try {
console.time('Linter'); let snippetFilenames = fs.readdirSync(snippetsPath);
snippetFilenames.sort((a, b) => {
if(process.argv.length < 3){ a = a.toLowerCase();
console.log('Please specify the filename of a snippet to be linted.'); b = b.toLowerCase();
console.log('Example usage: npm run lint "snippet-file.md"'); if (a < b) return -1;
process.exit(0); if (a > b) return 1;
} return 0;
else { });
snippetFilename = process.argv[2]; // Read each file, get its code, write it to a temporary file, pass it through
let snippetData = fs.readFileSync(path.join(snippetsPath,snippetFilename),'utf8'); // semistandard, get the output from the file, update the original file.
try { for(let snippet of snippetFilenames){
// Start a timer for the file
console.time(`Linter (${snippet})`);
// Synchronously read data from the snippet, get the code, write it to a temporary file
let snippetData = fs.readFileSync(path.join(snippetsPath,snippet),'utf8');
let originalCode = snippetData.slice(snippetData.indexOf('```js')+5,snippetData.lastIndexOf('```')); let originalCode = snippetData.slice(snippetData.indexOf('```js')+5,snippetData.lastIndexOf('```'));
fs.writeFileSync('currentSnippet.js',`${originalCode}`); fs.writeFileSync(`${snippet}.temp.js`,`${originalCode}`);
cp.exec('semistandard "currentSnippet.js" --fix',{},(error, stdOut, stdErr) => { // Run semistandard asynchronously (only way this manages to run), get linted code
let lintedCode = fs.readFileSync('currentSnippet.js','utf8'); // and write back to the original snippet file. Remove temporary file
fs.writeFile(path.join(snippetsPath,snippetFilename), `${snippetData.slice(0, snippetData.indexOf('```js')+5)+lintedCode+'```\n'}`); cp.exec(`semistandard "${snippet}.temp.js" --fix`,{},(error, stdOut, stdErr) => {
console.timeEnd('Linter'); let lintedCode = fs.readFileSync(`${snippet}.temp.js`,'utf8');
fs.writeFile(path.join(snippetsPath,snippet), `${snippetData.slice(0, snippetData.indexOf('```js')+5)+lintedCode+'```\n'}`);
fs.unlink(`${snippet}.temp.js`);
// Log a success message
console.log(`${chalk.red('SUCCESS!')} Linted snippet: ${snippet}`);
// Log the time taken for the file
console.timeEnd(`Linter (${snippet})`);
}); });
} }
catch (err){ }
console.log('Error during snippet loading: '+err); catch (err){ // Handle errors (hopefully not!)
process.exit(1); console.log(`${chalk.red('ERROR!')} During linting: ${err}`);
} process.exit(1);
} }

View File

@ -3,6 +3,6 @@
Create a `Set` from `b`, then use `Array.filter()` on `a` to only keep values not contained in `b`. Create a `Set` from `b`, then use `Array.filter()` on `a` to only keep values not contained in `b`.
```js ```js
const difference = (a, b) => { const s = new Set(b); return a.filter(x => !s.has(x)); } const difference = (a, b) => { const s = new Set(b); return a.filter(x => !s.has(x)); };
// difference([1,2,3], [1,2]) -> [3] // difference([1,2,3], [1,2]) -> [3]
``` ```

View File

@ -3,6 +3,6 @@
Create a `Set` from `b`, then use `Array.filter()` on `a` to only keep values contained in `b`. Create a `Set` from `b`, then use `Array.filter()` on `a` to only keep values contained in `b`.
```js ```js
const intersection = (a, b) => { const s = new Set(b); return a.filter(x => s.has(x)); } const intersection = (a, b) => { const s = new Set(b); return a.filter(x => s.has(x)); };
// intersection([1,2,3], [4,3,2]) -> [2,3] // intersection([1,2,3], [4,3,2]) -> [2,3]
``` ```

View File

@ -3,6 +3,6 @@
Create a `Set` with all values of `a` and `b` and convert to an array. Create a `Set` with all values of `a` and `b` and convert to an array.
```js ```js
const union = (a, b) => Array.from(new Set([...a, ...b])) const union = (a, b) => Array.from(new Set([...a, ...b]));
// union([1,2,3], [4,3,2]) -> [1,2,3,4] // union([1,2,3], [4,3,2]) -> [1,2,3,4]
``` ```

View File

@ -3,7 +3,7 @@
If `n` is even, return `n/2`. Otherwise return `3n+1`. If `n` is even, return `n/2`. Otherwise return `3n+1`.
```js ```js
const collatz = n => (n % 2 == 0) ? (n/2) : (3*n+1); const collatz = n => (n % 2 == 0) ? (n / 2) : (3 * n + 1);
// collatz(8) --> 4 // collatz(8) --> 4
// collatz(5) --> 16 // collatz(5) --> 16
``` ```

View File

@ -4,9 +4,9 @@ Loop through the array, using `Array.shift()` to drop the first element of the a
Returns the remaining elements. Returns the remaining elements.
```js ```js
const dropElements = (arr,func) => { const dropElements = (arr, func) => {
while(arr.length > 0 && !func(arr[0])) arr.shift(); while (arr.length > 0 && !func(arr[0])) arr.shift();
return arr; return arr;
} };
// dropElements([1, 2, 3, 4], n => n >= 3) -> [3,4] // dropElements([1, 2, 3, 4], n => n >= 3) -> [3,4]
``` ```

View File

@ -12,7 +12,7 @@ const elementIsVisibleInViewport = (el, partiallyVisible = false) => {
? ((top > 0 && top < innerHeight) || (bottom > 0 && bottom < innerHeight)) && ? ((top > 0 && top < innerHeight) || (bottom > 0 && bottom < innerHeight)) &&
((left > 0 && left < innerWidth) || (right > 0 && right < innerWidth)) ((left > 0 && left < innerWidth) || (right > 0 && right < innerWidth))
: top >= 0 && left >= 0 && bottom <= innerHeight && right <= innerWidth; : top >= 0 && left >= 0 && bottom <= innerHeight && right <= innerWidth;
} };
// e.g. 100x100 viewport and a 10x10px element at position {top: -1, left: 0, bottom: 9, right: 10} // 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) -> false (not fully visible)
// elementIsVisibleInViewport(el, true) -> true (partially visible) // elementIsVisibleInViewport(el, true) -> true (partially visible)

View File

@ -5,6 +5,6 @@ Omit `start` to start at the first element and/or `end` to finish at the last.
```js ```js
const fillArray = (arr, value, start = 0, end = arr.length) => const fillArray = (arr, value, start = 0, end = arr.length) =>
arr.map((v,i) => i>=start && i<end ? value : v); arr.map((v, i) => i >= start && i < end ? value : v);
// fillArray([1,2,3,4],'8',1,3) -> [1,'8','8',4] // fillArray([1,2,3,4],'8',1,3) -> [1,'8','8',4]
``` ```

View File

@ -7,7 +7,7 @@ Omit the second element, `depth` to flatten only to a depth of `1` (single flatt
```js ```js
const flattenDepth = (arr, depth = 1) => const flattenDepth = (arr, depth = 1) =>
depth != 1 ? arr.reduce((a, v) => a.concat(Array.isArray(v) ? flattenDepth (v, depth-1) : v), []) depth != 1 ? arr.reduce((a, v) => a.concat(Array.isArray(v) ? flattenDepth(v, depth - 1) : v), [])
: arr.reduce((a,v) => a.concat(v),[]); : arr.reduce((a, v) => a.concat(v), []);
// flattenDepth([1,[2],[[[3],4],5]], 2) -> [1,2,[3],4,5] // flattenDepth([1,[2],[[[3],4],5]], 2) -> [1,2,[3],4,5]
``` ```

View File

@ -7,9 +7,9 @@ If digit is found in teens pattern, use teens ordinal.
```js ```js
const toOrdinalSuffix = num => { const toOrdinalSuffix = num => {
const int = parseInt(num), digits = [(int % 10), (int % 100)], const int = parseInt(num), digits = [(int % 10), (int % 100)],
ordinals = ["st", "nd", "rd", "th"], oPattern = [1,2,3,4], ordinals = ['st', 'nd', 'rd', 'th'], oPattern = [1, 2, 3, 4],
tPattern = [11, 12, 13, 14, 15, 16, 17, 18, 19] tPattern = [11, 12, 13, 14, 15, 16, 17, 18, 19];
return oPattern.includes(digits[0]) && !tPattern.includes(digits[1]) ? int + ordinals[digits[0]-1] : int + ordinals[3]; return oPattern.includes(digits[0]) && !tPattern.includes(digits[1]) ? int + ordinals[digits[0] - 1] : int + ordinals[3];
} };
// toOrdinalSuffix("123") -> "123rd" // toOrdinalSuffix("123") -> "123rd"
``` ```

View File

@ -11,7 +11,7 @@ const standardDeviation = (arr, usePopulation = false) => {
arr.reduce((acc, val) => acc.concat(Math.pow(val - mean, 2)), []) arr.reduce((acc, val) => acc.concat(Math.pow(val - mean, 2)), [])
.reduce((acc, val) => acc + val, 0) / (arr.length - (usePopulation ? 0 : 1)) .reduce((acc, val) => acc + val, 0) / (arr.length - (usePopulation ? 0 : 1))
); );
} };
// standardDeviation([10,2,38,23,38,23,21]) -> 13.284434142114991 (sample) // 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], true) -> 12.29899614287479 (population)
``` ```