File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 88jobs :
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
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
Original file line number Diff line number Diff line change 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>" ,
Original file line number Diff line number Diff 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
Original file line number Diff line number Diff 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}
You can’t perform that action at this time.
0 commit comments