Skip to content

Commit

Permalink
feat: add functions with the types and tests
Browse files Browse the repository at this point in the history
+ `isDate()`
+ `isFalse()`
+ `isNumberBetween()`
+ `isRegExp()`
+ `isStringLength()`
+ `isTrue()`
+ constants for date
+ constant for `RegExp`
+ update api
  • Loading branch information
sciborrudnicki committed Jun 25, 2021
1 parent f7aec7b commit 21f3f48
Show file tree
Hide file tree
Showing 27 changed files with 928 additions and 5 deletions.
6 changes: 6 additions & 0 deletions src/is/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,29 @@ export { isBoolean } from './lib/is-boolean.func';
export { isBooleanObject } from './lib/is-boolean-object.func';
export { isBooleanType } from './lib/is-boolean-type.func';
export { isClass } from './lib/is-class.func';
export { isDate } from './lib/is-date.func'; // From 4.2.0
export { isDefined } from './lib/is-defined.func';
export { isFalse } from './lib/is-false.func'; // From 4.2.0
export { isFunction } from './lib/is-function.func';
export { isInstance } from './lib/is-instance.func';
export { isKey } from './lib/is-key.func';
export { isNull } from './lib/is-null.func';
export { isNumber } from './lib/is-number.func';
export { isNumberBetween } from './lib/is-number-between.func'; // From 4.2.0
export { isNumberObject } from './lib/is-number-object.func';
export { isNumberType } from './lib/is-number-type.func';
export { isObject } from './lib/is-object.func';
export { isObjectKey } from './lib/is-object-key.func';
export { isObjectKeyIn } from './lib/is-object-key-in.func';
export { isObjectKeys } from './lib/is-object-keys.func';
export { isPrimitive } from './lib/is-primitive.func';
export { isRegExp } from './lib/is-regexp.func'; // From 4.2.0
export { isString } from './lib/is-string.func';
export { isStringLength } from './lib/is-string-length.func'; // From 4.2.0
export { isStringObject } from './lib/is-string-object.func';
export { isStringType } from './lib/is-string-type.func';
export { isSymbol } from './lib/is-symbol.func';
export { isTrue } from './lib/is-true.func'; // From 4.2.0
export { isType } from './lib/is-type.func';
export { isUndefined } from './lib/is-undefined.func';
// `is` object.
Expand Down
14 changes: 14 additions & 0 deletions src/is/interface/is.interface.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,38 @@
// Import: Type.
import { IsArray } from '../type/is-array.type';
import { IsBigInt } from '../type/is-big-int.type';
import { IsBoolean } from '../type/is-boolean.type';
import { IsBooleanObject } from '../type/is-boolean-object.type';
import { IsBooleanType } from '../type/is-boolean-type.type';
import { IsClass } from '../type/is-class.type';
import { IsDate } from '../type/is-date.type';
import { IsDefined } from '../type/is-defined.type';
import { IsFalse } from '../type/is-false.type';
import { IsFunction } from '../type/is-function.type';
import { IsInstance } from '../type/is-instance.type';
import { IsKey } from '../type/is-key.type';
import { IsNot } from '../not/interface/is-not.interface';
import { IsNull } from '../type/is-null.type';
import { IsNumber } from '../type/is-number.type';
import { IsNumberBetween } from '../type/is-number-between.type';
import { IsNumberObject } from '../type/is-number-object.type';
import { IsNumberType } from '../type/is-number-type.type';
import { IsObject } from '../type/is-object.type';
import { IsObjectKey } from '../type/is-object-key.type';
import { IsObjectKeyIn } from '../type/is-object-key-in.type';
import { IsObjectKeys } from '../type/is-object-keys.type';
import { IsPrimitive } from '../type/is-primitive.type';
import { IsRegExp } from '../type/is-regexp.type';
import { IsString } from '../type/is-string.type';
import { IsStringLength } from '../type/is-string-length.type';
import { IsStringObject } from '../type/is-string-object.type';
import { IsStringType } from '../type/is-string-type.type';
import { IsSymbol } from '../type/is-symbol.type';
import { IsTrue } from '../type/is-true.type';
import { IsType } from '../type/is-type.type';
import { IsUndefined } from '../type/is-undefined.type';
/**
* Export: Interface.
* Object with prefixed `is` functions.
*/
export interface Is {
Expand All @@ -35,24 +43,30 @@ export interface Is {
booleanObject: IsBooleanObject;
booleanType: IsBooleanType;
class: IsClass;
date: IsDate; // From 4.2.0
defined: IsDefined;
false: IsFalse; // From `4.2.0`
function: IsFunction;
instance: IsInstance;
key: IsKey;
not: IsNot;
null: IsNull;
number: IsNumber;
numberBetween: IsNumberBetween; // From `4.2.0`
numberObject: IsNumberObject;
numberType: IsNumberType;
object: IsObject;
objectKey: IsObjectKey;
objectKeyIn: IsObjectKeyIn;
objectKeys: IsObjectKeys;
primitive: IsPrimitive;
regexp: IsRegExp; // From 4.2.0
string: IsString;
stringLength: IsStringLength;
stringObject: IsStringObject;
stringType: IsStringType;
symbol: IsSymbol;
true: IsTrue; // From 4.2.0
type: IsType;
undefined: IsUndefined;
}
23 changes: 23 additions & 0 deletions src/is/lib/is-date.func.ts
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
);
24 changes: 24 additions & 0 deletions src/is/lib/is-false.func.ts
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
);
32 changes: 32 additions & 0 deletions src/is/lib/is-number-between.func.ts
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
);
};
47 changes: 47 additions & 0 deletions src/is/lib/is-object-keys-type.func.ts
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);
22 changes: 22 additions & 0 deletions src/is/lib/is-regexp.func.ts
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
);
37 changes: 37 additions & 0 deletions src/is/lib/is-string-length.func.ts
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
);
24 changes: 24 additions & 0 deletions src/is/lib/is-true.func.ts
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
);
20 changes: 16 additions & 4 deletions src/is/lib/is.object.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,40 @@
// Function.
// Import: Function.
import { isArray } from './is-array.func';
import { isBigInt } from './is-big-int.func';
import { isBoolean } from './is-boolean.func';
import { isBooleanObject } from './is-boolean-object.func';
import { isBooleanType } from './is-boolean-type.func';
import { isClass } from './is-class.func';
import { isDate } from './is-date.func';
import { isDefined } from './is-defined.func';
import { isFalse } from './is-false.func';
import { isFunction } from './is-function.func';
import { isInstance } from './is-instance.func';
import { isKey } from './is-key.func';
import { isNull } from './is-null.func';
import { isNumber } from './is-number.func';
import { isNumberBetween } from './is-number-between.func';
import { isNumberObject } from './is-number-object.func';
import { isNumberType } from './is-number-type.func';
import { isObject } from './is-object.func';
import { isObjectKey } from './is-object-key.func';
import { isObjectKeyIn } from './is-object-key-in.func';
import { isObjectKeys } from './is-object-keys.func';
import { isPrimitive } from './is-primitive.func';
import { isRegExp } from './is-regexp.func';
import { isString } from './is-string.func';
import { isStringLength } from './is-string-length.func';
import { isStringObject } from './is-string-object.func';
import { isStringType } from './is-string-type.func';
import { isSymbol } from './is-symbol.func';
import { isType } from './is-type.func';
import { isUndefined } from './is-undefined.func';
// Object.
// Import: Object.
import { isNot } from '../not/lib/is-not.object';
// Interface.
// Import: Interface.
import { Is } from '../interface/is.interface';
// `is`.
import { isTrue } from './is-true.func';
// Export: `is`.
export const is: Is = {
array: isArray,
bigInt: isBigInt, // deprecated
Expand All @@ -37,24 +43,30 @@ export const is: Is = {
booleanObject: isBooleanObject,
booleanType: isBooleanType,
class: isClass,
date: isDate, // From `4.2.0`
defined: isDefined,
false: isFalse, // From `4.2.0`
function: isFunction,
instance: isInstance,
key: isKey,
not: isNot,
null: isNull,
number: isNumber,
numberBetween: isNumberBetween, // From `4.2.0`
numberObject: isNumberObject,
numberType: isNumberType,
object: isObject,
objectKey: isObjectKey,
objectKeyIn: isObjectKeyIn,
objectKeys: isObjectKeys,
primitive: isPrimitive,
regexp: isRegExp, // From `4.2.0`
string: isString,
stringLength: isStringLength, // From `4.2.0`
stringObject: isStringObject,
stringType: isStringType,
symbol: isSymbol,
true: isTrue, // From `4.2.0`
type: isType,
undefined: isUndefined
};

0 comments on commit 21f3f48

Please sign in to comment.