diff --git a/.eslintrc.json b/.eslintrc.json index 3faf34799..c90cb34dc 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -6,7 +6,7 @@ }, "extends": "eslint:recommended", "parserOptions": { - "ecmaVersion": 2016, + "ecmaVersion": 2018, "sourceType": "module" }, "rules": { @@ -46,7 +46,7 @@ ], "brace-style": [ "error", - "stroustrup", + "1tbs", { "allowSingleLine": true } diff --git a/package-lock.json b/package-lock.json index 8fe4eed28..9d1988e75 100644 --- a/package-lock.json +++ b/package-lock.json @@ -3122,12 +3122,6 @@ "integrity": "sha512-4zaPW5uRFasf2uRZkE19Y+W84KBV3q+oyWYOsgUN+5DQXE5HCsh7ZxeWDXxozk7NPycGm0kXcsJzLe5GZ1jCeg==", "dev": true }, - "eslint-config-standard": { - "version": "12.0.0", - "resolved": "https://registry.npmjs.org/eslint-config-standard/-/eslint-config-standard-12.0.0.tgz", - "integrity": "sha512-COUz8FnXhqFitYj4DTqHzidjIL/t4mumGZto5c7DrBpvWoie+Sn3P4sLEzUGeYhRElWuFEf8K1S1EfvD1vixCQ==", - "dev": true - }, "eslint-config-standard-jsx": { "version": "5.0.0", "resolved": "https://registry.npmjs.org/eslint-config-standard-jsx/-/eslint-config-standard-jsx-5.0.0.tgz", @@ -3185,144 +3179,10 @@ } } }, - "eslint-plugin-es": { - "version": "1.3.1", - "resolved": "https://registry.npmjs.org/eslint-plugin-es/-/eslint-plugin-es-1.3.1.tgz", - "integrity": "sha512-9XcVyZiQRVeFjqHw8qHNDAZcQLqaHlOGGpeYqzYh8S4JYCWTCO3yzyen8yVmA5PratfzTRWDwCOFphtDEG+w/w==", - "dev": true, - "requires": { - "eslint-utils": "1.3.1", - "regexpp": "2.0.1" - } - }, - "eslint-plugin-import": { - "version": "2.14.0", - "resolved": "https://registry.npmjs.org/eslint-plugin-import/-/eslint-plugin-import-2.14.0.tgz", - "integrity": "sha512-FpuRtniD/AY6sXByma2Wr0TXvXJ4nA/2/04VPlfpmUDPOpOY264x+ILiwnrk/k4RINgDAyFZByxqPUbSQ5YE7g==", - "dev": true, - "requires": { - "contains-path": "0.1.0", - "debug": "2.6.9", - "doctrine": "1.5.0", - "eslint-import-resolver-node": "0.3.2", - "eslint-module-utils": "2.2.0", - "has": "1.0.1", - "lodash": "4.17.11", - "minimatch": "3.0.4", - "read-pkg-up": "2.0.0", - "resolve": "1.8.1" - }, - "dependencies": { - "debug": { - "version": "2.6.9", - "resolved": "https://registry.npmjs.org/debug/-/debug-2.6.9.tgz", - "integrity": "sha512-bC7ElrdJaJnPbAP+1EotYvqZsb3ecl5wi6Bfi6BJTUcNowp6cvspg0jXznRTKDjm/E7AdgFBVeAPVMNcKGsHMA==", - "dev": true, - "requires": { - "ms": "2.0.0" - } - }, - "doctrine": { - "version": "1.5.0", - "resolved": "https://registry.npmjs.org/doctrine/-/doctrine-1.5.0.tgz", - "integrity": "sha1-N53Ocw9hZvds76TmcHoVmwLFpvo=", - "dev": true, - "requires": { - "esutils": "2.0.2", - "isarray": "1.0.0" - } - }, - "find-up": { - "version": "2.1.0", - "resolved": "https://registry.npmjs.org/find-up/-/find-up-2.1.0.tgz", - "integrity": "sha1-RdG35QbHF93UgndaK3eSCjwMV6c=", - "dev": true, - "requires": { - "locate-path": "2.0.0" - } - }, - "path-type": { - "version": "2.0.0", - "resolved": "https://registry.npmjs.org/path-type/-/path-type-2.0.0.tgz", - "integrity": "sha1-8BLMuEFbcJb8LaoQVMPXI4lZTHM=", - "dev": true, - "requires": { - "pify": "2.3.0" - } - }, - "read-pkg": { - "version": "2.0.0", - "resolved": "https://registry.npmjs.org/read-pkg/-/read-pkg-2.0.0.tgz", - "integrity": "sha1-jvHAYjxqbbDcZxPEv6xGMysjaPg=", - "dev": true, - "requires": { - "load-json-file": "2.0.0", - "normalize-package-data": "2.4.0", - "path-type": "2.0.0" - } - }, - "read-pkg-up": { - "version": "2.0.0", - "resolved": "https://registry.npmjs.org/read-pkg-up/-/read-pkg-up-2.0.0.tgz", - "integrity": "sha1-a3KoBImE4MQeeVEP1en6mbO1Sb4=", - "dev": true, - "requires": { - "find-up": "2.1.0", - "read-pkg": "2.0.0" - } - }, - "resolve": { - "version": "1.8.1", - "resolved": "https://registry.npmjs.org/resolve/-/resolve-1.8.1.tgz", - "integrity": "sha512-AicPrAC7Qu1JxPCZ9ZgCZlY35QgFnNqc+0LtbRNxnVw4TXvjQ72wnuL9JQcEBgXkI9JM8MsT9kaQoHcpCRJOYA==", - "dev": true, - "requires": { - "path-parse": "1.0.5" - } - } - } - }, - "eslint-plugin-node": { - "version": "7.0.1", - "resolved": "https://registry.npmjs.org/eslint-plugin-node/-/eslint-plugin-node-7.0.1.tgz", - "integrity": "sha512-lfVw3TEqThwq0j2Ba/Ckn2ABdwmL5dkOgAux1rvOk6CO7A6yGyPI2+zIxN6FyNkp1X1X/BSvKOceD6mBWSj4Yw==", - "dev": true, - "requires": { - "eslint-plugin-es": "1.3.1", - "eslint-utils": "1.3.1", - "ignore": "4.0.6", - "minimatch": "3.0.4", - "resolve": "1.8.1", - "semver": "5.6.0" - }, - "dependencies": { - "ignore": { - "version": "4.0.6", - "resolved": "https://registry.npmjs.org/ignore/-/ignore-4.0.6.tgz", - "integrity": "sha512-cyFDKrqc/YdcWFniJhzI42+AzS+gNwmUzOSFcRCQYwySuBBBy/KjuxWLZ/FHEH6Moq1NizMOBWyTcv8O4OZIMg==", - "dev": true - }, - "resolve": { - "version": "1.8.1", - "resolved": "https://registry.npmjs.org/resolve/-/resolve-1.8.1.tgz", - "integrity": "sha512-AicPrAC7Qu1JxPCZ9ZgCZlY35QgFnNqc+0LtbRNxnVw4TXvjQ72wnuL9JQcEBgXkI9JM8MsT9kaQoHcpCRJOYA==", - "dev": true, - "requires": { - "path-parse": "1.0.5" - } - }, - "semver": { - "version": "5.6.0", - "resolved": "https://registry.npmjs.org/semver/-/semver-5.6.0.tgz", - "integrity": "sha512-RS9R6R35NYgQn++fkDWaOmqGoj4Ek9gGs+DPxNUZKuwE183xjJroKvyo1IzVFeXvUrvmALy6FWD5xrdJT25gMg==", - "dev": true - } - } - }, - "eslint-plugin-promise": { - "version": "4.0.1", - "resolved": "https://registry.npmjs.org/eslint-plugin-promise/-/eslint-plugin-promise-4.0.1.tgz", - "integrity": "sha512-Si16O0+Hqz1gDHsys6RtFRrW7cCTB6P7p3OJmKp3Y3dxpQE2qwOA7d3xnV+0mBmrPoi0RBnxlCKvqu70te6wjg==", + "eslint-plugin-async-await": { + "version": "0.0.0", + "resolved": "https://registry.npmjs.org/eslint-plugin-async-await/-/eslint-plugin-async-await-0.0.0.tgz", + "integrity": "sha1-DyrhejgUeAY11I8kCd+eN4mMoJ8=", "dev": true }, "eslint-plugin-react": { @@ -3337,12 +3197,6 @@ "prop-types": "15.6.1" } }, - "eslint-plugin-standard": { - "version": "4.0.0", - "resolved": "https://registry.npmjs.org/eslint-plugin-standard/-/eslint-plugin-standard-4.0.0.tgz", - "integrity": "sha512-OwxJkR6TQiYMmt1EsNRMe5qG3GsbjlcOhbGUBY4LtavF9DsLaTcoR+j2Tdjqi23oUwKNUqX7qcn5fPStafMdlA==", - "dev": true - }, "eslint-scope": { "version": "3.7.1", "resolved": "https://registry.npmjs.org/eslint-scope/-/eslint-scope-3.7.1.tgz", @@ -5958,7 +5812,7 @@ }, "load-json-file": { "version": "2.0.0", - "resolved": "http://registry.npmjs.org/load-json-file/-/load-json-file-2.0.0.tgz", + "resolved": "https://registry.npmjs.org/load-json-file/-/load-json-file-2.0.0.tgz", "integrity": "sha1-eUfkIUmvgNaWy/eXvKq8/h/inKg=", "dev": true, "requires": { diff --git a/package.json b/package.json index c84195859..b169cc073 100644 --- a/package.json +++ b/package.json @@ -17,8 +17,7 @@ "prismjs": "^1.15.0", "rollup": "^0.58.2", "rollup-plugin-babel": "^4.0.3", - "rollup-plugin-babel-minify": "^4.0.0", - "semistandard": "^12.0.1" + "rollup-plugin-babel-minify": "^4.0.0" }, "name": "30-seconds-of-code", "description": "A collection of useful JavaScript snippets.", diff --git a/scripts/lint.js b/scripts/lint.js index ada4edd2b..c3b1a11f7 100644 --- a/scripts/lint.js +++ b/scripts/lint.js @@ -52,8 +52,8 @@ try { } const cmd = - `semistandard "${TEMP_PATH}" --fix & ` + - `prettier "${TEMP_PATH}/*.js" --single-quote --print-width=100 --write`; + `prettier "${TEMP_PATH}/*.js" --single-quote --print-width=100 --write & ` + + `eslint "${TEMP_PATH}/*.js" --quiet --fix --rule "no-undef: 0, no-unused-vars: 0, no-multiple-empty-lines: 0" -o eslint_errors.log -f table`; cp.exec(cmd, {}, () => { // Loop through each snippet now that semistandard and prettier did their job diff --git a/snippets/call.md b/snippets/call.md index 22cba9afd..80dbaf764 100644 --- a/snippets/call.md +++ b/snippets/call.md @@ -11,9 +11,9 @@ const call = (key, ...args) => context => context[key](...args); ```js Promise.resolve([1, 2, 3]) .then(call('map', x => 2 * x)) - .then(console.log); //[ 2, 4, 6 ] + .then(console.log); // [ 2, 4, 6 ] const map = call.bind(null, 'map'); Promise.resolve([1, 2, 3]) .then(map(x => 2 * x)) - .then(console.log); //[ 2, 4, 6 ] + .then(console.log); // [ 2, 4, 6 ] ``` diff --git a/snippets/deepFreeze.md b/snippets/deepFreeze.md index f358278c1..fe87461e1 100644 --- a/snippets/deepFreeze.md +++ b/snippets/deepFreeze.md @@ -8,7 +8,7 @@ Calls `Object.freeze(obj)` recursively on all unfrozen properties of passed obje const deepFreeze = obj => Object.keys(obj).forEach( prop => - !obj[prop] instanceof Object || Object.isFrozen(obj[prop]) ? null : deepFreeze(obj[prop]) + !(obj[prop] instanceof Object) || Object.isFrozen(obj[prop]) ? null : deepFreeze(obj[prop]) ) || Object.freeze(obj); ``` diff --git a/snippets/defer.md b/snippets/defer.md index e06b3bc69..e6cd03052 100644 --- a/snippets/defer.md +++ b/snippets/defer.md @@ -14,6 +14,6 @@ defer(console.log, 'a'), console.log('b'); // logs 'b' then 'a' // Example B: document.querySelector('#someElement').innerHTML = 'Hello'; -longRunningFunction(); //Browser will not update the HTML until this has finished +longRunningFunction(); // Browser will not update the HTML until this has finished defer(longRunningFunction); // Browser will update the HTML then run the function ``` diff --git a/snippets/dig.md b/snippets/dig.md index c97559833..737130b6e 100644 --- a/snippets/dig.md +++ b/snippets/dig.md @@ -10,9 +10,9 @@ const dig = (obj, target) => target in obj ? obj[target] : Object.values(obj).reduce((acc, val) => { - if (acc !== undefined) return acc; - if (typeof val === 'object') return dig(val, target); - }, undefined); + if (acc !== undefined) return acc; + if (typeof val === 'object') return dig(val, target); + }, undefined); ``` ```js diff --git a/snippets/elo.md b/snippets/elo.md index 07156ca8a..3479e4ccb 100644 --- a/snippets/elo.md +++ b/snippets/elo.md @@ -15,9 +15,9 @@ const elo = ([...ratings], kFactor = 32, selfRating) => { const expectedScore = (self, opponent) => 1 / (1 + 10 ** ((opponent - self) / 400)); const newRating = (rating, i) => (selfRating || rating) + kFactor * (i - expectedScore(i ? a : b, i ? b : a)); - if (ratings.length === 2) { + if (ratings.length === 2) return [newRating(a, 1), newRating(b, 0)]; - } + for (let i = 0, len = ratings.length; i < len; i++) { let j = i; while (j < len - 1) { diff --git a/snippets/factorial.md b/snippets/factorial.md index 86ebe69b6..cf3acc9cc 100644 --- a/snippets/factorial.md +++ b/snippets/factorial.md @@ -11,8 +11,8 @@ Throws an exception if `n` is a negative number. const factorial = n => n < 0 ? (() => { - throw new TypeError('Negative numbers are not allowed!'); - })() + throw new TypeError('Negative numbers are not allowed!'); + })() : n <= 1 ? 1 : n * factorial(n - 1); diff --git a/snippets/httpGet.md b/snippets/httpGet.md index 33f6d72d8..73e2035b2 100644 --- a/snippets/httpGet.md +++ b/snippets/httpGet.md @@ -21,7 +21,7 @@ const httpGet = (url, callback, err = console.error) => { httpGet( 'https://jsonplaceholder.typicode.com/posts/1', console.log -); /* +); /* Logs: { "userId": 1, "id": 1, diff --git a/snippets/httpPost.md b/snippets/httpPost.md index 502e1044e..8328d07fc 100644 --- a/snippets/httpPost.md +++ b/snippets/httpPost.md @@ -42,7 +42,7 @@ Logs: { */ httpPost( 'https://jsonplaceholder.typicode.com/posts', - null, //does not send a body + null, // does not send a body console.log ); /* Logs: { diff --git a/snippets/pipeAsyncFunctions.md b/snippets/pipeAsyncFunctions.md index 8c93add3b..47d6f620c 100644 --- a/snippets/pipeAsyncFunctions.md +++ b/snippets/pipeAsyncFunctions.md @@ -17,7 +17,7 @@ const sum = pipeAsyncFunctions( x => x + 3, async x => (await x) + 4 ); -(async () => { +(async() => { console.log(await sum(5)); // 15 (after one second) })(); ``` diff --git a/snippets/remove.md b/snippets/remove.md index a8c472774..58de9c2e0 100644 --- a/snippets/remove.md +++ b/snippets/remove.md @@ -9,9 +9,9 @@ The `func` is invoked with three arguments (`value, index, array`). const remove = (arr, func) => Array.isArray(arr) ? arr.filter(func).reduce((acc, val) => { - arr.splice(arr.indexOf(val), 1); - return acc.concat(val); - }, []) + arr.splice(arr.indexOf(val), 1); + return acc.concat(val); + }, []) : []; ``` diff --git a/snippets/runAsync.md b/snippets/runAsync.md index 09c116fbf..eaedb4f08 100644 --- a/snippets/runAsync.md +++ b/snippets/runAsync.md @@ -29,9 +29,9 @@ const longRunningFunction = () => { let result = 0; for (let i = 0; i < 1000; i++) { for (let j = 0; j < 700; j++) { - for (let k = 0; k < 300; k++) { + for (let k = 0; k < 300; k++) result = result + i + j + k; - } + } } return result; diff --git a/snippets/sumPower.md b/snippets/sumPower.md index 19fcb8015..a89c640d5 100644 --- a/snippets/sumPower.md +++ b/snippets/sumPower.md @@ -16,6 +16,6 @@ const sumPower = (end, power = 2, start = 1) => ```js sumPower(10); // 385 -sumPower(10, 3); //3025 -sumPower(10, 3, 5); //2925 +sumPower(10, 3); // 3025 +sumPower(10, 3, 5); // 2925 ``` diff --git a/snippets/unzip.md b/snippets/unzip.md index 6254acac9..003b03fb7 100644 --- a/snippets/unzip.md +++ b/snippets/unzip.md @@ -16,6 +16,6 @@ const unzip = arr => ``` ```js -unzip([['a', 1, true], ['b', 2, false]]); //[['a', 'b'], [1, 2], [true, false]] -unzip([['a', 1, true], ['b', 2]]); //[['a', 'b'], [1, 2], [true]] +unzip([['a', 1, true], ['b', 2, false]]); // [['a', 'b'], [1, 2], [true, false]] +unzip([['a', 1, true], ['b', 2]]); // [['a', 'b'], [1, 2], [true]] ``` diff --git a/test/deepFreeze/deepFreeze.js b/test/deepFreeze/deepFreeze.js index a6e1ac6cb..9d6447812 100644 --- a/test/deepFreeze/deepFreeze.js +++ b/test/deepFreeze/deepFreeze.js @@ -1,6 +1,6 @@ const deepFreeze = obj => Object.keys(obj).forEach( prop => - !obj[prop] instanceof Object || Object.isFrozen(obj[prop]) ? null : deepFreeze(obj[prop]) + !(obj[prop] instanceof Object) || Object.isFrozen(obj[prop]) ? null : deepFreeze(obj[prop]) ) || Object.freeze(obj); module.exports = deepFreeze; diff --git a/test/dig/dig.js b/test/dig/dig.js index 90233fa3a..3b9d33393 100644 --- a/test/dig/dig.js +++ b/test/dig/dig.js @@ -2,7 +2,7 @@ const dig = (obj, target) => target in obj ? obj[target] : Object.values(obj).reduce((acc, val) => { - if (acc !== undefined) return acc; - if (typeof val === 'object') return dig(val, target); - }, undefined); + if (acc !== undefined) return acc; + if (typeof val === 'object') return dig(val, target); + }, undefined); module.exports = dig; diff --git a/test/elo/elo.js b/test/elo/elo.js index 28f571510..4ea215191 100644 --- a/test/elo/elo.js +++ b/test/elo/elo.js @@ -3,9 +3,9 @@ const elo = ([...ratings], kFactor = 32, selfRating) => { const expectedScore = (self, opponent) => 1 / (1 + 10 ** ((opponent - self) / 400)); const newRating = (rating, i) => (selfRating || rating) + kFactor * (i - expectedScore(i ? a : b, i ? b : a)); - if (ratings.length === 2) { + if (ratings.length === 2) return [newRating(a, 1), newRating(b, 0)]; - } + for (let i = 0, len = ratings.length; i < len; i++) { let j = i; while (j < len - 1) { diff --git a/test/factorial/factorial.js b/test/factorial/factorial.js index 18e1c719f..8248f1051 100644 --- a/test/factorial/factorial.js +++ b/test/factorial/factorial.js @@ -1,8 +1,8 @@ const factorial = n => n < 0 ? (() => { - throw new TypeError('Negative numbers are not allowed!'); - })() + throw new TypeError('Negative numbers are not allowed!'); + })() : n <= 1 ? 1 : n * factorial(n - 1); diff --git a/test/getImages/getImages.test.js b/test/getImages/getImages.test.js index 668172d31..835785816 100644 --- a/test/getImages/getImages.test.js +++ b/test/getImages/getImages.test.js @@ -15,4 +15,4 @@ test("getImages returns an Array", () => { test("getImages removes duplicates from images Array", () => { expect(getImages(TEST_HTML, false).length).toBeLessThanOrEqual(getImages(TEST_HTML, true).length); expect(getImages(TEST_HTML, true)).toEqual(expect.arrayContaining(getImages(TEST_HTML, false))); -});\n +}); diff --git a/test/isPrimitive/isPrimitive.js b/test/isPrimitive/isPrimitive.js index 89d7cc577..eaab7f941 100644 --- a/test/isPrimitive/isPrimitive.js +++ b/test/isPrimitive/isPrimitive.js @@ -1,2 +1,2 @@ -const isPrimitive = val => !['object', 'function'].includes(typeof val) || val === null; +const isPrimitive = val => Object(val) !== val; module.exports = isPrimitive; diff --git a/test/remove/remove.js b/test/remove/remove.js index e1dda8ccb..034bc9436 100644 --- a/test/remove/remove.js +++ b/test/remove/remove.js @@ -1,8 +1,8 @@ const remove = (arr, func) => Array.isArray(arr) ? arr.filter(func).reduce((acc, val) => { - arr.splice(arr.indexOf(val), 1); - return acc.concat(val); - }, []) + arr.splice(arr.indexOf(val), 1); + return acc.concat(val); + }, []) : []; module.exports = remove; diff --git a/test/squareSum/squareSum.js b/test/squareSum/squareSum.js new file mode 100644 index 000000000..ed3c4a743 --- /dev/null +++ b/test/squareSum/squareSum.js @@ -0,0 +1,2 @@ +const squareSum = (...args) => args.reduce((squareSum, number) => squareSum + Math.pow(number, 2), 0); +module.exports = squareSum; diff --git a/test/squareSum/squareSum.test.js b/test/squareSum/squareSum.test.js new file mode 100644 index 000000000..447cd90d7 --- /dev/null +++ b/test/squareSum/squareSum.test.js @@ -0,0 +1,6 @@ +const expect = require('expect'); +const squareSum = require('./squareSum.js'); + +test('squareSum is a Function', () => { + expect(squareSum).toBeInstanceOf(Function); +}); diff --git a/test/throttle/throttle.js b/test/throttle/throttle.js index 2ed612018..052a3da47 100644 --- a/test/throttle/throttle.js +++ b/test/throttle/throttle.js @@ -14,7 +14,7 @@ const throttle = (fn, wait) => { fn.apply(context, args); lastTime = Date.now(); } - }, wait - (Date.now() - lastTime)); + }, Math.max(wait - (Date.now() - lastTime), 0)); } }; };