1.2.0 • Published 1 year ago

@vycana/iteract v1.2.0

Weekly downloads
-
License
ISC
Repository
github
Last release
1 year ago

Test GitHub GitHub Code Size in bytes GitHub Last Commit GitHub Release Date - Published_At

Iteract

Iteract is a data manipulation library that allows for filtering data in a more readable and straightforward way. It provides an easy-to-use API for manipulating arrays of objects.

Installation

Install the package using npm:

npm install @vycana/iteract

Usage

Getting Started

Node JS ENV

To use Iteract, simply import it and create a new instance with your data:

const { Iteract } = require("iteract");

const data = [
  { id: 1, name: "John", age: 30 },
  { id: 2, name: "Mary", age: 25 },
  { id: 3, name: "Peter", age: 35 }
];

const iteract = new Iteract(data);

Browser ENV

To use iteract in browser env, just use Iteract and you are ready to go.

<script>
        window.addEventListener("DOMContentLoaded", () => {
            console.log(Iteract);
        })
</script>

Creating a new Iteract object

Create a new Iteract object by passing an array or an object to the constructor:

const data = [1, 2, 3];
const iteract = new Iteract(data);

Method

all()

Use the all() method to get all the data stored in the object:

const data = [1, 2, 3];
const iteract = new Iteract(data);
console.log(iteract.all()); // [1, 2, 3]

where(...)

Use the where(...) method to filter the data based on certain conditions:

const data = [
  { name: 'John', age: 25 },
  { name: 'Mary', age: 30 },
  { name: 'Peter', age: 35 },
];
const iteract = new Iteract(data);
console.log(iteract.where('age', '>=', 30).all()); // [{ name: 'Mary', age: 30 }, { name: 'Peter', age: 35 }]

sort(...)

To use the sort method of the Iteract class, you can create a new instance of Iteract and pass an array or an object to it. Then, you can call the sort method on the Iteract instance, passing in the sortAscending parameter (which determines whether to sort the data in ascending or descending order) and the key parameter (which specifies the key to use for sorting if the data is an array of objects).

const { Iteract } = require('iteract');

// Create a new instance of Iteract with an array of numbers
const arr = [4, 2, 1, 3];
const iteractArr = new Iteract(arr);

// Sort the array in ascending order
const sortedArr = iteractArr.sort(true);
console.log(sortedArr.all()); // [1, 2, 3, 4]

// Sort the array in descending order
const reverseSortedArr = iteractArr.sort(false);
console.log(reverseSortedArr.all()); // [4, 3, 2, 1]

// Create a new instance of Iteract with an array of objects
const objArr = [
	{ name: 'John', age: 25 },
	{ name: 'Mary', age: 30 },
	{ name: 'Peter', age: 20 },
];

const iteractObjArr = new Iteract(objArr);

// Sort the array of objects in ascending order based on the 'age' key
const sortedObjArr = iteractObjArr.sort(true, 'age');
console.log(sortedObjArr.all()); // [{ name: 'Peter', age: 20 }, { name: 'John', age: 25 }, { name: 'Mary', age: 30 }]

// Sort the array of objects in descending order based on the 'name' key
const reverseSortedObjArr = iteractObjArr.sort(false, 'name');
console.log(reverseSortedObjArr.all()); // [{ name: 'Peter', age: 20 }, { name: 'Mary', age: 30 }, { name: 'John', age: 25 }]

has(value)

Use the has(value) method to check if a given value exists as a key in the object:

const data = { name: 'John', age: 25 };
const iteract = new Iteract(data);
console.log(iteract.has('name')); // true
console.log(iteract.has('email')); // false

hasKeys()

Use the hasKeys() method to check if the object has any keys:

const data = { name: 'John', age: 25 };
const iteract = new Iteract(data);
console.log(iteract.hasKeys()); // true

unique(key)

Use the unique(key) method to return an object with unique elements based on a specified key:

const data = [
  { name: 'John', age: 25 },
  { name: 'Mary', age: 30 },
  { name: 'John', age: 35 },
];
const iteract = new Iteract(data);
console.log(iteract.unique('name').all()); // [{ name: 'John', age: 25 }, { name: 'Mary', age: 30 }]

push(value)

Use the push(value) method to add a value to the data array and return a new Iteract object:

const data = [1, 2, 3];
const iteract = new Iteract(data);
console.log(iteract.push(4).all()); // [1, 2, 3, 4]

Contributors

License

Iteract is released under the Vycana's MIT License.

1.2.0

1 year ago