From cb26e0ba4936aaed8d9e716f046b2b1d44e58ffa Mon Sep 17 00:00:00 2001 From: 30secondsofcode <30secondsofcode@gmail.com> Date: Fri, 18 Oct 2019 06:34:35 +0000 Subject: [PATCH] Travis build: 1516 --- README.md | 79 +++++++++++++------------- snippet_data/snippetList.json | 21 ++++--- snippet_data/snippets.json | 43 +++++++------- snippets/checkProp.md | 1 + snippets/deepClone.md | 5 +- snippets/deepMapKeys.md | 5 +- snippets/dig.md | 7 ++- snippets/factorial.md | 9 +-- snippets/getMeridiemSuffixOfInteger.md | 9 +-- snippets/hasKey.md | 5 +- snippets/join.md | 5 +- snippets/objectToQueryString.md | 12 ++-- test/_30s.js | 54 +++++++++--------- vscode_snippets/snippets.json | 56 +++++++++--------- 14 files changed, 159 insertions(+), 152 deletions(-) diff --git a/README.md b/README.md index f06f8805d..e0b9f2a7e 100644 --- a/README.md +++ b/README.md @@ -1725,8 +1725,8 @@ const join = (arr, separator = ',', end = separator) => i === arr.length - 2 ? acc + val + end : i === arr.length - 1 - ? acc + val - : acc + val + separator, + ? acc + val + : acc + val + separator, '' ); ``` @@ -4274,10 +4274,10 @@ const getMeridiemSuffixOfInteger = num => num === 0 || num === 24 ? 12 + 'am' : num === 12 - ? 12 + 'pm' - : num < 12 - ? (num % 12) + 'am' - : (num % 12) + 'pm'; + ? 12 + 'pm' + : num < 12 + ? (num % 12) + 'am' + : (num % 12) + 'pm'; ```
@@ -5489,11 +5489,11 @@ 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); + ? 1 + : n * factorial(n - 1); ```
@@ -6704,8 +6704,8 @@ const deepClone = obj => { return Array.isArray(obj) && obj.length ? (clone.length = obj.length) && Array.from(clone) : Array.isArray(obj) - ? Array.from(obj) - : clone; + ? Array.from(obj) + : clone; }; ``` @@ -6793,13 +6793,13 @@ const deepMapKeys = (obj, f) => Array.isArray(obj) ? obj.map(val => deepMapKeys(val, f)) : typeof obj === 'object' - ? Object.keys(obj).reduce((acc, current) => { + ? Object.keys(obj).reduce((acc, current) => { const val = obj[current]; acc[f(current)] = val !== null && typeof val === 'object' ? deepMapKeys(val, f) : (acc[f(current)] = val); return acc; }, {}) - : obj; + : obj; ```
@@ -6870,9 +6870,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); ```
@@ -7120,17 +7120,19 @@ get(obj, 'selector.to.val', 'target[0]', 'target[2].a'); // ['val to select', 1, Returns `true` if the target value exists in a JSON object, `false` otherwise. -Check if the key contains `.`, use `String.prototype.split('.')[0]` to get the first part and store as `_key`. -Use `typeof` to check if the contents of `obj[key]` are an `object` and, if so, call `hasKey` with that object and the remainder of the `key`. -Otherwise, use `Object.keys(obj)` in combination with `Array.prototype.includes()` to check if the given `key` exists. +Check if `keys` is non-empty and use `Array.prototype.every()` to sequentially check its keys to internal depth of the object, `obj`. +Use `Object.prototype.hasOwnProperty()` to check if `obj` does not have the current key or is not an object, stop propagation and return `false`. +Otherwise assign the key's value to `obj` to use on the next iteration. + +Return `false` beforehand if given key list is empty. ```js -const hasKey = (obj, key) => { - if (key.includes('.')) { - let _key = key.split('.')[0]; - if (typeof obj[_key] === 'object') return hasKey(obj[_key], key.slice(key.indexOf('.') + 1)); - } - return Object.keys(obj).includes(key); +const hasKey = (obj, keys) => { + return (keys.length > 0) && keys.every(key => { + if (typeof obj !== 'object' || !obj.hasOwnProperty(key)) return false; + obj = obj[key]; + return true; + }); }; ``` @@ -7141,14 +7143,15 @@ const hasKey = (obj, key) => { let obj = { a: 1, b: { c: 4 }, - 'd.e': 5 + 'b.d': 5 }; -hasKey(obj, 'a'); // true -hasKey(obj, 'b'); // true -hasKey(obj, 'b.c'); // true -hasKey(obj, 'd.e'); // true -hasKey(obj, 'd'); // false -hasKey(obj, 'f'); // false +hasKey(obj, ['a']); // true +hasKey(obj, ['b']); // true +hasKey(obj, ['b', 'c']); // true +hasKey(obj, ['b.d']); // true +hasKey(obj, ['d']); // false +hasKey(obj, ['c']); // false +hasKey(obj, ['b', 'f']); // false ```
@@ -9380,10 +9383,10 @@ Return the `queryString` or an empty string when the `queryParameters` are falsy const objectToQueryString = queryParameters => { return queryParameters ? Object.entries(queryParameters).reduce((queryString, [key, val], index) => { - const symbol = index === 0 ? '?' : '&'; - queryString += (typeof val === 'string') ? `${symbol}${key}=${val}` : ''; - return queryString; - }, '') + const symbol = index === 0 ? '?' : '&'; + queryString += typeof val === 'string' ? `${symbol}${key}=${val}` : ''; + return queryString; + }, '') : ''; }; ``` @@ -9392,7 +9395,7 @@ const objectToQueryString = queryParameters => { Examples ```js -objectToQueryString({page: '1', size: '2kg', key: undefined}); // '?page=1&size=2kg' +objectToQueryString({ page: '1', size: '2kg', key: undefined }); // '?page=1&size=2kg' ```
diff --git a/snippet_data/snippetList.json b/snippet_data/snippetList.json index aa32d305b..43b71c3fa 100644 --- a/snippet_data/snippetList.json +++ b/snippet_data/snippetList.json @@ -412,7 +412,7 @@ ] }, "meta": { - "hash": "34fedcb1752dc3907869f323b7fc519add6925d0868c0495ffc112b0d5706267" + "hash": "f7e4d2f80cc19a12f09673c550b8c46dc33f615f3ab2c1bd6560781d9f5adde0" } }, { @@ -835,7 +835,7 @@ ] }, "meta": { - "hash": "0a4684d6fc79bdbbac31df3af6c493ba7c881936ada5bc52824b4f26ca177459" + "hash": "5ab25ab96afd4f1f481fc318b5b290ba8c57a468ef6bca0ca200cfb7fcf3ba9f" } }, { @@ -898,7 +898,7 @@ ] }, "meta": { - "hash": "a4e1e33c0688dbf1ca231d9d8ea315ffed93b7f83f5d8cbf0714f10fdfeda8cf" + "hash": "7a228b650ff668f697e524e0d27ebeff1bfa35e04333b6cd5e742ff63bfea25d" } }, { @@ -1037,7 +1037,7 @@ ] }, "meta": { - "hash": "484bd222e636e8a8409c30ddb1fe6e3fe72ab7a43f2edf089b2758d5e9bee528" + "hash": "5f38360819f9225b887a94221bfee1a80f1bcc224a364440b3388f60491b03ba" } }, { @@ -1273,7 +1273,7 @@ ] }, "meta": { - "hash": "0eac852db7a7add352b0d36677b22718b342ed9dc12f11780cac87e3b8260a05" + "hash": "55b1ce0a892110d792a9487e40331774015525479faa2b8961f6c2ea6291c27b" } }, { @@ -1678,7 +1678,7 @@ ] }, "meta": { - "hash": "9e39c6a3a8ec5b51c5e16f69107fc9e90b2697b2cf2689850872071bb968723e" + "hash": "16c3b724b653dcb31f3e59f1664a59951abb15a93eb3697cade4d3ae0e63c532" } }, { @@ -1847,15 +1847,14 @@ "type": "snippetListing", "title": "hasKey", "attributes": { - "text": "Returns `true` if the target value exists in a JSON object, `false` otherwise.\n\nCheck if the key contains `.`, use `String.prototype.split('.')[0]` to get the first part and store as `_key`.\nUse `typeof` to check if the contents of `obj[key]` are an `object` and, if so, call `hasKey` with that object and the remainder of the `key`.\nOtherwise, use `Object.keys(obj)` in combination with `Array.prototype.includes()` to check if the given `key` exists.\n\n", + "text": "Returns `true` if the target value exists in a JSON object, `false` otherwise.\n\nCheck if `keys` is non-empty and use `Array.prototype.every()` to sequentially check its keys to internal depth of the object, `obj`. \nUse `Object.prototype.hasOwnProperty()` to check if `obj` does not have the current key or is not an object, stop propagation and return `false`.\nOtherwise assign the key's value to `obj` to use on the next iteration.\n\nReturn `false` beforehand if given key list is empty.\n\n", "tags": [ "object", - "recursion", "intermediate" ] }, "meta": { - "hash": "a34ac55719f935ad7a3a9a48291bb54cba6ee2c62713137b94f25ac065a78dbc" + "hash": "d4a8563ed14c77123d07e9d3b206beea4a12bafdba9612c2afa04b3f87563b36" } }, { @@ -2836,7 +2835,7 @@ ] }, "meta": { - "hash": "3db3faac666ee61ab86c70766d2ab5d1293ffd818da87edb971bfff7a366364a" + "hash": "362fddaa6244404741e84bca6fc442a101fdb642af53b299e8b9994d0d7162d8" } }, { @@ -3397,7 +3396,7 @@ ] }, "meta": { - "hash": "e87fc52b6d22804ebb9f4adb2acade44183b8db53d101839c6d5cb02aa32419b" + "hash": "012ebca6a90c50ec89278af2632d7d0d90eeb423f2bcf902ed015f6fce6d4f5a" } }, { diff --git a/snippet_data/snippets.json b/snippet_data/snippets.json index 46656e3f6..86cfa2462 100644 --- a/snippet_data/snippets.json +++ b/snippet_data/snippets.json @@ -568,7 +568,7 @@ ] }, "meta": { - "hash": "34fedcb1752dc3907869f323b7fc519add6925d0868c0495ffc112b0d5706267" + "hash": "f7e4d2f80cc19a12f09673c550b8c46dc33f615f3ab2c1bd6560781d9f5adde0" } }, { @@ -1142,7 +1142,7 @@ "fileName": "deepClone.md", "text": "Creates a deep clone of an object.\n\nUse recursion.\nCheck if the passed object is `null` and, if so, return `null`.\nUse `Object.assign()` and an empty object (`{}`) to create a shallow clone of the original.\nUse `Object.keys()` and `Array.prototype.forEach()` to determine which key-value pairs need to be deep cloned.\n\n", "codeBlocks": { - "es6": "const deepClone = obj => {\n if (obj === null) return null;\n let clone = Object.assign({}, obj);\n Object.keys(clone).forEach(\n key => (clone[key] = typeof obj[key] === 'object' ? deepClone(obj[key]) : obj[key])\n );\n return Array.isArray(obj) && obj.length\n ? (clone.length = obj.length) && Array.from(clone)\n : Array.isArray(obj)\n ? Array.from(obj)\n : clone;\n};", + "es6": "const deepClone = obj => {\n if (obj === null) return null;\n let clone = Object.assign({}, obj);\n Object.keys(clone).forEach(\n key => (clone[key] = typeof obj[key] === 'object' ? deepClone(obj[key]) : obj[key])\n );\n return Array.isArray(obj) && obj.length\n ? (clone.length = obj.length) && Array.from(clone)\n : Array.isArray(obj)\n ? Array.from(obj)\n : clone;\n};", "es5": "function _typeof(obj) { if (typeof Symbol === \"function\" && typeof Symbol.iterator === \"symbol\") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === \"function\" && obj.constructor === Symbol && obj !== Symbol.prototype ? \"symbol\" : typeof obj; }; } return _typeof(obj); }\n\nvar deepClone = function deepClone(obj) {\n if (obj === null) return null;\n var clone = Object.assign({}, obj);\n Object.keys(clone).forEach(function (key) {\n return clone[key] = _typeof(obj[key]) === 'object' ? deepClone(obj[key]) : obj[key];\n });\n return Array.isArray(obj) && obj.length ? (clone.length = obj.length) && Array.from(clone) : Array.isArray(obj) ? Array.from(obj) : clone;\n};", "example": "const a = { foo: 'bar', obj: { a: 1, b: 2 } };\nconst b = deepClone(a); // a !== b, a.obj !== b.obj" }, @@ -1153,7 +1153,7 @@ ] }, "meta": { - "hash": "0a4684d6fc79bdbbac31df3af6c493ba7c881936ada5bc52824b4f26ca177459" + "hash": "5ab25ab96afd4f1f481fc318b5b290ba8c57a468ef6bca0ca200cfb7fcf3ba9f" } }, { @@ -1229,7 +1229,7 @@ "fileName": "deepMapKeys.md", "text": "Deep maps an object's keys.\n\nCreates an object with the same values as the provided object and keys generated by running the provided function for each key.\nUse `Object.keys(obj)` to iterate over the object's keys. \nUse `Array.prototype.reduce()` to create a new object with the same values and mapped keys using `fn`.\n\n", "codeBlocks": { - "es6": "const deepMapKeys = (obj, f) =>\n Array.isArray(obj)\n ? obj.map(val => deepMapKeys(val, f))\n : typeof obj === 'object'\n ? Object.keys(obj).reduce((acc, current) => {\n const val = obj[current];\n acc[f(current)] =\n val !== null && typeof val === 'object' ? deepMapKeys(val, f) : (acc[f(current)] = val);\n return acc;\n }, {})\n : obj;", + "es6": "const deepMapKeys = (obj, f) =>\n Array.isArray(obj)\n ? obj.map(val => deepMapKeys(val, f))\n : typeof obj === 'object'\n ? Object.keys(obj).reduce((acc, current) => {\n const val = obj[current];\n acc[f(current)] =\n val !== null && typeof val === 'object' ? deepMapKeys(val, f) : (acc[f(current)] = val);\n return acc;\n }, {})\n : obj;", "es5": "function _typeof(obj) { if (typeof Symbol === \"function\" && typeof Symbol.iterator === \"symbol\") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === \"function\" && obj.constructor === Symbol && obj !== Symbol.prototype ? \"symbol\" : typeof obj; }; } return _typeof(obj); }\n\nvar deepMapKeys = function deepMapKeys(obj, f) {\n return Array.isArray(obj) ? obj.map(function (val) {\n return deepMapKeys(val, f);\n }) : _typeof(obj) === 'object' ? Object.keys(obj).reduce(function (acc, current) {\n var val = obj[current];\n acc[f(current)] = val !== null && _typeof(val) === 'object' ? deepMapKeys(val, f) : acc[f(current)] = val;\n return acc;\n }, {}) : obj;\n};", "example": "const obj = {\n foo: '1',\n nested: {\n child: {\n withArray: [\n {\n grandChild: ['hello']\n }\n ]\n }\n }\n};\nconst upperKeysObj = deepMapKeys(obj, key => key.toUpperCase());\n/*\n{\n \"FOO\":\"1\",\n \"NESTED\":{\n \"CHILD\":{\n \"WITHARRAY\":[\n {\n \"GRANDCHILD\":[ 'hello' ]\n }\n ]\n }\n }\n}\n*/" }, @@ -1240,7 +1240,7 @@ ] }, "meta": { - "hash": "a4e1e33c0688dbf1ca231d9d8ea315ffed93b7f83f5d8cbf0714f10fdfeda8cf" + "hash": "7a228b650ff668f697e524e0d27ebeff1bfa35e04333b6cd5e742ff63bfea25d" } }, { @@ -1422,7 +1422,7 @@ "fileName": "dig.md", "text": "Returns the target value in a nested JSON object, based on the given key.\n\nUse the `in` operator to check if `target` exists in `obj`.\nIf found, return the value of `obj[target]`, otherwise use `Object.values(obj)` and `Array.prototype.reduce()` to recursively call `dig` on each nested object until the first matching key/value pair is found.\n\n", "codeBlocks": { - "es6": "const dig = (obj, target) =>\n target in obj\n ? obj[target]\n : Object.values(obj).reduce((acc, val) => {\n if (acc !== undefined) return acc;\n if (typeof val === 'object') return dig(val, target);\n }, undefined);", + "es6": "const dig = (obj, target) =>\n target in obj\n ? obj[target]\n : Object.values(obj).reduce((acc, val) => {\n if (acc !== undefined) return acc;\n if (typeof val === 'object') return dig(val, target);\n }, undefined);", "es5": "function _typeof(obj) { if (typeof Symbol === \"function\" && typeof Symbol.iterator === \"symbol\") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === \"function\" && obj.constructor === Symbol && obj !== Symbol.prototype ? \"symbol\" : typeof obj; }; } return _typeof(obj); }\n\nvar dig = function dig(obj, target) {\n return target in obj ? obj[target] : Object.values(obj).reduce(function (acc, val) {\n if (acc !== undefined) return acc;\n if (_typeof(val) === 'object') return dig(val, target);\n }, undefined);\n};", "example": "const data = {\n level1: {\n level2: {\n level3: 'some data'\n }\n }\n};\ndig(data, 'level3'); // 'some data'\ndig(data, 'level4'); // undefined" }, @@ -1433,7 +1433,7 @@ ] }, "meta": { - "hash": "484bd222e636e8a8409c30ddb1fe6e3fe72ab7a43f2edf089b2758d5e9bee528" + "hash": "5f38360819f9225b887a94221bfee1a80f1bcc224a364440b3388f60491b03ba" } }, { @@ -1748,7 +1748,7 @@ "fileName": "factorial.md", "text": "Calculates the factorial of a number.\n\nUse recursion.\nIf `n` is less than or equal to `1`, return `1`.\nOtherwise, return the product of `n` and the factorial of `n - 1`.\nThrows an exception if `n` is a negative number.\n\n", "codeBlocks": { - "es6": "const factorial = n =>\n n < 0\n ? (() => {\n throw new TypeError('Negative numbers are not allowed!');\n })()\n : n <= 1\n ? 1\n : n * factorial(n - 1);", + "es6": "const factorial = n =>\n n < 0\n ? (() => {\n throw new TypeError('Negative numbers are not allowed!');\n })()\n : n <= 1\n ? 1\n : n * factorial(n - 1);", "es5": "var factorial = function factorial(n) {\n return n < 0 ? function () {\n throw new TypeError('Negative numbers are not allowed!');\n }() : n <= 1 ? 1 : n * factorial(n - 1);\n};", "example": "factorial(6); // 720" }, @@ -1759,7 +1759,7 @@ ] }, "meta": { - "hash": "0eac852db7a7add352b0d36677b22718b342ed9dc12f11780cac87e3b8260a05" + "hash": "55b1ce0a892110d792a9487e40331774015525479faa2b8961f6c2ea6291c27b" } }, { @@ -2310,7 +2310,7 @@ "fileName": "getMeridiemSuffixOfInteger.md", "text": "Converts an integer to a suffixed string, adding `am` or `pm` based on its value.\n\nUse the modulo operator (`%`) and conditional checks to transform an integer to a stringified 12-hour format with meridiem suffix.\n\n", "codeBlocks": { - "es6": "const getMeridiemSuffixOfInteger = num =>\n num === 0 || num === 24\n ? 12 + 'am'\n : num === 12\n ? 12 + 'pm'\n : num < 12\n ? (num % 12) + 'am'\n : (num % 12) + 'pm';", + "es6": "const getMeridiemSuffixOfInteger = num =>\n num === 0 || num === 24\n ? 12 + 'am'\n : num === 12\n ? 12 + 'pm'\n : num < 12\n ? (num % 12) + 'am'\n : (num % 12) + 'pm';", "es5": "var getMeridiemSuffixOfInteger = function getMeridiemSuffixOfInteger(num) {\n return num === 0 || num === 24 ? 12 + 'am' : num === 12 ? 12 + 'pm' : num < 12 ? num % 12 + 'am' : num % 12 + 'pm';\n};", "example": "getMeridiemSuffixOfInteger(0); // \"12am\"\ngetMeridiemSuffixOfInteger(11); // \"11am\"\ngetMeridiemSuffixOfInteger(13); // \"1pm\"\ngetMeridiemSuffixOfInteger(25); // \"1pm\"" }, @@ -2320,7 +2320,7 @@ ] }, "meta": { - "hash": "9e39c6a3a8ec5b51c5e16f69107fc9e90b2697b2cf2689850872071bb968723e" + "hash": "16c3b724b653dcb31f3e59f1664a59951abb15a93eb3697cade4d3ae0e63c532" } }, { @@ -2550,20 +2550,19 @@ "type": "snippet", "attributes": { "fileName": "hasKey.md", - "text": "Returns `true` if the target value exists in a JSON object, `false` otherwise.\n\nCheck if the key contains `.`, use `String.prototype.split('.')[0]` to get the first part and store as `_key`.\nUse `typeof` to check if the contents of `obj[key]` are an `object` and, if so, call `hasKey` with that object and the remainder of the `key`.\nOtherwise, use `Object.keys(obj)` in combination with `Array.prototype.includes()` to check if the given `key` exists.\n\n", + "text": "Returns `true` if the target value exists in a JSON object, `false` otherwise.\n\nCheck if `keys` is non-empty and use `Array.prototype.every()` to sequentially check its keys to internal depth of the object, `obj`. \nUse `Object.prototype.hasOwnProperty()` to check if `obj` does not have the current key or is not an object, stop propagation and return `false`.\nOtherwise assign the key's value to `obj` to use on the next iteration.\n\nReturn `false` beforehand if given key list is empty.\n\n", "codeBlocks": { - "es6": "const hasKey = (obj, key) => {\n if (key.includes('.')) {\n let _key = key.split('.')[0];\n if (typeof obj[_key] === 'object') return hasKey(obj[_key], key.slice(key.indexOf('.') + 1));\n }\n return Object.keys(obj).includes(key);\n};", - "es5": "function _typeof(obj) { if (typeof Symbol === \"function\" && typeof Symbol.iterator === \"symbol\") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === \"function\" && obj.constructor === Symbol && obj !== Symbol.prototype ? \"symbol\" : typeof obj; }; } return _typeof(obj); }\n\nvar hasKey = function hasKey(obj, key) {\n if (key.includes('.')) {\n var _key = key.split('.')[0];\n if (_typeof(obj[_key]) === 'object') return hasKey(obj[_key], key.slice(key.indexOf('.') + 1));\n }\n\n return Object.keys(obj).includes(key);\n};", - "example": "let obj = {\n a: 1,\n b: { c: 4 },\n 'd.e': 5\n};\nhasKey(obj, 'a'); // true\nhasKey(obj, 'b'); // true\nhasKey(obj, 'b.c'); // true\nhasKey(obj, 'd.e'); // true\nhasKey(obj, 'd'); // false\nhasKey(obj, 'f'); // false" + "es6": "const hasKey = (obj, keys) => {\n return (keys.length > 0) && keys.every(key => {\n if (typeof obj !== 'object' || !obj.hasOwnProperty(key)) return false;\n obj = obj[key];\n return true;\n });\n};", + "es5": "function _typeof(obj) { if (typeof Symbol === \"function\" && typeof Symbol.iterator === \"symbol\") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === \"function\" && obj.constructor === Symbol && obj !== Symbol.prototype ? \"symbol\" : typeof obj; }; } return _typeof(obj); }\n\nvar hasKey = function hasKey(obj, keys) {\n return keys.length > 0 && keys.every(function (key) {\n if (_typeof(obj) !== 'object' || !obj.hasOwnProperty(key)) return false;\n obj = obj[key];\n return true;\n });\n};", + "example": "let obj = {\n a: 1,\n b: { c: 4 },\n 'b.d': 5\n};\nhasKey(obj, ['a']); // true\nhasKey(obj, ['b']); // true\nhasKey(obj, ['b', 'c']); // true\nhasKey(obj, ['b.d']); // true\nhasKey(obj, ['d']); // false\nhasKey(obj, ['c']); // false\nhasKey(obj, ['b', 'f']); // false" }, "tags": [ "object", - "recursion", "intermediate" ] }, "meta": { - "hash": "a34ac55719f935ad7a3a9a48291bb54cba6ee2c62713137b94f25ac065a78dbc" + "hash": "d4a8563ed14c77123d07e9d3b206beea4a12bafdba9612c2afa04b3f87563b36" } }, { @@ -3906,7 +3905,7 @@ "fileName": "join.md", "text": "Joins all elements of an array into a string and returns this string.\nUses a separator and an end separator.\n\nUse `Array.prototype.reduce()` to combine elements into a string.\nOmit the second argument, `separator`, to use a default separator of `','`.\nOmit the third argument, `end`, to use the same value as `separator` by default.\n\n", "codeBlocks": { - "es6": "const join = (arr, separator = ',', end = separator) =>\n arr.reduce(\n (acc, val, i) =>\n i === arr.length - 2\n ? acc + val + end\n : i === arr.length - 1\n ? acc + val\n : acc + val + separator,\n ''\n );", + "es6": "const join = (arr, separator = ',', end = separator) =>\n arr.reduce(\n (acc, val, i) =>\n i === arr.length - 2\n ? acc + val + end\n : i === arr.length - 1\n ? acc + val\n : acc + val + separator,\n ''\n );", "es5": "var join = function join(arr) {\n var separator = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : ',';\n var end = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : separator;\n return arr.reduce(function (acc, val, i) {\n return i === arr.length - 2 ? acc + val + end : i === arr.length - 1 ? acc + val : acc + val + separator;\n }, '');\n};", "example": "join(['pen', 'pineapple', 'apple', 'pen'], ',', '&'); // \"pen,pineapple,apple&pen\"\njoin(['pen', 'pineapple', 'apple', 'pen'], ','); // \"pen,pineapple,apple,pen\"\njoin(['pen', 'pineapple', 'apple', 'pen']); // \"pen,pineapple,apple,pen\"" }, @@ -3916,7 +3915,7 @@ ] }, "meta": { - "hash": "3db3faac666ee61ab86c70766d2ab5d1293ffd818da87edb971bfff7a366364a" + "hash": "362fddaa6244404741e84bca6fc442a101fdb642af53b299e8b9994d0d7162d8" } }, { @@ -4675,9 +4674,9 @@ "fileName": "objectToQueryString.md", "text": "Returns a query string generated from the key-value pairs of the given object.\n\nUse `Array.prototype.reduce()` on `Object.entries(queryParameters)` to create the query string.\nDetermine the `symbol` to be either `?` or `&` based on the `index` and concatenate `val` to `queryString` only if it's a string.\nReturn the `queryString` or an empty string when the `queryParameters` are falsy.\n\n", "codeBlocks": { - "es6": "const objectToQueryString = queryParameters => {\n return queryParameters\n ? Object.entries(queryParameters).reduce((queryString, [key, val], index) => {\n const symbol = index === 0 ? '?' : '&';\n queryString += (typeof val === 'string') ? `${symbol}${key}=${val}` : '';\n return queryString;\n }, '')\n : '';\n};", + "es6": "const objectToQueryString = queryParameters => {\n return queryParameters\n ? Object.entries(queryParameters).reduce((queryString, [key, val], index) => {\n const symbol = index === 0 ? '?' : '&';\n queryString += typeof val === 'string' ? `${symbol}${key}=${val}` : '';\n return queryString;\n }, '')\n : '';\n};", "es5": "function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _nonIterableRest(); }\n\nfunction _nonIterableRest() { throw new TypeError(\"Invalid attempt to destructure non-iterable instance\"); }\n\nfunction _iterableToArrayLimit(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i[\"return\"] != null) _i[\"return\"](); } finally { if (_d) throw _e; } } return _arr; }\n\nfunction _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }\n\nvar objectToQueryString = function objectToQueryString(queryParameters) {\n return queryParameters ? Object.entries(queryParameters).reduce(function (queryString, _ref, index) {\n var _ref2 = _slicedToArray(_ref, 2),\n key = _ref2[0],\n val = _ref2[1];\n\n var symbol = index === 0 ? '?' : '&';\n queryString += typeof val === 'string' ? \"\".concat(symbol).concat(key, \"=\").concat(val) : '';\n return queryString;\n }, '') : '';\n};", - "example": "objectToQueryString({page: '1', size: '2kg', key: undefined}); // '?page=1&size=2kg'" + "example": "objectToQueryString({ page: '1', size: '2kg', key: undefined }); // '?page=1&size=2kg'" }, "tags": [ "utility", @@ -4687,7 +4686,7 @@ ] }, "meta": { - "hash": "e87fc52b6d22804ebb9f4adb2acade44183b8db53d101839c6d5cb02aa32419b" + "hash": "012ebca6a90c50ec89278af2632d7d0d90eeb423f2bcf902ed015f6fce6d4f5a" } }, { diff --git a/snippets/checkProp.md b/snippets/checkProp.md index dada4597b..390e8d4dc 100644 --- a/snippets/checkProp.md +++ b/snippets/checkProp.md @@ -34,6 +34,7 @@ const checkProp = (predicate, prop) => obj => !!predicate(obj[prop]); + const lengthIs4 = checkProp(l => l === 4, 'length'); diff --git a/snippets/deepClone.md b/snippets/deepClone.md index a0da1f0a5..5eefd68bd 100644 --- a/snippets/deepClone.md +++ b/snippets/deepClone.md @@ -11,6 +11,7 @@ Use `Object.assign()` and an empty object (`{}`) to create a shallow clone of th Use `Object.keys()` and `Array.prototype.forEach()` to determine which key-value pairs need to be deep cloned. ```js + const deepClone = obj => { if (obj === null) return null; let clone = Object.assign({}, obj); @@ -20,8 +21,8 @@ const deepClone = obj => { return Array.isArray(obj) && obj.length ? (clone.length = obj.length) && Array.from(clone) : Array.isArray(obj) - ? Array.from(obj) - : clone; + ? Array.from(obj) + : clone; }; ``` diff --git a/snippets/deepMapKeys.md b/snippets/deepMapKeys.md index e07084847..349ba652f 100644 --- a/snippets/deepMapKeys.md +++ b/snippets/deepMapKeys.md @@ -10,17 +10,18 @@ Use `Object.keys(obj)` to iterate over the object's keys. Use `Array.prototype.reduce()` to create a new object with the same values and mapped keys using `fn`. ```js + const deepMapKeys = (obj, f) => Array.isArray(obj) ? obj.map(val => deepMapKeys(val, f)) : typeof obj === 'object' - ? Object.keys(obj).reduce((acc, current) => { + ? Object.keys(obj).reduce((acc, current) => { const val = obj[current]; acc[f(current)] = val !== null && typeof val === 'object' ? deepMapKeys(val, f) : (acc[f(current)] = val); return acc; }, {}) - : obj; + : obj; ``` ```js diff --git a/snippets/dig.md b/snippets/dig.md index 16c08bebe..b1ec83f8e 100644 --- a/snippets/dig.md +++ b/snippets/dig.md @@ -9,13 +9,14 @@ Use the `in` operator to check if `target` exists in `obj`. If found, return the value of `obj[target]`, otherwise use `Object.values(obj)` and `Array.prototype.reduce()` to recursively call `dig` on each nested object until the first matching key/value pair is found. ```js + 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/factorial.md b/snippets/factorial.md index 0b1413c15..baa668b8f 100644 --- a/snippets/factorial.md +++ b/snippets/factorial.md @@ -11,14 +11,15 @@ Otherwise, return the product of `n` and the factorial of `n - 1`. Throws an exception if `n` is a negative number. ```js + 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); + ? 1 + : n * factorial(n - 1); ``` ```js diff --git a/snippets/getMeridiemSuffixOfInteger.md b/snippets/getMeridiemSuffixOfInteger.md index c460f5644..74ac86ec7 100644 --- a/snippets/getMeridiemSuffixOfInteger.md +++ b/snippets/getMeridiemSuffixOfInteger.md @@ -8,14 +8,15 @@ Converts an integer to a suffixed string, adding `am` or `pm` based on its value Use the modulo operator (`%`) and conditional checks to transform an integer to a stringified 12-hour format with meridiem suffix. ```js + const getMeridiemSuffixOfInteger = num => num === 0 || num === 24 ? 12 + 'am' : num === 12 - ? 12 + 'pm' - : num < 12 - ? (num % 12) + 'am' - : (num % 12) + 'pm'; + ? 12 + 'pm' + : num < 12 + ? (num % 12) + 'am' + : (num % 12) + 'pm'; ``` ```js diff --git a/snippets/hasKey.md b/snippets/hasKey.md index bf1df7370..6636ef3d6 100644 --- a/snippets/hasKey.md +++ b/snippets/hasKey.md @@ -12,8 +12,9 @@ Otherwise assign the key's value to `obj` to use on the next iteration. Return `false` beforehand if given key list is empty. ```js + const hasKey = (obj, keys) => { - return (keys.length > 0) && keys.every((key) => { + return (keys.length > 0) && keys.every(key => { if (typeof obj !== 'object' || !obj.hasOwnProperty(key)) return false; obj = obj[key]; return true; @@ -25,7 +26,7 @@ const hasKey = (obj, keys) => { let obj = { a: 1, b: { c: 4 }, - 'b.d': 5, + 'b.d': 5 }; hasKey(obj, ['a']); // true hasKey(obj, ['b']); // true diff --git a/snippets/join.md b/snippets/join.md index 6df3c9145..6bf258224 100644 --- a/snippets/join.md +++ b/snippets/join.md @@ -11,14 +11,15 @@ Omit the second argument, `separator`, to use a default separator of `','`. Omit the third argument, `end`, to use the same value as `separator` by default. ```js + const join = (arr, separator = ',', end = separator) => arr.reduce( (acc, val, i) => i === arr.length - 2 ? acc + val + end : i === arr.length - 1 - ? acc + val - : acc + val + separator, + ? acc + val + : acc + val + separator, '' ); ``` diff --git a/snippets/objectToQueryString.md b/snippets/objectToQueryString.md index f4e624581..970eaa7f4 100644 --- a/snippets/objectToQueryString.md +++ b/snippets/objectToQueryString.md @@ -10,19 +10,17 @@ Determine the `symbol` to be either `?` or `&` based on the `index` and concaten Return the `queryString` or an empty string when the `queryParameters` are falsy. ```js - const objectToQueryString = queryParameters => { return queryParameters ? Object.entries(queryParameters).reduce((queryString, [key, val], index) => { - const symbol = index === 0 ? '?' : '&'; - queryString += (typeof val === 'string') ? `${symbol}${key}=${val}` : ''; - return queryString; - }, '') + const symbol = index === 0 ? '?' : '&'; + queryString += typeof val === 'string' ? `${symbol}${key}=${val}` : ''; + return queryString; + }, '') : ''; }; ``` ```js - -objectToQueryString({page: '1', size: '2kg', key: undefined}); // '?page=1&size=2kg' +objectToQueryString({ page: '1', size: '2kg', key: undefined }); // '?page=1&size=2kg' ``` diff --git a/test/_30s.js b/test/_30s.js index 919300a50..ffea7f888 100644 --- a/test/_30s.js +++ b/test/_30s.js @@ -199,8 +199,8 @@ const deepClone = obj => { return Array.isArray(obj) && obj.length ? (clone.length = obj.length) && Array.from(clone) : Array.isArray(obj) - ? Array.from(obj) - : clone; + ? Array.from(obj) + : clone; }; const deepFlatten = arr => [].concat(...arr.map(v => (Array.isArray(v) ? deepFlatten(v) : v))); const deepFreeze = obj => @@ -212,13 +212,13 @@ const deepMapKeys = (obj, f) => Array.isArray(obj) ? obj.map(val => deepMapKeys(val, f)) : typeof obj === 'object' - ? Object.keys(obj).reduce((acc, current) => { + ? Object.keys(obj).reduce((acc, current) => { const val = obj[current]; acc[f(current)] = val !== null && typeof val === 'object' ? deepMapKeys(val, f) : (acc[f(current)] = val); return acc; }, {}) - : obj; + : obj; const defaults = (obj, ...defs) => Object.assign({}, obj, ...defs.reverse(), obj); const defer = (fn, ...args) => setTimeout(fn, 1, ...args); const degreesToRads = deg => (deg * Math.PI) / 180.0; @@ -240,9 +240,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); const digitize = n => [...`${n}`].map(i => parseInt(i)); const distance = (x0, y0, x1, y1) => Math.hypot(x1 - x0, y1 - y0); const drop = (arr, n = 1) => arr.slice(n); @@ -314,11 +314,11 @@ const extendHex = shortHex => 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); + ? 1 + : n * factorial(n - 1); const fibonacci = n => Array.from({ length: n }).reduce( (acc, val, i) => acc.concat(i > 1 ? acc[i - 1] + acc[i - 2] : i), @@ -419,10 +419,10 @@ const getMeridiemSuffixOfInteger = num => num === 0 || num === 24 ? 12 + 'am' : num === 12 - ? 12 + 'pm' - : num < 12 - ? (num % 12) + 'am' - : (num % 12) + 'pm'; + ? 12 + 'pm' + : num < 12 + ? (num % 12) + 'am' + : (num % 12) + 'pm'; const getScrollPosition = (el = window) => ({ x: el.pageXOffset !== undefined ? el.pageXOffset : el.scrollLeft, y: el.pageYOffset !== undefined ? el.pageYOffset : el.scrollTop @@ -466,12 +466,12 @@ const hashNode = val => 0 ) ); -const hasKey = (obj, key) => { - if (key.includes('.')) { - let _key = key.split('.')[0]; - if (typeof obj[_key] === 'object') return hasKey(obj[_key], key.slice(key.indexOf('.') + 1)); - } - return Object.keys(obj).includes(key); +const hasKey = (obj, keys) => { + return (keys.length > 0) && keys.every(key => { + if (typeof obj !== 'object' || !obj.hasOwnProperty(key)) return false; + obj = obj[key]; + return true; + }); }; const head = arr => arr[0]; const hexToRGB = hex => { @@ -651,8 +651,8 @@ const join = (arr, separator = ',', end = separator) => i === arr.length - 2 ? acc + val + end : i === arr.length - 1 - ? acc + val - : acc + val + separator, + ? acc + val + : acc + val + separator, '' ); const JSONtoCSV = (arr, columns, delimiter = ',') => @@ -771,10 +771,10 @@ const objectToPairs = obj => Object.keys(obj).map(k => [k, obj[k]]); const objectToQueryString = queryParameters => { return queryParameters ? Object.entries(queryParameters).reduce((queryString, [key, val], index) => { - const symbol = index === 0 ? '?' : '&'; - queryString += (typeof val === 'string') ? `${symbol}${key}=${val}` : ''; - return queryString; - }, '') + const symbol = index === 0 ? '?' : '&'; + queryString += typeof val === 'string' ? `${symbol}${key}=${val}` : ''; + return queryString; + }, '') : ''; }; const observeMutations = (element, callback, options) => { diff --git a/vscode_snippets/snippets.json b/vscode_snippets/snippets.json index 60c548ec0..bca656c89 100644 --- a/vscode_snippets/snippets.json +++ b/vscode_snippets/snippets.json @@ -512,8 +512,8 @@ " return Array.isArray(obj) && obj.length", " ? (clone.length = obj.length) && Array.from(clone)", " : Array.isArray(obj)", - " ? Array.from(obj)", - " : clone;", + " ? Array.from(obj)", + " : clone;", "};" ], "description": "Creates a deep clone of an object.\n\nUse recursion.\nCheck if the passed object is `null` and, if so, return `null`.\nUse `Object.assign()` and an empty object (`{}`) to create a shallow clone of the original.\nUse `Object.keys()` and `Array.prototype.forEach()` to determine which key-value pairs need to be deep cloned.\n" @@ -549,13 +549,13 @@ " Array.isArray(obj)", " ? obj.map(val => deepMapKeys(val, f))", " : typeof obj === 'object'", - " ? Object.keys(obj).reduce((acc, current) => {", + " ? Object.keys(obj).reduce((acc, current) => {", " const val = obj[current];", " acc[f(current)] =", " val !== null && typeof val === 'object' ? deepMapKeys(val, f) : (acc[f(current)] = val);", " return acc;", " }, {})", - " : obj;" + " : obj;" ], "description": "Deep maps an object's keys.\n\nCreates an object with the same values as the provided object and keys generated by running the provided function for each key.\nUse `Object.keys(obj)` to iterate over the object's keys. \nUse `Array.prototype.reduce()` to create a new object with the same values and mapped keys using `fn`.\n" }, @@ -631,9 +631,9 @@ " 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);" ], "description": "Returns the target value in a nested JSON object, based on the given key.\n\nUse the `in` operator to check if `target` exists in `obj`.\nIf found, return the value of `obj[target]`, otherwise use `Object.values(obj)` and `Array.prototype.reduce()` to recursively call `dig` on each nested object until the first matching key/value pair is found.\n" }, @@ -795,11 +795,11 @@ "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);" + " ? 1", + " : n * factorial(n - 1);" ], "description": "Calculates the factorial of a number.\n\nUse recursion.\nIf `n` is less than or equal to `1`, return `1`.\nOtherwise, return the product of `n` and the factorial of `n - 1`.\nThrows an exception if `n` is a negative number.\n" }, @@ -1056,10 +1056,10 @@ " num === 0 || num === 24", " ? 12 + 'am'", " : num === 12", - " ? 12 + 'pm'", - " : num < 12", - " ? (num % 12) + 'am'", - " : (num % 12) + 'pm';" + " ? 12 + 'pm'", + " : num < 12", + " ? (num % 12) + 'am'", + " : (num % 12) + 'pm';" ], "description": "Converts an integer to a suffixed string, adding `am` or `pm` based on its value.\n\nUse the modulo operator (`%`) and conditional checks to transform an integer to a stringified 12-hour format with meridiem suffix.\n" }, @@ -1169,15 +1169,15 @@ "hasKey": { "prefix": "30s_hasKey", "body": [ - "const hasKey = (obj, key) => {", - " if (key.includes('.')) {", - " let _key = key.split('.')[0];", - " if (typeof obj[_key] === 'object') return hasKey(obj[_key], key.slice(key.indexOf('.') + 1));", - " }", - " return Object.keys(obj).includes(key);", + "const hasKey = (obj, keys) => {", + " return (keys.length > 0) && keys.every(key => {", + " if (typeof obj !== 'object' || !obj.hasOwnProperty(key)) return false;", + " obj = obj[key];", + " return true;", + " });", "};" ], - "description": "Returns `true` if the target value exists in a JSON object, `false` otherwise.\n\nCheck if the key contains `.`, use `String.prototype.split('.')[0]` to get the first part and store as `_key`.\nUse `typeof` to check if the contents of `obj[key]` are an `object` and, if so, call `hasKey` with that object and the remainder of the `key`.\nOtherwise, use `Object.keys(obj)` in combination with `Array.prototype.includes()` to check if the given `key` exists.\n" + "description": "Returns `true` if the target value exists in a JSON object, `false` otherwise.\n\nCheck if `keys` is non-empty and use `Array.prototype.every()` to sequentially check its keys to internal depth of the object, `obj`. \nUse `Object.prototype.hasOwnProperty()` to check if `obj` does not have the current key or is not an object, stop propagation and return `false`.\nOtherwise assign the key's value to `obj` to use on the next iteration.\n\nReturn `false` beforehand if given key list is empty.\n" }, "head": { "prefix": "30s_head", @@ -1726,8 +1726,8 @@ " i === arr.length - 2", " ? acc + val + end", " : i === arr.length - 1", - " ? acc + val", - " : acc + val + separator,", + " ? acc + val", + " : acc + val + separator,", " ''", " );" ], @@ -2056,10 +2056,10 @@ "const objectToQueryString = queryParameters => {", " return queryParameters", " ? Object.entries(queryParameters).reduce((queryString, [key, val], index) => {", - " const symbol = index === 0 ? '?' : '&';", - " queryString += (typeof val === 'string') ? `${symbol}${key}=${val}` : '';", - " return queryString;", - " }, '')", + " const symbol = index === 0 ? '?' : '&';", + " queryString += typeof val === 'string' ? `${symbol}${key}=${val}` : '';", + " return queryString;", + " }, '')", " : '';", "};" ],