Library that has complex utility functions used to handle complex utility operations in any react project.
Installation guide for @xmanscript/utils, a versatile npm package that provides various utilities for your JavaScript projects.
It is not recommended to add it to your project's devDependencies
.
Before you can install and use @xmanscript/utils, ensure that you have the following prerequisites installed on your system:
- Node.js: Download Node.js
- npm: npm is included with Node.js, so there's no need to install it separately.
You can install @xmanscript/utils using one of the following package managers: npm, yarn, or pnpm.
To install @xmanscript/utils using npm, open your terminal and run the following command:
npm install @xmanscript/utils
To install @xmanscript/utils using npm, open your terminal and run the following command:
yarn add @xmanscript/utils
To install @xmanscript/utils using npm, open your terminal and run the following command:
pnpm add @xmanscript/utils
If you're using @xmanscript/utils in a TypeScript project, you'll need to add a specific configuration to your tsconfig.json
file. This is necessary because the TypeScript compiler (TSC) might not automatically detect the declaration files of the package.
To ensure proper recognition of declaration files, follow these steps:
-
Open your
tsconfig.json
file in your project. -
Add the following configuration within the
"compilerOptions"
section:
{
"compilerOptions": {
"baseUrl": "./",
"paths": {
"@xmanscript/utils/*": ["./node_modules/@xmanscript/utils/@types/*"]
}
}
}
This configuration tells TypeScript where to find the declaration files for @xmanscript/utils. With this setup, TypeScript will be able to properly utilize the types provided by the package.
Note: The path ./node_modules/@xmanscript/utils/@types/*
depends upon the baseUrl
. For example; if the baseUrl
is ./src/
then the path wll be ../node_modules/@xmanscript/utils/@types/*
.
The idea is to point to the location that contains decleration files.
- partitionObjectsByKey
- convertNestedKeysToObject
- markCheckedByStringMatch
- intersectObjects
- containsBinaryData
- isJSONObject
- objectToFormDataWithFiles
- areObjectsEqual
- abbreviateCurrencyValue
- omitKey
- setKeysToValueInObjects
- toggleStringInArray
- toggleObjectInArray
- calculateAndInjectPercentageByMaxValue
- calculateAndInjectPercentageBySum
- calculateSumOfKey
- setValueOfKeyForMatchingValuesOfAKey
- scrollToComponent
- distributePercentageEquallyWithRemainder
- splitArrayIntoChunks
- getFileExtension
- removeObjectFromArray
- parseToBoolean
- groupArrayOfObjectsByValueOfAKey
- countKeyOccurrences
- distributePercentageEquall
- uniqueValuesOfKey
- formatNumberToCommaString
- getMinMax
- getDifferenceObject (new)
- strictIntersectObjects (new)
The partitionObjectsByKey
function splits an array of objects into two separate arrays based on the presence of a specified key.
arr
(Type:Record<string, any>[]
): An array of objects, where each object has a string key and any value.key
(Type:string
): The key to be used for splitting the objects.
[Record<string, any>[], Record<string, any>[]]
: Returns an array containing two elements. The first element is an array of objects that have the specifiedkey
, and the second element is an array of objects that do not have the specifiedkey
.
import partitionObjectsByKey from '@xmanscript/utils/partitionObjectsByKey';
const array = [
{ id: 1, name: 'Alice' },
{ id: 2, name: 'Bob', iteratorId: 'x1' },
{ id: 3, name: 'Charlie' },
{ id: 4, name: 'David', iteratorId: 'x2' },
];
const [withKey, withoutKey] = partitionObjectsByKey(array, 'iteratorId');
console.log(withKey);
// Result: [
// { id: 2, name: 'Bob', iteratorId: 'x1' },
// { id: 4, name: 'David', iteratorId: 'x2' },
// ]
console.log(withoutKey);
// Result: [
// { id: 1, name: 'Alice' },
// { id: 3, name: 'Charlie' },
// ]
The convertNestedKeysToObject
function converts an input object with nested keys in the format of "name[index].nestedKey" into an output object with nested arrays and objects.
input
(Type:Record<string, any>
): A JavaScript object with string keys and any values.
any
: Returns an object with the converted input. The input is an object with string keys and any values. The function converts any keys that match the pattern of "name[index].nestedKey" into nested objects within an array. The function returns the converted object.
import convertNestedKeysToObject from '@xmanscript/utils/convertNestedKeysToObject';
const input = {
'user[0].name': 'Alice',
'user[0].age': 30,
'user[1].name': 'Bob',
'user[1].age': 25,
status: 'active',
};
const convertedObject = convertNestedKeysToObject(input);
// Result:
// {
// user: [
// { name: 'Alice', age: 30 },
// { name: 'Bob', age: 25 },
// ],
// status: 'active',
// }
The markCheckedByStringMatch
function adds a "checked" field to each object in an array based on whether a corresponding string value is included in another array.
objects
(Type:T[]
): An array of objects of typeT
.strings
(Type:string[]
): An array of strings to be used for checking against the values of thechooseKey
property in each object.chooseKey
(Type:keyof T
): The key of the property in the objects that you want to compare with thestrings
array. It can be any valid key of the objects in theobjects
array.
T[]
: Returns an array of objects of typeT
with an additionalchecked
field.
import markCheckedByStringMatch from '@xmanscript/utils/markCheckedByStringMatch';
interface Item {
id: number;
name: string;
}
const items: Item[] = [
{ id: 1, name: 'Apple' },
{ id: 2, name: 'Banana' },
{ id: 3, name: 'Cherry' },
];
const selectedItems: string[] = ['Banana', 'Cherry'];
const itemsWithCheckedField = markCheckedByStringMatch(items, selectedItems, 'name');
// Result:
// [
// { id: 1, name: 'Apple', checked: false },
// { id: 2, name: 'Banana', checked: true },
// { id: 3, name: 'Cherry', checked: true },
// ]
The intersectObjects
function takes two objects as input and returns a new object containing only the properties that exist in both input objects.
obj1
(Type:Record<string, any>
): An object that can have any number of properties of any type.obj2
(Type:Record<string, any>
): A record object that contains key-value pairs.
Record<string, any>
: Returns a new object that contains the intersection of properties betweenobj1
andobj2
.
import intersectObjects from '@xmanscript/utils/intersectObjects';
const object1 = {
name: 'John',
age: 30,
country: 'USA',
};
const object2 = {
age: 30,
city: 'New York',
};
const intersection = intersectObjects(object1, object2);
// Result: { age: 30 }
The containsBinaryData
function recursively checks if an object or any of its nested properties contain binary data.
obj
(Type:Record<string, any>
): An object with string keys and values of any type.
boolean
: Returnstrue
if the object or any of its nested properties contains binary data (e.g.,Blob
,File
, orArrayBuffer
), otherwise returnsfalse
.
import containsBinaryData from '@xmanscript/utils/containsBinaryData';
const objectWithBinaryData = {
image: new Blob(['binary data'], { type: 'image/png' }),
document: new File(['binary data'], 'document.pdf', { type: 'application/pdf' }),
};
const objectWithoutBinaryData = {
name: 'John',
age: 30,
};
const hasBinary = containsBinaryData(objectWithBinaryData);
console.log(hasBinary); // Result: true
const hasNoBinary = containsBinaryData(objectWithoutBinaryData);
console.log(hasNoBinary); // Result: false
The isJSONObject
function checks if a given string is a valid JSON object.
value
(Type:string
): The string to validate as a JSON object.
boolean
: Returnstrue
if the input string represents a valid JSON object, andfalse
otherwise.
import isJSONObject from '@xmanscript/utils/isJSONObject';
const jsonString1 = '{"name": "John", "age": 30}';
const jsonString2 = 'This is not a JSON object';
console.log(isJSONObject(jsonString1)); // true
console.log(isJSONObject(jsonString2)); // false
The objectToFormDataWithFiles
function converts a JSON object into a FormData object, handling file uploads and nested objects.
obj
(Type:Record<string, any>
): The object containing key-value pairs. The keys represent the names of the form fields, and the values represent the corresponding values for those fields. Values can be of any type, including strings, numbers, booleans, arrays, or objects.
FormData
: Returns a FormData object representing the converted data.
import objectToFormDataWithFiles from '@xmanscript/utils/objectToFormDataWithFiles';
const formData = objectToFormDataWithFiles({
name: 'John',
age: 30,
file: [new File(['data'], 'profile.jpg')],
address: {
street: '123 Main St',
city: 'New York',
},
});
// Use `formData` in a fetch request or form submission.
The areObjectsEqual
function checks if two objects are equal by comparing their keys and values.
obj1
(Type:any
): The first object to compare.obj2
(Type:any
): The second object to compare withobj1
to check if they are equal.
boolean
: Returnstrue
if the two objects are equal (have the same keys and corresponding values), andfalse
otherwise.
import areObjectsEqual from '@xmanscript/utils/areObjectsEqual';
const obj1 = { name: 'John', age: 30 };
const obj2 = { age: 30, name: 'John' };
const obj3 = { name: 'Alice', age: 25 };
console.log(areObjectsEqual(obj1, obj2)); // true
console.log(areObjectsEqual(obj1, obj3)); // false
The abbreviateCurrencyValue
function converts a number into a currency system by abbreviating it with "B" for billions, "M" for millions, and "K" for thousands.
labelValue
(Type:number
): The number to convert into a currency system. It represents a value in the currency system.
string | number
: Returns a string if the number is in the currency system (e.g., "1.5 B", "7.5 M", "2.5 K"), and returns a number if it's not in the specified range.
import abbreviateCurrencyValue from '@xmanscript/utils/abbreviateCurrencyValue';
const value1 = 1500000000; // 1.5 billion
const value2 = 7500000; // 7.5 million
const value3 = 2500; // 2.5 thousand
const value4 = 42; // 42
console.log(abbreviateCurrencyValue(value1)); // "1.5 B"
console.log(abbreviateCurrencyValue(value2)); // "7.5 M"
console.log(abbreviateCurrencyValue(value3)); // "2.5 K"
console.log(abbreviateCurrencyValue(value4)); // 42
The omitKey
function removes a specified key from an object and returns a new object without that key.
obj
(Type:T
): The object from which you want to remove a key. It can be of any typeT
.key
(Type:K
): The key of the property to remove from the object. It should be a valid key of the object's typeT
.
Omit<T, K>
: Returns an object of typeOmit<T, K>
, which is the original objectobj
with the specifiedkey
removed.
import omitKey from '@xmanscript/utils/omitKey';
const user = {
id: 1,
name: 'John',
age: 30,
};
const userWithoutId = omitKey(user, 'id');
// Result: { name: 'John', age: 30 }
The setKeysToValueInObjects
function takes an array of objects and an array of keys, and returns a new array of objects where the specified keys are assigned the value of zero.
arr
(Type:Record<string, any>[]
): An array of objects, where each object represents a record with key-value pairs.keys
(Type:string[]
): An array of strings representing the keys that need to be assigned thevalue
in each object in the array.value
(Type:any
): A value that need to be assigned in each object in the array.
Record<string, any>[]
: Returns an array of objects where the specified keys are assigned the specified value.
import setKeysToValueInObjects from '@xmanscript/utils/setKeysToValueInObjects';
const data = [
{ id: 1, value1: 10, value2: 20 },
{ id: 2, value1: 30, value2: 40 },
];
const newData = setKeysToValueInObjects(data, ['value1', 'value2', 0]);
// Result: [
// { id: 1, value1: 0, value2: 0 },
// { id: 2, value1: 0, value2: 0 },
// ]
The toggleStringInArray
function removes the target string from the array if it exists, otherwise, it adds the target string to the array.
arr
(Type:string[]
): An array of strings.targetString
(Type:string
): The string that you want to either remove from the array or add to the array.
string[]
: Returns an array of strings after the operation. If the target string existed, it is removed; if not, it is added.
import toggleStringInArray from '@xmanscript/utils/toggleStringInArray';
const myArray = ['apple', 'banana', 'cherry'];
// Remove 'banana' from the array
const newArray1 = toggleStringInArray(myArray, 'banana');
// Result: ['apple', 'cherry']
// Add 'strawberry' to the array
const newArray2 = toggleStringInArray(myArray, 'strawberry');
// Result: ['apple', 'banana', 'cherry', 'strawberry']
The toggleObjectInArray
function removes the target object from the array if it exists, otherwise, it adds it to the array. The equality comparison is done using the isEqual
function.
arr
(Type:T[]
): An array of objects of typeT
.targetObject
(Type:T
): The object that you want to either remove from the array if it exists or add to the array if it doesn't exist.
T[]
: Returns the updated array after either removing or adding the target object.
import toggleObjectInArray from '@xmanscript/utils/toggleObjectInArray';
const data = [
{ id: 1, name: 'Alice' },
{ id: 2, name: 'Bob' },
];
// Remove the object with id: 2
const updatedData1 = toggleObjectInArray(data, { id: 2, name: 'Bob' });
// Result: [{ id: 1, name: 'Alice' }]
// Add a new object
const updatedData2 = toggleObjectInArray(data, { id: 3, name: 'Charlie' });
// Result: [{ id: 1, name: 'Alice' }, { id: 3, name: 'Charlie' }]
The calculateAndInjectPercentageByMaxValue
function calculates the percentage of a specific key's value in each object of an array and injects the percentage value into each object based on the max value of that key.
arr
(Type:Record<string, any>[]
): An array of objects. Each object represents a data entry and contains various properties.key
(Type:string
): The string representing the key or property name in each object of thearr
array. This key is used to access the corresponding value in each object for calculating the percentage.
Record<string, any>[]
: Returns the modified arrayarr
with an additionalpercentage
property added to each object.
import calculateAndInjectPercentageByMaxValue from '@xmanscript/utils/calculateAndInjectPercentageByMaxValue';
const data = [
{ name: 'A', value: 50 },
{ name: 'B', value: 75 },
{ name: 'C', value: 100 },
];
const newData = calculateAndInjectPercentageByMaxValue(data, 'value');
// Result:
// [
// { name: 'A', value: 50, percentage: 50 },
// { name: 'B', value: 75, percentage: 75 },
// { name: 'C', value: 100, percentage: 100 },
// ]
The calculateAndInjectPercentageBySum
function calculates the percentage of a specific key's value in each object of an array and injects the percentage value into each object based on the sum of values of the key.
arr
(Type:Record<string, any>[]
): An array of objects. Each object represents a data entry and contains various properties.key
(Type:string
): The string representing the key or property name in each object of thearr
array. This key is used to access the corresponding value in each object for calculating the percentage.
Record<string, any>[]
: Returns the modified arrayarr
with an additionalpercentage
property added to each object.
import calculateAndInjectPercentageBySum from '@xmanscript/utils/calculateAndInjectPercentageBySum';
const data = [
{ name: 'A', value: 25 },
{ name: 'B', value: 25 },
{ name: 'C', value: 25 },
{ name: 'D', value: 25 },
];
const newData = calculateAndInjectPercentageBySum(data, 'value');
// Result:
// [
// { name: 'A', value: 25, percentage: 25 },
// { name: 'B', value: 25, percentage: 25 },
// { name: 'C', value: 25, percentage: 25 },
// { name: 'D', value: 25, percentage: 25 },
// ]
The calculateSumOfKey
function calculates the sum of a specified key in an array of objects.
data
(Type:Record<string, any>[]
): An array of objects where each object has one or more key-value pairs.key
(Type:string
): A string representing the key of the property in each object of thedata
array that you want to sum up.
number
: Returns the sum of the values of the specifiedkey
in an array of objects. If the array is empty, it returns 0.
import calculateSumOfKey from '@xmanscript/utils/calculateSumOfKey';
const data = [{ value: 10 }, { value: 20 }, { value: 30 }];
const total = calculateSumOfKey(data, 'value');
// Result: 60
The setValueOfKeyForMatchingValuesOfAKey
function changes the 'value' property of objects in an array if their specified key-value pair matches the provided values. The 'value' property of matching objects will be set to zero.
arr
(Type:Record<string, any>[]
): An array of objects where each object has key-value pairs.key
(Type:string
): A string representing the key or property name of the object in the array that you want to check for a specific value.values
(Type:any[]
): An array of values to be matched against the value of the specified key in each object. Objects with matching key-value pairs will have their 'value' property set to zero.updateKey
(Type:string
): A key that need to be updated in each object that matches the criteria in the array.value
(Type:any
): A value that need to be assigned in each object in the array.
Record<string, any>[]
: Returns the modified array where the 'value' property of matching objects is set to zero.
import setValueOfKeyForMatchingValuesOfAKey from '@xmanscript/utils/setValueOfKeyForMatchingValuesOfAKey';
const data = [
{ name: 'A', code: 'x', value: 100 },
{ name: 'B', code: 'y' },
{ name: 'C', code: 'x' },
];
const modifiedData = setValueOfKeyForMatchingValuesOfAKey(data, 'code', ['x'], 'value', 50);
// Result:
// [
// { name: 'A', code: 'x', value: 50 },
// { name: 'B', code: 'y' },
// { name: 'C', code: 'x', value: 50 },
// ]
The scrollToComponent
function scrolls to a specified component on the page and optionally focuses on it after scrolling.
props
(Type:scrollToComponentProps
): An object with two properties:componentId
(Type:string
): The id of the component to scroll to.focusAfterScroll
(Type:boolean
): A boolean indicating whether to focus on the component after scrolling.scrollDelay
(Type:number
): Delay time to scrollfocusDelay
(Type:number
): Delay time to focus
- No return value.
import scrollToComponent from '@xmanscript/utils/scrollToComponent';
// Scroll to component with id "myComponent" and focus on it after scrolling. There is no delay in scrolling and focusing by default.
scrollToComponent({ componentId: 'myComponent', focusAfterScroll: true, scrollDelay: 100, focusDelay: 100 });
The distributePercentageEquallyWithRemainder
function divides a given percentage into equal parts and distributes any remaining percentage across the parts.
array
(Type:Record<string, any>[]
): An array of objects, where each object represents a part that needs to be divided into equal parts. Each object should have apercentage
property.
Record<string, any>[]
: Returns an array of objects, where each object represents a part and contains apercentage
property.
import distributePercentageEquallyWithRemainder from '@xmanscript/utils/distributePercentageEquallyWithRemainder';
const partsToDivide = [{ id: 30 }, { id: 20 }, { id: 25 }];
const dividedParts = distributePercentageEquallyWithRemainder(partsToDivide);
// Result: [
// { id: 30, percentage:33 },
// { id: 20, percentage:33 },
// { id: 25, percentage:34 },]
The splitArrayIntoChunks
function splits an array into multiple arrays of a specified size.
array
(Type:Record<string, any>[]
): An array of objects, where each object has string keys and any values.size
(Type:number
): The desired size of each subarray. It determines how many elements should be included in each subarray when splitting the original array.
Record<string, any>[][]
: Returns an array of arrays. Each inner array contains a subset of the original array, with each subset having a maximum size specified by thesize
parameter.
import splitArrayIntoChunks from '@xmanscript/utils/splitArrayIntoChunks';
const originalArray = [
{ id: 1, name: 'Item 1' },
{ id: 2, name: 'Item 2' },
{ id: 3, name: 'Item 3' },
{ id: 4, name: 'Item 4' },
{ id: 5, name: 'Item 5' },
];
const subarrays = splitArrayIntoChunks(originalArray, 2);
// Result:
// [
// [{ id: 1, name: 'Item 1' }, { id: 2, name: 'Item 2' }],
// [{ id: 3, name: 'Item 3' }, { id: 4, name: 'Item 4' }],
// [{ id: 5, name: 'Item 5' }],
// ]
The getFileExtension
function takes a URL as input and returns the file extension of the URL as a lowercase string. If the URL is undefined or empty, it returns an empty string. If no file extension is found in the URL, it returns null.
url
(Type:string | undefined
): A string that represents the URL of a file.
string | null
: Returns the file extension of the given URL as a lowercase string. If the URL is undefined or empty, an empty string is returned. If no file extension is found in the URL, null is returned.
import getFileExtension from '@xmanscript/utils/getFileExtension';
const url1 = 'https://example.com/image.jpg';
const url2 = 'https://example.com/document.pdf';
const url3 = 'https://example.com/file-without-extension';
const extension1 = getFileExtension(url1); // Result: 'jpg'
const extension2 = getFileExtension(url2); // Result: 'pdf'
const extension3 = getFileExtension(url3); // Result: null
const extension4 = getFileExtension(undefined); // Result: ''
The removeObjectFromArray
function removes a specified object from an array of objects based on a deep comparison.
objects
(Type:Record<string, any>[]
): An array of objects.object
(Type:Record<string, any>
): The object that you want to remove from theobjects
array.
Record<string, any>[]
: Returns the updated array after removing the specified object. If the object is not found in the array, the original array is returned unchanged.
import removeObjectFromArray from '@xmanscript/utils/removeObjectFromArray';
const array = [
{ id: 1, name: 'Alice' },
{ id: 2, name: 'Bob' },
{ id: 3, name: 'Charlie' },
];
const objectToRemove = { id: 2, name: 'Bob' };
const updatedArray = removeObjectFromArray(array, objectToRemove);
console.log(updatedArray);
// Result: [
// { id: 1, name: 'Alice' },
// { id: 3, name: 'Charlie' },
// ]
The parseToBoolean
function takes a string value and returns a boolean value based on whether the string is equal to "true."
val
(Type:string
): A string that represents a boolean value.
boolean
: Returns a boolean value. If the input string is equal to 'true', it will return true. Otherwise, it will return false.
import parseToBoolean from '@xmanscript/utils/parseToBoolean';
const result1 = parseToBoolean('true');
// Result: true
const result2 = parseToBoolean('false');
// Result: false
The groupArrayOfObjectsByValueOfAKey
function takes an array of objects and a key, and groups the objects based on the similarity of their values for that key.
arr
(Type:Record<string, any>[]
): An array of objects. Each object in the array has properties with key-value pairs.key
(Type:string
): Thekey
parameter is a string that represents the key in each object of thearr
array that will be used to group the objects.
Record<string, any>[][]
: Returns an array of arrays. Each inner array contains objects from the input arrayarr
that have the same value for the specifiedkey
.
import groupArrayOfObjectsByValueOfAKey from '@xmanscript/utils/groupArrayOfObjectsByValueOfAKey';
const data = [
{ category: 'A', value: 1 },
{ category: 'B', value: 2 },
{ category: 'A', value: 3 },
{ category: 'C', value: 4 },
];
const groupedData = groupArrayOfObjectsByValueOfAKey(data, 'category');
// Result: [ [{ category: 'A', value: 1 }, { category: 'A', value: 3 }], [{ category: 'B', value: 2 }], [{ category: 'C', value: 4 }] ]
The countKeyOccurrences
function counts the number of occurrences of a specific key in a JSON object or array.
json
(Type:any
): Thejson
parameter is the JSON object or array that you want to search for the specified key in.key
(Type:string
): Thekey
parameter is a string that represents the key you want to count in the JSON object.
number
: Returns the count of how many times the specified key appears in the given JSON object.
import countKeyOccurrences from '@xmanscript/utils/countKeyOccurrences';
const jsonObject = {
key1: 'value',
key2: 'value',
nested: {
key1: 'value',
key3: 'value',
},
};
const keyCount = countKeyOccurrences(jsonObject, 'key1');
// Result: 2 (Occurrences of 'key1' in the JSON object)
The distributePercentageEqually
function takes a JSON object and a key, and updates the values of that key in the object to distribute a percentage evenly among all occurrences of the key.
json
(Type:any
): Thejson
parameter is an object or an array that represents a JSON structure. It can contain nested objects and arrays.key
(Type:string
): Thekey
parameter is a string that represents the key in the JSON object that you want to divide the percentage for.
- Updated JSON object: Returns the updated JSON object with the percentage values divided evenly among the objects that have the specified key.
import distributePercentageEqually from '@xmanscript/utils/distributePercentageEqually';
const jsonObject = {
items: [
{ name: 'A', percentage: 0 },
{ name: 'B', percentage: 0 },
{ name: 'C', percentage: 0 },
],
};
const updatedJsonObject = distributePercentageEqually(jsonObject, 'percentage');
// Result: All items' 'percentage' values in the JSON object are evenly distributed. {"items": [{ "name": "A", "percentage": 33 },{ "name": "B", "percentage": 33 },{ "name": "C", "percentage": 34 }]}
The uniqueValuesOfKey
function is used to extract unique string values from a specified key in an array of objects.
data
(Type:Record<string, any>[]
): An array of objects containing various key-value pairs.key
(Type:string
): The key to extract unique values from.
- (Type:
string[]
): An array of unique string values from the specified key.
import uniqueValuesOfKey from '@xmanscript/utils/uniqueValuesOfKey';
const data = [
{ id: 1, name: 'John' },
{ id: 2, name: 'Jane' },
{ id: 3, name: 'John' },
];
const uniqueNames = uniqueValuesOfKey(data, 'name');
console.log(uniqueNames);
// Output: ['John', 'Jane']
The formatNumberToCommaString
function converts a number to a comma-separated string representation, rounded to the nearest whole number.
value
(Type:number
): The number to be converted.
- (Type:
string
): A comma-separated string representation of the rounded number.
import formatNumberToCommaString from '@xmanscript/utils/formatNumberToCommaString';
const number = 1234567.89;
const formattedNumber = formatNumberToCommaString(number);
console.log(formattedNumber);
// Output: '1,234,568'
The getMinMax
function finds the minimum and maximum values of a specified key in an array of objects.
arr
(Type:any[]
): An array of objects where each object has a property specified by thekey
parameter.key
(Type:string
): The property name to be used for finding the minimum and maximum values.
- (Type:
object
): An object with two properties: "min" and "max". "min" represents the minimum value found in the array of objects based on the specified key, and "max" represents the maximum value.
import getMinMax from '@xmanscript/utils/getMinMax';
const data = [{ age: 25 }, { age: 32 }, { age: 18 }, { age: 42 }];
const result = getMinMax(data, 'age');
console.log(result);
// Output: { min: 18, max: 42 }
The getDifferenceObject
function finds the minimum and maximum values of a specified key in an array of objects.
object1
: The first object to compare. It should be of typeRecord<string, any>
, which means it can have any number of properties of any type.object2
: Theobject2
parameter is a record (object) containing key-value pairs.
- (Type:
object
): a record object that represents the difference betweenobject1
andobject2
.
import getDifferenceObject from '@xmanscript/utils/getDifferenceObject';
const obj1 = { name: 'john', family: 'deo' };
const obj1 = { name: 'john', family: { familyName: 'deo' } };
const result = getDifferenceObject(obj1, obj2);
console.log(result);
// Output: { family: { familyName: 'deo' } }
The strictIntersectObjects
takes two objects as input and returns a new object that
- contains only the key-value pairs that exist in both input objects and have the same value.
obj1
(Type:Record<string, any>
): An object that can have any number of properties of any type.obj2
(Type:Record<string, any>
): A record object that contains key-value pairs.
Record<string, any>
: a new object that contains the key-value pairs fromobj2
that also exist inobj1
and
- have the same value.
import strictIntersectObjects from '@xmanscript/utils/strictIntersectObjects';
const object1 = {
name: 'John',
age: 30,
country: 'USA',
};
const object2 = {
name: 'Sara',
age: 30,
city: 'New York',
};
const intersection = strictIntersectObjects(object1, object2);
// Result: { age: 30 }