fliphub-monorepo
Version:
the builder of builders
71 lines (52 loc) • 1.8 kB
Markdown
//img.shields.io/npm/v/to-arr.svg
[ ]: https://npmjs.org/package/to-arr
[ ]: http://img.shields.io/badge/license-MIT-blue.svg?style=flat
[ ]: https://spdx.org/licenses/MIT
[ ]: https://img.shields.io/gitter/room/fliphub/pink.svg
[ ]: https://gitter.im/fliphub/Lobby
[ ]: https://img.shields.io/badge/%F0%9F%8F%97%20%F0%9F%92%A0-flipfam-9659F7.svg
[ ]: https://www.npmjs.com/package/flipfam
> take any value, turn it to an array if it is not already one
```bash
yarn add to-arr
npm i to-arr --save
```
```js
const toArr = require('to-arr')
```
makes it very easy to loop over arguments without having to deal with checks
```js
function doSomethingCrazy(arg) {}
const allOpts = {includeEmpty: true}
const splitOpts = {split: '-'}
const loopData = (args) => toArr(args).forEach(doSomethingCrazy)
const loopAllData = (args) => toArr(args, allOpts).forEach(doSomethingCrazy)
const loopSplit = (args) => toArr(args, splitOpts).forEach(doSomethingCrazy)
// will loop an empty array, 0 iterations
loopData(null)
// will loop an array including null
loopAllData(null)
// loops 3 strings
const arrOfStrs = toArr('string1-string2-string3')
```
```js
const arrOfStrs = toArr('string1,string2,string3')
const arrOfStr = toArr('just one array entry')
```
```js
const obj = {luke: 'use the strings, forget me'}
const keys = toArr(obj, {keys: true})
const objs = toArr(obj)
const objs = toArr(objs)
console.assert(objs === objs)
```
[![NPM version][to-arr-npm-image]][to-arr-npm-url]
[![MIT License][license-image]][license-url]
[![fliphub][gitter-badge]][gitter-url]
[![flipfam][flipfam-image]][flipfam-url]
[ ]: https: