Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
e6cace2
chore: add union types
sciborrudnicki May 15, 2021
1bb14ba
docs(areString): update
sciborrudnicki May 15, 2021
1b65917
refactor(isBoolean): change the return type to Boolean
sciborrudnicki May 15, 2021
4940a11
refactor(isNumberObject): change the return result to Number
sciborrudnicki May 15, 2021
8e699e4
refactor: remove union types
sciborrudnicki May 15, 2021
bf8fb68
test: fix file name
sciborrudnicki May 15, 2021
d13ebb3
test(guardString): add guarding string objects
sciborrudnicki May 15, 2021
84f2c12
feat(types): add types for guards primitive objects
sciborrudnicki May 15, 2021
09634bd
test: move primitve objects to its own section
sciborrudnicki May 15, 2021
ea620c4
refactor(guardBoolean): add guard for primitive `Boolean` object
sciborrudnicki May 15, 2021
cae5ea1
refactor(guardNumber): add guard for primitive `Number` object
sciborrudnicki May 15, 2021
c98b0e1
refactor(guardString): add guard for primitive `String` object
sciborrudnicki May 15, 2021
3412d23
refactor(GuardBoolean): add guard for primitive `Boolean` object
sciborrudnicki May 15, 2021
1826d0b
docs(guardBoolean): update
sciborrudnicki May 16, 2021
ca4fd1e
docs(isBoolean): update jsdoc
sciborrudnicki May 16, 2021
cbd1cdc
docs(isNumber): update jsdoc
sciborrudnicki May 16, 2021
93d1f85
fix(isPrimitive): check only primitive types not an objects
sciborrudnicki May 16, 2021
0ea9a85
fix(isStringObject): return `String` object
sciborrudnicki May 16, 2021
6355919
docs(isStringType): update jsdoc
sciborrudnicki May 16, 2021
39a9582
docs(isString): update jsdoc
sciborrudnicki May 16, 2021
46ece83
fix(IsStringObject): return `String` object
sciborrudnicki May 16, 2021
4185c72
fix(isType): use `isStringType` instead of `isString`
sciborrudnicki May 16, 2021
d811468
docs(README.md): update
sciborrudnicki May 16, 2021
a0706b5
docs(README.md): update
sciborrudnicki May 16, 2021
790ce10
chore(package): 3.4.0
sciborrudnicki May 16, 2021
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
266 changes: 143 additions & 123 deletions README.md

Large diffs are not rendered by default.

2,106 changes: 2,106 additions & 0 deletions README.temp.md

Large diffs are not rendered by default.

