Skip to content

Commit 6a05518

Browse files
committed
Update readme to include new instance withInheritedProperties
[Fix] Rename withOwnProperties to withInheritedProperties
1 parent e0deb11 commit 6a05518

File tree

3 files changed

+59
-13
lines changed

3 files changed

+59
-13
lines changed

README.md

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,17 @@ Access deep properties using a path
1414

1515
## Changelog
1616

17+
### 0.11.0
18+
19+
* Introduce ability to specify options and create new instances of `object-path`
20+
* Introduce option to control the way `object-path` deals with inherited properties (`includeInheritedProps`)
21+
* New default `object-path` instance already configured to handle not-own object properties (`withInheritedProps`)
22+
1723
### 0.10.0
1824

1925
* Improved performance of `get`, `set`, and `push` by 2x-3x
2026
* Introduced a benchmarking test suite
21-
* **BREAKING CHANGE**: `del`, `empty`, `set` will not affect not-own object's properties
27+
* **BREAKING CHANGE**: `del`, `empty`, `set` will not affect not-own object's properties (made them consistent with the other methods)
2228

2329
## Install
2430

@@ -117,9 +123,49 @@ model.del("a.b"); // obj.a.b is now undefined
117123
model.has("a.b"); // false
118124

119125
```
120-
### Notes
126+
### How `object-path` deals with inherited properties
127+
128+
By default `object-path` will only access an object's own properties. Look at the following example:
129+
130+
```javascript
131+
var Obj = function() {};
132+
Obj.prototype.notOwn = {prop: 'a'};
133+
var obj = new Obj();
134+
135+
//This will return undefined (or the default value you specified), because notOwn is
136+
//an inherited property
137+
objectPath.get(obj, 'notOwn.prop');
138+
139+
//This will set the property on the obj instance and not the prototype.
140+
//In other words Obj.notOwn.prop === 'a' and obj.notOwn.prop === 'b'
141+
objectPath.set(obj, 'notOwn.prop', 'b');
142+
```
143+
To configure `object-path` to also deal with inherited properties, you need to create a new instance and specify
144+
the `includeInheritedProps = true` in the options object:
145+
146+
```javascript
147+
var objectPath = require("object-path");
148+
var objectPathWithInheritedProps = objectPath.create({includeInheritedProps: true})
149+
```
150+
151+
Alternatively, `object-path` exposes an instance already configured to handle inherited properties (`objectPath.withInheritedProps`):
152+
```javascript
153+
var objectPath = require("object-path");
154+
var objectPathWithInheritedProps = objectPath.withInheritedProps
155+
```
156+
157+
Once you have the new instance, you can access inherited properties as you access other properties:
158+
```javascript
159+
var Obj = function() {};
160+
Obj.prototype.notOwn = {prop: 'a'};
161+
var obj = new Obj();
121162

122-
`object-path` is intentionally designed to access only an object's own properties
163+
//This will return 'a'
164+
objectPath.withInheritedProps.get(obj, 'notOwn.prop');
165+
166+
//This will set Obj.notOwn.prop to 'b'
167+
objectPath.set(obj, 'notOwn.prop', 'b');
168+
```
123169

124170
### Immutability
125171

index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@
7979
};
8080

8181
function getShallowProperty(obj, prop) {
82-
if(options.includeOwnProperties || (typeof prop === 'number' && Array.isArray(obj)) || obj.hasOwnProperty(prop)) {
82+
if(options.includeInheritedProps || (typeof prop === 'number' && Array.isArray(obj)) || obj.hasOwnProperty(prop)) {
8383
return obj[prop];
8484
}
8585
}
@@ -133,7 +133,7 @@
133133
for (var i = 0; i < path.length; i++) {
134134
var j = getKey(path[i]);
135135
if((typeof j === 'number' && isArray(obj) && j < obj.length) ||
136-
(options.includeOwnProperties ? (j in Object(obj)) : _hasOwnProperty.call(obj, j))) {
136+
(options.includeInheritedProps ? (j in Object(obj)) : _hasOwnProperty.call(obj, j))) {
137137
obj = obj[j];
138138
} else {
139139
return false;
@@ -284,6 +284,6 @@
284284

285285
var mod = factory();
286286
mod.create = factory;
287-
mod.withOwnProperties = factory({includeOwnProperties: true})
287+
mod.withInheritedProps = factory({includeInheritedProps: true})
288288
return mod;
289289
});

test.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -835,7 +835,7 @@ describe('Access own properties [optional]', function () {
835835
Obj.prototype.notOwn = {a: 'a'};
836836
var obj = new Obj();
837837

838-
expect(objectPath.withOwnProperties.get(obj, 'notOwn.a')).to.be.equal('a')
838+
expect(objectPath.withInheritedProps.get(obj, 'notOwn.a')).to.be.equal('a')
839839
});
840840

841841
it('should set a deep not own property on the prototype (if exists)', function() {
@@ -844,7 +844,7 @@ describe('Access own properties [optional]', function () {
844844
}
845845
var obj = Object.create(proto)
846846

847-
objectPath.withOwnProperties.set(obj, 'notOwn.test', 'a');
847+
objectPath.withInheritedProps.set(obj, 'notOwn.test', 'a');
848848
expect(obj.notOwn.test).to.be.equal('a');
849849
expect(proto.notOwn).to.be.deep.equal({test: 'a'});
850850
});
@@ -856,8 +856,8 @@ describe('Access own properties [optional]', function () {
856856
}
857857
var obj = Object.create(proto)
858858

859-
expect(objectPath.withOwnProperties.has(obj, 'notOwn')).to.be.true;
860-
expect(objectPath.withOwnProperties.has(obj, 'notOwn.a')).to.be.true;
859+
expect(objectPath.withInheritedProps.has(obj, 'notOwn')).to.be.true;
860+
expect(objectPath.withInheritedProps.has(obj, 'notOwn.a')).to.be.true;
861861
});
862862

863863
it('empty should empty a not own property', function() {
@@ -866,7 +866,7 @@ describe('Access own properties [optional]', function () {
866866
}
867867
var obj = Object.create(proto);
868868

869-
objectPath.withOwnProperties.empty(obj, 'notOwn');
869+
objectPath.withInheritedProps.empty(obj, 'notOwn');
870870
expect(proto.notOwn).to.be.deep.equal({});
871871
expect(obj.notOwn).to.be.deep.equal({});
872872
});
@@ -877,10 +877,10 @@ describe('Access own properties [optional]', function () {
877877
}
878878
var obj = Object.create(proto);
879879

880-
objectPath.withOwnProperties.del(obj, 'notOwn.a');
880+
objectPath.withInheritedProps.del(obj, 'notOwn.a');
881881
expect(proto.notOwn).to.be.deep.equal({});
882882
//expect(obj.notOwn).to.be.deep.equal({});
883-
objectPath.withOwnProperties.del(obj, 'notOwn');
883+
objectPath.withInheritedProps.del(obj, 'notOwn');
884884
//expect(proto).to.be.deep.equal({notOwn: {}});
885885
//expect(obj).to.be.deep.equal({notOwn: {}});
886886
});

0 commit comments

Comments
 (0)