diff --git a/scripts/lint-script.js b/scripts/lint-script.js index c1f380642..d67980f17 100644 --- a/scripts/lint-script.js +++ b/scripts/lint-script.js @@ -25,7 +25,8 @@ try { 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 codeStart = snippetData.indexOf('```js'), codeEnd = snippetData.search(/```[\n\r\s]+```js/g); + let originalCode = snippetData.slice(codeStart+5,codeEnd); while(jobCounter >= 20){ setTimeout(()=>{},1000); } @@ -36,7 +37,7 @@ try { cp.exec(`semistandard "${tempSnippet}.temp.js" --fix`,{},(error, stdOut, stdErr) => { jobCounter += 1; let lintedCode = fs.readFileSync(`${tempSnippet}.temp.js`,'utf8'); - fs.writeFile(path.join(snippetsPath,snippet), `${snippetData.slice(0, snippetData.indexOf('```js')+5)+lintedCode+'```\n'}`); + fs.writeFile(path.join(snippetsPath,snippet), `${snippetData.slice(0, codeStart+5)+lintedCode+snippetData.slice(codeEnd)}`); fs.unlink(`${tempSnippet}.temp.js`); // Log a success message console.log(`${chalk.green('SUCCESS!')} Linted snippet: ${snippet}`); diff --git a/snippets/JSONToFile.md b/snippets/JSONToFile.md index 2e88f336d..713183838 100644 --- a/snippets/JSONToFile.md +++ b/snippets/JSONToFile.md @@ -6,7 +6,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)) +const JSONToFile = (obj, filename) => fs.writeFile(`${filename}.json`, JSON.stringify(obj, null, 2)); ``` ```js diff --git a/snippets/arrayGcd.md b/snippets/arrayGcd.md index 83e577761..6fbb7f2f5 100644 --- a/snippets/arrayGcd.md +++ b/snippets/arrayGcd.md @@ -7,8 +7,8 @@ Use `Array.reduce()` and the `gcd` formula (uses recursion) to calculate the gre ```js const arrayGcd = arr => { const gcd = (x, y) => !y ? x : gcd(y, x % y); - return arr.reduce((a,b) => gcd(a,b)); -} + return arr.reduce((a, b) => gcd(a, b)); +}; ``` ```js diff --git a/snippets/arrayLcm.md b/snippets/arrayLcm.md index f39d0f3dc..beca717b4 100644 --- a/snippets/arrayLcm.md +++ b/snippets/arrayLcm.md @@ -7,9 +7,9 @@ Use `Array.reduce()` and the `lcm` formula (uses recursion) to calculate the low ```js const arrayLcm = arr => { const gcd = (x, y) => !y ? x : gcd(y, x % y); - const lcm = (x, y) => (x*y)/gcd(x, y); - return arr.reduce((a,b) => lcm(a,b)); -} + const lcm = (x, y) => (x * y) / gcd(x, y); + return arr.reduce((a, b) => lcm(a, b)); +}; ``` ```js diff --git a/snippets/arrayToHtmlList.md b/snippets/arrayToHtmlList.md index 27a66369f..32f98c178 100644 --- a/snippets/arrayToHtmlList.md +++ b/snippets/arrayToHtmlList.md @@ -5,7 +5,7 @@ Converts the given array elements into `
  • ` tags and appends them to the list Use `Array.map()` and `document.querySelector()` to create a list of html tags. ```js -const arrayToHtmlList = (arr, listID) => arr.map(item => document.querySelector("#"+listID).innerHTML+=`
  • ${item}
  • `); +const arrayToHtmlList = (arr, listID) => arr.map(item => document.querySelector('#' + listID).innerHTML += `
  • ${item}
  • `); ``` ```js diff --git a/snippets/call.md b/snippets/call.md index e30b395e0..e9f2b4d6c 100644 --- a/snippets/call.md +++ b/snippets/call.md @@ -5,7 +5,7 @@ Given a key and a set of arguments, call them when given a context. Primarily us Use a closure to call a stored key with stored arguments. ```js -const call = ( key, ...args ) => context => context[ key ]( ...args ); +const call = (key, ...args) => context => context[ key ](...args); ``` ```js diff --git a/snippets/clampNumber.md b/snippets/clampNumber.md index 1431e6b37..fc46766be 100644 --- a/snippets/clampNumber.md +++ b/snippets/clampNumber.md @@ -6,7 +6,7 @@ If `num` falls within the range, return `num`. Otherwise, return the nearest number in the range. ```js -const clampNumber = (num, a, b) => Math.max(Math.min(num, Math.max(a,b)),Math.min(a,b)); +const clampNumber = (num, a, b) => Math.max(Math.min(num, Math.max(a, b)), Math.min(a, b)); ``` ```js diff --git a/snippets/coalesce.md b/snippets/coalesce.md index c112c77d4..ce4dfc03d 100644 --- a/snippets/coalesce.md +++ b/snippets/coalesce.md @@ -5,7 +5,7 @@ Returns the first non-null/undefined argument. Use `Array.find()` to return the first non `null`/`undefined` argument. ```js -const coalesce = (...args) => args.find(_ => ![undefined, null].includes(_)) +const coalesce = (...args) => args.find(_ => ![undefined, null].includes(_)); ``` ```js diff --git a/snippets/collectInto.md b/snippets/collectInto.md index 141a13fa2..9446f2bc3 100644 --- a/snippets/collectInto.md +++ b/snippets/collectInto.md @@ -5,7 +5,7 @@ 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 ); +const collectInto = fn => (...args) => fn(args); ``` ```js diff --git a/snippets/detectDeviceType.md b/snippets/detectDeviceType.md index b262e194d..3f0ec2357 100644 --- a/snippets/detectDeviceType.md +++ b/snippets/detectDeviceType.md @@ -5,7 +5,7 @@ Detects wether the website is being opened in a mobile device or a desktop/lapto Use a regular expression to test the `navigator.userAgent` property to figure out if the device is a mobile device or a desktop/laptop. ```js -const detectDeviceType = () => /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent) ? "Mobile" : "Desktop"; +const detectDeviceType = () => /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent) ? 'Mobile' : 'Desktop'; ``` ```js diff --git a/snippets/differenceWith.md b/snippets/differenceWith.md index f5ad345d6..384f401b8 100644 --- a/snippets/differenceWith.md +++ b/snippets/differenceWith.md @@ -5,7 +5,7 @@ Filters out all values from an array for which the comparator function does not Use `Array.filter()` and `Array.find()` to find the appropriate values. ```js -const differenceWith = (arr, val, comp) => arr.filter(a => !val.find(b => comp(a, b))) +const differenceWith = (arr, val, comp) => arr.filter(a => !val.find(b => comp(a, b))); ``` ```js diff --git a/snippets/digitize.md b/snippets/digitize.md index 9d52fd04b..a8a605ef7 100644 --- a/snippets/digitize.md +++ b/snippets/digitize.md @@ -6,7 +6,7 @@ Convert the number to a string, using spread operators in ES6(`[...string]`) bui Use `Array.map()` and `parseInt()` to transform each value to an integer. ```js -const digitize = n => [...''+n].map(i => parseInt(i)); +const digitize = n => [...'' + n].map(i => parseInt(i)); ``` ```js diff --git a/snippets/extendHex.md b/snippets/extendHex.md index 40253a7f1..a9d60fdcf 100644 --- a/snippets/extendHex.md +++ b/snippets/extendHex.md @@ -6,7 +6,7 @@ Use `Array.map()`, `split()` and `Array.join()` to join the mapped array for con `String.slice()` is used to remove `#` from string start since it's added once. ```js const extendHex = shortHex => - '#' + shortHex.slice(shortHex.startsWith('#') ? 1 : 0).split('').map(x => x+x).join('') + '#' + shortHex.slice(shortHex.startsWith('#') ? 1 : 0).split('').map(x => x + x).join(''); ``` ```js diff --git a/snippets/fibonacciCountUntilNum.md b/snippets/fibonacciCountUntilNum.md index 719e8cef3..b822adf0c 100644 --- a/snippets/fibonacciCountUntilNum.md +++ b/snippets/fibonacciCountUntilNum.md @@ -6,7 +6,7 @@ Use a mathematical formula to calculate the number of fibonacci numbers until `n ```js const fibonacciCountUntilNum = num => - Math.ceil(Math.log(num * Math.sqrt(5) + 1/2) / Math.log((Math.sqrt(5)+1)/2)); + Math.ceil(Math.log(num * Math.sqrt(5) + 1 / 2) / Math.log((Math.sqrt(5) + 1) / 2)); ``` ```js diff --git a/snippets/fibonacciUntilNum.md b/snippets/fibonacciUntilNum.md index aef99ca37..25303b348 100644 --- a/snippets/fibonacciUntilNum.md +++ b/snippets/fibonacciUntilNum.md @@ -10,7 +10,7 @@ Uses a mathematical formula to calculate the length of the array required. const fibonacciUntilNum = num => { let n = Math.ceil(Math.log(num * Math.sqrt(5) + 1 / 2) / Math.log((Math.sqrt(5) + 1) / 2)); return Array.from({ length: n}).reduce((acc, val, i) => acc.concat(i > 1 ? acc[i - 1] + acc[i - 2] : i), []); -} +}; ``` ```js diff --git a/snippets/flatten.md b/snippets/flatten.md index 29cb52ad1..fc4e7e74a 100644 --- a/snippets/flatten.md +++ b/snippets/flatten.md @@ -5,7 +5,7 @@ Flattens an array. Use a new array and concatenate it with the spread input array causing a shallow denesting of any contained arrays. ```js -const flatten = arr => [ ].concat( ...arr ); +const flatten = arr => [ ].concat(...arr); ``` ```js diff --git a/snippets/inRange.md b/snippets/inRange.md index 044b32a0c..aedef5ef1 100644 --- a/snippets/inRange.md +++ b/snippets/inRange.md @@ -6,10 +6,10 @@ Use arithmetic comparison to check if the given number is in the specified range If the second parameter, `end`, is not specified, the range is considered to be from `0` to `start`. ```js -const inRange = (n, start, end=null) => { - if(end && start > end) end = [start, start=end][0]; - return (end == null) ? (n>=0 && n=start && n { + if (end && start > end) end = [start, start = end][0]; + return (end == null) ? (n >= 0 && n < start) : (n >= start && n < end); +}; ``` ```js diff --git a/snippets/isArmstrongNumber.md b/snippets/isArmstrongNumber.md index 81f59409e..171019069 100644 --- a/snippets/isArmstrongNumber.md +++ b/snippets/isArmstrongNumber.md @@ -6,7 +6,7 @@ Convert the given number into an array of digits. Use `Math.pow()` to get the ap ```js const isArmstrongNumber = digits => - ( arr => arr.reduce( ( a, d ) => a + Math.pow( parseInt( d ), arr.length ), 0 ) == digits ? true : false )( ( digits+'' ).split( '' ) ); + (arr => arr.reduce((a, d) => a + Math.pow(parseInt(d), arr.length), 0) == digits)((digits + '').split('')); ``` ```js diff --git a/snippets/mapObject.md b/snippets/mapObject.md index 3eebc515f..bead463a6 100644 --- a/snippets/mapObject.md +++ b/snippets/mapObject.md @@ -6,7 +6,7 @@ Use an anonymous inner function scope to declare an undefined memory space, usin ```js const mapObject = (arr, fn) => - (a => (a = [arr, arr.map(fn)], a[0].reduce( (acc,val,ind) => (acc[val] = a[1][ind], acc), {}) )) ( ); + (a => (a = [arr, arr.map(fn)], a[0].reduce((acc, val, ind) => (acc[val] = a[1][ind], acc), {})))(); ``` ```js diff --git a/snippets/nthElement.md b/snippets/nthElement.md index 9c1fbf387..61cabe26e 100644 --- a/snippets/nthElement.md +++ b/snippets/nthElement.md @@ -7,7 +7,7 @@ If the index is out of bounds, return `[]`. Omit the second argument, `n`, to get the first element of the array. ```js -const nthElement = (arr, n=0) => (n>0? arr.slice(n,n+1) : arr.slice(n))[0]; +const nthElement = (arr, n = 0) => (n > 0 ? arr.slice(n, n + 1) : arr.slice(n))[0]; ``` ```js diff --git a/snippets/palindrome.md b/snippets/palindrome.md index f6c8f4204..41993b9d1 100644 --- a/snippets/palindrome.md +++ b/snippets/palindrome.md @@ -7,9 +7,9 @@ Then, `split('')` into individual characters, `reverse()`, `join('')` and compar ```js const palindrome = str => { - const s = str.toLowerCase().replace(/[\W_]/g,''); + const s = str.toLowerCase().replace(/[\W_]/g, ''); return s === s.split('').reverse().join(''); -} +}; ``` ```js diff --git a/snippets/percentile.md b/snippets/percentile.md index 92a9d2eab..ad7a456d1 100644 --- a/snippets/percentile.md +++ b/snippets/percentile.md @@ -6,7 +6,7 @@ Use `Array.reduce()` to calculate how many numbers are below the value and how m ```js const percentile = (arr, val) => - 100 * arr.reduce((acc,v) => acc + (v < val ? 1 : 0) + (v === val ? 0.5 : 0), 0) / arr.length; + 100 * arr.reduce((acc, v) => acc + (v < val ? 1 : 0) + (v === val ? 0.5 : 0), 0) / arr.length; ``` ```js diff --git a/snippets/primes.md b/snippets/primes.md index 87ac21a70..c98acca9b 100644 --- a/snippets/primes.md +++ b/snippets/primes.md @@ -6,12 +6,12 @@ Generate an array from `2` to the given number. Use `Array.filter()` to filter o ```js const primes = num => { - let arr = Array.from({length:num-1}).map((x,i)=> i+2), - sqroot = Math.floor(Math.sqrt(num)), - numsTillSqroot = Array.from({length:sqroot-1}).map((x,i)=> i+2); - numsTillSqroot.forEach(x => arr = arr.filter(y => ((y%x)!==0)||(y==x))); + let arr = Array.from({length: num - 1}).map((x, i) => i + 2), + sqroot = Math.floor(Math.sqrt(num)), + numsTillSqroot = Array.from({length: sqroot - 1}).map((x, i) => i + 2); + numsTillSqroot.forEach(x => arr = arr.filter(y => ((y % x) !== 0) || (y == x))); return arr; -} +}; ``` ```js diff --git a/snippets/pullAtIndex.md b/snippets/pullAtIndex.md index 459c2c7b2..ba2646d9a 100644 --- a/snippets/pullAtIndex.md +++ b/snippets/pullAtIndex.md @@ -10,11 +10,11 @@ Use `Array.push()` to keep track of pulled values const pullAtIndex = (arr, pullArr) => { let removed = []; let pulled = arr.map((v, i) => pullArr.includes(i) ? removed.push(v) : v) - .filter((v, i) => !pullArr.includes(i)) + .filter((v, i) => !pullArr.includes(i)); arr.length = 0; pulled.forEach(v => arr.push(v)); return removed; -} +}; ``` ```js diff --git a/snippets/pullAtValue.md b/snippets/pullAtValue.md index ff2559a53..aa38eb625 100644 --- a/snippets/pullAtValue.md +++ b/snippets/pullAtValue.md @@ -14,7 +14,7 @@ const pullAtValue = (arr, pullArr) => { arr.length = 0; mutateTo.forEach(v => arr.push(v)); return removed; -} +}; ``` ```js diff --git a/snippets/randomHexColorCode.md b/snippets/randomHexColorCode.md index 5009e1977..f426f1bf8 100644 --- a/snippets/randomHexColorCode.md +++ b/snippets/randomHexColorCode.md @@ -5,7 +5,7 @@ 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); +const randomHexColorCode = () => '#' + (Math.random() * 0xFFFFFF << 0).toString(16); ``` ```js diff --git a/snippets/repeatString.md b/snippets/repeatString.md index 2f66bb047..008e976a6 100644 --- a/snippets/repeatString.md +++ b/snippets/repeatString.md @@ -5,9 +5,9 @@ Repeats a string n times using `String.repeat()` If no string is provided the default is `""` and the default number of times is 2. ```js -const repeatString = (str="",num=2) => { - return num >= 0 ? str.repeat(num) : str; -} +const repeatString = (str = '', num = 2) => { + return num >= 0 ? str.repeat(num) : str; +}; ``` ```js diff --git a/snippets/round.md b/snippets/round.md index 8ece6dcd1..f6af435bb 100644 --- a/snippets/round.md +++ b/snippets/round.md @@ -6,7 +6,7 @@ Use `Math.round()` and template literals to round the number to the specified nu Omit the second argument, `decimals` to round to an integer. ```js -const round = (n, decimals=0) => Number(`${Math.round(`${n}e${decimals}`)}e-${decimals}`); +const round = (n, decimals = 0) => Number(`${Math.round(`${n}e${decimals}`)}e-${decimals}`); ``` ```js diff --git a/snippets/symmetricDifference.md b/snippets/symmetricDifference.md index a26a4db4a..8e55bdab4 100644 --- a/snippets/symmetricDifference.md +++ b/snippets/symmetricDifference.md @@ -8,7 +8,7 @@ Create a `Set` from each array, then use `Array.filter()` on each of them to onl const symmetricDifference = (a, b) => { const sA = new Set(a), sB = new Set(b); return [...a.filter(x => !sB.has(x)), ...b.filter(x => !sA.has(x))]; -} +}; ``` ```js diff --git a/snippets/toCamelCase.md b/snippets/toCamelCase.md index 3a106311f..65958acea 100644 --- a/snippets/toCamelCase.md +++ b/snippets/toCamelCase.md @@ -10,8 +10,8 @@ const toCamelCase = str => { let s = str && str.match(/[A-Z]{2,}(?=[A-Z][a-z]+[0-9]*|\b)|[A-Z]?[a-z]+[0-9]*|[A-Z]|[0-9]+/g) .map(x => x.slice(0, 1).toUpperCase() + x.slice(1).toLowerCase()) .join(''); - return s.slice(0,1).toLowerCase() + s.slice(1) - } + return s.slice(0, 1).toLowerCase() + s.slice(1); +}; ``` ```js diff --git a/snippets/toDecimalMark.md b/snippets/toDecimalMark.md index b670622cd..726a5a019 100644 --- a/snippets/toDecimalMark.md +++ b/snippets/toDecimalMark.md @@ -3,7 +3,7 @@ Use `toLocaleString()` to convert a float-point arithmetic to the [Decimal mark](https://en.wikipedia.org/wiki/Decimal_mark) form. It makes a comma separated string from a number. ```js -const toDecimalMark = num => num.toLocaleString("en-US"); +const toDecimalMark = num => num.toLocaleString('en-US'); ``` ```js diff --git a/snippets/zip.md b/snippets/zip.md index 92613ff34..68da486eb 100644 --- a/snippets/zip.md +++ b/snippets/zip.md @@ -10,9 +10,9 @@ If lengths of the argument-arrays vary, `undefined` is used where no value could const zip = (...arrays) => { const maxLength = Math.max(...arrays.map(x => x.length)); return Array.from({length: maxLength}).map((_, i) => { - return Array.from({length: arrays.length}, (_, k) => arrays[k][i]); - }) -} + return Array.from({length: arrays.length}, (_, k) => arrays[k][i]); + }); +}; ``` ```js diff --git a/snippets/zipObject.md b/snippets/zipObject.md index 99d134ac8..b2673a9ae 100644 --- a/snippets/zipObject.md +++ b/snippets/zipObject.md @@ -5,7 +5,7 @@ Given an array of valid property identifiers and an array of values, return an o Since an object can have undefined values but not undefined property pointers, the array of properties is used to decide the structure of the resulting object using `Array.reduce()`. ```js -const zipObject = ( props, values ) => props.reduce( ( obj, prop, index ) => ( obj[prop] = values[index], obj ), {} ) +const zipObject = (props, values) => props.reduce((obj, prop, index) => (obj[prop] = values[index], obj), {}); ``` ```js