Skip to content

Instantly share code, notes, and snippets.

@davidalekna
Last active January 6, 2020 11:20
Show Gist options
  • Select an option

  • Save davidalekna/10f5a23037b730f1d2c7e1a1ab89a2f4 to your computer and use it in GitHub Desktop.

Select an option

Save davidalekna/10f5a23037b730f1d2c7e1a1ab89a2f4 to your computer and use it in GitHub Desktop.

Revisions

  1. davidalekna revised this gist Jan 6, 2020. No changes.
  2. Alekna revised this gist Jan 6, 2020. 1 changed file with 72 additions and 67 deletions.
    139 changes: 72 additions & 67 deletions transform.ts
    Original 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' } }],
    ],
    ],
    ],
    },
    },
    },
    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;
    if (!obj) return;

    return Object.keys(obj).reduce((acc, key) => {
    const item = obj[key];
    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 (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;
    if (typeof item !== "object") {
    let value = item;

    switch (typeof item) {
    case 'string':
    value = item.replace(match, replaceWith);
    break;
    default:
    value = item;
    break;
    }
    switch (typeof item) {
    case "string":
    value = item.replace(match, replaceWith);
    break;
    default:
    value = item;
    break;
    }

    return {
    ...acc,
    [key]: value,
    };
    }
    return {
    ...acc,
    [key]: value
    };
    }

    return {
    ...acc,
    [key]: transform(item, match, replaceWith),
    };
    }, {});
    return {
    ...acc,
    [key]: transform(item, match, replaceWith)
    };
    }, {});
    }
    console.log(transform(example, /\_/g, '-'));
    console.log(transform(example, /\_/g, "-"));
  3. davidalekna renamed this gist Jan 6, 2020. 1 changed file with 1 addition and 2 deletions.
    3 changes: 1 addition & 2 deletions gistfile1.txt → transform.ts
    Original file line number Diff line number Diff line change
    @@ -75,5 +75,4 @@ function transform(obj: object, match: RegExp | string, replaceWith: string) {
    };
    }, {});
    }

    transform(example, /\_/g, '-')
    console.log(transform(example, /\_/g, '-'));
  4. davidalekna created this gist Jan 6, 2020.
    79 changes: 79 additions & 0 deletions gistfile1.txt
    Original 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, '-')