ts-primitive-types
v1.0.3
Published
TypeScript static methods to validate primitive and array types
Downloads
14
Maintainers
Readme
ts-primitive-types
Description
This project provides a set of types and classes in TypeScript to work with booleans, numbers, and strings.
It is designed to facilitate the manipulation of these types in TypeScript applications, offering a consistent and easy-to-use API.
Prerequisites
- Node.js (>= version
22.0.0
) - npm (>= version
10.5.0
)
Installation with npm
To install the package, run:
npm install ts-primitive-types
Services you will use
ArrayBoolean methods
allBooleans(arr: boolean[]): boolean
countTrue(arr: boolean[]): number
countFalse(arr: boolean[]): number
allTrue(arr: boolean[]): boolean
allFalse(arr: boolean[]): boolean
anyTrue(arr: boolean[]): boolean
anyFalse(arr: boolean[]): boolean
toggle(arr: boolean[]): boolean[]
and(arr: boolean[]): boolean
or(arr: boolean[]): boolean
Usage:
const boolArray = [true, false, true, false, true];
// Check if all elements are booleans
console.log(ArrayBoolean.allBooleans(boolArray)); // Output: true
// Count the number of true values
console.log(ArrayBoolean.countTrue(boolArray)); // Output: 3
// Count the number of false values
console.log(ArrayBoolean.countFalse(boolArray)); // Output: 2
// Check if all elements are true
console.log(ArrayBoolean.allTrue(boolArray)); // Output: false
// Check if all elements are false
console.log(ArrayBoolean.allFalse(boolArray)); // Output: false
// Check if any element is true
console.log(ArrayBoolean.anyTrue(boolArray)); // Output: true
// Check if any element is false
console.log(ArrayBoolean.anyFalse(boolArray)); // Output: true
// Toggle all elements in the array
console.log(ArrayBoolean.toggle(boolArray)); // Output: [false, true, false, true, false]
// Logical AND of all elements (same as allTrue)
console.log(ArrayBoolean.and(boolArray)); // Output: false
// Logical OR of all elements (same as anyTrue)
console.log(ArrayBoolean.or(boolArray)); // Output: true
ArrayNumber methods
allNumbers(arr: number[]): boolean
uniqueNumbers(arr: number[]): boolean
sum(arr: number[]): number
average(arr: number[]): number
max(arr: number[]): number
min(arr: number[]): number
sortByAscending(arr: number[]): number[]
sortByDescending(arr: number[]): number[]
filterGreaterThan(arr: number[], threshold: number): number[]
Usage:
// Test array
const numArray = [3, 7, 1, 9, 4, 1];
// allNumbers: Check if all elements are numbers
console.log(ArrayNumber.allNumbers(numArray)); // Output: true
// uniqueNumbers: Check if all elements are unique
console.log(ArrayNumber.uniqueNumbers(numArray)); // Output: false
// sum: Calculate the sum of all elements
console.log(ArrayNumber.sum(numArray)); // Output: 25
// average: Calculate the average of all elements
console.log(ArrayNumber.average(numArray)); // Output: 4.166666666666667
// max: Find the maximum value in the array
console.log(ArrayNumber.max(numArray)); // Output: 9
// min: Find the minimum value in the array
console.log(ArrayNumber.min(numArray)); // Output: 1
// sortByAscending: Sort the array in ascending order
console.log(ArrayNumber.sortByAscending(numArray)); // Output: [1, 1, 3, 4, 7, 9]
// sortByDescending: Sort the array in descending order
console.log(ArrayNumber.sortByDescending(numArray)); // Output: [9, 7, 4, 3, 1, 1]
// filterGreaterThan: Filter elements greater than a specified threshold
console.log(ArrayNumber.filterGreaterThan(numArray, 4)); // Output: [7, 9]
ArrayObject methods
count(arr: T[]): number
toArray(set: Set<T>): T[]
allObjects(arr: T[]): boolean
merge(arr1: T[], arr2: T[]): T[]
sortByProperty(arr: T[], prop: K): T[]
filterByProperty(arr: T[], prop: K, value: T[K]): T[]
findFirst(arr: T[], prop: K, value: T[K]): T | undefined
Usage:
// Test array of objects
const objArray = [
{ id: 1, name: 'John', age: 30 },
{ id: 2, name: 'Jane', age: 25 },
{ id: 3, name: 'Doe', age: 35 },
{ id: 4, name: 'Alice', age: 28 },
];
// allObjects: Check if all elements are valid objects
ArrayObject.allObjects(objArray);
// Output: true
// filterByProperty: Filter objects by a specific property value
ArrayObject.filterByProperty(objArray, 'name', 'Jane');
// Output:
[{ id: 2, name: 'Jane', age: 25 }]
// sortByProperty: Sort objects by a specific property
ArrayObject.sortByProperty(objArray, 'age');
// Output:
[
{ id: 2, name: "Jane", age: 25 },
{ id: 4, name: "Alice", age: 28 },
{ id: 1, name: "John", age: 30 },
{ id: 3, name: "Doe", age: 35 },
];
// count: Count the number of objects
console.log(ArrayObject.count(objArray)); // Output: 4
// toArray: Convert a set of objects to an array
const objSet = new Set(objArray);
console.log(ArrayObject.toArray(objSet));
// Output:
[
{ id: 1, name: "John", age: 30 },
{ id: 2, name: "Jane", age: 25 },
{ id: 3, name: "Doe", age: 35 },
{ id: 4, name: "Alice", age: 28 },
];
// merge: Merge two arrays of objects
const objArray2 = [
{ id: 5, name: 'Bob', age: 32 },
{ id: 6, name: 'Eve', age: 27 },
];
console.log(ArrayObject.merge(objArray, objArray2));
// Output:
[
{ id: 1, name: "John", age: 30 },
{ id: 2, name: "Jane", age: 25 },
{ id: 3, name: "Doe", age: 35 },
{ id: 4, name: "Alice", age: 28 },
{ id: 5, name: "Bob", age: 32 },
{ id: 6, name: "Eve", age: 27 },
];
// findFirst: Find the first object with a specific property value
console.log(ArrayObject.findFirst(objArray, 'id', 3));
// Output:
{ id: 3, name: 'Doe', age: 35 }
ArrayString methods
allStrings(arr: string[]): boolean
uniqueStrings(arr: string[]): boolean
length(arr: string[]): number
setToArray(set: Set<string>): string[]
sortByLength(arr: string[]): string[]
reverse(arr: string[]): string[]
concatenate(arr1: string[], arr2: string[]): string[]
filterByPrefix(arr: string[], prefix: string): string[]
Usage:
// Test array of strings
const strArray = ['apple', 'banana', 'cherry', 'date', 'banana'];
// allStrings: Check if all elements are strings
console.log(ArrayString.allStrings(strArray)); // Output: true
// uniqueStrings: Check if all elements are unique strings
console.log(ArrayString.uniqueStrings(strArray)); // Output: false
// length: Get the length of the array
console.log(ArrayString.length(strArray)); // Output: 5
// setToArray: Convert a set of strings to an array
const strSet = new Set(strArray);
console.log(ArrayString.setToArray(strSet)); // Output:
['apple', 'banana', 'cherry', 'date']
// sortByLength: Sort strings by their length
console.log(ArrayString.sortByLength(strArray)); // Output:
['date', 'apple', 'banana', 'cherry', 'banana']
// reverse: Reverse the order of strings in the array
console.log(ArrayString.reverse(strArray)); // Output:
['banana', 'date', 'cherry', 'banana', 'apple']
// concatenate: Concatenate two arrays of strings
const strArray2 = ['elderberry', 'fig'];
console.log(ArrayString.concatenate(strArray, strArray2)); // Output:
['apple', 'banana', 'cherry', 'date', 'banana', 'elderberry', 'fig']
// filterByPrefix: Filter strings by a prefix
console.log(ArrayString.filterByPrefix(strArray, 'b')); // Output:
['banana', 'banana']
TypeBool methods
IsBoolean(x: any): boolean
IsNotNullOrUndefined(x: any): boolean
IsTrue(x: boolean): boolean
IsFalse(x: boolean): boolean
ToBoolean(x: any): boolean
IsStrictBoolean(x: any): boolean
IsTruthy(x: any): boolean
IsFalsy(x: any): boolean
Usage:
// Test values
const value1 = true;
const value2 = false;
const value3 = 0;
const value4 = null;
const value5 = undefined;
// IsBoolean: Check if a value is a boolean
console.log(TypeBoolean.IsBoolean(value1)); // Output: true
// IsNotNullOrUndefined: Check if a value is neither null nor undefined
console.log(TypeBoolean.IsNotNullOrUndefined(value1)); // Output: true
console.log(TypeBoolean.IsNotNullOrUndefined(value4)); // Output: false
// IsTrue: Check if a boolean value is true
console.log(TypeBoolean.IsTrue(value1)); // Output: true
// IsFalse: Check if a boolean value is false
console.log(TypeBoolean.IsFalse(value2)); // Output: true
// ToBoolean: Convert a value to a boolean
console.log(TypeBoolean.ToBoolean(value3)); // Output: false
// IsStrictBoolean: Check if a value is strictly a boolean (true or false)
console.log(TypeBoolean.IsStrictBoolean(value1)); // Output: true
console.log(TypeBoolean.IsStrictBoolean(value3)); // Output: false
// IsTruthy: Check if a value is truthy (convertible to true)
console.log(TypeBoolean.IsTruthy(value1)); // Output: true
console.log(TypeBoolean.IsTruthy(value3)); // Output: false
// IsFalsy: Check if a value is falsy (convertible to false)
console.log(TypeBoolean.IsFalsy(value2)); // Output: true
console.log(TypeBoolean.IsFalsy(value3)); // Output: true
TypeClass methods
HasMethod(classInstance: any, methodName: string): boolean
HasProperty(classInstance: any, propertyName: string): boolean
InheritsFrom(classInstance: any, baseClass: Function): boolean
HasMethods(classInstance: any, methodNames: string[]): boolean
HasProperties( classInstance: any, propertyNames: string[] ): boolean
Usage:
// Example class
class ExampleClass {
public method1() {
console.log('Method 1');
}
public method2() {
console.log('Method 2');
}
public property1 = 'Property 1';
}
// Instantiate the example class
const exampleInstance = new ExampleClass();
// Test method and property names
const methodNames = ['method1', 'method2'];
const propertyNames = ['property1', 'property2']; // property2 does not exist
// HasMethod: Check if the instance has a specific method
console.log(TypeClass.HasMethod(exampleInstance, 'method1')); // Output: true
// HasProperty: Check if the instance has a specific property
console.log(TypeClass.HasProperty(exampleInstance, 'property1')); // Output: true
// InheritsFrom: Check if the instance inherits from a specific base class
console.log(TypeClass.InheritsFrom(exampleInstance, ExampleClass)); // Output: true
// HasMethods: Check if the instance has all specified methods
console.log(TypeClass.HasMethods(exampleInstance, methodNames)); // Output: true
// HasProperties: Check if the instance has all specified properties
console.log(TypeClass.HasProperties(exampleInstance, propertyNames)); // Output: false
TypeNumber methods
IsNumber(x: any): boolean
IsNotNaN(num: number): boolean
IsInteger(num: number): boolean
IsPositive(num: number): boolean
IsNegative(num: number): boolean
IsInRange(num: number, min: number, max: number): boolean
IsOdd(num: number): boolean
IsEven(num: number): boolean
MaxDecimals(num: number, maxDecimals: number): boolean
Usage:
// Test numbers
const num1 = 42;
const num2 = 3.14;
const num3 = NaN;
const num4 = -10;
const num5 = 7;
// IsNumber: Check if a value is a number and not NaN
console.log(TypeNumber.IsNumber(num1)); // Output: true
console.log(TypeNumber.IsNumber(num3)); // Output: false
// IsNotNaN: Check if a number is not NaN
console.log(TypeNumber.IsNotNaN(num2)); // Output: true
console.log(TypeNumber.IsNotNaN(num3)); // Output: false
// IsInteger: Check if a number is an integer
console.log(TypeNumber.IsInteger(num1)); // Output: true
console.log(TypeNumber.IsInteger(num2)); // Output: false
// IsPositive: Check if a number is positive
console.log(TypeNumber.IsPositive(num1)); // Output: true
console.log(TypeNumber.IsPositive(num4)); // Output: false
// IsNegative: Check if a number is negative
console.log(TypeNumber.IsNegative(num4)); // Output: true
console.log(TypeNumber.IsNegative(num1)); // Output: false
// IsInRange: Check if a number is within a specified range
console.log(TypeNumber.IsInRange(num5, 1, 10)); // Output: true
console.log(TypeNumber.IsInRange(num4, 0, 5)); // Output: false
// IsOdd: Check if a number is odd
console.log(TypeNumber.IsOdd(num5)); // Output: true
console.log(TypeNumber.IsOdd(num4)); // Output: false
// IsEven: Check if a number is even
console.log(TypeNumber.IsEven(num4)); // Output: true
console.log(TypeNumber.IsEven(num5)); // Output: false
// MaxDecimals: Check if a number has a maximum number of decimal places
console.log(TypeNumber.MaxDecimals(num2, 2)); // Output: true
console.log(TypeNumber.MaxDecimals(num2, 0)); // Output: false
TypeString methods
IsString(x: any): boolean
IsNotEmpty(str: string): boolean
NoSpecialCharacters(str: string): boolean
FirstLetterUppercase(str: string): boolean
MinLength(str: string, min: number): boolean
MaxLength(str: string, max: number): boolean
OnlyLetters(str: string): boolean
OnlyNumbers(str: string): boolean
IsAlphanumeric(str: string): boolean
IsEmail(str: string): boolean
NoConsecutiveSpaces(str: string): boolean
NoLeadingOrTrailingSpaces(str: string): boolean
Usage:
// Test strings
const str1 = 'Hello';
const str2 = '12345';
const str3 = 'abc123';
const str4 = ' Valid String ';
const str5 = 'invalid_email@example';
// IsString: Check if a value is a string
console.log(TypeString.IsString(str1)); // Output: true
console.log(TypeString.IsString(123)); // Output: false
// IsNotEmpty: Check if a string is not empty (after trimming)
console.log(TypeString.IsNotEmpty(str1)); // Output: true
console.log(TypeString.IsNotEmpty(' ')); // Output: false
// NoSpecialCharacters: Check if a string contains no special characters
console.log(TypeString.NoSpecialCharacters(str2)); // Output: true
console.log(TypeString.NoSpecialCharacters(str3)); // Output: true
console.log(TypeString.NoSpecialCharacters(str4)); // Output: false
// FirstLetterUppercase: Check if the first letter of a string is uppercase
console.log(TypeString.FirstLetterUppercase(str1)); // Output: true
console.log(TypeString.FirstLetterUppercase('lowercase')); // Output: false
// MinLength: Check if a string has at least a specified minimum length
console.log(TypeString.MinLength(str1, 5)); // Output: true
console.log(TypeString.MinLength(str2, 6)); // Output: false
// MaxLength: Check if a string does not exceed a specified maximum length
console.log(TypeString.MaxLength(str1, 10)); // Output: true
console.log(TypeString.MaxLength(str2, 4)); // Output: false
// OnlyLetters: Check if a string contains only letters
console.log(TypeString.OnlyLetters(str1)); // Output: true
console.log(TypeString.OnlyLetters(str2)); // Output: false
// OnlyNumbers: Check if a string contains only numbers
console.log(TypeString.OnlyNumbers(str2)); // Output: true
console.log(TypeString.OnlyNumbers(str3)); // Output: false
// IsAlphanumeric: Check if a string is alphanumeric (only letters and numbers)
console.log(TypeString.IsAlphanumeric(str2)); // Output: true
console.log(TypeString.IsAlphanumeric(str3)); // Output: true
console.log(TypeString.IsAlphanumeric(str4)); // Output: false
// IsEmail: Check if a string is a valid email address
console.log(TypeString.IsEmail('[email protected]')); // Output: true
console.log(TypeString.IsEmail(str5)); // Output: false
// NoConsecutiveSpaces: Check if a string has no consecutive spaces
console.log(TypeString.NoConsecutiveSpaces(str4)); // Output: false
console.log(TypeString.NoConsecutiveSpaces('No consecutive spaces')); // Output: true
// NoLeadingOrTrailingSpaces: Check if a string has no leading or trailing spaces
console.log(TypeString.NoLeadingOrTrailingSpaces(str1)); // Output: true
console.log(TypeString.NoLeadingOrTrailingSpaces(str4)); // Output: false