Last active
April 2, 2020 13:48
-
-
Save deepakshrma/79ddf2abb383e18e490027bc5f80dbee to your computer and use it in GitHub Desktop.
stackoverflow
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const snippets = [ | |
| { | |
| scope: "javascript,typescript", | |
| prefix: "st_key_map", | |
| body: `const mapKeys = (obj, fn) => { | |
| let mapper = fn; | |
| if (typeof fn === "string") mapper = x => x[fn]; | |
| return Object.keys(obj).reduce((acc, k) => { | |
| acc[mapper(obj[k], k, obj)] = obj[k]; | |
| return acc; | |
| }, {}); | |
| };`, | |
| description: "Key value map" | |
| }, | |
| { | |
| scope: "javascript,typescript", | |
| prefix: "st_clean_json", | |
| body: `const zero = obj => | |
| JSON.parse(JSON.stringify(obj, (_, v) => (typeof v === "object" ? v : 0))); | |
| console.log(zero(d));`, | |
| description: "Remove all json undefined value with 0" | |
| }, | |
| { | |
| scope: "css", | |
| prefix: "st_console_css", | |
| body: `.as-console-row {color: blue!important}`, | |
| description: "CSS for console" | |
| }, | |
| { | |
| scope: "javascript,typescript", | |
| prefix: "st_throttle", | |
| body: `const throttle = (fn, ms = 0) => { | |
| let lastRunTime; | |
| return function(...args) { | |
| const currTime = +new Date(); | |
| if (!lastRunTime || currTime - lastRunTime > ms) { | |
| lastRunTime = +new Date(); | |
| fn.apply(this, args); | |
| } | |
| }; | |
| };`, | |
| description: "Throttle" | |
| }, | |
| { | |
| scope: "javascript,typescript", | |
| prefix: "st_to_array", | |
| body: `const toArray = obj => { | |
| const generator = function*() { | |
| for (let key in obj) { | |
| yield [obj[key], key]; | |
| } | |
| }; | |
| return { | |
| [Symbol.iterator]: generator, | |
| map: function(fn) { | |
| let result = []; | |
| for (let [value, key] of this) { | |
| result.push(fn(value, key)); | |
| } | |
| return result; | |
| } | |
| }; | |
| }; | |
| const obj = { 1: 1, 2: 2, 3: 3 }; | |
| const arr = toArray(obj); | |
| console.log(arr.map((x, i) => x * i));`, | |
| description: "Convert object to array object, generator" | |
| }, | |
| { | |
| scope: "javascript,typescript", | |
| prefix: "st_deep_equal", | |
| body: `function deepEqual(obj1 = null, obj2 = null) { | |
| if (obj1 === null || obj2 === null) return false; | |
| if (typeof obj1 !== "object") return obj1 == obj2; | |
| if (Object.keys(obj1).length !== Object.keys(obj2).length) return false; | |
| for (let key in obj1) { | |
| const result = deepEqual(obj1[key], obj2[key]); | |
| if (!result) return false; | |
| } | |
| return true; | |
| }`, | |
| description: "Deep Equal" | |
| }, | |
| { | |
| scope: "javascript,typescript", | |
| prefix: "st_getQueryParams", | |
| body: `const getQueryParams = url => { | |
| const match = url.split(/\/?\?|#/)[1]; | |
| if (!match) return; | |
| return match.split(/&/g).reduce((m, v) => { | |
| const [key, val] = v.split("="); | |
| m[key] = val; | |
| return m; | |
| }, {}); | |
| }; | |
| const params = getQueryParams( | |
| "https://flank.xyz/contact/thank-you/?loginId=xsf234-22f8-48d3-c3d3-08d7c7f8b488&tag=17&xyz=123#&xyz=456" | |
| ); | |
| console.log(params.xyz); | |
| console.log(params.loginId);`, | |
| description: "Query Param" | |
| }, | |
| { | |
| scope: "javascript,typescript", | |
| prefix: "st_search_tree", | |
| body: `const searchFn = (tree, name) => { | |
| let result = null; | |
| if (typeof tree !== "object") return result; | |
| if (tree.name === name) return tree; | |
| if (tree.children && tree.children.length) { | |
| tree.children.some(data => (result = searchFn(data, name))); | |
| } | |
| return result; | |
| };`, | |
| description: "Search in Tree" | |
| }, | |
| { | |
| scope: "javascript,typescript", | |
| prefix: "st_permutation", | |
| body: `const permutation = arr => { | |
| return permute([], arr); | |
| }; | |
| const permute = (prefix, suffix, arr = []) => { | |
| if (suffix.length == 0) arr.push(prefix); | |
| else { | |
| for (let i = 0, len = suffix.length; i < len; i++) { | |
| permute( | |
| prefix.concat(suffix[i]), | |
| suffix.splice(i, 1), | |
| arr | |
| ); | |
| } | |
| } | |
| return arr; | |
| }; | |
| console.log(permutation(["A", "B", "C"]));`, | |
| description: "Permutation" | |
| } | |
| ]; | |
| const filePath = | |
| "/Users/xdeepakv/Library/Application Support/Code/User/snippets/stackoverflow.code-snippets"; | |
| const json = JSON.stringify( | |
| snippets.reduce((map, item) => { | |
| item.body = String(item.body).split(/\n/); | |
| map[item.prefix] = item; | |
| return map; | |
| },{}), | |
| null, | |
| 4 | |
| ); | |
| require("fs").writeFileSync(filePath, json); | |
| console.log("DONE!!!") | |
| module.exports = snippets; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment