diff --git a/package-lock.json b/package-lock.json index 641db31..15baeb1 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@athenna/common", - "version": "4.26.0", + "version": "4.27.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@athenna/common", - "version": "4.26.0", + "version": "4.27.0", "license": "MIT", "dependencies": { "@fastify/formbody": "^7.4.0", @@ -16,7 +16,7 @@ "change-case": "^4.1.2", "collect.js": "^4.36.1", "execa": "^8.0.1", - "fastify": "^4.25.0", + "fastify": "^4.25.1", "got": "^12.6.1", "http-status-codes": "^2.2.0", "is-wsl": "^2.2.0", @@ -35,7 +35,7 @@ "youch-terminal": "^2.2.2" }, "devDependencies": { - "@athenna/test": "^4.17.0", + "@athenna/test": "^4.18.0", "@athenna/tsconfig": "^4.12.0", "@types/bytes": "^3.1.1", "@types/callsite": "^1.0.31", @@ -103,9 +103,9 @@ "dev": true }, "node_modules/@athenna/test": { - "version": "4.17.0", - "resolved": "https://registry.npmjs.org/@athenna/test/-/test-4.17.0.tgz", - "integrity": "sha512-NRYXxjDf8+owgUZmrp217L68E4oVI7lnHEAohsAtKuwjPvqXtTxDKLYO8AviB+QOPo0zUFr6X0mMkNVGLH5MCA==", + "version": "4.18.0", + "resolved": "https://registry.npmjs.org/@athenna/test/-/test-4.18.0.tgz", + "integrity": "sha512-HpW6XFZ5GqAd31mj2FB9H7eiBe+Wx/3DAEbiFcaf9JhGoi20GstbS2Bno4YAKv84NxsaLJgJxMZExjonu/uSuw==", "dev": true, "dependencies": { "@japa/assert": "^2.1.0", @@ -130,6 +130,7 @@ "reflect-metadata": "^0.1.13", "rimraf": "^5.0.5", "ts-node": "^10.9.1", + "typescript": "^5.2.2" } }, @@ -4412,9 +4413,9 @@ "integrity": "sha512-cIusKBIt/R/oI6z/1nyfe2FvGKVTohVRfvkOhvx0nCEW+xf5NoCXjAHcWp93uOUBchzYcsvPlrapAdX1uW+YGg==" }, "node_modules/fastify": { - "version": "4.25.0", - "resolved": "https://registry.npmjs.org/fastify/-/fastify-4.25.0.tgz", - "integrity": "sha512-2XANZZDadl/PccnVbmrIC8BmUtb16MO5Hyfnqv1cZIslvf7GYTVwlPuVxLKL23NNxWRc5BikY8HyhWj+NJvokA==", + "version": "4.25.1", + "resolved": "https://registry.npmjs.org/fastify/-/fastify-4.25.1.tgz", + "integrity": "sha512-D8d0rv61TwqoAS7lom2tvIlgVMlx88lLsiwXyWNjA7CU/LC/mx/Gp2WAlC0S/ABq19U+y/aRvYFG5xLUu2aMrg==", "dependencies": { "@fastify/ajv-compiler": "^3.5.0", "@fastify/error": "^3.4.0", diff --git a/package.json b/package.json index 9becbf0..75497c5 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@athenna/common", - "version": "4.26.0", + "version": "4.27.0", "description": "The Athenna common helpers to use in any Node.js ESM project.", "license": "MIT", "author": "João Lenon ", @@ -61,7 +61,7 @@ "change-case": "^4.1.2", "collect.js": "^4.36.1", "execa": "^8.0.1", - "fastify": "^4.25.0", + "fastify": "^4.25.1", "got": "^12.6.1", "http-status-codes": "^2.2.0", "is-wsl": "^2.2.0", @@ -80,7 +80,7 @@ "youch-terminal": "^2.2.2" }, "devDependencies": { - "@athenna/test": "^4.17.0", + "@athenna/test": "^4.18.0", "@athenna/tsconfig": "^4.12.0", "@types/bytes": "^3.1.1", "@types/callsite": "^1.0.31", diff --git a/src/globals/Array.ts b/src/globals/Array.ts index d23df1c..bb9a05d 100644 --- a/src/globals/Array.ts +++ b/src/globals/Array.ts @@ -13,25 +13,48 @@ export {} declare global { interface Array { + /** + * Call the toJSON method of each item + * inside the array. + */ + toAthennaJSON(): Record[] + /** * Call the toResource method of each item * inside the array. */ - toResource(criterias?: any): T[] + toAthennaResource(criterias?: any): T[] /** * Transform the array to an Athenna collection. */ - toCollection(): Collection + toAthennaCollection(): Collection } } // eslint-disable-next-line no-extend-native -Array.prototype.toResource = function (criterias = {}) { - return this.map(model => model.toResource(criterias)) +Array.prototype.toAthennaJSON = function () { + return this.map(model => { + if (model && model.toJSON) { + return model.toJSON() + } + + return null + }).filter(Boolean) +} + +// eslint-disable-next-line no-extend-native +Array.prototype.toAthennaResource = function (criterias = {}) { + return this.map(model => { + if (model && model.toResource) { + return model.toResource(criterias) + } + + return null + }).filter(Boolean) } // eslint-disable-next-line no-extend-native -Array.prototype.toCollection = function () { +Array.prototype.toAthennaCollection = function () { return new Collection(this) } diff --git a/src/helpers/Collection.ts b/src/helpers/Collection.ts index 9ac93c7..6a7ce0d 100644 --- a/src/helpers/Collection.ts +++ b/src/helpers/Collection.ts @@ -29,13 +29,22 @@ export class Collection extends CollectJS { return [...new Set(this.all())] } + /** + * Execute the toJSON method inside objects if exists. + */ + public toJSON(): Record[] { + // eslint-disable-next-line @typescript-eslint/ban-ts-comment + // @ts-ignore + return this.all().toAthennaJSON() + } + /** * Execute the toResource method inside objects if exists. */ public toResource(criterias = {}): T[] { // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore - return this.all().map(item => item.toResource(criterias)) + return this.all().toAthennaResource(criterias) } } diff --git a/tests/unit/CollectionTest.ts b/tests/unit/CollectionTest.ts index 2cc8790..12af9b2 100644 --- a/tests/unit/CollectionTest.ts +++ b/tests/unit/CollectionTest.ts @@ -25,17 +25,34 @@ export default class CollectionTest { assert.deepEqual(new Collection().test(), { hello: 'world' }) } + @Test() + public async shouldBeAbleToExecuteTheToJSONMethodInsideObjectsOfCollections({ assert }: Context) { + const models = [ + undefined, + {}, + { + toJSON: () => ({ id: 1 }) + } + ] + + assert.deepEqual(models.toAthennaJSON(), [{ id: 1 }]) + assert.deepEqual(models.toAthennaCollection().toJSON(), [{ id: 1 }]) + assert.deepEqual(new Collection(models).toJSON(), [{ id: 1 }]) + } + @Test() public async shouldBeAbleToExecuteTheToResourceMethodInsideObjectsOfCollections({ assert }: Context) { const models = [ + undefined, + {}, { toResource: () => ({ id: 1 }) }, { toResource: criterias => criterias } ] - assert.deepEqual(models.toResource({ id: 2 }), [{ id: 1 }, { id: 2 }]) - assert.deepEqual(models.toCollection().toResource({ id: 2 }), [{ id: 1 }, { id: 2 }]) + assert.deepEqual(models.toAthennaResource({ id: 2 }), [{ id: 1 }, { id: 2 }]) + assert.deepEqual(models.toAthennaCollection().toResource({ id: 2 }), [{ id: 1 }, { id: 2 }]) assert.deepEqual(new Collection(models).toResource({ id: 2 }), [{ id: 1 }, { id: 2 }]) } }