-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add functions with the types and tests
+ `isDate()` + `isFalse()` + `isNumberBetween()` + `isRegExp()` + `isStringLength()` + `isTrue()` + constants for date + constant for `RegExp` + update api
- Loading branch information
1 parent
f7aec7b
commit 21f3f48
Showing
27 changed files
with
928 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// Function. | ||
import { typeOf } from '../../lib/type-of.func'; | ||
import { resultCallback } from '../../lib/result-callback.func'; | ||
// Type. | ||
import { IsDate } from '../type/is-date.type'; | ||
import { ResultCallback } from '../../type/result-callback.type'; | ||
/** | ||
* Checks if any `value` is an `object` type instance of `Date` and `Object`. | ||
* @param value Any `value` to check. | ||
* @param callback An optional `ResultCallback` function to handle the result before returns. | ||
* @returns A `boolean` indicating whether or not the value is a date. | ||
*/ | ||
export const isDate: IsDate = ( | ||
value: any, | ||
callback: ResultCallback = resultCallback | ||
): value is Date => | ||
callback( | ||
typeOf(value) === 'date' && | ||
typeof value === 'object' && | ||
value instanceof Date === true && | ||
value instanceof Object === true, | ||
value | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// Function. | ||
import { isBooleanObject } from './is-boolean-object.func'; | ||
import { isBooleanType } from './is-boolean-type.func'; | ||
import { resultCallback } from '../../lib/result-callback.func'; | ||
// Type. | ||
import { IsFalse } from '../type/is-false.type'; | ||
import { ResultCallback } from '../../type/result-callback.type'; | ||
/** | ||
* Checks if any `value` is a `boolean` type not an instance of `Boolean` and `Object`, or | ||
* is an `object` type and instance of `Boolean` and `Object` and equal to `false`. | ||
* @param value Any `value` to check. | ||
* @param callback A `ResultCallback` function to handle the result before returns. | ||
* @returns A `boolean` indicating whether or not the `value` is a `boolean` equal to `false`. | ||
*/ | ||
export const isFalse: IsFalse = ( | ||
value: any, | ||
callback: ResultCallback = resultCallback | ||
): value is false => | ||
callback( | ||
(isBooleanType(value) && value === false) | ||
|| | ||
(isBooleanObject(value) && value.valueOf() === false), | ||
value | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// Function. | ||
import { isNumberObject } from './is-number-object.func'; | ||
import { isNumberType } from './is-number-type.func'; | ||
import { resultCallback } from '../../lib/result-callback.func'; | ||
import { typeOf } from '../../lib/type-of.func'; | ||
// Type. | ||
import { NumberBetween } from '../../type/number-between.type'; | ||
import { ResultCallback } from '../../type/result-callback.type'; | ||
/** | ||
* Checks if any `value` is a `number` type, not instance of `Object` and `Number` or `object` type and instance of `Number` and `Object`, | ||
* in the specified range. | ||
* @param value Any `value` to check. | ||
* @param min A `number` of the minimum range of the provided `value`. | ||
* @param max A `number` of the maximum range of the provided `value`. | ||
* @param callback A `ResultCallback` function to handle the result before returns. | ||
* @returns A `boolean` indicating whether or not the `value` is a `number` type or `Number` instance in the specified range. | ||
*/ | ||
export const isNumberBetween = <Min extends number, Max extends number>( | ||
value: any, | ||
min: Min, | ||
max: Max, | ||
callback: ResultCallback = resultCallback | ||
): value is NumberBetween<Min, Max> => { | ||
return callback( | ||
typeOf(value) === 'number' && | ||
((isNumberType(value) ? value >= min && value <= max : false) || | ||
(isNumberObject(value) | ||
? value.valueOf() >= min && value.valueOf() <= max | ||
: false)), | ||
value | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// Function. | ||
import { isArray } from './is-array.func'; | ||
import { isKey } from './is-key.func'; | ||
// Type. | ||
import { IsObjectKeys } from '../type/is-object-keys.type'; | ||
import { isObject } from './is-object.func'; | ||
import { Types } from '../../type/types.type'; | ||
import { Func } from '../../type/func.type'; | ||
import { isType } from './is-type.func'; | ||
import { Primitives } from '../../type/primitives.type'; | ||
import { isPrimitive } from './is-primitive.func'; | ||
import { guardPrimitive } from '../../guard/lib/guard-primitive.func'; | ||
|
||
type IsObjectKeysType = | ||
<Type extends object>(...keys: (keyof Type)[] | ||
) => (...type: Primitives[]) => (value: Type) => value is Type; | ||
|
||
/** | ||
* Checks if any `value` is an `object` of a generic `Type` with some of its own specified `keys`. | ||
* @param value Any `value` to check if it contains some of the specified `keys`. | ||
* @param keys A rest parameter key of `Type` or an array of keys of `Type` to check the `value`. | ||
* @returns A `boolean` indicating whether or not the `value` is an `object` with some of its own specified keys. | ||
*/ | ||
export const isObjectKeysType: IsObjectKeysType = | ||
<Type extends object>(...keys: (keyof Type | Array<keyof Type>)[]) => | ||
(...type: Primitives[]) => | ||
(value: any): value is Type => | ||
isObject<Type>(value) | ||
? keys.some((key, keysIndex: number) => | ||
isArray(key) | ||
? key.every((k, arrIndex: number) => | ||
isKey(k) ? {}.hasOwnProperty.call(value, k) === true ? isPrimitive(value[k], type[arrIndex]) : false : false | ||
) | ||
: isKey(key) | ||
? {}.hasOwnProperty.call(value, key) === true ? isPrimitive(value[key], type[keysIndex]) : false | ||
: false | ||
) | ||
: false; | ||
|
||
class Person { | ||
firstName = 'bla'; | ||
surname = 'bla bla bla'; | ||
} | ||
|
||
const person: Person = new Person(); | ||
|
||
isObjectKeysType<Person>('firstName')('string')(person); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// Import: Function. | ||
import { resultCallback } from '../../lib/result-callback.func'; | ||
import { typeOf } from '../../lib/type-of.func'; | ||
// Import: Type. | ||
import { IsRegExp } from '../type/is-regexp.type'; | ||
import { ResultCallback } from '../../type/result-callback.type'; | ||
/** | ||
* Checks if any `value` is a `regexp` type, an instance of `Object` and `RegExp`. | ||
* @param value Any `value` to check. | ||
* @param callback An optional `ResultCallback` function to handle the result before returns. | ||
* @returns A `boolean` indicating whether or not the `value` is a regular expression. | ||
*/ | ||
export const isRegExp: IsRegExp = ( | ||
value: any, | ||
callback: ResultCallback = resultCallback | ||
): value is RegExp => | ||
callback( | ||
typeOf(value) === 'regexp' && | ||
typeof value === 'object' && | ||
value instanceof Object && | ||
value instanceof RegExp | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// Function. | ||
import { isStringObject } from './is-string-object.func'; | ||
import { isStringType } from './is-string-type.func'; | ||
import { resultCallback } from '../../lib/result-callback.func'; | ||
import { typeOf } from '../../lib/type-of.func'; | ||
// Type. | ||
import { IsStringLength } from '../type/is-string-length.type'; | ||
import { ResultCallback } from '../../type/result-callback.type'; | ||
import { StringOfLength } from '../../type/string-of-length.type'; | ||
/** | ||
* Checks if any `value` is a `string` type, not instance of `Object` and `String` or `object` type and instance of `String` and `Object`, | ||
* of a length in the specified range. | ||
* @param value Any `value` to check. | ||
* @param min A `number` of the minimum length of the provided `value`. | ||
* @param max A `number` of the maximum length of the provided `value`. | ||
* @param callback A `ResultCallback` function to handle the result before returns. | ||
* @returns A `boolean` indicating whether or not the `value` is a `string` type or `String` instance of length in the specified range. | ||
*/ | ||
export const isStringLength: IsStringLength = < | ||
Min extends number, | ||
Max extends number | ||
>( | ||
value: any, | ||
min: Min, | ||
max: Max, | ||
callback: ResultCallback = resultCallback | ||
): value is StringOfLength<Min, Max> => | ||
callback( | ||
typeOf(value) === 'string' && | ||
((isStringType(value) | ||
? value.length >= min && value.length <= max | ||
: false) || | ||
(isStringObject(value) | ||
? value.valueOf().length >= min && value.valueOf().length <= max | ||
: false)), | ||
value | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// Function. | ||
import { isBooleanObject } from './is-boolean-object.func'; | ||
import { isBooleanType } from './is-boolean-type.func'; | ||
import { resultCallback } from '../../lib/result-callback.func'; | ||
// Type. | ||
import { IsTrue } from '../type/is-true.type'; | ||
import { ResultCallback } from '../../type/result-callback.type'; | ||
/** | ||
* Checks if any `value` is a `boolean` type not an instance of `Boolean` and `Object`, or | ||
* is an `object` type and instance of `Boolean` and `Object` and equal to `true`. | ||
* @param value Any `value` to check. | ||
* @param callback A `ResultCallback` function to handle the result before returns. | ||
* @returns A `boolean` indicating whether or not the `value` is a `boolean` equal to `true`. | ||
*/ | ||
export const isTrue: IsTrue = ( | ||
value: any, | ||
callback: ResultCallback = resultCallback | ||
): value is true => | ||
callback( | ||
(isBooleanType(value) && value === true) | ||
|| | ||
(isBooleanObject(value) && value.valueOf() === true), | ||
value | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.