Skip to content

Instantly share code, notes, and snippets.

@pc035860
Forked from andrewchilds/deepGet.js
Created May 19, 2024 18:45
Show Gist options
  • Select an option

  • Save pc035860/1e4936e074df9ed68454f8a5f6c16ec8 to your computer and use it in GitHub Desktop.

Select an option

Save pc035860/1e4936e074df9ed68454f8a5f6c16ec8 to your computer and use it in GitHub Desktop.

Revisions

  1. @andrewchilds andrewchilds revised this gist Apr 28, 2022. No changes.
  2. @andrewchilds andrewchilds revised this gist Apr 28, 2022. 3 changed files with 70 additions and 19 deletions.
    15 changes: 15 additions & 0 deletions deepGet.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,15 @@
    // Simple implementation of lodash.get
    // Handles arrays, objects, and any nested combination of the two.
    // Also handles undefined as a valid value - see test case for details.
    // Based on: https://gist.github.com/harish2704/d0ee530e6ee75bad6fd30c98e5ad9dab
    export function deepGet(obj, query, defaultVal) {
    query = Array.isArray(query) ? query : query.replace(/(\[(\d)\])/g, '.$2').replace(/^\./, '').split('.');
    if (!(query[0] in obj)) {
    return defaultVal;
    }
    obj = obj[query[0]];
    if (obj && query.length > 1) {
    return deepGet(obj, query.slice(1), defaultVal);
    }
    return obj;
    }
    55 changes: 55 additions & 0 deletions deepGet.spec.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,55 @@
    import deepGet from './deepGet.js';

    describe('deepGet', () => {
    const testObj = {
    a: {
    b: {
    c: {
    d: 123
    }
    },
    e: [
    { f: 9 },
    { g: 10 }
    ]
    }
    };

    const testArr = [
    { id: 1, comments: [{ text: 'hello' }, { text: 'goodbye' }] },
    { id: 2, comments: [] }
    ];

    const falseyObj = {
    isUndefined: undefined,
    isNull: null,
    isZero: 0,
    isEmptyString: ''
    };

    it('handles nested objects', () => {
    expect(deepGet(testObj, 'a.b.c.d')).toBe(123);
    });

    it('handles arrays inside an object', () => {
    expect(deepGet(testObj, 'a.e[0].f')).toBe(9);
    });

    it('handles objects inside an array', () => {
    expect(deepGet(testArr, '[0].comments[1].text')).toBe('goodbye');
    });

    it('returns the default value if query was not found', () => {
    const defaultVal = 'oh no';
    expect(deepGet(testObj, 'invalid.not[0].found', defaultVal)).toBe(defaultVal);
    });

    it('returns falsey values, including undefined', () => {
    const defaultVal = 'my default';
    expect(deepGet(falseyObj, 'isUndefined', defaultVal)).toBe(undefined);
    expect(deepGet(falseyObj, 'isNull', defaultVal)).toBe(null);
    expect(deepGet(falseyObj, 'isZero', defaultVal)).toBe(0);
    expect(deepGet(falseyObj, 'isEmptyString', defaultVal)).toBe('');
    });

    });
    19 changes: 0 additions & 19 deletions lodash.get.js
    Original file line number Diff line number Diff line change
    @@ -1,19 +0,0 @@
    /* Implementation of lodash.get function */
    function getProp( object, keys, defaultVal ){
    keys = Array.isArray( keys )? keys : keys.split('.');
    object = object[keys[0]];
    if( object && keys.length>1 ){
    return getProp( object, keys.slice(1) );
    }
    return object === undefined? defaultVal : object;
    }

    /* Implementation of lodash.set function */
    function setProp( object, keys, val ){
    keys = Array.isArray( keys )? keys : keys.split('.');
    if( keys.length>1 ){
    object[keys[0]] = object[keys[0]] || {};
    return setProp( object[keys[0]], keys.slice(1), val );
    }
    object[keys[0]] = val;
    }
  3. @harish2704 harish2704 revised this gist Dec 7, 2016. 1 changed file with 9 additions and 0 deletions.
    9 changes: 9 additions & 0 deletions lodash.get.js
    Original file line number Diff line number Diff line change
    @@ -8,3 +8,12 @@ function getProp( object, keys, defaultVal ){
    return object === undefined? defaultVal : object;
    }

    /* Implementation of lodash.set function */
    function setProp( object, keys, val ){
    keys = Array.isArray( keys )? keys : keys.split('.');
    if( keys.length>1 ){
    object[keys[0]] = object[keys[0]] || {};
    return setProp( object[keys[0]], keys.slice(1), val );
    }
    object[keys[0]] = val;
    }
  4. @harish2704 harish2704 renamed this gist Dec 7, 2016. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  5. @harish2704 harish2704 revised this gist Nov 4, 2016. No changes.
  6. @harish2704 harish2704 created this gist Nov 4, 2016.
    10 changes: 10 additions & 0 deletions lodash.get
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,10 @@
    /* Implementation of lodash.get function */
    function getProp( object, keys, defaultVal ){
    keys = Array.isArray( keys )? keys : keys.split('.');
    object = object[keys[0]];
    if( object && keys.length>1 ){
    return getProp( object, keys.slice(1) );
    }
    return object === undefined? defaultVal : object;
    }