Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: mariocasciaro/object-path
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 3f1e4ea93b9eef77236f0a3dce73dfcf89c780c0
Choose a base ref
...
head repository: mariocasciaro/object-path
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 94f92d8932fce12eeff853116646160477c6ce11
Choose a head ref
Loading
Showing with 6,632 additions and 778 deletions.
  1. +2 −1 .gitignore
  2. +5 −4 .travis.yml
  3. +1 −1 LICENSE
  4. +78 −5 README.md
  5. +52 −0 benchmark.js
  6. +0 −1 bower.json
  7. +2 −2 component.json
  8. +208 −180 index.js
  9. +5,377 −0 package-lock.json
  10. +13 −9 package.json
  11. +894 −575 test.js
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
node_modules
.c9revisions
.settings
.idea
npm-debug.log
generated
coverage
.DS_Store
.nyc_output
9 changes: 5 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
sudo: false
language: node_js
node_js:
- "0.12"
- "0.10"
- "iojs"
after_success: "./node_modules/istanbul/lib/cli.js cover ./node_modules/mocha/bin/_mocha --report lcovonly -- -R spec && cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js && rm -rf ./coverage"
- "10"
- "12"
- "14"
after_script: NODE_ENV=test istanbul cover ./node_modules/mocha/bin/_mocha --report lcovonly -- -R spec && cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js && rm -rf ./coverage
2 changes: 1 addition & 1 deletion LICENSE
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2013 Mario Casciaro
Copyright (c) 2015 Mario Casciaro

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
83 changes: 78 additions & 5 deletions README.md
100755 → 100644
Original file line number Diff line number Diff line change
@@ -11,24 +11,42 @@ Access deep properties using a path
[![devDependency Status](https://david-dm.org/mariocasciaro/object-path/dev-status.svg)](https://david-dm.org/mariocasciaro/object-path#info=devDependencies)
![Downloads](http://img.shields.io/npm/dm/object-path.svg)

## Changelog

### 0.11.5

* **SECURITY FIX**. Fix a prototype pollution vulnerability in the `set()` function when using the "inherited props" mode (e.g. when a new `object-path` instance is created with the `includeInheritedProps` option set to `true` or when using the `withInheritedProps` default instance. The vulnerability does not exist in the default instance exposed by object path (e.g `objectPath.set()`) if using version >= `0.11.0`.

### 0.11.0

* Introduce ability to specify options and create new instances of `object-path`
* Introduce option to control the way `object-path` deals with inherited properties (`includeInheritedProps`)
* New default `object-path` instance already configured to handle not-own object properties (`withInheritedProps`)

### 0.10.0

* Improved performance of `get`, `set`, and `push` 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
npm install object-path --save
```

### Browser
### Bower

```
bower install object-path
bower install object-path --save
```

### Typescript typings

```
tsd query object-path --action install --save
typings install --save dt~object-path
```

## Usage
@@ -38,14 +56,18 @@ tsd query object-path --action install --save
var obj = {
a: {
b: "d",
c: ["e", "f"]
c: ["e", "f"],
'\u1200': 'unicode key',
'dot.dot': 'key'
}
};

var objectPath = require("object-path");

//get deep property
objectPath.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 value
objectPath.coalesce(obj, ['a.z', 'a.d', ['a','b']], 'default');
@@ -79,6 +101,7 @@ 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");
var oldVal = objectPath.ensureExists(obj, "a.b", "DEFAULT"); // oldval === "d"

//deletes a path
objectPath.del(obj, "a.b"); // obj.a.b is now undefined
@@ -103,6 +126,56 @@ model.del("a.b"); // obj.a.b is now undefined
model.has("a.b"); // false

```
### How `object-path` deals with inherited properties

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

```javascript
var proto = {
notOwn: {prop: 'a'}
}
var obj = Object.create(proto);

//This will return undefined (or the default value you specified), because notOwn is
//an inherited property
objectPath.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 configure `object-path` to also deal with inherited properties, you need to create a new instance and specify
the `includeInheritedProps = true` in the options object:

```javascript
var objectPath = require("object-path");
var objectPathWithInheritedProps = objectPath.create({includeInheritedProps: true})
```

Alternatively, `object-path` exposes an instance already configured to handle inherited properties (`objectPath.withInheritedProps`):
```javascript
var objectPath = require("object-path");
var objectPathWithInheritedProps = objectPath.withInheritedProps
```

Once you have the new instance, you can access inherited properties as you access other properties:
```javascript
var proto = {
notOwn: {prop: 'a'}
}
var obj = 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');
```

### Immutability

If you are looking for an *immutable* alternative of this library, you can take a look at: [object-path-immutable](https://github.com/mariocasciaro/object-path-immutable)


### Credits

52 changes: 52 additions & 0 deletions benchmark.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
var Benchpress = require('@mariocasciaro/benchpress')
var benchmark = new Benchpress()
var op = require('./')

var testObj = {
level1_a: {
level2_a: {
level3_a: {
level4_a: {
}
}
}
}
}

var testObj2

benchmark
.add('get existing', {
iterations: 100000,
fn: function() {
op.get(testObj, ['level1_a', 'level2_a', 'level3_a', 'level4_a'])
}
})
.add('get non-existing', {
iterations: 100000,
fn: function() {
op.get(testObj, ['level5_a'])
}
})
.add('push', {
iterations: 100000,
fn: function() {
op.push(testObj, ['level1_a', 'level2_a', 'level3_a', 'level4_a', 'level5_a'], 'val')
}
})
.add('set non existing', {
iterations: 100000,
fn: function() {
op.set(testObj2, ['level1_a', 'level2_b', 'level3_b', 'level4_b', 'level5_b'], 'val')
},
beforeEach: function() {
testObj2 = {}
}
})
.add('set existing', {
iterations: 100000,
fn: function() {
op.set(testObj, ['level1_a', 'level2_a', 'level3_a', 'level4_a', 'level5_b'], 'val')
}
})
.run()
1 change: 0 additions & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
{
"name": "object-path",
"version": "0.8.2",
"main": "index.js",
"keywords": [
"deep",
4 changes: 2 additions & 2 deletions component.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "object-path",
"description": "Access deep properties using a path",
"version": "0.6.0",
"version": "0.9.2",
"author": {
"name": "Mario Casciaro"
},
@@ -19,4 +19,4 @@
"bean"
],
"license": "MIT"
}
}
Loading