Skip to content

Commit

Permalink
update deps (#107)
Browse files Browse the repository at this point in the history
<!-- CLICK "Preview" FOR INSTRUCTIONS IN A MORE READABLE FORMAT -->

## Proposed changes

Describe the big picture of your changes here to communicate to the
maintainers why we should accept this pull request. If it fixes a bug
or resolves a feature request, be sure to link to that issue.

## Types of changes

What types of changes does your code introduce?

_Put an `x` in the boxes that apply_

- [ ] Bugfix (non-breaking change which fixes an issue)
- [ ] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing
functionality to not work as expected)

## Checklist

_Put an `x` in the boxes that apply. You can also fill these out after
creating the PR. If you're unsure about any of them, don't hesitate to
ask.
We're here to help! This is simply a reminder of what we are going to
look
for before merging your code._

- [ ] I have read the
[CONTRIBUTING](https://github.com/AthennaIO/Common/blob/master/CONTRIBUTING.md)
documentation
- [ ] Lint and unit tests pass locally with my changes
- [ ] I have added tests that prove my fix is effective or that my
feature works.
- [ ] I have added necessary documentation (if appropriate)

## Further comments

If this is a relatively large or complex change, kick off the discussion
by
explaining why you chose the solution you did and what alternatives you
considered, etc...

---------

Co-authored-by: snyk-bot <[email protected]>
  • Loading branch information
jlenon7 and snyk-bot authored Jan 9, 2024
1 parent c77c7b7 commit 92b8780
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 21 deletions.
21 changes: 11 additions & 10 deletions package-lock.json

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

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>",
Expand Down Expand Up @@ -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",
Expand All @@ -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",
Expand Down
33 changes: 28 additions & 5 deletions src/globals/Array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,48 @@ 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.
*/
toResource(criterias?: any): T[]
toAthennaResource(criterias?: any): T[]

/**
* Transform the array to an Athenna collection.
*/
toCollection(): Collection<T>
toAthennaCollection(): Collection<T>
}
}

// 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)
}
11 changes: 10 additions & 1 deletion src/helpers/Collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,22 @@ export class Collection<T = any> extends CollectJS<T> {
return [...new Set(this.all())]
}

/**
* Execute the toJSON method inside objects if exists.
*/
public toJSON(): Record<string, any>[] {
// 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)
}
}

Expand Down
21 changes: 19 additions & 2 deletions tests/unit/CollectionTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 }])
}
}

0 comments on commit 92b8780

Please sign in to comment.