From ebae849f393f49196de4d7d637e18375bebc67ea Mon Sep 17 00:00:00 2001 From: Patrick O'Meara Date: Wed, 22 Jun 2022 19:45:06 +1000 Subject: [PATCH 1/2] Add add * the add method adds a single item to the collection fixes ecrmnn/collect.js#293 --- README.md | 13 ++++++++++++- dist/index.js | 1 + dist/methods/add.js | 6 ++++++ docs/api/add.md | 12 ++++++++++++ src/index.js | 1 + src/methods/add.js | 7 +++++++ test/methods/add_test.js | 17 +++++++++++++++++ 7 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 dist/methods/add.js create mode 100644 docs/api/add.md create mode 100644 src/methods/add.js create mode 100644 test/methods/add_test.js 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/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]); + }); +}; From 1cb87dbf8132f00fbcfded7c8f60cc7353722461 Mon Sep 17 00:00:00 2001 From: Patrick O'Meara Date: Wed, 22 Jun 2022 20:02:19 +1000 Subject: [PATCH 2/2] Added type --- index.d.ts | 5 +++++ 1 file changed, 5 insertions(+) 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. */