Skip to content

Commit c374612

Browse files
feat(guard): add guardDate(), guardFalse(), guardNumberBetween(), guardObjectKeyIn(), guardObjectSomeKeys(), guardRegExp(), guardStringLength(), guardTrue()`.
1 parent 00e8841 commit c374612

36 files changed

+1641
-499
lines changed

src/guard/index.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,25 @@ export { guardArray } from './lib/guard-array.func';
55
export { guardBigInt } from './lib/guard-big-int.func';
66
export { guardBoolean } from './lib/guard-boolean.func';
77
export { guardClass } from './lib/guard-class.func';
8+
export { guardDate } from './lib/guard-date.func';
89
export { guardDefined } from './lib/guard-defined.func';
10+
export { guardFalse } from './lib/guard-false.func';
911
export { guardFunction } from './lib/guard-function.func';
1012
export { guardInstance } from './lib/guard-instance.func';
1113
export { guardKey } from './lib/guard-key.func';
12-
export { guardNumber } from './lib/guard-number.func';
1314
export { guardNull } from './lib/guard-null.func';
15+
export { guardNumber } from './lib/guard-number.func';
16+
export { guardNumberBetween } from './lib/guard-number-between.func';
1417
export { guardObject } from './lib/guard-object.func';
1518
export { guardObjectKey } from './lib/guard-object-key.func';
19+
export { guardObjectKeyIn } from './lib/guard-object-key-in.func';
1620
export { guardObjectKeys } from './lib/guard-object-keys.func';
21+
export { guardObjectSomeKeys } from './lib/guard-object-some-keys.func';
1722
export { guardPrimitive } from './lib/guard-primitive.func';
23+
export { guardRegExp } from './lib/guard-regexp.func';
1824
export { guardString } from './lib/guard-string.func';
25+
export { guardStringLength } from './lib/guard-string-length.func';
1926
export { guardSymbol } from './lib/guard-symbol.func';
27+
export { guardTrue } from './lib/guard-true.func';
2028
export { guardType } from './lib/guard-type.func';
2129
export { guardUndefined } from './lib/guard-undefined.func';

src/guard/lib/guard-date.func.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Function.
2+
import { isDate } from '../../is/lib/is-date.func';
3+
// Type.
4+
import { CallbackPayload } from '../../type/callback-payload.type';
5+
import { ResultCallback } from '../../type/result-callback.type';
6+
/**
7+
* Guards the value to be a date.
8+
* @param value The value of `Date` type to guard.
9+
* @param callback An optional `ResultCallback` function to handle the result before returns.
10+
* @param payload An optional `object` of `CallbackPayload<Payload>` that is assigned to the `payload` of the provided `callback` function.
11+
* @returns The return value is a `boolean` indicating whether the value is a date.
12+
*/
13+
export const guardDate = <Payload extends object>(
14+
value: Date,
15+
callback?: ResultCallback<Date, CallbackPayload<Payload>>,
16+
payload?: CallbackPayload<Payload>
17+
): value is Date =>
18+
isDate(value, callback, payload);

src/guard/lib/guard-false.func.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Function.
2+
import { isFalse } from '../../is/lib/is-false.func';
3+
// Type.
4+
import { CallbackPayload } from '../../type/callback-payload.type';
5+
import { ResultCallback } from '../../type/result-callback.type';
6+
/**
7+
* Guards the provided value to be `false`.
8+
* @param value The value of `false` type to guard.
9+
* @param callback An optional `ResultCallback` function to handle the result before returns.
10+
* @param payload An optional `object` of `CallbackPayload<Payload>` that is assigned to the `payload` of the provided `callback` function.
11+
* @returns The return value is a `boolean` indicating whether the `value` is a `boolean` type or an instance of `Boolean` equal to `false`.
12+
*/
13+
export const guardFalse = <Payload extends object>(
14+
value: false,
15+
callback?: ResultCallback<false, CallbackPayload<Payload>>,
16+
payload?: CallbackPayload<Payload>
17+
): value is false =>
18+
isFalse(value, callback, payload);
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Function.
2+
import { isNumberBetween } from '../../is/lib/is-number-between.func';
3+
// Interface.
4+
import { MinMax } from '../../interface/min-max.interface';
5+
// Type.
6+
import { AnyNumber } from '../../type/any-number.type';
7+
import { CallbackPayload } from '../../type/callback-payload.type';
8+
import { NumberBetween } from '../../type/number-between.type';
9+
import { ResultCallback } from '../../type/result-callback.type';
10+
/**
11+
* Guards the value to be `number` between the specified range.
12+
* @param value The value of a generic type variable `Type` constrained by `AnyNumber`, by default of the type captured from the
13+
* provided `value`, to guard.
14+
* @param range An `object` of optional minimum and maximum `range` of the provided `value`.
15+
* @param callback An optional `ResultCallback` function to handle the result before returns.
16+
* @param payload An optional `object` of `CallbackPayload<Payload>` that is assigned to the `payload` of the provided `callback` function.
17+
* @returns The return value is a `boolean` indicating whether the `value` is a `number` type or an instance of `Number` between the
18+
* specified range.
19+
*/
20+
export const guardNumberBetween = <
21+
Type extends AnyNumber,
22+
Min extends number,
23+
Max extends number,
24+
Payload extends object = object
25+
>(
26+
value: Type,
27+
range: MinMax<Min, Max>,
28+
callback?: ResultCallback<Type, CallbackPayload<MinMax<Min, Max> & Payload>>,
29+
payload?: CallbackPayload<Payload>
30+
): value is NumberBetween<Min, Max, Type> =>
31+
isNumberBetween(value, range, callback, payload);
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Function.
2+
import { isObjectKeyIn } from '../../is/lib/is-object-key-in.func';
3+
// Type.
4+
import { CallbackPayload } from '../../type/callback-payload.type';
5+
import { ResultCallback } from '../../type/result-callback.type';
6+
/**
7+
* Guards the `value` to be an `object` of a generic type variable `Obj` that contains(or its prototype chain) the given `key`.
8+
* @param value An `object` of a generic type variable `Obj`, by default of the type captured from the provided `value` that contains
9+
* (or its prototype chain) the given `key`.
10+
* @param key A key of `Obj` type as the name of the property that the given `value` contains(or its prototype chain).
11+
* @param callback An optional `ResultCallback` function to handle the result before returns.
12+
* @param payload An optional `object` of `CallbackPayload<Payload>` that is assigned to the `payload` of the provided `callback` function.
13+
* @returns The return value is a `boolean` indicating whether the `value` is an `object` that contains(or its prototype chain) the given
14+
* `key`.
15+
*/
16+
export const guardObjectKeyIn = <
17+
Obj extends object,
18+
Key extends keyof Obj,
19+
Payload extends object = object
20+
>(
21+
value: Obj,
22+
key: Key,
23+
callback?: ResultCallback<
24+
Obj,
25+
CallbackPayload<{ key: typeof key } & Payload>
26+
>,
27+
payload?: CallbackPayload<Payload>
28+
): value is Obj =>
29+
isObjectKeyIn(value, key, callback, payload as any);
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Function.
2+
import { isObjectSomeKeys } from '../../is/lib/is-object-some-keys.func';
3+
// Type.
4+
import { CallbackPayload } from '../../type/callback-payload.type';
5+
import { ResultCallback } from '../../type/result-callback.type';
6+
/**
7+
* Guards the value to be an `object` of a generic type variable `Obj` with its specified `keys`.
8+
* @param value An object of a generic type variable `Obj`, by default of the type captured from the `value` that contains some or some of
9+
* the groups of the given `keys`, to guard.
10+
* @param keys An `Array` of property names or a two-dimensional array of property names to check if the given `value` contains some of them
11+
* or some groups of them.
12+
* @param callback An optional `ResultCallback` function to handle the result before returns.
13+
* @param payload An optional `object` of `CallbackPayload<Payload>` that is assigned to the `payload` of the provided `callback` function.
14+
* @returns The return value is a `boolean` indicating whether the provided `value` is an `object` with some or some groups of its keys from
15+
* a given `keys`.
16+
* @angularpackage
17+
*/
18+
export const guardObjectSomeKeys = <
19+
Obj extends object,
20+
Payload extends object = object
21+
>(
22+
value: Obj,
23+
keys: (keyof Obj | Array<keyof Obj>)[],
24+
callback?: ResultCallback<
25+
Obj,
26+
CallbackPayload<{ keys: typeof keys } & Payload>
27+
>,
28+
payload?: CallbackPayload<Payload>
29+
): value is Obj => isObjectSomeKeys(value, keys, callback, payload as any);

src/guard/lib/guard-regexp.func.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Function.
2+
import { isRegExp } from '../../is/lib/is-regexp.func';
3+
// Type.
4+
import { CallbackPayload } from '../../type/callback-payload.type';
5+
import { ResultCallback } from '../../type/result-callback.type';
6+
/**
7+
* Guards the value to be a `RegExp`.
8+
* @param value A `RegExp` type value to guard.
9+
* @param callback An optional `ResultCallback` function to handle the result before returns.
10+
* @param payload An optional `object` of `CallbackPayload<Payload>` that is assigned to the `payload` of the provided `callback` function.
11+
* @returns The return `value` is a `boolean` indicating whether the `value` is a `RegExp`.
12+
*/
13+
export const guardRegExp = <Payload extends object>(
14+
value: RegExp,
15+
callback?: ResultCallback<RegExp, CallbackPayload<Payload>>,
16+
payload?: CallbackPayload<Payload>
17+
): value is RegExp =>
18+
isRegExp(value, callback, payload);
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Function.
2+
import { isStringLength } from '../../is';
3+
// Interface.
4+
import { CallbackPayload } from '../../type/callback-payload.type';
5+
import { MinMax } from '../../interface/min-max.interface';
6+
// Type.
7+
import { AnyString } from '../../type/any-string.type';
8+
import { ResultCallback } from '../../type/result-callback.type';
9+
import { StringOfLength } from '../../type/string-of-length.type';
10+
/**
11+
* Guards the value to be a `string` of a length between the specified range.
12+
* @param value The value of a generic type variable `Type` constrained by `AnyString`, by default of the type captured from the
13+
* provided `value` to guard.
14+
* @param length An `object` of optional minimum and a maximum `length` of the given `value`.
15+
* @param callback An optional `ResultCallback` function to handle the result before returns.
16+
* @param payload An optional `object` of `CallbackPayload<Payload>` that is assigned to the `payload` of the provided `callback` function.
17+
* @returns The return value is a `boolean` indicating whether the `value` is a `string` type or an instance of `String` of a length between
18+
* the specified range.
19+
*/
20+
export const guardStringLength = <
21+
Type extends AnyString,
22+
Min extends number,
23+
Max extends number,
24+
Payload extends object = object
25+
>(
26+
value: Type,
27+
length: MinMax<Min, Max>,
28+
callback?: ResultCallback<Type, CallbackPayload<MinMax<Min, Max> & Payload>>,
29+
payload?: CallbackPayload<Payload>
30+
): value is StringOfLength<Min, Max, Type> =>
31+
isStringLength(value, length, callback, payload);

src/guard/lib/guard-true.func.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Function.
2+
import { isTrue } from '../../is/lib/is-true.func';
3+
// Type.
4+
import { CallbackPayload } from '../../type/callback-payload.type';
5+
import { ResultCallback } from '../../type/result-callback.type';
6+
/**
7+
* Guards the value to be `true`.
8+
* @param value The value of `true` type to guard.
9+
* @param callback An optional `ResultCallback` function to handle the result before returns.
10+
* @param payload An optional `object` of `CallbackPayload<Payload>` that is assigned to the `payload` of the provided `callback` function.
11+
* @returns The return value is a `boolean` indicating whether the `value` is a `boolean` type or an instance of `Boolean` equal to `true`.
12+
*/
13+
export const guardTrue = <Payload extends object>(
14+
value: true,
15+
callback?: ResultCallback<true, CallbackPayload<Payload>>,
16+
payload?: CallbackPayload<Payload>
17+
): value is true =>
18+
isTrue(value, callback, payload);

src/guard/test/guard-array.spec.ts

Lines changed: 47 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,53 @@
11
// Function.
22
import { guardArray } from '../lib/guard-array.func';
3-
// Constant.
3+
// Testing.
44
import {
5-
ARRAY_BIGINT,
6-
ARRAY_BOOLEAN,
7-
ARRAY_CLASS,
8-
ARRAY_FUNCTION,
9-
ARRAY_NULL,
10-
ARRAY_NUMBER,
11-
ARRAY_OBJECT_ONE,
12-
ARRAY_OBJECT_TWO,
13-
ARRAY_STRING,
14-
ARRAY_SYMBOL_NUMBER,
15-
ARRAY_SYMBOL_STRING,
16-
ARRAY_UNDEFINED
17-
} from '../../testing/src/strict/array.const';
18-
import { Class } from '../../testing/src/strict/class.const';
19-
// Type.
20-
import { Func } from '../../type/func.type';
21-
// Interface.
22-
import { ObjectOne, ObjectTwo } from '../../testing/interface';
5+
// Main.
6+
Testing,
237

24-
describe(guardArray.name, () => {
25-
// TRUE
26-
it('is DEFINED', () => {
27-
expect(guardArray).toBeDefined();
28-
});
8+
// Constants.
9+
TESTING_ARRAY_BIGINT,
10+
TESTING_ARRAY_BOOLEAN,
11+
TESTING_ARRAY_CLASS,
12+
TESTING_ARRAY_FUNCTION,
13+
TESTING_ARRAY_NULL,
14+
TESTING_ARRAY_NUMBER,
15+
TESTING_ARRAY_OBJECT_ONE,
16+
TESTING_ARRAY_STRING,
17+
TESTING_ARRAY_SYMBOL_NUMBER,
18+
TESTING_ARRAY_SYMBOL_STRING,
19+
TESTING_ARRAY_UNDEFINED,
2920

30-
it('Array<bigint>', () => expect(guardArray<bigint>(ARRAY_BIGINT)).toBeTruthy());
31-
it('Array<boolean>', () => expect(guardArray<boolean | Boolean>(ARRAY_BOOLEAN)).toBeTruthy());
32-
it('Array<Class>', () => expect(guardArray<Class>(ARRAY_CLASS)).toBeTruthy());
33-
it('Array<Func>', () => expect(guardArray<Func>(ARRAY_FUNCTION)).toBeTruthy());
34-
it('Array<null>', () => expect(guardArray<null>(ARRAY_NULL)).toBeTruthy());
35-
it('Array<number>', () => expect(guardArray<number | Number>(ARRAY_NUMBER)).toBeTruthy());
36-
it('Array<ObjectOne> Array<ObjectTwo>', () => {
37-
expect(guardArray<ObjectOne>(ARRAY_OBJECT_ONE)).toBeTruthy();
38-
expect(guardArray<ObjectTwo>(ARRAY_OBJECT_TWO)).toBeTruthy();
39-
});
40-
it('Array<string>', () => expect(guardArray<string | String>(ARRAY_STRING)).toBeTruthy());
41-
it('Array<symbol>', () => {
42-
expect(guardArray<symbol>(ARRAY_SYMBOL_STRING)).toBeTruthy();
43-
expect(guardArray<symbol>(ARRAY_SYMBOL_NUMBER)).toBeTruthy();
44-
});
45-
it('Array<undefined>', () => expect(guardArray<undefined | unknown>(ARRAY_UNDEFINED)).toBeTruthy());
21+
// Class.
22+
TestingClass,
23+
} from '@angular-package/testing';
24+
// Execute tests.
25+
import { tests } from '../../execute-tests';
26+
/**
27+
* Initialize testing.
28+
*/
29+
const testing = new Testing(
30+
tests.is.array.describe,
31+
tests.is.array.it
32+
);
33+
/**
34+
* Tests.
35+
*/
36+
testing.describe(guardArray.name, () => {
37+
testing
38+
// TRUE
39+
.it('is DEFINED', () => expect(guardArray).toBeDefined())
40+
.it('Array<bigint>', () => expect(guardArray<bigint>(TESTING_ARRAY_BIGINT)).toBeTruthy())
41+
.it('Array<boolean>', () => expect(guardArray<boolean | Boolean>(TESTING_ARRAY_BOOLEAN)).toBeTruthy())
42+
.it('Array<TestingClass>', () => expect(guardArray<TestingClass>(TESTING_ARRAY_CLASS)).toBeTruthy())
43+
.it('Array<Func>', () => expect(guardArray<Function>(TESTING_ARRAY_FUNCTION)).toBeTruthy())
44+
.it('Array<null>', () => expect(guardArray<null>(TESTING_ARRAY_NULL)).toBeTruthy())
45+
.it('Array<number>', () => expect(guardArray<number | Number>(TESTING_ARRAY_NUMBER)).toBeTruthy())
46+
.it('Array<ObjectOne> Array<ObjectTwo>', () => expect(guardArray(TESTING_ARRAY_OBJECT_ONE)).toBeTruthy())
47+
.it('Array<string>', () => expect(guardArray<string | String>(TESTING_ARRAY_STRING)).toBeTruthy())
48+
.it('Array<symbol>', () => {
49+
expect(guardArray(TESTING_ARRAY_SYMBOL_STRING)).toBeTruthy();
50+
expect(guardArray(TESTING_ARRAY_SYMBOL_NUMBER)).toBeTruthy();
51+
})
52+
.it('Array<undefined>', () => expect(guardArray<undefined | unknown>(TESTING_ARRAY_UNDEFINED)).toBeTruthy());
4653
});

src/guard/test/guard-bigint.spec.ts

Lines changed: 45 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,50 @@
1+
// Testing.
2+
import {
3+
// Main.
4+
Testing,
5+
6+
// Constant.
7+
TESTING_TRUE,
8+
TESTING_BIGINT
9+
} from '@angular-package/testing';
10+
// Execute tests.
11+
import { tests } from '../../execute-tests';
112
// Function.
213
import { guardBigInt } from '../lib/guard-big-int.func';
3-
// Constant.
4-
import { BIGINT, BIGINT_EXPECTATION, BIGINT_INSTANCE } from '../../testing/src/strict/big-int.const';
5-
import { TRUE } from '../../testing/src/strict/boolean.const';
6-
7-
describe(guardBigInt.name, () => {
8-
// Defined.
9-
it('is DEFINED', () => expect(guardBigInt).toBeDefined());
10-
11-
// Checks ...
12-
describe(`checks`, () => {
13-
it('callback', () => {
14-
guardBigInt(BIGINT, (result: boolean, value: bigint) => {
15-
expect(result).toBe(TRUE);
16-
expect(value).toEqual(BIGINT);
17-
return result;
18-
});
19-
});
20-
21-
// ... primitives.
22-
describe(`primitive`, () => {
23-
// bigint
24-
describe(`bigint`, () => {
25-
it(`${BIGINT}`, () => expect(guardBigInt(BIGINT)).toBe(TRUE));
26-
it(`${BIGINT_EXPECTATION}`, () => expect(guardBigInt(BIGINT_INSTANCE)).toBe(TRUE));
14+
/**
15+
* Initialize testing.
16+
*/
17+
const testing = new Testing(
18+
tests.guard.bigint.describe,
19+
tests.guard.bigint.it
20+
);
21+
/**
22+
* Tests.
23+
*/
24+
testing.describe(guardBigInt.name, () => {
25+
testing
26+
// Defined.
27+
.it('is DEFINED', () => expect(guardBigInt).toBeDefined())
28+
// Checks ...
29+
.describe(`guards`, () => {
30+
testing
31+
.it('with callback and payload', () => {
32+
guardBigInt(TESTING_BIGINT, (result, value, payload) => {
33+
expect(result).toBe(TESTING_TRUE);
34+
expect(value).toEqual(TESTING_BIGINT);
35+
if (payload) {
36+
expect(payload.action).toEqual('action');
37+
expect(payload.name).toEqual('name');
38+
expect(payload.param).toEqual('param');
39+
}
40+
return result;
41+
}, { action: 'action', name: 'name', param: 'param' });
42+
})
43+
// ... primitives.
44+
.describe(`primitive`, () => {
45+
testing
46+
// bigint
47+
.describe(`bigint`, () => it(`${TESTING_BIGINT}`, () => expect(guardBigInt(TESTING_BIGINT)).toBe(TESTING_TRUE)));
2748
});
2849
});
29-
});
3050
});

0 commit comments

Comments
 (0)