arias-tools v1.0.2
HELLO WORLD!,
What is arias-tools?
arias-tools
is a versatile and powerful library for JavaScript and TypeScript that provides a collection of useful utility functions. This library aims to simplify common tasks such as date range checks, object property access, filtering, and sorting.
Installation
You can install the library via npm:
npm install arias-tools
Usage
Below is a detailed guide on how to use each function provided by arias-tools
.
Functions
checkDateInRange
Checks if a given date or date range falls within a specified range.
import { checkDateInRange } from 'arias-tools';
/**
* @param {string[]} value - An array containing one or two date strings.
* @param {string} rangeStart - The start date of the range.
* @param {string} rangeEnd - The end date of the range.
* @param {boolean} [strict=false] - If true, the date range must be strictly within the specified range.
* @returns {boolean} - True if the date(s) fall within the range, false otherwise.
*/
const result = checkDateInRange(['2023-01-01', '2023-12-31'], '2023-01-01', '2023-12-31', true);
console.log(result); // Output: true
genericFilter
Filters an array of objects based on an array of filters.
import { genericFilter } from 'arias-tools';
/**
* @param {T[]} arr - The array to be filtered.
* @param {Filter[]} filters - An array of filter objects.
* @returns {T[]} - The filtered array.
*/
const data = [
{ name: 'Alice', age: 30 },
{ name: 'Bob', age: 40 }
];
const filters = [
{ type: 'numberRange', path: ['age'], value: [30, 35] }
];
const filteredData = genericFilter(data, filters);
console.log(filteredData); // Output: [{ name: 'Alice', age: 30 }]
gatherValuesByPath
Gathers values from a given object or array of objects based on a specified path.
import { gatherValuesByPath } from 'arias-tools';
/**
* @param {T | T[]} obj - The object or array of objects to gather values from.
* @param {string} path - The path (dot-separated) to the desired property.
* @param {boolean} [unique=true] - Whether to return only unique values.
* @returns {unknown[]} - An array of values collected from the specified path.
*/
const obj = [{ a: { b: 1 } }, { a: { b: 2 } }, { a: { b: 1 } }];
const values = gatherValuesByPath(obj, 'a.b');
console.log(values); // Output: [1, 2]
filterObjectsByPath
Filters an array of objects based on a specified path and set of values.
import { filterObjectsByPath } from 'arias-tools';
/**
* @param {T[]} data - The array of objects to filter.
* @param {string} path - The path (dot-separated) to the desired property.
* @param {unknown[]} values - The values to filter by.
* @returns {T[]} - An array of objects that match the specified values at the given path.
*/
const data = [
{ user: { id: 1, name: 'Alice' } },
{ user: { id: 2, name: 'Bob' } }
];
const filteredData = filterObjectsByPath(data, 'user.name', ['Alice']);
console.log(filteredData); // Output: [{ user: { id: 1, name: 'Alice' } }]
dynamicSort
Creates a comparison function for sorting objects by a specified property.
import { dynamicSort } from 'arias-tools';
/**
* @param {string} property - The property to sort by. Prefix with '-' for descending order.
* @returns {(a: DynamicObject, b: DynamicObject) => number} - The comparison function for sorting.
*/
const data = [
{ name: 'Bob', age: 40 },
{ name: 'Alice', age: 30 }
];
data.sort(dynamicSort('name'));
console.log(data); // Output: [{ name: 'Alice', age: 30 }, { name: 'Bob', age: 40 }]
Continuous Improvement
I am continuously adding new utilities to arias-tools
to make it even more useful. I welcome contributions from the community, and anyone can add useful functions to the library.
Contributing
Contributions are welcome! Please open an issue or submit a pull request on GitHub.
License
This project is licensed under the MIT License.