Skip to content

Commit 21f3f48

Browse files
feat: add functions with the types and tests
+ `isDate()` + `isFalse()` + `isNumberBetween()` + `isRegExp()` + `isStringLength()` + `isTrue()` + constants for date + constant for `RegExp` + update api
1 parent f7aec7b commit 21f3f48

27 files changed

+928
-5
lines changed

src/is/index.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,29 @@ export { isBoolean } from './lib/is-boolean.func';
55
export { isBooleanObject } from './lib/is-boolean-object.func';
66
export { isBooleanType } from './lib/is-boolean-type.func';
77
export { isClass } from './lib/is-class.func';
8+
export { isDate } from './lib/is-date.func'; // From 4.2.0
89
export { isDefined } from './lib/is-defined.func';
10+
export { isFalse } from './lib/is-false.func'; // From 4.2.0
911
export { isFunction } from './lib/is-function.func';
1012
export { isInstance } from './lib/is-instance.func';
1113
export { isKey } from './lib/is-key.func';
1214
export { isNull } from './lib/is-null.func';
1315
export { isNumber } from './lib/is-number.func';
16+
export { isNumberBetween } from './lib/is-number-between.func'; // From 4.2.0
1417
export { isNumberObject } from './lib/is-number-object.func';
1518
export { isNumberType } from './lib/is-number-type.func';
1619
export { isObject } from './lib/is-object.func';
1720
export { isObjectKey } from './lib/is-object-key.func';
1821
export { isObjectKeyIn } from './lib/is-object-key-in.func';
1922
export { isObjectKeys } from './lib/is-object-keys.func';
2023
export { isPrimitive } from './lib/is-primitive.func';
24+
export { isRegExp } from './lib/is-regexp.func'; // From 4.2.0
2125
export { isString } from './lib/is-string.func';
26+
export { isStringLength } from './lib/is-string-length.func'; // From 4.2.0
2227
export { isStringObject } from './lib/is-string-object.func';
2328
export { isStringType } from './lib/is-string-type.func';
2429
export { isSymbol } from './lib/is-symbol.func';
30+
export { isTrue } from './lib/is-true.func'; // From 4.2.0
2531
export { isType } from './lib/is-type.func';
2632
export { isUndefined } from './lib/is-undefined.func';
2733
// `is` object.

