Skip to content

Commit

Permalink
chore(npm): update dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
jlenon7 committed Feb 10, 2024
1 parent 2300b17 commit 6ef1130
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 82 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@athenna/common",
"version": "4.32.0",
"version": "4.33.0",
"description": "The Athenna common helpers to use in any Node.js ESM project.",
"license": "MIT",
"author": "João Lenon <[email protected]>",
Expand Down
82 changes: 44 additions & 38 deletions src/globals/Array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,48 +13,54 @@ export {}

declare global {
interface Array<T> {
/**
* Call the toJSON method of each item
* inside the array.
*/
toAthennaJSON(): Record<string, any>[]

/**
* Call the toResource method of each item
* inside the array.
*/
toAthennaResource(criterias?: any): T[]

/**
* Transform the array to an Athenna collection.
*/
toAthennaCollection(): Collection<T>
}
}
athenna: {
/**
* Call the toJSON method of each item
* inside the array.
*/
toJSON(criterias?: any): Record<string, any>[]

// eslint-disable-next-line no-extend-native
Array.prototype.toAthennaJSON = function () {
return this.map(model => {
if (model && model.toJSON) {
return model.toJSON()
}
/**
* Call the toResource method of each item
* inside the array.
*/
toResource(criterias?: any): T[]

return null
}).filter(Boolean)
/**
* Transform the array to an Athenna collection.
*/
toCollection(): Collection<T>
}
}
}

// eslint-disable-next-line no-extend-native
Array.prototype.toAthennaResource = function (criterias = {}) {
return this.map(model => {
if (model && model.toResource) {
return model.toResource(criterias)
}
if (!Array.prototype.athenna) {
// eslint-disable-next-line no-extend-native
Object.defineProperty(Array.prototype, 'athenna', {
get: function () {
return {
toJSON: (criterias: any = {}) => {
return this.map(model => {
if (model && model.toJSON) {
return model.toJSON(criterias)
}

return null
}).filter(Boolean)
}
return null
}).filter(Boolean)
},
toResource: (criterias: any = {}) => {
return this.map(model => {
if (model && model.toResource) {
return model.toResource(criterias)
}

// eslint-disable-next-line no-extend-native
Array.prototype.toAthennaCollection = function () {
return new Collection(this)
return null
}).filter(Boolean)
},
toCollection: () => {
return new Collection(this)
}
}
}
})
}
73 changes: 38 additions & 35 deletions src/globals/Error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,46 +26,49 @@ declare global {
}
}

// eslint-disable-next-line no-extend-native
Error.prototype.toAthennaException = function (options: ExceptionJson = {}) {
options.name = options.name || this.name
options.stack = options.stack || this.stack
options.message = options.message || this.message
options.code =
options.code || this.code || changeCase.constantCase(options.name)
options.otherInfos = {
...options.otherInfos,
...Json.omit(this, [
'name',
'stack',
'message',
'code',
'details',
'errors'
])
}
if (!Error.prototype.toAthennaException) {
// eslint-disable-next-line no-extend-native
Error.prototype.toAthennaException = function (options: ExceptionJson = {}) {
options.name = options.name || this.name
options.stack = options.stack || this.stack
options.message = options.message || this.message
options.code =
options.code || this.code || changeCase.constantCase(options.name)
options.otherInfos = {
...options.otherInfos,
...Json.omit(this, [
'name',
'stack',
'message',
'code',
'details',
'errors',
'toAthennaException'
])
}

if (!Is.Undefined(options.details)) {
return new Exception(options)
}
if (!Is.Undefined(options.details)) {
return new Exception(options)
}

options.details = []
options.details = []

if (!Is.Empty(this.details)) {
if (Is.Array(this.details)) {
options.details.push(...this.details)
} else {
options.details.push(this.details)
if (!Is.Empty(this.details)) {
if (Is.Array(this.details)) {
options.details.push(...this.details)
} else {
options.details.push(this.details)
}
}
}

if (!Is.Empty(this.errors)) {
if (Is.Array(this.errors)) {
options.details.push(...this.errors)
} else {
options.details.push(this.errors)
if (!Is.Empty(this.errors)) {
if (Is.Array(this.errors)) {
options.details.push(...this.errors)
} else {
options.details.push(this.errors)
}
}
}

return new Exception(options)
return new Exception(options)
}
}
4 changes: 2 additions & 2 deletions src/helpers/Collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export class Collection<T = any> extends CollectJS<T> {
public toJSON(): Record<string, any>[] {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
return this.all().toAthennaJSON()
return this.all().athenna.toJSON()
}

/**
Expand All @@ -44,7 +44,7 @@ export class Collection<T = any> extends CollectJS<T> {
public toResource(criterias = {}): T[] {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
return this.all().toAthennaResource(criterias)
return this.all().athenna.toResource(criterias)
}
}

Expand Down
8 changes: 4 additions & 4 deletions tests/unit/CollectionTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ export default class CollectionTest {
}
]

assert.deepEqual(models.toAthennaJSON(), [{ id: 1 }])
assert.deepEqual(models.toAthennaCollection().toJSON(), [{ id: 1 }])
assert.deepEqual(models.athenna.toJSON(), [{ id: 1 }])
assert.deepEqual(models.athenna.toCollection().toJSON(), [{ id: 1 }])
assert.deepEqual(new Collection(models).toJSON(), [{ id: 1 }])
}

Expand All @@ -51,8 +51,8 @@ export default class CollectionTest {
{ toResource: criterias => criterias }
]

assert.deepEqual(models.toAthennaResource({ id: 2 }), [{ id: 1 }, { id: 2 }])
assert.deepEqual(models.toAthennaCollection().toResource({ id: 2 }), [{ id: 1 }, { id: 2 }])
assert.deepEqual(models.athenna.toResource({ id: 2 }), [{ id: 1 }, { id: 2 }])
assert.deepEqual(models.athenna.toCollection().toResource({ id: 2 }), [{ id: 1 }, { id: 2 }])
assert.deepEqual(new Collection(models).toResource({ id: 2 }), [{ id: 1 }, { id: 2 }])
}
}

0 comments on commit 6ef1130

Please sign in to comment.