Skip to content

Commit afddf56

Browse files
committed
feat(enum): add new random helpers
1 parent 37ce7fd commit afddf56

5 files changed

Lines changed: 65 additions & 5 deletions

File tree

.github/workflows/cd.yml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ on:
88
jobs:
99
build:
1010
runs-on: ubuntu-latest
11+
permissions:
12+
contents: write
13+
id-token: write
1114
steps:
1215
- name: Checkout
1316
uses: actions/checkout@v2
@@ -32,8 +35,7 @@ jobs:
3235
id: release
3336

3437
- name: Publish to NPM Registry
35-
run: cd build && npm publish --access public
38+
run: cd build && npm publish --provenance --access public
3639
if: steps.release.outputs.released == 'true'
3740
env:
38-
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
3941
name: Deploy

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@athenna/common",
3-
"version": "5.27.0",
3+
"version": "5.28.0",
44
"description": "The Athenna common helpers to use in any Node.js ESM project.",
55
"license": "MIT",
66
"author": "João Lenon <lenon@athenna.io>",

src/helpers/Enum.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,42 @@ export class Enum extends Macroable {
6262
public static entries() {
6363
return this.keys().map(key => [key, this[key]])
6464
}
65+
66+
/**
67+
* Get a random key from the enum.
68+
*
69+
* @example
70+
* ```ts
71+
* export class StatusEnum extends Enum {
72+
* public static PENDING = 'pending' as const
73+
* public static APPROVED = 'approved' as const
74+
* public static BLOCKED = 'blocked' as const
75+
* }
76+
*
77+
* const randomKey = StatusEnum.randomKey() // 'PENDING'
78+
* ```
79+
*/
80+
public static randomKey() {
81+
return this.keys().athenna.random()
82+
}
83+
84+
/**
85+
* Get a random value from the enum.
86+
*
87+
* @example
88+
* ```ts
89+
* export class StatusEnum extends Enum {
90+
* public static PENDING = 'pending' as const
91+
* public static APPROVED = 'approved' as const
92+
* public static BLOCKED = 'blocked' as const
93+
* }
94+
*
95+
* const randomValue = StatusEnum.randomValue() // 'pending'
96+
* ```
97+
*/
98+
public static randomValue() {
99+
return this.values().athenna.random()
100+
}
65101
}
66102

67103
// eslint-disable-next-line @typescript-eslint/no-namespace

tests/unit/helpers/EnumTest.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,4 +125,26 @@ export default class EnumTest {
125125
['DELETED', 2]
126126
])
127127
}
128+
129+
@Test()
130+
public async shouldBeAbleToGetARandomKeyFromEnum({ assert }: Context) {
131+
class Status extends Enum {
132+
static PENDING = 0
133+
static BLOCKED = 1
134+
static DELETED = 2
135+
}
136+
137+
assert.isTrue(Status.keys().includes(Status.randomKey()))
138+
}
139+
140+
@Test()
141+
public async shouldBeAbleToGetARandomValueFromEnum({ assert }: Context) {
142+
class Status extends Enum {
143+
static PENDING = 0
144+
static BLOCKED = 1
145+
static DELETED = 2
146+
}
147+
148+
assert.isTrue(Status.values().includes(Status.randomValue()))
149+
}
128150
}

0 commit comments

Comments
 (0)