Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add add #301

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ Using Laravel as your backend? Collect.js offers an (almost) identical api to [L

All available methods

- [add](#add)
- [all](#all)
- [average](#average)
- [avg](#avg)
Expand Down Expand Up @@ -179,6 +180,16 @@ All comparisons in `collect.js` are done using strict equality. Using loose equa
- ~~`whereInStrict`~~ use `whereIn()`
- ~~`whereNotInStrict`~~ use `whereNotIn()`

#### `add()`

The add method adds a single item to the collection.

```js
collect([1, 2, 3, 4]).add(5);

// [1, 2, 3, 4, 5]
```

#### `all()`

The all method returns the underlying array or object represented by the collection:
Expand Down Expand Up @@ -3316,4 +3327,4 @@ PRs are welcomed to this project, and help is needed in order to keep up with th

### License

MIT © [Daniel Eckermann](https://danieleckermann.com)
MIT © [Daniel Eckermann](https://danieleckermann.com)
1 change: 1 addition & 0 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ Collection.prototype.toJSON = function toJSON() {
return this.items;
};

Collection.prototype.add = require('./methods/add');
Collection.prototype.all = require('./methods/all');
Collection.prototype.average = require('./methods/average');
Collection.prototype.avg = require('./methods/avg');
Expand Down
6 changes: 6 additions & 0 deletions dist/methods/add.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
'use strict';

module.exports = function add(item) {
this.items.push(item);
return this;
};
12 changes: 12 additions & 0 deletions docs/api/add.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# `add()`

The add method adds a single item to the collection.

```js
collect([1, 2, 3, 4]).add(5);

// [1, 2, 3, 4, 5]
```


[View source on GitHub](https://github.com/ecrmnn/collect.js/blob/master/src/methods/add.js)
5 changes: 5 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ declare module 'collect.js' {
export class Collection<Item> {
constructor(collection?: Item[] | Object);

/**
* The add method adds a single item to the collection.
*/
add(item: Item): this;

/**
* The all method returns the underlying array represented by the collection.
*/
Expand Down
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ Collection.prototype.toJSON = function toJSON() {
return this.items;
};

Collection.prototype.add = require('./methods/add');
Collection.prototype.all = require('./methods/all');
Collection.prototype.average = require('./methods/average');
Collection.prototype.avg = require('./methods/avg');
Expand Down
7 changes: 7 additions & 0 deletions src/methods/add.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'use strict';

module.exports = function add(item) {
this.items.push(item);

return this;
};
17 changes: 17 additions & 0 deletions test/methods/add_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
'use strict';

module.exports = (it, expect, collect) => {
it('should append an item to the end of the collection', () => {
const collection = collect([1, 2, 3, 4]);

expect(collection.add(5).all()).to.eql([1, 2, 3, 4, 5]);
});

it('should modify the collection', () => {
const collection = collect([1, 2, 3, 4]);
expect(collection.all()).to.eql([1, 2, 3, 4]);

collection.add(5);
expect(collection.all()).to.eql([1, 2, 3, 4, 5]);
});
};