okiba-gang/okiba

View on GitHub
packages/arrays/README.md

Summary

Maintainability
Test Coverage


# Okiba / arrays
Array utils for okiba js

__



### Installation

```bash
npm i --save @okiba/arrays
```

Or import it directly in the browser
```html
<script type="module" src="https://unpkg.com/@okiba/arrays/index.js"></script>
```

## Usage

```javascript
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:

```javascript
{
  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






```javascript
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






```javascript
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






```javascript
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