find_all_data v1.0.4
๐ฆ Data Manipulation Module
The ๐ฆ data manipulation module is a library that simplifies the manipulation and analysis of data arrays. Whether your data is raw or you need to perform complex operations like sorting, searching ๐ต๏ธโโ๏ธ, or calculations ๐, this module can make your task easier.
Why Use This Module? ๐คทโโ๏ธ
Data Structuring ๐๏ธ
One of the essential aspects of data manipulation is having well-structured data. This module allows you to take an existing data array and add a unique 'id' property to each object. It also ensures that all objects in the array have the same keys, ensuring a consistent structure.
Data Exploration ๐
You can easily explore your data by extracting essential information. The module provides methods for searching for specific objects by their 'id' or filtering data based on specific criteria. This allows you to quickly answer questions like "What items match a particular criterion?"
Data Aggregation ๐
If you need to aggregate or summarize data, this module allows you to calculate statistics such as sum, maximum, and minimum values for any numeric key. This enables you to obtain valuable insights from your data quickly.
Data Pagination ๐
To handle large amounts of data, the module offers a pagination function. You can specify the page number and the number of items per page, and the module will return the corresponding data range. This makes it easy to create paginated views in your application.
Text Search ๐
Another powerful feature of this module is text search. You can perform case-insensitive searches without considering accents. This allows you to search for objects containing specific terms within a given key, thereby improving the user-friendliness of your application.
How to Use This Module? ๐ ๏ธ
Table of Contents ๐
- Installation
- Creating the Module Instance
- Data Structure
- Data Exploration
- Data Aggregation
- Data Pagination
- Text Search
- Functions
1. Installation ๐
To use the data manipulation module, you need to install it in your Node.js project using npm. Run the following command in your project directory:
npm i find_all_data
2. Creating the Module Instance ๐ญ
After installing the module, you can import it into your JavaScript code as follows:
const findData = require("find_all_data");
Then, create an instance of the module by passing your data array (data) as an argument:
const user = findData(data);
3. Data Structure ๐งฑ
The module ensures a consistent data structure by adding a unique 'id' property to each object in the array. It also verifies that all objects have the same keys. Here's how it works:
const data = require("./user.json");
const findData = require("findData");
const user = findData(data);
console.log(user.all());
In the above example, user.all()
will return your data array with the 'id' added.
4. Data Exploration ๐
Searching by 'id' ๐
You can search for an object by its 'id' using the findById
method. For example:
const myUser = user.findById(1);
console.log(myUser);
Data Filtering ๐งน
The findAll
method allows you to search for objects in a data set using various filtering and sorting options. This method is particularly useful for extracting specific data from a collection.
Signature ๐๏ธ
findAll(params);
Parameters ๐๏ธ
params
(object): An object containing search and filtering options.
params
Options ๐ฆ
params.where
(array of functions): An array of functions to filter objects based on return values.params.order
(array of two strings): An array containing the name of the sorting key and the sorting order ("ASC" for ascending or "DESC" for descending).params.limit
(array of two numbers): An array containing the limit of items to return, with an offset value first and a limit value second.
Return ๐
- An array of objects that match the specified search criteria.
Usage Examples ๐ ๏ธ
- Filtering by a Single Key ๐ฏ
const newData = user.findAll({
where: [
user => user.age <= 10
]
});
console.log(newData);
- Sorting in Ascending Order ๐
const newData = user.findAll({
order: ['age', 'ASC']
});
console.log(newData);
- Sorting in Descending Order ๐
const newData = user.findAll({
order: ['age', 'DESC']
});
console.log(newData);
- Limiting Results with an Offset ๐
const newData = user.findAll({
limit: [5, 10], // Return 10 results starting from the 6th result
});
console.log(newData);
- Combining Multiple Options ๐
const newData = user.findAll({
where: [
user => user.age <= 10,
user => user.isAdmin === true,
],
order: ['age', 'DESC'],
limit: [5, 10],
});
console.log(newData);
5. Data Aggregation ๐
You can perform several aggregation operations on your data, such as calculating the sum, finding the maximum and minimum values for a numeric key. Here are some examples:
Sum of Values ๐
const sum = user.sum("age");
console.log(sum);
Maximum Value ๐
const maxValue = user.max("age");
console.log(maxValue);
Minimum Value ๐
const minValue = user.min("age");
console.log(minValue);
6. Data Pagination ๐
Pagination allows you to manage large amounts of data by retrieving only a portion of the results at a time. Here's how to paginate your data:
const [offset, limit] = user.page(2, 10); // Page 2, 10 items per page
const newData = user.findAll(
{
limit: user.page(2, 10)
});
console.log(newData);
7. Text Search ๐
You can perform case-insensitive and accent-insensitive text searches on a specific key. For example, to search for objects containing the term "apple" in the 'description' key:
const newData = user.findAll(
{
where: [
user.search('description', 'apple')
]
});
console.log(newData);
8. Functions ๐ ๏ธ
It provides several useful functions for working with data sets. Here's a description of the key functions of the module.
keys
Function ๐๏ธ
The keys
function returns an array of keys (property names) of the objects in the data array. This allows you to understand the structure of objects and the properties you can access.
const keys = user.keys();
console.log(keys); // Displays an array of keys
distinct
Function ๐
The distinct
function takes a key as input and returns an array of unique values for that key in the data array. It's useful for obtaining unique values from a specific column.
const uniqueValues = user.distinct('age');
console.log(uniqueValues); // Displays an array of unique values
type
Function ๐
The type
function takes a key as input and returns the data type (string, number, boolean, etc.) of the associated property in the objects of the data array.
const propertyType = user.type('age');
console.log(propertyType); // Displays the data type
reset
Function ๐
The reset
function resets the temporary data array to the original data. This cancels all previous filtering, sorting, or pagination operations applied.
user.reset(); // Resets the temporary data
These functions allow you to better understand the structure of your data, extract unique values, and determine the data types associated with your objects' properties. You can also reset temporarily modified data at any time using the reset
function.
This documentation covers the main features of the data manipulation module. You can now use these tools to explore, filter, aggregate, and paginate your data with ease in your JavaScript projects. Feel free to experiment further with these functions to master them better. ๐