Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

A tiny JavaScript utility to access deep properties using a path (for Node and the Browser)

License

NotificationsYou must be signed in to change notification settings

mariocasciaro/object-path

Repository files navigation

Access deep properties using a path

NPM versionBuild StatusCoverage StatusdevDependency StatusDownloads

Changelog

0.11.8

  • SECURITY FIX. Fix a prototype pollution vulnerability in thedel(),empty(),push(),insert() functions when using the "inherited props" mode (e.g. when a newobject-path instance is created with theincludeInheritedProps option set totrue or when using thewithInheritedProps default instance. To help with preventing this type of vulnerability in the client code, also theget() function will now throw an exception if an object's magic properties are accessed. The vulnerability does not exist in the default instance exposed by object path (e.gobjectPath.del()) if using version >=0.11.0.

0.11.6

  • SECURITY FIX. Fix a circumvention of the security fix released in 0.11.5 when non-string/non-numeric values are used in the path (e.g.op.withInheritedProps.set({}, [['__proto__'], 'polluted'], true))

0.11.5

  • SECURITY FIX. Fix a prototype pollution vulnerability in theset() function when using the "inherited props" mode (e.g. when a newobject-path instance is created with theincludeInheritedProps option set totrue or when using thewithInheritedProps default instance. The vulnerability does not exist in the default instance exposed by object path (e.gobjectPath.set()) if using version >=0.11.0.

0.11.0

  • Introduce ability to specify options and create new instances ofobject-path
  • Introduce option to control the wayobject-path deals with inherited properties (includeInheritedProps)
  • New defaultobject-path instance already configured to handle not-own object properties (withInheritedProps)

0.10.0

  • Improved performance ofget,set, andpush by 2x-3x
  • Introduced a benchmarking test suite
  • BREAKING CHANGE:del,empty,set will not affect not-own object's properties (made them consistent with the other methods)

Install

Node.js

npm install object-path --save

Bower

bower install object-path --save

Typescript typings

typings install --save dt~object-path

Usage

varobj={a:{b:"d",c:["e","f"],'\u1200':'unicode key','dot.dot':'key'}};varobjectPath=require("object-path");//get deep propertyobjectPath.get(obj,"a.b");//returns "d"objectPath.get(obj,["a","dot.dot"]);//returns "key"objectPath.get(obj,'a.\u1200');//returns "unicode key"//get the first non-undefined valueobjectPath.coalesce(obj,['a.z','a.d',['a','b']],'default');//empty a given path (but do not delete it) depending on their type,so it retains reference to objects and arrays.//functions that are not inherited from prototype are set to null.//object instances are considered objects and just own property names are deletedobjectPath.empty(obj,'a.b');// obj.a.b is now ''objectPath.empty(obj,'a.c');// obj.a.c is now []objectPath.empty(obj,'a');// obj.a is now {}//works also with arraysobjectPath.get(obj,"a.c.1");//returns "f"objectPath.get(obj,["a","c","1"]);//returns "f"//can return a default value with getobjectPath.get(obj,["a.c.b"],"DEFAULT");//returns "DEFAULT", since a.c.b path doesn't exists, if omitted, returns undefined//setobjectPath.set(obj,"a.h","m");// or objectPath.set(obj, ["a","h"], "m");objectPath.get(obj,"a.h");//returns "m"//set will create intermediate object/arraysobjectPath.set(obj,"a.j.0.f","m");//will insert values in arrayobjectPath.insert(obj,"a.c","m",1);// obj.a.c = ["e", "m", "f"]//push into arrays (and create intermediate objects/arrays)objectPath.push(obj,"a.k","o");//ensure a path exists (if it doesn't, set the default value you provide)objectPath.ensureExists(obj,"a.k.1","DEFAULT");varoldVal=objectPath.ensureExists(obj,"a.b","DEFAULT");// oldval === "d"//deletes a pathobjectPath.del(obj,"a.b");// obj.a.b is now undefinedobjectPath.del(obj,["a","c",0]);// obj.a.c is now ['f']//tests path existenceobjectPath.has(obj,"a.b");// trueobjectPath.has(obj,["a","d"]);// false//bind objectvarmodel=objectPath({a:{b:"d",c:["e","f"]}});//now any method from above is supported directly w/o passing an objectmodel.get("a.b");//returns "d"model.get(["a.c.b"],"DEFAULT");//returns "DEFAULT"model.del("a.b");// obj.a.b is now undefinedmodel.has("a.b");// false

Howobject-path deals with inherited properties

By defaultobject-path will only access an object's own properties. Look at the following example:

varproto={notOwn:{prop:'a'}}varobj=Object.create(proto);//This will return undefined (or the default value you specified), because notOwn is//an inherited propertyobjectPath.get(obj,'notOwn.prop');//This will set the property on the obj instance and not the prototype.//In other words proto.notOwn.prop === 'a' and obj.notOwn.prop === 'b'objectPath.set(obj,'notOwn.prop','b');

To configureobject-path to also deal with inherited properties, you need to create a new instance and specifytheincludeInheritedProps = true in the options object:

varobjectPath=require("object-path");varobjectPathWithInheritedProps=objectPath.create({includeInheritedProps:true})

Alternatively,object-path exposes an instance already configured to handle inherited properties (objectPath.withInheritedProps):

varobjectPath=require("object-path");varobjectPathWithInheritedProps=objectPath.withInheritedProps

Once you have the new instance, you can access inherited properties as you access other properties:

varproto={notOwn:{prop:'a'}}varobj=Object.create(proto);//This will return 'a'objectPath.withInheritedProps.get(obj,'notOwn.prop');//This will set proto.notOwn.prop to 'b'objectPath.set(obj,'notOwn.prop','b');

NOTE: For security reasonsobject-path will throw an exception when trying to access an object's magic properties (e.g.__proto__,constructor) when in "inherited props" mode.

Immutability

If you are looking for animmutable alternative of this library, you can take a look at:object-path-immutable

Credits

About

A tiny JavaScript utility to access deep properties using a path (for Node and the Browser)

Resources

License

Security policy

Stars

Watchers

Forks

Packages

No packages published

Contributors14


[8]ページ先頭

©2009-2025 Movatter.jp