diff --git a/README.md b/README.md index 75f1ed6..c2cc3dc 100644 --- a/README.md +++ b/README.md @@ -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) @@ -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: @@ -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) \ No newline at end of file +MIT © [Daniel Eckermann](https://danieleckermann.com) diff --git a/dist/index.js b/dist/index.js index 5560ab9..e5e9e8c 100644 --- a/dist/index.js +++ b/dist/index.js @@ -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'); diff --git a/dist/methods/add.js b/dist/methods/add.js new file mode 100644 index 0000000..c8fbc42 --- /dev/null +++ b/dist/methods/add.js @@ -0,0 +1,6 @@ +'use strict'; + +module.exports = function add(item) { + this.items.push(item); + return this; +}; \ No newline at end of file diff --git a/docs/api/add.md b/docs/api/add.md new file mode 100644 index 0000000..ea1a7bb --- /dev/null +++ b/docs/api/add.md @@ -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) diff --git a/index.d.ts b/index.d.ts index 95618a0..f133214 100644 --- a/index.d.ts +++ b/index.d.ts @@ -6,6 +6,11 @@ declare module 'collect.js' { export class Collection { 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. */ diff --git a/src/index.js b/src/index.js index a62def5..bbf2cb9 100644 --- a/src/index.js +++ b/src/index.js @@ -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'); diff --git a/src/methods/add.js b/src/methods/add.js new file mode 100644 index 0000000..69e99ad --- /dev/null +++ b/src/methods/add.js @@ -0,0 +1,7 @@ +'use strict'; + +module.exports = function add(item) { + this.items.push(item); + + return this; +}; diff --git a/test/methods/add_test.js b/test/methods/add_test.js new file mode 100644 index 0000000..5b66d2c --- /dev/null +++ b/test/methods/add_test.js @@ -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]); + }); +};