src/is/interface/is.interface.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,38 @@
1+
// Import: Type.
12
import { IsArray } from '../type/is-array.type';
23
import { IsBigInt } from '../type/is-big-int.type';
34
import { IsBoolean } from '../type/is-boolean.type';
45
import { IsBooleanObject } from '../type/is-boolean-object.type';
56
import { IsBooleanType } from '../type/is-boolean-type.type';
67
import { IsClass } from '../type/is-class.type';
8+
import { IsDate } from '../type/is-date.type';
79
import { IsDefined } from '../type/is-defined.type';
10+
import { IsFalse } from '../type/is-false.type';
811
import { IsFunction } from '../type/is-function.type';
912
import { IsInstance } from '../type/is-instance.type';
1013
import { IsKey } from '../type/is-key.type';
1114
import { IsNot } from '../not/interface/is-not.interface';
1215
import { IsNull } from '../type/is-null.type';
1316
import { IsNumber } from '../type/is-number.type';
17+
import { IsNumberBetween } from '../type/is-number-between.type';
1418
import { IsNumberObject } from '../type/is-number-object.type';
1519
import { IsNumberType } from '../type/is-number-type.type';
1620
import { IsObject } from '../type/is-object.type';
1721
import { IsObjectKey } from '../type/is-object-key.type';
1822
import { IsObjectKeyIn } from '../type/is-object-key-in.type';
1923
import { IsObjectKeys } from '../type/is-object-keys.type';
2024
import { IsPrimitive } from '../type/is-primitive.type';
25+
import { IsRegExp } from '../type/is-regexp.type';
2126
import { IsString } from '../type/is-string.type';
27+
import { IsStringLength } from '../type/is-string-length.type';
2228
import { IsStringObject } from '../type/is-string-object.type';
2329
import { IsStringType } from '../type/is-string-type.type';
2430
import { IsSymbol } from '../type/is-symbol.type';
31+
import { IsTrue } from '../type/is-true.type';
2532
import { IsType } from '../type/is-type.type';
2633
import { IsUndefined } from '../type/is-undefined.type';
2734
/**
35+
* Export: Interface.
2836
* Object with prefixed `is` functions.
2937
*/
3038
export interface Is {
@@ -35,24 +43,30 @@ export interface Is {
3543
booleanObject: IsBooleanObject;
3644
booleanType: IsBooleanType;
3745
class: IsClass;
46+
date: IsDate; // From 4.2.0
3847
defined: IsDefined;
48+
false: IsFalse; // From `4.2.0`
3949
function: IsFunction;
4050
instance: IsInstance;
4151
key: IsKey;
4252
not: IsNot;
4353
null: IsNull;
4454
number: IsNumber;
55+
numberBetween: IsNumberBetween; // From `4.2.0`
4556
numberObject: IsNumberObject;
4657
numberType: IsNumberType;
4758
object: IsObject;
4859
objectKey: IsObjectKey;
4960
objectKeyIn: IsObjectKeyIn;
5061
objectKeys: IsObjectKeys;
5162
primitive: IsPrimitive;
63+
regexp: IsRegExp; // From 4.2.0
5264
string: IsString;
65+
stringLength: IsStringLength;
5366
stringObject: IsStringObject;
5467
stringType: IsStringType;
5568
symbol: IsSymbol;
69+
true: IsTrue; // From 4.2.0
5670
type: IsType;
5771
undefined: IsUndefined;
5872
}

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Function.
2+
import { typeOf } from '../../lib/type-of.func';
3+
import { resultCallback } from '../../lib/result-callback.func';
4+
// Type.
5+
import { IsDate } from '../type/is-date.type';
6+
import { ResultCallback } from '../../type/result-callback.type';
7+
/**
8+
* Checks if any `value` is an `object` type instance of `Date` and `Object`.
9+
* @param value Any `value` to check.
10+
* @param callback An optional `ResultCallback` function to handle the result before returns.
11+
* @returns A `boolean` indicating whether or not the value is a date.
12+
*/
13+
export const isDate: IsDate = (
14+
value: any,
15+
callback: ResultCallback = resultCallback
16+
): value is Date =>
17+
callback(
18+
typeOf(value) === 'date' &&
19+
typeof value === 'object' &&
20+
value instanceof Date === true &&
21+
value instanceof Object === true,
22+
value
23+
);

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Function.
2+
import { isBooleanObject } from './is-boolean-object.func';
3+
import { isBooleanType } from './is-boolean-type.func';
4+
import { resultCallback } from '../../lib/result-callback.func';
5+
// Type.
6+
import { IsFalse } from '../type/is-false.type';
7+
import { ResultCallback } from '../../type/result-callback.type';
8+
/**
9+
* Checks if any `value` is a `boolean` type not an instance of `Boolean` and `Object`, or
10+
* is an `object` type and instance of `Boolean` and `Object` and equal to `false`.
11+
* @param value Any `value` to check.
12+
* @param callback A `ResultCallback` function to handle the result before returns.
13+
* @returns A `boolean` indicating whether or not the `value` is a `boolean` equal to `false`.
14+
*/
15+
export const isFalse: IsFalse = (
16+
value: any,
17+
callback: ResultCallback = resultCallback
18+
): value is false =>
19+
callback(
20+
(isBooleanType(value) && value === false)
21+
||
22+
(isBooleanObject(value) && value.valueOf() === false),
23+
value
24+
);
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Function.
2+
import { isNumberObject } from './is-number-object.func';
3+
import { isNumberType } from './is-number-type.func';
4+
import { resultCallback } from '../../lib/result-callback.func';
5+
import { typeOf } from '../../lib/type-of.func';
6+
// Type.
7+
import { NumberBetween } from '../../type/number-between.type';
8+
import { ResultCallback } from '../../type/result-callback.type';
9+
/**
10+
* Checks if any `value` is a `number` type, not instance of `Object` and `Number` or `object` type and instance of `Number` and `Object`,
11+
* in the specified range.
12+
* @param value Any `value` to check.
13+
* @param min A `number` of the minimum range of the provided `value`.
14+
* @param max A `number` of the maximum range of the provided `value`.
15+
* @param callback A `ResultCallback` function to handle the result before returns.
16+
* @returns A `boolean` indicating whether or not the `value` is a `number` type or `Number` instance in the specified range.
17+
*/
18+
export const isNumberBetween = <Min extends number, Max extends number>(
19+
value: any,
20+
min: Min,
21+
max: Max,
22+
callback: ResultCallback = resultCallback
23+
): value is NumberBetween<Min, Max> => {
24+
return callback(
25+
typeOf(value) === 'number' &&
26+
((isNumberType(value) ? value >= min && value <= max : false) ||
27+
(isNumberObject(value)
28+
? value.valueOf() >= min && value.valueOf() <= max
29+
: false)),
30+
value
31+
);
32+
};
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Function.
2+
import { isArray } from './is-array.func';
3+
import { isKey } from './is-key.func';
4+
// Type.
5+
import { IsObjectKeys } from '../type/is-object-keys.type';
6+
import { isObject } from './is-object.func';
7+
import { Types } from '../../type/types.type';
8+
import { Func } from '../../type/func.type';
9+
import { isType } from './is-type.func';
10+
import { Primitives } from '../../type/primitives.type';
11+
import { isPrimitive } from './is-primitive.func';
12+
import { guardPrimitive } from '../../guard/lib/guard-primitive.func';
13+
14+
type IsObjectKeysType =
15+
<Type extends object>(...keys: (keyof Type)[]
16+
) => (...type: Primitives[]) => (value: Type) => value is Type;
17+
18+
/**
19+
* Checks if any `value` is an `object` of a generic `Type` with some of its own specified `keys`.
20+
* @param value Any `value` to check if it contains some of the specified `keys`.
21+
* @param keys A rest parameter key of `Type` or an array of keys of `Type` to check the `value`.
22+
* @returns A `boolean` indicating whether or not the `value` is an `object` with some of its own specified keys.
23+
*/
24+
export const isObjectKeysType: IsObjectKeysType =
25+
<Type extends object>(...keys: (keyof Type | Array<keyof Type>)[]) =>
26+
(...type: Primitives[]) =>
27+
(value: any): value is Type =>
28+
isObject<Type>(value)
29+
? keys.some((key, keysIndex: number) =>
30+
isArray(key)
31+
? key.every((k, arrIndex: number) =>
32+
isKey(k) ? {}.hasOwnProperty.call(value, k) === true ? isPrimitive(value[k], type[arrIndex]) : false : false
33+
)
34+
: isKey(key)
35+
? {}.hasOwnProperty.call(value, key) === true ? isPrimitive(value[key], type[keysIndex]) : false
36+
: false
37+
)
38+
: false;
39+
40+
class Person {
41+
firstName = 'bla';
42+
surname = 'bla bla bla';
43+
}
44+
45+
const person: Person = new Person();
46+
47+
isObjectKeysType<Person>('firstName')('string')(person);

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Import: Function.
2+
import { resultCallback } from '../../lib/result-callback.func';
3+
import { typeOf } from '../../lib/type-of.func';
4+
// Import: Type.
5+
import { IsRegExp } from '../type/is-regexp.type';
6+
import { ResultCallback } from '../../type/result-callback.type';
7+
/**
8+
* Checks if any `value` is a `regexp` type, an instance of `Object` and `RegExp`.
9+
* @param value Any `value` to check.
10+
* @param callback An optional `ResultCallback` function to handle the result before returns.
11+
* @returns A `boolean` indicating whether or not the `value` is a regular expression.
12+
*/
13+
export const isRegExp: IsRegExp = (
14+
value: any,
15+
callback: ResultCallback = resultCallback
16+
): value is RegExp =>
17+
callback(
18+
typeOf(value) === 'regexp' &&
19+
typeof value === 'object' &&
20+
value instanceof Object &&
21+
value instanceof RegExp
22+
);
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Function.
2+
import { isStringObject } from './is-string-object.func';
3+
import { isStringType } from './is-string-type.func';
4+
import { resultCallback } from '../../lib/result-callback.func';
5+
import { typeOf } from '../../lib/type-of.func';
6+
// Type.
7+
import { IsStringLength } from '../type/is-string-length.type';
8+
import { ResultCallback } from '../../type/result-callback.type';
9+
import { StringOfLength } from '../../type/string-of-length.type';
10+
/**
11+
* Checks if any `value` is a `string` type, not instance of `Object` and `String` or `object` type and instance of `String` and `Object`,
12+
* of a length in the specified range.
13+
* @param value Any `value` to check.
14+
* @param min A `number` of the minimum length of the provided `value`.
15+
* @param max A `number` of the maximum length of the provided `value`.
16+
* @param callback A `ResultCallback` function to handle the result before returns.
17+
* @returns A `boolean` indicating whether or not the `value` is a `string` type or `String` instance of length in the specified range.
18+
*/
19+
export const isStringLength: IsStringLength = <
20+
Min extends number,
21+
Max extends number
22+
>(
23+
value: any,
24+
min: Min,
25+
max: Max,
26+
callback: ResultCallback = resultCallback
27+
): value is StringOfLength<Min, Max> =>
28+
callback(
29+
typeOf(value) === 'string' &&
30+
((isStringType(value)
31+
? value.length >= min && value.length <= max
32+
: false) ||
33+
(isStringObject(value)
34+
? value.valueOf().length >= min && value.valueOf().length <= max
35+
: false)),
36+
value
37+
);

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Function.
2+
import { isBooleanObject } from './is-boolean-object.func';
3+
import { isBooleanType } from './is-boolean-type.func';
4+
import { resultCallback } from '../../lib/result-callback.func';
5+
// Type.
6+
import { IsTrue } from '../type/is-true.type';
7+
import { ResultCallback } from '../../type/result-callback.type';
8+
/**
9+
* Checks if any `value` is a `boolean` type not an instance of `Boolean` and `Object`, or
10+
* is an `object` type and instance of `Boolean` and `Object` and equal to `true`.
11+
* @param value Any `value` to check.
12+
* @param callback A `ResultCallback` function to handle the result before returns.
13+
* @returns A `boolean` indicating whether or not the `value` is a `boolean` equal to `true`.
14+
*/
15+
export const isTrue: IsTrue = (
16+
value: any,
17+
callback: ResultCallback = resultCallback
18+
): value is true =>
19+
callback(
20+
(isBooleanType(value) && value === true)
21+
||
22+
(isBooleanObject(value) && value.valueOf() === true),
23+
value
24+
);

src/is/lib/is.object.ts

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,40 @@
1-
// Function.
1+
// Import: Function.
22
import { isArray } from './is-array.func';
33
import { isBigInt } from './is-big-int.func';
44
import { isBoolean } from './is-boolean.func';
55
import { isBooleanObject } from './is-boolean-object.func';
66
import { isBooleanType } from './is-boolean-type.func';
77
import { isClass } from './is-class.func';
8+
import { isDate } from './is-date.func';
89
import { isDefined } from './is-defined.func';
10+
import { isFalse } from './is-false.func';
911
import { isFunction } from './is-function.func';
1012
import { isInstance } from './is-instance.func';
1113
import { isKey } from './is-key.func';
1214
import { isNull } from './is-null.func';
1315
import { isNumber } from './is-number.func';
16+
import { isNumberBetween } from './is-number-between.func';
1417
import { isNumberObject } from './is-number-object.func';
1518
import { isNumberType } from './is-number-type.func';
1619
import { isObject } from './is-object.func';
1720
import { isObjectKey } from './is-object-key.func';
1821
import { isObjectKeyIn } from './is-object-key-in.func';
1922
import { isObjectKeys } from './is-object-keys.func';
2023
import { isPrimitive } from './is-primitive.func';
24+
import { isRegExp } from './is-regexp.func';
2125
import { isString } from './is-string.func';
26+
import { isStringLength } from './is-string-length.func';
2227
import { isStringObject } from './is-string-object.func';
2328
import { isStringType } from './is-string-type.func';
2429
import { isSymbol } from './is-symbol.func';
2530
import { isType } from './is-type.func';
2631
import { isUndefined } from './is-undefined.func';
27-
// Object.
32+
// Import: Object.
2833
import { isNot } from '../not/lib/is-not.object';
29-
// Interface.
34+
// Import: Interface.
3035
import { Is } from '../interface/is.interface';
31-
// `is`.
36+
import { isTrue } from './is-true.func';
37+
// Export: `is`.
3238
export const is: Is = {
3339
array: isArray,
3440
bigInt: isBigInt, // deprecated
@@ -37,24 +43,30 @@ export const is: Is = {
3743
booleanObject: isBooleanObject,
3844
booleanType: isBooleanType,
3945
class: isClass,
46+
date: isDate, // From `4.2.0`
4047
defined: isDefined,
48+
false: isFalse, // From `4.2.0`
4149
function: isFunction,
4250
instance: isInstance,
4351
key: isKey,
4452
not: isNot,
4553
null: isNull,
4654
number: isNumber,
55+
numberBetween: isNumberBetween, // From `4.2.0`
4756
numberObject: isNumberObject,
4857
numberType: isNumberType,
4958
object: isObject,
5059
objectKey: isObjectKey,
5160
objectKeyIn: isObjectKeyIn,
5261
objectKeys: isObjectKeys,
5362
primitive: isPrimitive,
63+
regexp: isRegExp, // From `4.2.0`
5464
string: isString,
65+
stringLength: isStringLength, // From `4.2.0`
5566
stringObject: isStringObject,
5667
stringType: isStringType,
5768
symbol: isSymbol,
69+
true: isTrue, // From `4.2.0`
5870
type: isType,
5971
undefined: isUndefined
6072
};

0 commit comments

Comments
 (0)