Skip to content

Commit

Permalink
add push/unshift helper methods
Browse files Browse the repository at this point in the history
- add `update.push`, `update.unshift` helper methods. `push` is an
alias for existing `add` helper method, `unshift` is a new one with
an alias of `prepend`
  • Loading branch information
akuzko committed Jul 6, 2017
1 parent d5a2987 commit e9569d6
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 4 deletions.
21 changes: 19 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,19 +90,36 @@ Notes on object lookup:

### Array Update Helpers

#### `update.add`
#### `update.unshift`

Adds item to the array.

```js
import update from 'update-js';

const obj = { foo: { bar: [1, 2] } };
const upd = update.add(obj, 'foo.bar', 3);
const upd = update.unshift(obj, 'foo.bar', 3);

upd.foo.bar // => [3, 1, 2];
```

_Alias:_ `update.prepend`

#### `update.push`

Adds item to the array.

```js
import update from 'update-js';

const obj = { foo: { bar: [1, 2] } };
const upd = update.push(obj, 'foo.bar', 3);

upd.foo.bar // => [1, 2, 3];
```

_Alias:_ `update.add`

#### `update.remove`

Removes item from the array by index or lookup key.
Expand Down
27 changes: 25 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@
var set = require('lodash.set');

update.with = updateWith;
update.add = updateAdd;
update.unshift = updateUnshift;
update.prepend = updateUnshift;
update.shift = updateShift;
update.push = updatePush;
update.add = updatePush;
update.pop = updatePop;
update.remove = updateRemove;
update.assign = updateAssign;
update.del = updateDel;
Expand Down Expand Up @@ -38,12 +43,30 @@ function updateWith(obj, path, fn) {
return updateInWith(current, path, fn);
}

function updateAdd(obj, path, item) {
function updateUnshift(obj, path, item) {
return updateWith(obj, path, function(collection) {
return [item].concat(collection);
});
}

function updateShift(obj, path) {
return updateWith(obj, path, function(collection) {
return collection.slice(1);
});
}

function updatePush(obj, path, item) {
return updateWith(obj, path, function(collection) {
return collection.concat([item]);
});
}

function updatePop(obj, path) {
return updateWith(obj, path, function(collection) {
return collection.slice(0, -1);
});
}

function updateRemove(obj, path) {
var match = path.match(/^(.+)\.(?!\.)(.+)$/);

Expand Down
27 changes: 27 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,33 @@ describe('update', function() {
});
});

describe('update.unshift', function() {
it('prepends new item to the collection', function() {
var obj = { foo: { bar: [1, 2] } };
var upd = update.unshift(obj, 'foo.bar', 3);

assert.deepEqual(upd.foo.bar, [3, 1, 2]);
});
});

describe('update.prepend', function() {
it('prepends new item to the collection', function() {
var obj = { foo: { bar: [1, 2] } };
var upd = update.prepend(obj, 'foo.bar', 3);

assert.deepEqual(upd.foo.bar, [3, 1, 2]);
});
});

describe('update.push', function() {
it('adds new item to the collection', function() {
var obj = { foo: { bar: [1, 2] } };
var upd = update.push(obj, 'foo.bar', 3);

assert.deepEqual(upd.foo.bar, [1, 2, 3]);
});
});

describe('update.add', function() {
it('adds new item to the collection', function() {
var obj = { foo: { bar: [1, 2] } };
Expand Down

0 comments on commit e9569d6

Please sign in to comment.