Skip to content

Commit 782230e

Browse files
committed
feat: add more methods to collection
1 parent c5a11b9 commit 782230e

File tree

4 files changed

+79
-1
lines changed

4 files changed

+79
-1
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@secjs/utils",
3-
"version": "2.0.0",
3+
"version": "2.0.1",
44
"description": "Utils functions and classes for Node.js",
55
"license": "MIT",
66
"author": "João Lenon <lenon@athenna.io>",

src/Helpers/Collection.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,18 @@
11
import { Collection as CollectJS } from 'collect.js'
22

33
export class Collection extends CollectJS {
4+
/**
5+
* An alias for macro instance method:
6+
*
7+
* @example
8+
* new Collection().macro()
9+
* @param {string} name
10+
* @param {Function} fn
11+
*/
12+
static macro(name, fn) {
13+
return new Collection().macro(name, fn)
14+
}
15+
416
/**
517
* Remove all duplicated values from the array.
618
*
@@ -9,6 +21,26 @@ export class Collection extends CollectJS {
921
removeDuplicated() {
1022
return [...new Set(this.all())]
1123
}
24+
25+
/**
26+
* Execute the toResource method inside objects if exists.
27+
*
28+
* @param {any} [criterias]
29+
* @return {any[]}
30+
*/
31+
toResource(criterias = {}) {
32+
return this.all().map(item => item.toResource(criterias))
33+
}
34+
}
35+
36+
// eslint-disable-next-line no-extend-native
37+
Array.prototype.toResource = function (criterias = {}) {
38+
return this.map(model => model.toResource(criterias))
39+
}
40+
41+
// eslint-disable-next-line no-extend-native
42+
Array.prototype.toCollection = function () {
43+
return new Collection(this)
1244
}
1345

1446
Collection.prototype.order = Collection.prototype.sort

src/index.d.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,13 +124,32 @@ export declare class Clean {
124124
}
125125

126126
export declare class Collection<Item = any> extends CollectJS<Item> {
127+
/**
128+
* An alias for macro instance method:
129+
*
130+
* @example
131+
* new Collection().macro()
132+
*
133+
* @param {string} name
134+
* @param {Function} fn
135+
*/
136+
static macro(name: string, fn: Function): void
137+
127138
/**
128139
* Remove all duplicated values from the array.
129140
*
130141
* @return {any[]}
131142
*/
132143
removeDuplicated(): Item[]
133144

145+
/**
146+
* Execute the toResource method inside objects if exists.
147+
*
148+
* @param {any} [criterias]
149+
* @return {any[]}
150+
*/
151+
toResource(criterias?: any): any[]
152+
134153
/**
135154
* The sortDesc method sort the collection in descending mode.
136155
*/
@@ -1938,3 +1957,11 @@ export declare class Uuid {
19381957
*/
19391958
static changeOrGenerate(prefix: string, token?: string): string
19401959
}
1960+
1961+
declare global {
1962+
interface Array<T> {
1963+
toResource(criterias?: any): T[];
1964+
1965+
toCollection(): Collection<T>;
1966+
}
1967+
}

tests/Unit/CollectionTest.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,23 @@ test.group('CollectionTest', () => {
1616

1717
assert.deepEqual(collection.removeDuplicated(), [1, 2, 3, 4, 5])
1818
})
19+
20+
test('should be able to extend collections by static macro method', async ({ assert }) => {
21+
Collection.macro('test', () => ({ hello: 'world' }))
22+
23+
assert.deepEqual(new Collection().test(), { hello: 'world' })
24+
})
25+
26+
test('should be able to execute the toResource method inside objects of collections', async ({ assert }) => {
27+
const models = [
28+
{
29+
toResource: () => ({ id: 1 }),
30+
},
31+
{ toResource: criterias => criterias },
32+
]
33+
34+
assert.deepEqual(models.toResource({ id: 2 }), [{ id: 1 }, { id: 2 }])
35+
assert.deepEqual(models.toCollection().toResource({ id: 2 }), [{ id: 1 }, { id: 2 }])
36+
assert.deepEqual(new Collection(models).toResource({ id: 2 }), [{ id: 1 }, { id: 2 }])
37+
})
1938
})

0 commit comments

Comments
 (0)