266 changes: 143 additions & 123 deletions packages/type/README.md

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions packages/type/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/type/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@angular-package/type",
"version": "3.3.2",
"version": "3.4.0",
"description": "Common types, type guards and type checkers.",
"author": "Angular Package <angular-package@wvvw.dev> (https://wvvw.dev)",
"homepage": "https://github.com/angular-package/type#readme",
Expand Down
4 changes: 2 additions & 2 deletions packages/type/src/are/lib/are-string.func.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { check } from './check-args.func';
/**
* Checks if any of all the values are a `string`.
* @param value Any arguments to check if they're all a `'string`.
* @returns A `boolean` indicating whether or not all the `values` are an `Array`.
* @param value Any values to check.
* @returns A `boolean` indicating whether or not all the values are an `Array`.
*/
export const areString = (...value: any): boolean => check('string', ...value);
11 changes: 6 additions & 5 deletions packages/type/src/guard/lib/guard-boolean.func.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
// Function.
import { isBoolean } from '../../is/lib/is-boolean.func';
// Type.
import { AnyBoolean } from '../../type/any-boolean.type';
import { GuardBoolean } from '../type/guard-boolean.type';
import { ResultCallback } from '../../type/result-callback.type';
/**
* Guard the `value` to be a `boolean`.
* @param value A `boolean` type `value` to guard.
* @param callback Optional `ResultCallback` function to handle result before returns.
* @returns A `boolean` indicating whether or not the `value` is a `boolean`.
* Guard the `value` to be any type of a boolean.
* @param value An `AnyBoolean` type `value` to guard.
* @param callback An optional `ResultCallback` function to handle result before returns.
* @returns A `boolean` indicating whether or not the `value` is a `boolean` type or `Boolean` object.
*/
export const guardBoolean: GuardBoolean = (value: boolean, callback?: ResultCallback): value is boolean =>
export const guardBoolean: GuardBoolean = <B extends AnyBoolean>(value: B, callback?: ResultCallback): value is B =>
isBoolean(value, callback);
11 changes: 6 additions & 5 deletions packages/type/src/guard/lib/guard-number.func.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
// Function.
import { isNumber } from '../../is/lib/is-number.func';
// Type.
import { AnyNumber } from '../../type/any-number.type';
import { GuardNumber } from '../type/guard-number.type';
import { ResultCallback } from '../../type/result-callback.type';
/**
* Guard the `value` to be a `number`.
* @param value A `number` type `value` to guard.
* @param callback Optional `ResultCallback` function to handle result before returns.
* @returns A `boolean` indicating whether or not the `value` is a `number`.
* Guard the `value` to be any type of a number.
* @param value An `AnyNumber` type `value` to guard.
* @param callback An optional `ResultCallback` function to handle result before returns.
* @returns A `boolean` indicating whether or not the `value` is a `number` type or `Number` object.
*/
export const guardNumber: GuardNumber = (value: number, callback?: ResultCallback): value is number =>
export const guardNumber: GuardNumber = <N extends AnyNumber>(value: N, callback?: ResultCallback): value is N =>
isNumber(value, callback);
11 changes: 6 additions & 5 deletions packages/type/src/guard/lib/guard-string.func.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
// Function.
import { isString } from '../../is/lib/is-string.func';
// Type.
import { AnyString } from '../../type/any-string.type';
import { GuardString } from '../type/guard-string.type';
import { ResultCallback } from '../../type/result-callback.type';
/**
* Guard the `value` to be a `string`.
* @param value A `string` type `value` to guard.
* @param callback Optional `ResultCallback` function to handle result before returns.
* @returns A `boolean` indicating whether or not the `value` is a `string`.
* Guard the `value` to be any type of a string.
* @param value An `AnyString` type `value` to guard.
* @param callback An Optional `ResultCallback` function to handle result before returns.
* @returns A `boolean` indicating whether or not the `value` is a `string` type or `String` object.
*/
export const guardString: GuardString = (value: string, callback?: ResultCallback): value is string =>
export const guardString: GuardString = <S extends AnyString>(value: S, callback?: ResultCallback): value is S =>
isString(value, callback);
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Function.
import { guardBoolean } from '../lib/guard-boolean.func';
// Variables.
import { FALSE_EXPECTATION, TRUE, TRUE_EXPECTATION, FALSE } from './variables/boolean.const';
import { FALSE_EXPECTATION, TRUE, TRUE_EXPECTATION, FALSE, TRUE_INSTANCE, FALSE_INSTANCE } from './variables/boolean.const';

describe(guardBoolean.name, () => {
// Defined.
Expand All @@ -16,14 +16,22 @@ describe(guardBoolean.name, () => {
return result;
});
});

// ... primitives.
describe(`primitive`, () => {
// boolean
describe(`boolean`, () => {
it(`${FALSE_EXPECTATION}`, () => expect(guardBoolean(FALSE)).toBe(TRUE));
it(`${TRUE_EXPECTATION}`, () => expect(guardBoolean(TRUE)).toBe(TRUE));
it(`FALSE`, () => expect(guardBoolean(FALSE)).toBe(TRUE));
it(`TRUE`, () => expect(guardBoolean(TRUE)).toBe(TRUE));
});
});
// ... objective.
describe(`objective`, () => {
// boolean
describe(`boolean`, () => {
it(`${TRUE_EXPECTATION}`, () => expect(guardBoolean(TRUE_INSTANCE)).toBe(TRUE));
it(`${FALSE_EXPECTATION}`, () => expect(guardBoolean(FALSE_INSTANCE)).toBe(TRUE));
});
});

});
});
9 changes: 7 additions & 2 deletions packages/type/src/guard/test/guard-string.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Function.
import { guardString } from '../lib/guard-string.func';
// Variables.
import { STRING } from './variables/string.const';
import { STRING, STRING_NEW_INSTANCE } from './variables/string.const';
import { TRUE } from './variables/boolean.const';

describe(guardString.name, () => {
Expand All @@ -18,6 +18,11 @@ describe(guardString.name, () => {
});
});
// ... primitives.
describe(`primitive`, () => describe(`string`, () => it(`${STRING}`, () => expect(guardString(STRING)).toBe(TRUE))));
describe(`primitive`, () => {
describe(`string`, () => it(`${STRING}`, () => expect(guardString(STRING)).toBe(TRUE)));
describe(`object`, () => {
describe(`String`, () => it(`new String(${STRING})`, () => expect(guardString(STRING_NEW_INSTANCE)).toBe(TRUE)));
})
});
});
});
3 changes: 2 additions & 1 deletion packages/type/src/guard/type/guard-boolean.type.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
import { AnyBoolean } from '../../type/any-boolean.type';
import { ResultCallback } from '../../type/result-callback.type';
export type GuardBoolean = (value: boolean, callback?: ResultCallback) => value is boolean;
export type GuardBoolean = <B extends AnyBoolean>(value: B, callback?: ResultCallback) => value is B;
3 changes: 2 additions & 1 deletion packages/type/src/guard/type/guard-number.type.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
import { ResultCallback } from '../../type/result-callback.type';
export type GuardNumber = (value: number, callback?: ResultCallback) => value is number;
import { AnyNumber } from '../../type/any-number.type';
export type GuardNumber = <N extends AnyNumber>(value: N, callback?: ResultCallback) => value is N;
3 changes: 2 additions & 1 deletion packages/type/src/guard/type/guard-string.type.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
import { AnyString } from '../../type/any-string.type';
import { ResultCallback } from '../../type/result-callback.type';
export type GuardString = (value: string, callback?: ResultCallback) => value is string;
export type GuardString = <S extends AnyString>(value: S, callback?: ResultCallback) => value is S;
2 changes: 1 addition & 1 deletion packages/type/src/is/lib/is-boolean-object.func.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ import { ResultCallback } from '../../type/result-callback.type';
* @callback `resultCallback`.
* @returns A `boolean` indicating whether or not the `value` is a `Boolean` instance.
*/
export const isBooleanObject: IsBooleanObject = (value: any, callback: ResultCallback = resultCallback): value is boolean =>
export const isBooleanObject: IsBooleanObject = (value: any, callback: ResultCallback = resultCallback): value is Boolean =>
callback(typeof value === 'object' && value instanceof Boolean === true && value instanceof Object === true, value);
2 changes: 1 addition & 1 deletion packages/type/src/is/lib/is-boolean.func.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { ResultCallback } from '../../type/result-callback.type';
* @param value Any `value` to check.
* @param callback `ResultCallback` function to handle result before returns.
* @callback `resultCallback`.
* @returns A `boolean` indicating whether or not the `value` is a `boolean`.
* @returns A `boolean` indicating whether or not the `value` is a `boolean` type or `Boolean` object.
*/
export const isBoolean: IsBoolean = (value: any, callback: ResultCallback = resultCallback): value is boolean =>
callback(typeOf(value) === 'boolean' && (isBooleanType(value) || isBooleanObject(value)), value);
2 changes: 1 addition & 1 deletion packages/type/src/is/lib/is-number-object.func.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ import { ResultCallback } from '../../type/result-callback.type';
* @callback `resultCallback`.
* @returns A `boolean` indicating whether or not the `value` is a `Number` instance.
*/
export const isNumberObject: IsNumberObject = (value: any, callback: ResultCallback = resultCallback): value is number =>
export const isNumberObject: IsNumberObject = (value: any, callback: ResultCallback = resultCallback): value is Number =>
callback(typeof value === 'object' && value instanceof Number === true && value instanceof Object === true, value);
2 changes: 1 addition & 1 deletion packages/type/src/is/lib/is-number.func.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { ResultCallback } from '../../type/result-callback.type';
* @param value Any `value` to check.
* @param callback `ResultCallback` function to handle result before returns.
* @callback `resultCallback`.
* @returns A `boolean` indicating whether or not the `value` is a `number`.
* @returns A `boolean` indicating whether or not the `value` is a `number` type or `Number` object.
*/
export const isNumber: IsNumber = (value: any, callback: ResultCallback = resultCallback): value is number =>
callback(typeOf(value) === 'number' && isFinite(value) === true && (isNumberType(value) || isNumberObject(value)), value);
14 changes: 7 additions & 7 deletions packages/type/src/is/lib/is-primitive.func.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// Function.
import { isBigInt } from './is-big-int.func';
import { isBoolean } from './is-boolean.func';
import { isBooleanType } from './is-boolean-type.func';
import { isNull } from './is-null.func';
import { isNumber } from './is-number.func';
import { isString } from './is-string.func';
import { isNumberType } from './is-number-type.func';
import { isStringType } from './is-string-type.func';
import { isSymbol } from './is-symbol.func';
import { isUndefined } from './is-undefined.func';
import { resultCallback } from '../../lib/result-callback.func';
Expand All @@ -25,13 +25,13 @@ export const isPrimitive: IsPrimitive = <T extends Primitive>(
type: Primitives,
callback: ResultCallback = resultCallback
): value is T => {
if (isString(type)) {
if (isStringType(type)) {
switch (type) {
case 'bigint': return isBigInt(value, callback);
case 'boolean': return isBoolean(value, callback);
case 'number': return isNumber(value, callback);
case 'boolean': return isBooleanType(value, callback);
case 'number': return isNumberType(value, callback);
case 'null': return isNull(value, callback);
case 'string': return isString(value, callback);
case 'string': return isStringType(value, callback);
case 'symbol': return isSymbol(value, callback);
case 'undefined': return isUndefined(value, callback);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/type/src/is/lib/is-string-object.func.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ import { ResultCallback } from '../../type/result-callback.type';
* @callback `resultCallback`.
* @returns A `boolean` indicating whether or not the `value` is a `String` instance.
*/
export const isStringObject: IsStringObject = (value: any, callback: ResultCallback = resultCallback): value is string =>
export const isStringObject: IsStringObject = (value: any, callback: ResultCallback = resultCallback): value is String =>
callback(value instanceof Object === true && value instanceof String === true && typeof value === 'object', value);
2 changes: 1 addition & 1 deletion packages/type/src/is/lib/is-string-type.func.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { ResultCallback } from '../../type/result-callback.type';
* @param value Any `value` to check.
* @param callback `ResultCallback` function to handle result before returns.
* @callback `resultCallback`.
* @returns A `boolean` indicating whether or not the value is a `string`.
* @returns A `boolean` indicating whether or not the value is a `string` type.
*/
export const isStringType: IsStringType = (value: any, callback: ResultCallback = resultCallback): value is string =>
callback(value instanceof Object === false && value instanceof String === false && typeof value === 'string', value);
2 changes: 1 addition & 1 deletion packages/type/src/is/lib/is-string.func.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { ResultCallback } from '../../type/result-callback.type';
* @param value Any `value` to check.
* @param callback `ResultCallback` function to handle result before returns.
* @callback `resultCallback`.
* @returns A `boolean` indicating whether or not the `value` is a `string`.
* @returns A `boolean` indicating whether or not the `value` is a `string` type or `String` object.
*/
export const isString: IsString = (value: any, callback: ResultCallback = resultCallback): value is string =>
callback(typeOf(value) === 'string' && (isStringType(value) || isStringObject(value)), value);
4 changes: 2 additions & 2 deletions packages/type/src/is/lib/is-type.func.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { isInstance } from './is-instance.func';
import { isNotNull } from '../not/lib/is-not-null.func';
import { isObject } from './is-object.func';
import { isPrimitive } from './is-primitive.func';
import { isString } from './is-string.func';
import { isStringType } from './is-string-type.func';
import { resultCallback } from '../../lib/result-callback.func';
// Type.
import { IsType } from '../type/is-type.type';
Expand All @@ -20,7 +20,7 @@ import { Types } from '../../type/types.type';
* @returns A `boolean` indicating whether or not the `value` is the `Type` from a `type` of the `Types`.
*/
export const isType: IsType = <T extends Type>(value: any, type: Types<T>, callback: ResultCallback = resultCallback): value is T => {
if (isString(type)) {
if (isStringType(type)) {
switch (type) {
// Primitives.
case 'bigint':
Expand Down
26 changes: 15 additions & 11 deletions packages/type/src/is/test/is-key.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,33 +45,23 @@ describe(isKey.name, () => {
// ... primitives.
describe(`primitive`, () => {
// bigint
describe(`bigint`, () => {
it(`${BIGINT}`, () => expect(isKey(BIGINT)).toBe(FALSE));
it(`${BIGINT_EXPECTATION}`, () => expect(isKey(BIGINT_INSTANCE)).toBe(FALSE));
});

describe(`bigint`, () => it(`${BIGINT}`, () => expect(isKey(BIGINT)).toBe(FALSE)));
// boolean
describe(`boolean`, () => {
it(`${TRUE}`, () => expect(isKey(TRUE)).toBe(FALSE));
it(`${FALSE}`, () => expect(isKey(FALSE)).toBe(FALSE));
it(`${TRUE_EXPECTATION}`, () => expect(isKey(TRUE_INSTANCE)).toBe(FALSE));
it(`${FALSE_EXPECTATION}`, () => expect(isKey(FALSE_INSTANCE)).toBe(FALSE));
});

// null
it(`${NULL}`, () => expect(isKey(NULL)).toBe(FALSE));

// number
describe(`number`, () => {
it(`${NUMBER}`, () => expect(isKey(NUMBER)).toBe(TRUE));
it(`Number(${NUMBER})`, () => expect(isKey(NUMBER_INSTANCE)).toBe(TRUE));
it(`new Number(${NUMBER})`, () => expect(isKey(NUMBER_NEW_INSTANCE)).toBe(TRUE));
});
// string
describe(`string`, () => {
it(`${STRING}`, () => expect(isKey(STRING)).toBe(TRUE));
it(`String(${STRING})`, () => expect(isKey(STRING_INSTANCE)).toBe(TRUE));
it(`new String(${STRING})`, () => expect(isKey(STRING_NEW_INSTANCE)).toBe(TRUE));
});
// symbol
describe(`symbol`, () => {
Expand All @@ -80,6 +70,20 @@ describe(isKey.name, () => {
});
// undefined
it(`${UNDEFINED}`, () => expect(isKey(UNDEFINED)).toBe(FALSE));
// ... object.
describe(`object`, () => {
// BigInt
describe(`BigInt`, () => it(`${BIGINT_EXPECTATION}`, () => expect(isKey(BIGINT_INSTANCE)).toBe(FALSE)));
// Boolean
describe(`Boolean`, () => {
it(`${TRUE_EXPECTATION}`, () => expect(isKey(TRUE_INSTANCE)).toBe(FALSE));
it(`${FALSE_EXPECTATION}`, () => expect(isKey(FALSE_INSTANCE)).toBe(FALSE));
});
// Number
describe(`Number`, () => it(`new Number(${NUMBER})`, () => expect(isKey(NUMBER_NEW_INSTANCE)).toBe(TRUE)));
// String
describe(`String`, () => it(`new String(${STRING})`, () => expect(isKey(STRING_NEW_INSTANCE)).toBe(TRUE)));
});
});
});
});
26 changes: 15 additions & 11 deletions packages/type/src/is/test/is-number-object.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,33 +49,23 @@ describe(isNumberObject.name, () => {
// ... primitives.
describe(`primitive`, () => {
// bigint
describe(`bigint`, () => {
it(`${BIGINT}`, () => expect(isNumberObject(BIGINT)).toBe(FALSE));
it(`${BIGINT_EXPECTATION}`, () => expect(isNumberObject(BIGINT_INSTANCE)).toBe(FALSE));
});

describe(`bigint`, () => it(`${BIGINT}`, () => expect(isNumberObject(BIGINT)).toBe(FALSE)));
// boolean
describe(`boolean`, () => {
it(`${TRUE}`, () => expect(isNumberObject(TRUE)).toBe(FALSE));
it(`${FALSE}`, () => expect(isNumberObject(FALSE)).toBe(FALSE));
it(`${TRUE_EXPECTATION}`, () => expect(isNumberObject(TRUE_INSTANCE)).toBe(FALSE));
it(`${FALSE_EXPECTATION}`, () => expect(isNumberObject(FALSE_INSTANCE)).toBe(FALSE));
});

// null
it(`${NULL}`, () => expect(isNumberObject(NULL)).toBe(FALSE));

// number
describe(`number`, () => {
it(`${NUMBER}`, () => expect(isNumberObject(NUMBER)).toBe(FALSE));
it(`Number(${NUMBER})`, () => expect(isNumberObject(NUMBER_INSTANCE)).toBe(FALSE));
it(`new Number(${NUMBER})`, () => expect(isNumberObject(NUMBER_NEW_INSTANCE)).toBe(TRUE));
});
// string
describe(`string`, () => {
it(`${STRING}`, () => expect(isNumberObject(STRING)).toBe(FALSE));
it(`String(${STRING})`, () => expect(isNumberObject(STRING_INSTANCE)).toBe(FALSE));
it(`new String(${STRING})`, () => expect(isNumberObject(STRING_NEW_INSTANCE)).toBe(FALSE));
});
// symbol
describe(`symbol`, () => {
Expand All @@ -84,6 +74,20 @@ describe(isNumberObject.name, () => {
});
// undefined
it(`${UNDEFINED}`, () => expect(isNumberObject(UNDEFINED)).toBe(FALSE));
// ... object.
describe(`object`, () => {
// BigInt
describe(`BigInt`, () => it(`${BIGINT_EXPECTATION}`, () => expect(isNumberObject(BIGINT_INSTANCE)).toBe(FALSE)));
// Boolean
describe(`Boolean`, () => {
it(`${TRUE_EXPECTATION}`, () => expect(isNumberObject(TRUE_INSTANCE)).toBe(FALSE));
it(`${FALSE_EXPECTATION}`, () => expect(isNumberObject(FALSE_INSTANCE)).toBe(FALSE));
});
// Number
describe(`Number`, () => it(`new Number(${NUMBER})`, () => expect(isNumberObject(NUMBER_NEW_INSTANCE)).toBe(TRUE)));
// String
describe(`String`, () => it(`new String(${STRING})`, () => expect(isNumberObject(STRING_NEW_INSTANCE)).toBe(FALSE)));
});
});
});
});
Loading