@okiba/arrays

Array helpers for okiba.js

Usage no npm install needed!

<script type="module">
  import okibaArrays from 'https://cdn.skypack.dev/@okiba/arrays';
</script>

README

Okiba / arrays

Array utils for okiba js

__

Installation

npm i --save @okiba/arrays

Or import it directly in the browser

<script type="module" src="https://unpkg.com/@okiba/arrays/index.js"></script>

Usage

import arrays from '@okiba/arrays'

Untranspiled code 🛑

Okiba Core packages are not transpiled, so don't forget to transpile them with your favourite bundler. For example, using Babel with Webpack, you should prevent imports from okiba to be excluded from transpilation, like follows:

{
  test: /\.js$/,
  exclude: /node_modules\/(?!(@okiba)\/).*/,
  use: {
    loader: 'babel-loader',
    options: {
      presets: ['@babel/preset-env']
    }
  }
}

arrayOrOne(arrayLike)

Return the first element if it only contains one

const els = arrayOrOne([🍏, 🍌])
console.log(els) // [🍏, 🍌]

const els = arrayOrOne([🍏])
console.log(els) // 🍏

Arguments

+ arrayLike: Array-like

The options object.

Returns

any The first element or the argument, undefined if empty array

castArray(castable)

Cast an array-like object or single element to Array

const elements = castArray(document.querySelectorAll('p')) // [p, p]
const fruits = castArray(🍒) // [🍒]

Arguments

+ castable: any

Array to cast

Returns

Array The array-like converted to Array, or an Array containing the element

spliceOne(array, index)

Removes an element from an array in-place without causing Garbage Collection

const array = [🍎, 🍐, 🍌]
spliceOne(array, 1)
console.log(array) // Logs: [🍎, 🍌]

Arguments

+ array: Array

Array you want to remove an element from

+ index: Number

The index of the element to remove