Skip to content

Commit 960a3d6

Browse files
committed
Closes #4
1 parent 7d0c066 commit 960a3d6

File tree

7 files changed

+151
-1
lines changed

7 files changed

+151
-1
lines changed

README.md

+22
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ All available methods
2525
- [chunk](#chunk)
2626
- [collapse](#collapse)
2727
- [combine](#combine)
28+
- [concat](#concat)
2829
- [contains](#contains)
2930
- [count](#count)
3031
- [diff](#diff)
@@ -184,6 +185,27 @@ combine.all();
184185
//=> number: 8
185186
//=> }
186187
```
188+
189+
#### ``concat()``
190+
The concat method is used to merge two or more collections/arrays/objects:
191+
192+
*You can also ``concat()`` an array of objects, or a multidimensional array*
193+
194+
```js
195+
const collection = collect([1, 2, 3]);
196+
197+
collection
198+
.concat(['a', 'b', 'c'])
199+
.concat({
200+
name: 'Steven Gerrard',
201+
number: 8
202+
});
203+
204+
collection.all();
205+
206+
//=> [1, 2, 3, 'a', 'b', 'c', 'Steven Gerrard', 8]
207+
```
208+
187209
#### ``contains()``
188210
The contains method determines whether the collection contains a given item:
189211
```js

dist/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ Collection.prototype.avg = require('./methods/average');
1212
Collection.prototype.chunk = require('./methods/chunk');
1313
Collection.prototype.collapse = require('./methods/collapse');
1414
Collection.prototype.combine = require('./methods/combine');
15+
Collection.prototype.concat = require('./methods/concat');
1516
Collection.prototype.contains = require('./methods/contains');
1617
Collection.prototype.count = require('./methods/count');
1718
Collection.prototype.diff = require('./methods/diff');

dist/methods/concat.js

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
'use strict';
2+
3+
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
4+
5+
module.exports = function concat(collectionOrArrayOrObject) {
6+
var _this = this;
7+
8+
var list = collectionOrArrayOrObject;
9+
10+
if (collectionOrArrayOrObject instanceof this.constructor) {
11+
list = collectionOrArrayOrObject.all();
12+
} else if ((typeof collectionOrArrayOrObject === 'undefined' ? 'undefined' : _typeof(collectionOrArrayOrObject)) === 'object') {
13+
list = [];
14+
Object.keys(collectionOrArrayOrObject).forEach(function (property) {
15+
list.push(collectionOrArrayOrObject[property]);
16+
});
17+
}
18+
19+
list.forEach(function (item) {
20+
if ((typeof item === 'undefined' ? 'undefined' : _typeof(item)) === 'object') {
21+
Object.keys(item).forEach(function (key) {
22+
return _this.items.push(item[key]);
23+
});
24+
} else {
25+
_this.items.push(item);
26+
}
27+
});
28+
29+
return this;
30+
};

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "collect.js",
3-
"version": "3.0.45",
3+
"version": "3.0.46",
44
"description": "Convenient and dependency free wrapper for working with arrays and objects.",
55
"main": "dist/index.js",
66
"typings": "index.d.ts",

src/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ Collection.prototype.avg = require('./methods/average');
1212
Collection.prototype.chunk = require('./methods/chunk');
1313
Collection.prototype.collapse = require('./methods/collapse');
1414
Collection.prototype.combine = require('./methods/combine');
15+
Collection.prototype.concat = require('./methods/concat');
1516
Collection.prototype.contains = require('./methods/contains');
1617
Collection.prototype.count = require('./methods/count');
1718
Collection.prototype.diff = require('./methods/diff');

src/methods/concat.js

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
'use strict';
2+
3+
module.exports = function concat(collectionOrArrayOrObject) {
4+
let list = collectionOrArrayOrObject;
5+
6+
if (collectionOrArrayOrObject instanceof this.constructor) {
7+
list = collectionOrArrayOrObject.all();
8+
} else if (typeof collectionOrArrayOrObject === 'object') {
9+
list = [];
10+
Object.keys(collectionOrArrayOrObject).forEach((property) => {
11+
list.push(collectionOrArrayOrObject[property]);
12+
});
13+
}
14+
15+
list.forEach((item) => {
16+
if (typeof item === 'object') {
17+
Object.keys(item).forEach(key => this.items.push(item[key]));
18+
} else {
19+
this.items.push(item);
20+
}
21+
});
22+
23+
return this;
24+
};

test/tests.js

+72
Original file line numberDiff line numberDiff line change
@@ -1872,6 +1872,78 @@ describe('Collect.js Test Suite', function () {
18721872
expect(collectionObject.toArray()).to.eql(collectionObject.values().all());
18731873
});
18741874

1875+
it('should append arrays to collection', function () {
1876+
const expected = [
1877+
4,
1878+
5,
1879+
6,
1880+
'a',
1881+
'b',
1882+
'c',
1883+
'Jonny',
1884+
'from',
1885+
'Laroe',
1886+
'Jonny',
1887+
'from',
1888+
'Laroe'
1889+
];
1890+
1891+
const firstCollection = collect([4, 5, 6]);
1892+
const firstArray = ['a', 'b', 'c'];
1893+
const secondArray = [{
1894+
who: 'Jonny'
1895+
}, {
1896+
preposition: 'from'
1897+
}, {
1898+
where: 'Laroe'
1899+
}];
1900+
const firstObj = {
1901+
who: 'Jonny',
1902+
preposition: 'from',
1903+
where: 'Laroe'
1904+
};
1905+
1906+
firstCollection.concat(firstArray).concat(secondArray).concat(firstObj);
1907+
1908+
expect(firstCollection.count()).to.eql(12);
1909+
expect(firstCollection.all()).to.eql(expected);
1910+
});
1911+
1912+
it('should append collections to collection', function () {
1913+
const expected = [
1914+
4,
1915+
5,
1916+
6,
1917+
'a',
1918+
'b',
1919+
'c',
1920+
'Jonny',
1921+
'from',
1922+
'Laroe',
1923+
'Jonny',
1924+
'from',
1925+
'Laroe'
1926+
];
1927+
1928+
const firstCollection = collect([4, 5, 6]);
1929+
const secondCollection = collect(['a', 'b', 'c']);
1930+
const thirdCollection = collect([{
1931+
who: 'Jonny'
1932+
}, {
1933+
preposition: 'from'
1934+
}, {
1935+
where: 'Laroe'
1936+
}]);
1937+
1938+
firstCollection
1939+
.concat(secondCollection)
1940+
.concat(thirdCollection)
1941+
.concat(thirdCollection);
1942+
1943+
expect(firstCollection.count()).to.eql(12);
1944+
expect(firstCollection.all()).to.eql(expected);
1945+
});
1946+
18751947
it('should be iterable', function () {
18761948
let result = '';
18771949

0 commit comments

Comments
 (0)