Last active
January 6, 2020 11:20
-
-
Save davidalekna/10f5a23037b730f1d2c7e1a1ab89a2f4 to your computer and use it in GitHub Desktop.
Revisions
-
davidalekna revised this gist
Jan 6, 2020 . No changes.There are no files selected for viewing
-
Alekna revised this gist
Jan 6, 2020 . 1 changed file with 72 additions and 67 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,78 +1,83 @@ const example = { name: "hello_world", children: { name: "hello_world_2", something: "hello_world_2", children: { someNumberValue: 23213, name: "hey______whatsup", children: { name: "h_e_ll_o_world_2", something: "h____ello_world_2", arrayOfThings: [ "asjd__asdasd", "dqwndiuwqd_213213", 213213, [ "asjd__asdasd", "dqwndiuwqd_213213", 213213, [ "asjd__asdasd", "dqwndiuwqd_213213", 213213, [ "asjd__asdasd", "dqwndiuwqd_213213", 213213, { name: "abu_gelis", children: { name: "kobra______11" } } ] ] ] ] } } } }; function transform(obj: object, match: RegExp | string, replaceWith: string) { if (!obj) return; return Object.keys(obj).reduce((acc, key) => { const item = obj[key]; if (Array.isArray(item)) { return { ...acc, [key]: item.map(i => { if (Array.isArray(i)) { return transform(i, match, replaceWith); } if (typeof i === "string") { return i.replace(match, replaceWith); } return i; }) }; } if (typeof item !== "object") { let value = item; switch (typeof item) { case "string": value = item.replace(match, replaceWith); break; default: value = item; break; } return { ...acc, [key]: value }; } return { ...acc, [key]: transform(item, match, replaceWith) }; }, {}); } console.log(transform(example, /\_/g, "-")); -
davidalekna renamed this gist
Jan 6, 2020 . 1 changed file with 1 addition and 2 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -75,5 +75,4 @@ function transform(obj: object, match: RegExp | string, replaceWith: string) { }; }, {}); } console.log(transform(example, /\_/g, '-')); -
davidalekna created this gist
Jan 6, 2020 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,79 @@ const example = { name: 'hello_world', children: { name: 'hello_world_2', something: 'hello_world_2', children: { someNumberValue: 23213, name: 'hey______whatsup', children: { name: 'h_e_ll_o_world_2', something: 'h____ello_world_2', arrayOfThings: [ 'asjd__asdasd', 'dqwndiuwqd_213213', 213213, [ 'asjd__asdasd', 'dqwndiuwqd_213213', 213213, [ 'asjd__asdasd', 'dqwndiuwqd_213213', 213213, ['asjd__asdasd', 'dqwndiuwqd_213213', 213213, { name: 'abu_gelis', children: { name: 'kobra______11' } }], ], ], ], }, }, }, }; function transform(obj: object, match: RegExp | string, replaceWith: string) { if (!obj) return; return Object.keys(obj).reduce((acc, key) => { const item = obj[key]; if (Array.isArray(item)) { return { ...acc, [key]: item.map(i => { if (Array.isArray(i)) { return transform(i, match, replaceWith); } if (typeof i === 'string') { return i.replace(match, replaceWith); } return i; }), }; } if (typeof item !== 'object') { let value = item; switch (typeof item) { case 'string': value = item.replace(match, replaceWith); break; default: value = item; break; } return { ...acc, [key]: value, }; } return { ...acc, [key]: transform(item, match, replaceWith), }; }, {}); } transform(example, /\_/g, '-')