Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/dreipol/looppa

Simple functional script to loop arrays, numbers, strings, objects, Map and Set
https://github.com/dreipol/looppa

Last synced: 7 days ago
JSON representation

Simple functional script to loop arrays, numbers, strings, objects, Map and Set

Awesome Lists containing this project

README

        

# looppa

[![Build Status][travis-image]][travis-url]

[![NPM version][npm-version-image]][npm-url]
[![NPM downloads][npm-downloads-image]][npm-url]
[![MIT License][license-image]][license-url]

Simple functional script to loop array, strings, numbers, objects, Map and Set.
Looppa will always returns a function to map your primitives

```
looppa(collection:any)(function(value:any, key:string|number, index:number) {}):array

```

# Installation

```sh
npm i looppa -S
```

# Usage
```js
import looppa from 'looppa';

// normalize null and undefined
const nullCollection = looppa(null)(); // []
const undefinedCollection = looppa(undefined)(); // []

// arrays will be left untouched
const array = looppa(['foo', null, undefined])(); // [['foo', 0], [null, 1], [undefined, 2]]

// numbers to array
const numbers = looppa(0, 4)(n => n * 2); // [2, 4, 6, 8]

// strings to array
const string = looppa('ciao')(); // [['c', 0], ['i', 1], ['a', 2], ['o', 3]]

// objects to array
const obj = looppa({ foo: 'bar', buz: 'baz' })(); // [['foo', 'bar'], ['buz', 'baz']]

// Map to array
const myMap = new Map();
myMap.set('foo', 'bar');
myMap.set('buz', 'baz');
const map = looppa(myMap)(); // [['foo', 'bar'], ['buz', 'baz']]

// Set to array
const mySet = new Set();
mySet.add('foo');
mySet.add('bar');
const map = looppa(mySet)(); // [['foo', 'foo'], ['bar', 'bar']]
```

# With React.js

This script is really handy if you need to deal with React loops

```jsx


Array



    {looppa([1, 2, 3])(number => (
  • {number}

  • ))}

Numbers



    {looppa(0, 5)(number => (
  • {number}

  • ))}

Letters



    {looppa('ciao')(letter => (
  • {letter}

  • ))}

Object



    {looppa({ foo: 'bar', baz: 'buz' })((value, key) => (
  • {key}, {value}

  • ))}

Map



    {looppa(new Map().set(1, 'bar'))((value, key) => (
  • {key}, {value}

  • ))}

Set



    {looppa(new Set().add('foo').add('bar'))(value => (
  • {value}

  • ))}


```

[check the demo](https://plnkr.co/edit/1DHUkr1mCUafiwz68f62?p=preview)

[travis-image]:https://img.shields.io/travis/dreipol/looppa.svg?style=flat-square
[travis-url]:https://travis-ci.org/dreipol/looppa

[license-image]:http://img.shields.io/badge/license-MIT-000000.svg?style=flat-square
[license-url]:LICENSE.txt

[npm-version-image]:http://img.shields.io/npm/v/looppa.svg?style=flat-square
[npm-downloads-image]:http://img.shields.io/npm/dm/looppa.svg?style=flat-square
[npm-url]:https://npmjs.org/package/looppa