1.0.2 • Published 3 years ago

@jitiendran/funarray v1.0.2

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

funarray-package

This is the light weight array package with fun array manipulations and methods.

Get Started

npm install --save @jitiendran/funarray

Or if you're not into package management, just download a ZIP file.

Setup

After installing the package first the require the package from node modules into your javasctript file

const { FunArray } = require("@jitiendran/funarray");

Now, you can create a empty array by creating a object for the class FunArray.

let array = new FunArray();

You can also add element to array array while creating itself by doing the below.

let array = new FunArray(1, 2, 3, 4);

Common Operations

1. Create an Array

let fruits = new FunArray("Apple", "Bannana", "Orange");
let fruits = new FunArray();

fruits.push("Apple");
fruits.push("Bannana");

2. Access an Array elements

let fruit = fruits.atIndex(0); //returns value
const index = fruits.indexOf('Apple') //returns index

let fruit = fruits[index].

3. Looping through array

for (let i = 0; i < fruits.length(); i++) {
    console.log(fruits.atIndex(i));
}

Output

Apple
Bannana
Orange

4. Removing an item from last

fruits.pop(); //removes last item

console.log(fruits.array()); //converts funarray to array

Output

[ 'Apple', 'Bannana' ]

5. Remove an item from the beginning of an Array

fruits.shift(); //removes Apple from front

6. Add an item to the beginning of an Array

fruits.unshift("Strawberry");

//output: ['Strawberry','Bannana','Orange']
fruits.unshift('Strawberry','PineApple',...)

7. Remove an item by index position

fruits.removeAt(0); //removes the elements at index 0

8. Remove an item by value

fruits.remove("Apple"); //removes apple

9. Copy an array

let copy = fruits.slice();

10. Print fun array

fruits.print(); //prints the array

11. Convert Array to String

const str = fruits.toString();

12. Empty the whole array

fruits.trash();

13. Fill the array

fruits.fill("Avacado"); //fills everything with Avacado
fruits.fill("Avacado", 0, 1); //fills Avacado starting from index 0 upto 1

14. Merge with another array

let vegetables = new FunArray("Tomato", "Onion", "Brinjal");

fruits.merge(vegetables);

fruits.print();

Output

[ 'Apple', 'Bannana', 'Orange', 'Vegetables', 'Onion', 'Brinjal' ]

15. Make the Array distinct

let num = new FunArray(1, 2, 3, 3).distinct(); //returns normal array

console.log(num);
//output [1,2,3]

16. Remove the element from Object array using expression

let users = new FunArray(
    {
        _id: 1,
        Username: "Albert",
    },
    { _id: 2, Username: "Jason" }
);

users.removeWhere("_id === 1"); //expression should be string
console.log(users.array());

Output

[ { _id: 2, Username: 'Jason' } ]

17. Find the elements from Object array using expression

let users = new FunArray(
    {
        _id: 1,
        Username: "Albert",
    },
    { _id: 2, Username: "Albert" },
    { _id: 3, Username: "Jason" }
);

let foundUsers = users.findWhere("Username === Albert"); //returns normal array

console.log(foundUsers);

Output

[ { _id: 1, Username: 'Albert' }, { _id: 2, Username: 'Albert' } ]

18. Sort the Array in ascending Order

const num = new FunArray(4, 2, 3, 3);

let users = new FunArray(
    {
        _id: 2,
        Username: "Albert",
    },
    { _id: 1, Username: "Albert" },
    { _id: 3, Username: "Jason" }
);

num.sortAsc();

users.sortAsc("_id"); //sort by any key;

num.print();
users.print();

Output

[ 2, 3, 3, 4 ]
[
  { _id: 1, Username: 'Albert' },
  { _id: 2, Username: 'Albert' },
  { _id: 3, Username: 'Jason' }
]

19. Sort the Array in ascending Order

const num = new FunArray(4, 2, 3, 3);

let users = new FunArray(
    {
        _id: 2,
        Username: "Albert",
    },
    { _id: 1, Username: "Albert" },
    { _id: 3, Username: "Jason" }
);

num.sortDesc();

users.sortDesc("_id"); //sort by any key;

num.print();
users.print();

Output

[ 4, 3, 3, 2 ]
[
  { _id: 3, Username: 'Jason' },
  { _id: 2, Username: 'Albert' },
  { _id: 1, Username: 'Albert' }
]

20. Replace a item in the array

const names = new FunArray("Albert", "Albert", "Jason");

names.replace("Albert", "Happy"); // By default occurence is 1

console.log(names.array());

names.replace("Albert", "Happy", 2); //specifying occurence

console.log(names.array());

Output

[ 'Happy', 'Albert', 'Jason' ]
[ 'Happy', 'Happy', 'Jason' ]

21. Replace All elements in the array

const names = new FunArray("Albert", "Albert", "Jason");

names.replaceAll("Albert", "Happy");

Output

[ 'Happy', 'Happy', 'Jason' ]

22. Multiple push similar to creation

const names = new FunArray("Albert", "Albert", "Jason");

names.funPush("James", "Elric", "Alphonse");

names.print();

Output

[ 'Albert', 'Albert', 'Jason', 'James', 'Elric', 'Alphonse' ]

23. Find and Update the elements in array

const names = new FunArray("Albert", "Albert", "Jason");

names.findAndUpdate(0, "Alphonse");

names.print();

Ouput

[ 'Alphonse', 'Albert', 'Jason' ]
1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago