Skip to content

Commit

Permalink
Allow number for path (#4)
Browse files Browse the repository at this point in the history
* Allow number for path

* Fix typos

* Add more tests
  • Loading branch information
Flamenco authored and eagleeye committed Dec 29, 2016
1 parent e0aabc3 commit 9c75217
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 4 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ set(object, path, value) --> object
delete(object, path) --> object
```

None of the function mutate the input object. For efficiency the returned object is not a deep clone of the original, but a shallow copy of the objects in the mutated path.
None of the functions mutate the input object. For efficiency the returned object is not a deep clone of the original, but a shallow copy of the objects in the mutated path.


## Usage
Expand Down
6 changes: 3 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

module.exports.set = function(obj, prop, value) {
prop = typeof prop === 'string' ? propToArray(prop) : prop;
prop = typeof prop === 'number' ? propToArray(prop.toString()) : typeof prop === 'string' ? propToArray(prop) : prop;

var setPropImmutableRec = function(obj, prop, value, i) {
var clone, head = prop[i];
Expand All @@ -24,7 +24,7 @@ module.exports.set = function(obj, prop, value) {
};

module.exports.get = function(obj, prop) {
prop = typeof prop === 'string' ? propToArray(prop) : prop;
prop = typeof prop === 'number' ? propToArray(prop.toString()) : typeof prop === 'string' ? propToArray(prop) : prop;

for (var i = 0; i < prop.length; i++) {
if (typeof obj !== 'object') {
Expand All @@ -41,7 +41,7 @@ module.exports.get = function(obj, prop) {
};

module.exports.delete = function(obj, prop) {
prop = typeof prop === 'string' ? propToArray(prop) : prop;
prop = typeof prop === 'number' ? propToArray(prop.toString()) : typeof prop === 'string' ? propToArray(prop) : prop;

var deletePropImmutableRec = function(obj, prop, i) {
var clone, head = prop[i];
Expand Down
62 changes: 62 additions & 0 deletions test/dot-prop-immutable-number.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
var dotProp = require('..');

describe('dot-prop-immutable.number.spec.js', function () {

var arr = [1, {a: false}];

var result;
describe('when have an array', () => {

describe('when set prop using number as path', () => {

before(function () {
result = dotProp.set(arr, 1, 3);
});

it('should replace prop', () => {
expect(result).to.eql([
1,
3
]);
});

it('invariant', arrInvariant);
});


describe('when get prop using number as path', () => {

before(function () {
result = dotProp.get(arr, 1);
});

it('should get prop', () => {
expect(result).to.eql({a: false});
});

it('invariant', arrInvariant);
});

describe('when delete prop using number as path', () => {

before(function () {
result = dotProp.delete(arr, 1);
});

it('should remove prop', () => {
expect(result).to.eql([
1
]);
});
it('invariant', arrInvariant);
});
});

function arrInvariant() {
expect(arr).to.eql(
[1, {a: false}]
);
}
});


0 comments on commit 9c75217

Please sign in to comment.