Giter Club home page Giter Club logo

Comments (6)

epoberezkin avatar epoberezkin commented on August 12, 2024 3

@evan-king The reason to add something to the library is performance - the pointer in the wrapper above will be parsed and processed twice.

from json-pointer.

manuelstofer avatar manuelstofer commented on August 12, 2024

I'm ok with returning undefined instead of throwing an exception. We should increase the version and add a note about backwards compatibility

from json-pointer.

epoberezkin avatar epoberezkin commented on August 12, 2024

I think breaking backward compatibility is not a good idea, given how many packages use json-pointer. I already have tests that expect this exception. I think others will have a problem too, even if it's a major version change.

I would look at the option(s) in get, set, remove and api methods. I will think a bit and suggest something.

from json-pointer.

futurist avatar futurist commented on August 12, 2024

Throwing error is really annoying. I think add a new option is just for backward compatibility, e.g.

api.get = function get (obj, pointer, silent) {
  .....
  if(!silent) throw new Error('Invalid reference token: ' + tok);
  ....
}

For old code, silent===undefined, and will throw error;
For new code, author can pass silent=true, and get undefined.

It's the same for get, remove, parse.

Hoping a version bump!

from json-pointer.

evan-king avatar evan-king commented on August 12, 2024

Having to deal with thrown errors is not ideal for performance or brevity, nor necessarily is the vagueness of getting undefined and not knowing if the content did not exist or existed with value undefined.

However users have the necessary power as-is to efficiently provide the desired behavior for themselves. If you want silent failure, just write a wrapper function incorporating api.has():

function safeGet(obj, pointer) {
    return api.has(obj, pointer)
        ? api.get(obj, pointer)
        : undefined;
}

I'm not sure that adding bloat to the underlying library is warranted.

from json-pointer.

danrot avatar danrot commented on August 12, 2024

I would prefer if get does not throw an error in case the property does not exist... As well as for performance and practical reasons. I also think that this is a case that happens quite a lot, and therefore is not an exceptional case (as throwing an exception should be).

from json-pointer.

Related Issues (14)

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.