@crossfox/utils
v1.1.9
Published
The package is a collection of useful utilities and functions designed to streamline and enhance the development process of web applications and other software projects. This package provides developers with a set of tools for more efficient work, reducin
Maintainers
Readme
Utils by CrossFox
The @crossfox/utils package is a collection of useful utilities and functions designed to streamline and enhance the development process of web applications and other software projects. This package provides developers with a set of tools for more efficient work, reducing code duplication, and improving overall productivity.
- 🌟 Easy to use
- ⚡ High performance
- 🔗 No dependency
Install
npm install @crossfox/utilsyarn add @crossfox/utilsFunctions and Utilities
Object
isEmpty(value: any): boolean
The
isEmptyfunction checks if a value is empty (null, undefined, empty string, or empty array).value: The value to check.
Returns:
trueif the value is empty,falseotherwise.crop(obj: Record<string, any>, keys: string[], isDelete = false): Record<string, any>
The
cropfunction creates a new object containing the specified keys from the source object, with an option to delete the keys from the source object.obj: The source object.keys: An array of keys to extract.isDelete(defaultfalse): An option to delete the keys from the source object.
Returns a new object with the selected keys.
setObject(obj: Record<string, any>): (...args: any[]) => Record<string, any>
The
setObjectfunction dynamically sets keys and values in an object using a variadic function.obj: The source object.
Returns a function that accepts key-value pairs to add to the object and returns the updated object.
takeItem(arr: Record<string, any>, column: string, def = null): any
The
takeItemfunction is used to retrieve a value from an object and optionally remove it.arr: The source object.column: The key to retrieve the value.def(defaultnull): The default value to return if the key is missing.
Returns the extracted value or the default value if the key is missing.
Types
isString(value: any): boolean
The
isStringfunction checks if a value is a string.value: The value to check.
Returns
trueif the value is a string,falseotherwise.isNull(value: any): boolean
The
isNullfunction checks if a value is null.value: The value to check.
Returns
trueif the value is null,falseotherwise.isBoolean(value: any): boolean
The
isBooleanfunction checks if a value is a boolean (true or false).value: The value to check.
Returns
trueif the value is a boolean,falseotherwise.checkType(prefix: string, name: string, value: any, type: (() => void) | string): void
The
checkTypefunction validates the type of a value and compares it to the specified type or a custom validation function.prefix: The prefix for error messages.name: The name of the value.value: The value to check.type: The type to compare (string or function).
Throws a
TypeErrorexception if the type does not match the expected type.
Other
range(min: number, max: number, step: number | null): number[]
The
rangefunction generates an array of numbers within a specified range.min: The lower limit of the range.max: The upper limit of the range.step(defaultnull): The step for generating numbers. If not specified, a step of 1 is used.
Returns an array of numbers within the specified range.
parseJSON(str: string, def = {}): Record<string, any>
The
parseJSONfunction parses a JSON string and returns a JavaScript object with error handling.str: The JSON string to parse.def(default{}): The default value to return in case of parsing errors.
Returns the parsed object or the default value in case of errors.
maskValue(v: string, mask: string, clearExp = /\s+|[^0-9]+/g): string
The
maskValuefunction applies a mask to a string value, formatting it according to the provided mask.value: The string value.mask: The mask for formatting.clearExp(default/\s+|[^0-9]+/g): A regular expression for clearing the string of unnecessary characters.
Returns the formatted string.
classNames(...args: any[]): string (Added in v1.1)
The
classNamesfunction takes a variable number of arguments and combines them into a single string, separating them with spaces. It filters the provided arguments, excluding any falsy values (false,null,undefined,'')....args: Variable-length arguments representing classes.
This function is useful for dynamically generating a string of classes in various scenarios.
Returns a string containing the combined classes.
**isSimpleObject **(...args: any[]): string (Added in v1.1)
The isSimpleObject function is a utility that checks whether the given object is a plain, simple object.
Returns
trueif the value is a boolean,falseotherwise.
Author
- Oleksii Fursov @nodePro777
Example
import {
takeItem,
crop,
setObject,
isEmpty,
isString,
isNull,
isBoolean,
checkType,
range,
parseJSON,
maskValue,
classNames,
isSimpleObject
} from '@crossfox/utils';
// Example of using the takeItem function
const data = { name: 'John', age: 30 };
const name = takeItem(data, 'name');
// Example of using the crop function
const sourceObj = { name: 'Alice', age: 25, country: 'USA' };
const keysToCrop = ['name', 'age'];
const croppedObj = crop(sourceObj, keysToCrop, true);
// Example of using the setObject function
const obj = { firstName: 'Jane' };
const updatedObj = setObject(obj)('lastName', 'Doe', age, 21);
// Example of using the isEmpty function
const emptyValue = null;
const isValueEmpty = isEmpty(emptyValue);
// Example of using the isString function
const stringValue = 'Hello';
const isStringValue = isString(stringValue);
// Example of using the isNull function
const nullValue = null;
const isNullValue = isNull(nullValue);
// Example of using the isBoolean function
const booleanValue = true;
const isBooleanValue = isBoolean(booleanValue);
// Example of using the checkType function
checkType('validate', 'stringValue', stringValue, 'string');
// Uncomment the following line to see a TypeError exception, as the type 'string' does not match the nullValue variable
// checkType('validate', 'nullValue', nullValue, 'string');
// Example of using the range function
const numbersInRange = range(1, 5); // [1, 2, 3, 4, 5]
// Example of using the parseJSON function
const jsonString = '{"name": "Karina", "age": 26}';
const parsedObject = parseJSON(jsonString);
// Example of using the maskValue function
const maskedString = maskValue('12345678', 'XXX-XXX-XXX'); // 123-456-789
// Example of using the classNames function
const maskedString = classNames(props.className, false && 'active', 'container'); // 'classWithProps container'
// Example of using the isSimpleObject
if(isSimpleObject({name: 'Alex'})){
console.log("This is a plain object.");
}