Skip to content

Commit adbc749

Browse files
committed
fix: add operator $size
1 parent 7a7d872 commit adbc749

File tree

3 files changed

+18
-0
lines changed

3 files changed

+18
-0
lines changed

packages/flowerbase/src/utils/__tests__/operators.test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,12 @@ describe('operators', () => {
8686
expect(operators.$all({ name: 'ciao' }, [{ name: 'ciao' }, 4, 5])).toBe(false)
8787
expect(operators.$all([{ name: 'ciao' }, 4, 5], [{ name: 'ciao' }, 4, 5])).toBe(false)
8888
})
89+
it('should check array size', () => {
90+
expect(operators.$size([1, 2, 3], 3)).toBe(true)
91+
expect(operators.$size([1, 2, 3], 2)).toBe(false)
92+
expect(operators.$size([], 0)).toBe(true)
93+
expect(operators.$size('not-array', 0)).toBe(false)
94+
})
8995
it('should check regex', () => {
9096
const emailRegex = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/
9197
expect(operators.$regex('test@gmail.com', emailRegex)).toBe(true)

packages/flowerbase/src/utils/rules-matcher/interface.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,15 @@ export type Operators = {
311311
* @returns
312312
*/
313313
$all: OperatorsFunction
314+
/**
315+
* @param a
316+
* @param b
317+
*
318+
* Checks whether the length of the user input array is equal to the rule value.
319+
*
320+
* @returns
321+
*/
322+
$size: OperatorsFunction
314323
/**
315324
* @param a
316325
* @param b
@@ -339,6 +348,7 @@ export enum RulesOperators {
339348
$in = '$in',
340349
$nin = '$nin',
341350
$all = '$all',
351+
$size = '$size',
342352
$regex = '$regex'
343353
}
344354

packages/flowerbase/src/utils/rules-matcher/utils.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,8 @@ export const operators: Operators = {
277277
.length
278278
),
279279

280+
$size: (a, b) => Array.isArray(a) && a.length === parseFloat(b),
281+
280282
$regex: (a, b, opt) =>
281283
rulesMatcherUtils
282284
.forceArray(b)

0 commit comments

Comments
 (0)