Giter VIP home page Giter VIP logo

Comments (5)

TheFoot avatar TheFoot commented on August 16, 2024 2

@msudgh In case it helps you or others who stumble across this, I created a wrapper module to recursively parse an object:

CommonJS Module: (recursive-dot-object.js)

const dot = require ( 'dot-object' );

const parseIt = obj => {

    const parsed = dot.object ( obj );

    for ( const [ key, value ] of Object.entries ( parsed ) ) {

        if ( typeof value === 'object' ) {
            parsed[ key ] = parseIt ( value );
        }

    }

    return parsed;

};

module.exports = obj => {
    return parseIt ( obj );
};

Test code:

const dotParser = require ( './recursive-dot-object' );

const data = {
    request        : {
        query : {
            'scopes.platformOwner': true,
            'scopes.developers'   : true
        },
        params: {
            foo: 'bar'
        }
    },
    'one.two.three': 123
};

console.log (
    JSON.stringify( dotParser ( data ), null, 4)
);

Produces:

{
    "request": {
        "query": {
            "scopes": {
                "platformOwner": true,
                "developers": true
            }
        },
        "params": {
            "foo": "bar"
        }
    },
    "one": {
        "two": {
            "three": 123
        }
    }
}

from dot-object.

jonesdhtx avatar jonesdhtx commented on August 16, 2024

I did look through sources and see there is a constructor argument override and noted that fixes the issues if I do the following:

> dot = new require('dot-object')('.', /*override:*/true);
DotObject { seperator: '.', override: true, useArray: true, cleanup: [] }
> x = { a: 1 }
{ a: 1 }
> dot.str('b', 2, x)
{ a: 1, b: 2 }

Can you explain the purpose of the override parameter and if it has any other consequence?

This can be a work around for me, however imho it feels like the default behavior is an issue.

from dot-object.

rhalff avatar rhalff commented on August 16, 2024

The check for override was wrong in the str() method, I've updated it and pushed a patch release.

I notice the set() method is not described in the documentation, but you might as well use that one.
Currently they are almost the same, the difference being that str() allows a set of modifiers.

str = function (path, val, obj, mod)
set = function (path, val, obj, merge)

I'll look into joining them so both will have the same signature.
set = function (path, val, obj, mod, merge)

from dot-object.

jonesdhtx avatar jonesdhtx commented on August 16, 2024

Awesome - thanks for the info & the quick fix!

from dot-object.

msudgh avatar msudgh commented on August 16, 2024

I have a similar issue but a bit different.

Consider the following example as the request's body:

{
  id: "1",
  data: { "x.y": true }
}

but dot.object(req.body) method is unable to parse it as the below expected:

{
  id: "1",
  data: {
    x: {
      y: true
    }
  }
}

Since it works with dot.object(req.body["data") case i assume its not going to iterate deeply.

from dot-object.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.