Skip to content

yamankatby/indices

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

35 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

indices

Work with the immutable arrays in a more convenient way.

Latest Stable Version NPM Downloads GitHub issues Used Languages

🚧 Table of Contents

πŸš€ Motivation

⬇️ Installation

☘️ Examples

πŸ”Œ API

push()

This method works like Array.prototype.push() method which allows you to insert an element to the end of the array but this method does not mutate the original array. For example:

import { indices } from "indices";

const current = [1, 2, 3];
const next = indices(current).push(4).toArray();

console.log(current); // output => [1, 2, 3];
console.log(next); // output => [1, 2, 3, 4];

The next variable will contain the new array where the current variable still has the same value as initialized.

Also, it's possible to insert multi-element to the end of the array using the push method by passing them as separate arguments. For example:

const current = [1, 2, 3];
const next = indices(current).push(4, 5, 6).toArray();

console.log(current); // output => [1, 2, 3];
console.log(next); // output => [1, 2, 3, 4, 5, 6];

unshift()

This method works like Array.prototype.unshift() method which allows you to insert an element to the beginning of the array but this method does not mutate the original array. For example:

import { indices } from "indices";

const current = [1, 2, 3];
const next = indices(current).unshift(0).toArray();

console.log(current); // output => [1, 2, 3];
console.log(next); // output => [0, 1, 2, 3];

The next variable will contain the new array where the current variable still has the same value as initialized.

Also, it's possible to insert multi-element to the beginning of the array using the unshift method by passing them as separate arguments. For example:

const current = [1, 2, 3];
const next = indices(current).unshift(-2, -1, 0).toArray();

console.log(current); // output => [1, 2, 3];
console.log(next); // output => [-2, -1, 0, 1, 2, 3];

pop()

This method works like Array.prototype.pop() method which allows you to remove an element from the end of the array but this method does not mutate the original array. For example:

import { indices } from "indices";

const current = [1, 2, 3];
const next = indices(current).pop().toArray();

console.log(current); // output => [1, 2, 3];
console.log(next); // output => [1, 2];

The next variable will contain the new array where the current variable still has the same value as initialized.

Also, it's possible to remove multi-element from the end of the array using the pop method by passing number of elements you want to remove as the first parameter. For example:

const current = [1, 2, 3];
const next = indices(current).pop(2).toArray();

console.log(current); // output => [1, 2, 3];
console.log(next); // output => [1];

shift()

This method works like Array.prototype.shift() method which allows you to remove an element from the beginning of the array but this method does not mutate the original array. For example:

import { indices } from "indices";

const current = [1, 2, 3];
const next = indices(current).shift().toArray();

console.log(current); // output => [1, 2, 3];
console.log(next); // output => [2, 3];

The next variable will contain the new array where the current variable still has the same value as initialized.

Also, it's possible to remove multi-element from the beginning of the array using the shift method by passing number of elements you want to remove as the first parameter. For example:

const current = [1, 2, 3];
const next = indices(current).shift(2).toArray();

console.log(current); // output => [1, 2, 3];
console.log(next); // output => [3];

concat()

The concat() method is used to merge two or more arrays. This method does not change the existing arrays, but instead returns a new array. For example:

import { indices } from "indices";

const array1 = [1, 2, 3];
const array2 = [4, 5, 6];
const array3 = indices(array1).concat(array2).toArray();

console.log(array1); // output => [1, 2, 3];
console.log(array2); // output => [4, 5, 6];
console.log(array3); // output => [1, 2, 3, 4, 5, 6];

Note: This method works exactly like Array.prototype.concat() method. We just reimplemented it inside the package to be able to use it with the other methods.

replace()

This method is the most powerful in the package. It allows you to edit a specific element inside an array by replacing it with a new one. This method takes two parameters first one is the index of the element you want to replace, and the second one is the new element. For example:

import { indices } from "indices";

const current = ["a", "b", "c"];
const next = indices(current).replace(0, "d").toArray();

console.log(current); // output => ["a", "b", "c"];
console.log(next); // output => ["d", "b", "c"];

If you don't know the exact index of the element that you want to replace, You can pass a predicate function as the first parameter.

const current = ["ab", "bc", "cd"];
const next = indices(current).replace(element => element.startsWith("a"), "de").toArray();

console.log(current); // output => ["ab", "bc", "cd"];
console.log(next); // output => ["de", "bc", "cd"];

Also, if you are calculating the new element with an expensive operation, and you want to use the previous element to calculate the new one you can pass a callback function as the second parameter to the replace() method.

const current = ["ab", "bc", "cd"];
const next = indices(current).replace(0, prevElement => prevElement + "c").toArray();

console.log(current); // output => ["ab", "bc", "cd"];
console.log(next); // output => ["abc", "bc", "cd"];

insertAfter()

This method helps you to insert an element after a specific element by passing the index of the element you want to replace as the first parameter and the element you want to insert as the second parameter. For example:

import { indices } from "indices";

const current = ["a", "c", "d"];
const next = indices(current).insertAfter(0, "b").toArray();

console.log(current); // output => ["a", "c", "d"];
console.log(next); // output => ["a", "b", "c", "d"];

If you don't know the exact index of the element that you want to insert an element after it, You can pass a predicate function as the first parameter.

const current = ["ab", "cd", "de"];
const next = indices(current).insertAfter(element => element.startsWith("a"), "bc").toArray();

console.log(current); // output => ["ab", "cd", "de"];
console.log(next); // output => ["ab", "bc", "cd", "de"];

insertBefore()

remove()

🀝 Contributing

We would love to have community contributions and support! A few areas where could use help right now:

  • Bug reports and/or fixes
  • Writing tests
  • Creating examples for the docs

If you want to contribute, please submit a pull request, or contact mohakapt@gmail.com for more information. When you commit your messages, follow this convention:

Main changes subject
- Optional message
- Another optional message

If you do a breaking change, add an explanation preceded by BREAKING CHANGE: keyword. For example:

BREAKING CHANGE: Main changes subject
- Optional message
- Another optional message

πŸ’‘ FAQ

πŸ‘ Support

See also the list of contributors who participated in this project.

πŸ“ License

This library is licensed under the MIT License - see the LICENSE.md file for details.

πŸ“„ Release Notes

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Packages

No packages published