logo

Array

This is a collection of functions for working with arrays.

Create a clone

The cloneArray function creates a copy of an array.

import { cloneArray } from '@aracna/core';

const a = [0, 1];
const b = cloneArray(a);

b[0] = 1;
b[1] = 2;

// will log [0, 1]
console.log(a);

// will log [1, 2]
console.log(b);

Get the symmetric difference

The getArraysDifference function returns the symmetric difference between two or more arrays. Optionally you can pass a custom includes function to check if an item is included in the result array.

import { getArraysDifference } from '@aracna/core';

const a = [0, 1];
const b = [1, 2];

// will log [0, 2]
console.log(getArraysDifference([a, b]));

Intersect two or more arrays

The getArraysIntersection function returns the intersection between two or more arrays. Optionally you can pass a custom includes function to check if an item is included in the result array.

import { getArraysIntersection } from '@aracna/core';

const a = [0, 1];
const b = [1, 2];

// will log [1]
console.log(getArraysIntersection([a, b]));

Get the last item

The getArrayLastItem function returns the last item of an array. Optionally you can pass a fallback value that will be returned if the array is empty.

import { getArrayLastItem } from '@aracna/core';

// will log 1
console.log(getArrayLastItem([0, 1]));

// will log undefined
console.log(getArrayLastItem([]));

// will log 0
console.log(getArrayLastItem([]), 0);

Remove duplicates

The removeArrayDuplicates function removes all duplicates from an array. Optionally you can pass a custom includes function to check if an item is included in the result array.

import { removeArrayDuplicates } from '@aracna/core'

// will log [0, 1, 2, 3]
console.log(removeArrayDuplicates([0, 1, 2, 2, 3, 3, 3]))

// will log [{ value: 0 }, { value: 1 }, { value: 2 }, { value: 3 }]
console.log(
  removeArrayDuplicates(
    [
      { value: 0 },
      { value: 1 },
      { value: 2 },
      { value: 2 },
      { value: 3 },
      { value: 3 },
      { value: 3 }
    ],
    (array: Item[], item: Item) =>
      array.findIndex((_item: Item) => _item.value === item.value) > -1
  )
)

Remove items

The removeArrayItems function removes items from an array that match the predicate or are in the items array.

import { removeArrayItems } from '@aracna/core'

// will log [1]
console.log(removeArrayItems([0, 1, 2], [0, 2]))

// will log [0]
console.log(
  removeArrayItems([0, 1, 2], (array: number[], item: number) => item > 0)
)

Check if a value is an array

The isArray function checks if the given value is an array.

import { isArray } from '@aracna/core';

// will log true
console.log(isArray([]));

// will log false
console.log(isArray({}));