Skip to content

Commit

Permalink
Added the negation of each & fixed documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
Avram Cosmin committed Feb 21, 2024
1 parent a88b3e2 commit e08ff57
Show file tree
Hide file tree
Showing 69 changed files with 1,684 additions and 321 deletions.
6 changes: 3 additions & 3 deletions README.md
Expand Up @@ -23,24 +23,24 @@ if (OfArrayType<string>(array)) {
## List of helpers

### Types

- TypeofValues
- ObjectTypeValues

### Helpers

- ReturnConstructor

### Assertion helpers

- OfArrayType
- ArrayOfGivenTypeClass
- ArrayOfGivenTypePrimitive
- OfBooleanType
- OfBooleanTypeAsString
- EmptyArray
- NotEmptyArray
- EmptyObject
- NotEmptyObject
- EmptyString
- NotEmptyString
- OfFalseType
- OfFalseTypeAsString
- OfFloatType
Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "@24vlh/ts-assert",
"version": "1.0.7",
"version": "1.0.8",
"description": "Small footprint library that helps with typescript assertions.",
"author": "@vlah.io",
"license": "Apache-2.0",
Expand Down
54 changes: 27 additions & 27 deletions src/lib/array-type-class.spec.ts
@@ -1,33 +1,33 @@
import { ArrayOfGivenTypeClass } from './array-type-class';
import {ArrayOfGivenTypeClass, NotArrayOfGivenTypeClass} from './array-type-class';

describe('ArrayOfGivenTypeClass', () => {
// Defining simple type classes
class TypeClass1 {}
class TypeClass2 {}
// Defining simple type classes
class TypeClass1 {
}

// valid test cases
it('should return true if the argument is an array of instances of the expected type class', () => {
const arg = [new TypeClass1(), new TypeClass1()];
const result = ArrayOfGivenTypeClass(arg, TypeClass1);
expect(result).toBe(true);
});
class TypeClass2 {
}

// invalid test cases
it('should return false if the argument is not an array of instances of the expected type class', () => {
const arg = [new TypeClass1(), new TypeClass2()];
const result = ArrayOfGivenTypeClass(arg, TypeClass1);
expect(result).toBe(false);
});
const testData: [unknown, new (...args: never[]) => any, boolean][] = [
[[new TypeClass1(), new TypeClass1()], TypeClass1, false],
[[new TypeClass1(), new TypeClass2()], TypeClass1, true],
[[], TypeClass1, true],
['not an array', TypeClass1, true]
];

it('should return false if the argument is empty array', () => {
const arg: never[] = [];
const result = ArrayOfGivenTypeClass(arg, TypeClass1);
expect(result).toBe(false);
});
describe('ArrayOfGivenTypeClass', () => {
test.each(testData)(
'NotArrayOfGivenTypeClass: Expecting array of %p to be %p',
(inputValue: unknown, expectedTypeClass: new (...args: never[]) => any, expectedOutput: boolean) => {
expect(ArrayOfGivenTypeClass(inputValue, expectedTypeClass)).toBe(!expectedOutput);
}
);
});

it('should return false if the argument is not an array', () => {
const arg = 'not an array';
const result = ArrayOfGivenTypeClass(arg, TypeClass1);
expect(result).toBe(false);
});
describe('NotArrayOfGivenTypeClass', () => {
test.each(testData)(
'NotArrayOfGivenTypeClass: Expecting array of %p to be %p',
(inputValue: unknown, expectedTypeClass: new (...args: never[]) => any, expectedOutput: boolean) => {
expect(NotArrayOfGivenTypeClass(inputValue, expectedTypeClass)).toBe(expectedOutput);
}
);
});
31 changes: 31 additions & 0 deletions src/lib/array-type-class.ts
Expand Up @@ -3,9 +3,14 @@ import { ReturnConstructor } from './return-constructor';
/**
* Checks if the given argument is an array of a specific type.
*
* @template T - The type of elements in the array.
* @param {unknown} arg - The argument to be checked.
* @param {function} expectedTypeClass - The class of the expected type.
* @returns {boolean} - Returns true if the argument is an array of the expected type, false otherwise.
* @example
* const value = [new MyClass(), new MyClass()];
* const result = ArrayOfGivenTypeClass(value, MyClass);
* // result is true
*/
export function ArrayOfGivenTypeClass<T>(
arg: unknown,
Expand All @@ -20,3 +25,29 @@ export function ArrayOfGivenTypeClass<T>(
)
);
}

/**
* Checks if the given argument is not an array of a specific type.
*
* @template T - The type of elements in the array.
* @param {unknown} arg - The argument to be checked.
* @param {function} expectedTypeClass - The class of the expected type.
* @returns {boolean} - Returns true if the argument is not an array of the expected type, false otherwise.
* @example
* const value = [new MyClass(), new MyClass()];
* const result = NotArrayOfGivenTypeClass(value, MyClass);
* // result is false
*/
export function NotArrayOfGivenTypeClass<T>(
arg: unknown,
expectedTypeClass: new (...args: never[]) => T
): boolean {
return (
!Array.isArray(arg) ||
arg.length === 0 ||
!arg.every(
(item: unknown): boolean =>
item instanceof ReturnConstructor(expectedTypeClass)
)
);
}
52 changes: 36 additions & 16 deletions src/lib/array-type-primitive.spec.ts
@@ -1,21 +1,41 @@
import { ArrayOfGivenTypePrimitive } from './array-type-primitive';
import {ArrayOfGivenTypePrimitive, NotArrayOfGivenTypePrimitive} from './array-type-primitive';

describe('ArrayOfGivenTypePrimitive function', () => {
it('should return true when all array elements match the given type', () => {
const arr = [1, 2, 3];
const expectedTypeofValue = 'number';
expect(ArrayOfGivenTypePrimitive<number>(arr, expectedTypeofValue)).toBe(true);
});
it('should return true when all array elements match the given type', () => {
const arr = [1, 2, 3];
const expectedTypeofValue = 'number';
expect(ArrayOfGivenTypePrimitive<number>(arr, expectedTypeofValue)).toBe(true);
});

it('should return false when not all array elements match the given type', () => {
const arr = [1, 'a', 3];
const expectedTypeofValue = 'number';
expect(ArrayOfGivenTypePrimitive<number>(arr, expectedTypeofValue)).toBe(false);
});
it('should return false when not all array elements match the given type', () => {
const arr = [1, 'a', 3];
const expectedTypeofValue = 'number';
expect(ArrayOfGivenTypePrimitive<number>(arr, expectedTypeofValue)).toBe(false);
});

it('should return false when input is not an array', () => {
const arg = 'test';
const expectedTypeofValue = 'string';
expect(ArrayOfGivenTypePrimitive<string>(arg, expectedTypeofValue)).toBe(false);
});
it('should return false when input is not an array', () => {
const arg = 'test';
const expectedTypeofValue = 'string';
expect(ArrayOfGivenTypePrimitive<string>(arg, expectedTypeofValue)).toBe(false);
});
});

describe('NotArrayOfGivenTypePrimitive function', () => {
it('should return true when not all array elements match the given type', () => {
const arr = [1, 'a', 3];
const expectedTypeofValue = 'number';
expect(NotArrayOfGivenTypePrimitive<number>(arr, expectedTypeofValue)).toBe(true);
});

it('should return true when input is not an array', () => {
const arg = 'test';
const expectedTypeofValue = 'string';
expect(NotArrayOfGivenTypePrimitive<string>(arg, expectedTypeofValue)).toBe(true);
});

it('should return false when all array elements match the given type', () => {
const arr = [1, 2, 3];
const expectedTypeofValue = 'number';
expect(NotArrayOfGivenTypePrimitive<number>(arr, expectedTypeofValue)).toBe(false);
});
});
27 changes: 27 additions & 0 deletions src/lib/array-type-primitive.ts
Expand Up @@ -3,9 +3,14 @@ import { TypeofValues } from './24vlh.type';
/**
* Check if the given argument is an array of a specific primitive type.
*
* @template T - The type of elements in the array.
* @param {unknown} arg - The argument to be checked.
* @param {TypeofValues} expectedTypeofValue - The expected primitive type of the values in the array.
* @returns {boolean} True if the argument is an array of the expected primitive type, false otherwise.
* @example
* const value = [1, 2, 3];
* const result = ArrayOfGivenTypePrimitive<number>(value, 'number');
* // result is true
*/
export function ArrayOfGivenTypePrimitive<T>(
arg: unknown,
Expand All @@ -17,3 +22,25 @@ export function ArrayOfGivenTypePrimitive<T>(
arg.every((item: unknown): boolean => typeof item === expectedTypeofValue)
);
}

/**
* Checks if the provided argument is not an array of a specific primitive type.
*
* @param {unknown} arg - The argument to be checked.
* @param {TypeofValues} expectedTypeofValue - The expected primitive type of the values in the array.
* @returns {boolean} - Returns true if the argument is not an array of the expected primitive type, otherwise false.
* @example
* const value = [1, 2, 3];
* const result = NotArrayOfGivenTypePrimitive<number>(value, 'number');
* // result is false
*/
export function NotArrayOfGivenTypePrimitive(
arg: unknown,
expectedTypeofValue: TypeofValues
): boolean {
return (
!Array.isArray(arg) ||
arg.length === 0 ||
!arg.every((item: unknown): boolean => typeof item === expectedTypeofValue)
);
}
35 changes: 22 additions & 13 deletions src/lib/array.spec.ts
@@ -1,19 +1,28 @@
import { OfArrayType } from './array';
import {NotOfArrayType, OfArrayType} from './array';

const testData: [unknown, boolean][] = [
[[], true],
[new Array(2), true],
[Object.keys({}), true],
[[].keys(), false],
[true, false],
[{}, false]
[[], true],
[new Array(2), true],
[Object.keys({}), true],
[[].keys(), false],
[true, false],
[{}, false]
];

describe('OfTypeArray', () => {
test.each(testData)(
'OfTypeArray: Expecting %p to be %p',
(inputValue: unknown, expectedOutput: boolean) => {
expect(OfArrayType(inputValue)).toBe(expectedOutput);
}
);
test.each(testData)(
'OfTypeArray: Expecting %p to be %p',
(inputValue: unknown, expectedOutput: boolean) => {
expect(OfArrayType(inputValue)).toBe(expectedOutput);
}
);
});

describe('NotOfArrayType', () => {
test.each(testData)(
'NotOfArrayType: Expecting %p to be %p',
(inputValue: unknown, expectedOutput: boolean) => {
expect(NotOfArrayType(inputValue)).toBe(!expectedOutput);
}
);
});
18 changes: 18 additions & 0 deletions src/lib/array.ts
Expand Up @@ -4,7 +4,25 @@
* @template T - The type of elements in the array.
* @param {unknown} arg - The argument to be checked.
* @returns {boolean} - Returns true if the argument is of type array, otherwise false.
* @example
* const value = [1, 2, 3];
* const result = OfArrayType<number>(value);
* // result is true
*/
export function OfArrayType<T>(arg: unknown): arg is T[] {
return Array.isArray(arg);
}

/**
* Checks if the provided argument is not of type array.
*
* @param {unknown} arg - The argument to be checked.
* @returns {boolean} - Returns true if the argument is not of type array, otherwise false.
* @example
* const value = 'string';
* const result = NotOfArrayType(value);
* // result is true
*/
export function NotOfArrayType(arg: unknown): boolean {
return !Array.isArray(arg);
}
32 changes: 26 additions & 6 deletions src/lib/boolean-as-string.spec.ts
@@ -1,21 +1,41 @@
import { OfBooleanTypeAsString } from './boolean-as-string';
import {NotOfBooleanTypeAsString, OfBooleanTypeAsString} from './boolean-as-string';

test('OfBooleanTypeAsString: Expecting true as a string to be true', () => {
expect(OfBooleanTypeAsString('true')).toBe(true);
expect(OfBooleanTypeAsString('true')).toBe(true);
});

test('OfBooleanTypeAsString: Expecting false as a string to be true', () => {
expect(OfBooleanTypeAsString('false')).toBe(true);
expect(OfBooleanTypeAsString('false')).toBe(true);
});

test('OfBooleanTypeAsString: Expecting xyz string to be false', () => {
expect(OfBooleanTypeAsString('xyz')).toBe(false);
expect(OfBooleanTypeAsString('xyz')).toBe(false);
});

test('OfBooleanTypeAsString: Expecting true to be false', () => {
expect(OfBooleanTypeAsString(true)).toBe(false);
expect(OfBooleanTypeAsString(true)).toBe(false);
});

test('OfBooleanTypeAsString: Expecting {} to be false', () => {
expect(OfBooleanTypeAsString({})).toBe(false);
expect(OfBooleanTypeAsString({})).toBe(false);
});

test('NotOfBooleanTypeAsString: Expecting true as a string to be false', () => {
expect(NotOfBooleanTypeAsString('true')).toBe(false);
});

test('NotOfBooleanTypeAsString: Expecting false as a string to be false', () => {
expect(NotOfBooleanTypeAsString('false')).toBe(false);
});

test('NotOfBooleanTypeAsString: Expecting xyz string to be true', () => {
expect(NotOfBooleanTypeAsString('xyz')).toBe(true);
});

test('NotOfBooleanTypeAsString: Expecting true to be true', () => {
expect(NotOfBooleanTypeAsString(true)).toBe(true);
});

test('NotOfBooleanTypeAsString: Expecting {} to be true', () => {
expect(NotOfBooleanTypeAsString({})).toBe(true);
});
20 changes: 19 additions & 1 deletion src/lib/boolean-as-string.ts
Expand Up @@ -2,11 +2,29 @@ import { OfTrueTypeAsString } from './true-as-string';
import { OfFalseTypeAsString } from './false-as-string';

/**
* Determines if the given argument is of boolean type as a string.
* Checks if the given argument is of type string and has value 'boolean'.
*
* @param {unknown} arg - The argument to check.
* @return {boolean} - Returns true if the argument is of boolean type as a string, otherwise returns false.
* @example
* const value = 'true';
* const result = OfBooleanTypeAsString(value);
* // result is true
*/
export function OfBooleanTypeAsString(arg: unknown): arg is string {
return OfTrueTypeAsString(arg) || OfFalseTypeAsString(arg);
}

/**
* Checks if the given argument is not of type string or has value not 'boolean'.
*
* @param {unknown} arg - The argument to check.
* @return {boolean} - Returns true if the argument is not of boolean type as a string, otherwise returns false.
* @example
* const value = 'string';
* const result = NotOfBooleanTypeAsString(value);
* // result is true
*/
export function NotOfBooleanTypeAsString(arg: unknown): boolean {
return !OfBooleanTypeAsString(arg);
}

0 comments on commit e08ff57

Please sign in to comment.