Skip to content

Commit c678149

Browse files
committed
chore: minor updates
and tons of tests chore: wip
1 parent 8b897d5 commit c678149

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

63 files changed

+14819
-136
lines changed

src/lib/isEmpty.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@ import assertString from './util/assertString'
1010
export default function isEmpty(str: string, options: IsEmptyOptions = {}): boolean {
1111
assertString(str)
1212

13-
return (options.ignoreWhitespace ? str.trim().length : str.length) === 0
13+
return (options?.ignoreWhitespace ? str.trim().length : str.length) === 0
1414
}

src/lib/isLength.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,12 @@ export default function isLength(str: string, options: IsLengthOptions): boolean
1414
let min
1515
let max
1616

17-
if (typeof (options) === 'object') {
17+
if (typeof (options) === 'object' && options !== null) {
1818
min = options.min || 0
1919
max = options.max
20+
// Handle NaN values
21+
if (Number.isNaN(min))
22+
min = 0
2023
}
2124
else { // backwards compatibility: isLength(str, min [, max])
2225
min = arguments[1] || 0
@@ -26,7 +29,7 @@ export default function isLength(str: string, options: IsLengthOptions): boolean
2629
const presentationSequences = str.match(/(\uFE0F|\uFE0E)/g) || []
2730
const surrogatePairs = str.match(/[\uD800-\uDBFF][\uDC00-\uDFFF]/g) || []
2831
const len = str.length - presentationSequences.length - surrogatePairs.length
29-
const isInsideRange = len >= min && (typeof max === 'undefined' || len <= max)
32+
const isInsideRange = len >= min && (typeof max === 'undefined' || Number.isNaN(max) || len <= max)
3033

3134
if (isInsideRange && Array.isArray(options?.discreteLengths)) {
3235
return options.discreteLengths.includes(len)

src/types/number.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,11 @@ export interface NumberValidatorType extends Validator<number>, LengthValidator<
3939
custom: (fn: (value: number) => boolean, message: string) => NumberValidator
4040
}
4141

42-
export interface BigintValidatorType extends NumberValidatorType {}
42+
export interface BigintValidatorType extends Validator<bigint> {
43+
min: (min: bigint) => BigintValidatorType
44+
max: (max: bigint) => BigintValidatorType
45+
positive: () => BigintValidatorType
46+
negative: () => BigintValidatorType
47+
divisibleBy: (divisor: bigint) => BigintValidatorType
48+
custom: (fn: (value: bigint) => boolean, message: string) => BigintValidatorType
49+
}

src/types/string.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import type { StringValidator } from '../validators/strings'
55
import type { LengthValidator, Validator } from './base'
66

77
export interface ContainsOptions {
8-
ignoreCase: boolean
9-
minOccurrences: number
8+
ignoreCase?: boolean
9+
minOccurrences?: number
1010
}
1111

1212
export interface AlphanumericOptions {

src/validators/bigint.ts

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { BigintValidatorType, ValidationNames } from '../types'
2-
import { NumberValidator } from './numbers'
2+
import { BaseValidator } from './base'
33

4-
export class BigintValidator extends NumberValidator implements BigintValidatorType {
4+
export class BigintValidator extends BaseValidator<bigint> implements BigintValidatorType {
55
public name: ValidationNames = 'bigint'
66

77
constructor() {
@@ -12,6 +12,57 @@ export class BigintValidator extends NumberValidator implements BigintValidatorT
1212
message: 'Must be a bigint',
1313
})
1414
}
15+
16+
min(min: bigint): BigintValidatorType {
17+
return this.addRule({
18+
name: 'min',
19+
test: (value: bigint) => value >= min,
20+
message: 'Must be at least {min}',
21+
params: { min },
22+
})
23+
}
24+
25+
max(max: bigint): BigintValidatorType {
26+
return this.addRule({
27+
name: 'max',
28+
test: (value: bigint) => value <= max,
29+
message: 'Must be at most {max}',
30+
params: { max },
31+
})
32+
}
33+
34+
positive(): BigintValidatorType {
35+
return this.addRule({
36+
name: 'positive',
37+
test: (value: bigint) => value > 0n,
38+
message: 'Must be a positive bigint',
39+
})
40+
}
41+
42+
negative(): BigintValidatorType {
43+
return this.addRule({
44+
name: 'negative',
45+
test: (value: bigint) => value < 0n,
46+
message: 'Must be a negative bigint',
47+
})
48+
}
49+
50+
divisibleBy(divisor: bigint): BigintValidatorType {
51+
return this.addRule({
52+
name: 'divisibleBy',
53+
test: (value: bigint) => value % divisor === 0n,
54+
message: 'Must be divisible by {divisor}',
55+
params: { divisor },
56+
})
57+
}
58+
59+
custom(fn: (value: bigint) => boolean, message: string): BigintValidatorType {
60+
return this.addRule({
61+
name: 'custom',
62+
test: fn,
63+
message,
64+
})
65+
}
1566
}
1667

1768
export function bigint(): BigintValidator {

test/index.test.ts

Lines changed: 0 additions & 128 deletions
This file was deleted.

0 commit comments

Comments
 (0)