@fec/ape
v2.1.0
Published
Array processing engine
Downloads
1
Readme
Ape
Ape is an array processing engine. It takes an array of records and gives you a convenient interface to operate on it. Operations are processed in batch.
Made by 👨💻 Florian Eckerstorfer in 🎡 Vienna, Europe.
Table of Contents
Installation
You can install ape
with NPM or Yarn:
npm install --save @fec/ape
yarn add @fec/ape
Usage
ape
does only have a single export, the titular ape
function. It takes an array of records and returns an object with various functions to process the array.
import { ape } from 'ape';
const data = ape([{ a: 'val 1' }, { a: 'val 2' }])
.map((record) => ({ a: record.a.toUpperCase() }))
.renameKey('a', 'b').data;
// → [{ b: 'VAL 1' }, { b: 'VAL 2' }]
API documentation
In the following API documentation we will be using the following types:
ApeRecordKey
: alias forstring | number
, used as key inApeRecord
Value
: value used inApeRecord
ApeRecord
: object with key-value pairsApeData
: array ofApeRecord
Map
map((record: ApeRecord, index: number, data: ApeData) => ApeRecord) => ape
Takes a map
function that is applied for each record in the array.
Example
import { ape } from 'ape';
const data = [{ a: 'val 1' }, { a: 'val 2' }];
const newData = ape(data).map((record) => ({ a: record.a.toUpperCase() })).data;
// → [{ a: 'VAL 1' }, { a: 'VAL 2' }]
Map value
mapValue(key: ApeRecordKey, (value: Value, key: ApeRecordKey, index: number, data: ApeData) => Value) => ape
Takes a mapValue
function that is applied to the value with the given key for each record in the array.
Example
import { ape } from 'ape';
const data = [{ a: 'val 1' }, { a: 'val 2' }];
const newData = ape(data).mapValue('a', (a) => a.toUpperCase())).data;
// → [{ a: 'VAL 1' }, { a: 'VAL 2' }]
Add value
addValue(newKey: ApeRecordKey, (record: ApeRecord, key: ApeRecordKey, index: number, data: ApeData) => Value) => ape
Takes a addValue
function that is used to add a new value to each record with the given key.
import { ape } from 'ape';
const data = [{ a: 'val 1' }, { a: 'val 2' }];
const newData = ape(data).addValue('b', (r) => Object.keys(r)).data;
// → [{ a: 'val 1', b: ['a'] }, { a: 'val 2', b: ['a'] }]
Rename key
renameKey(key: ApeRecordKey, newKey: ApeRecordKey) => ape
Renames the given key to newKey
in each record in the array.
Example
import { ape } from 'ape';
const data = [{ a: 'val 1' }, { a: 'val 2' }];
const newData = ape(data).renameKey('a', 'b').data;
// → [{ b: 'val 1' }, { b: 'val 2' }]
Create index
createIndex(keys: ApeRecordKey | ApeRecordKey[]) => ape
Creates an index for the given key or keys, by creating a hash-map of all possible values. This is a pre-requisite for using the findByIndex()
function.
Example
See findByIndex()
.
Merge by index
mergeByIndex(keys: ApeRecordKey | ApeRecordKey[], mergeData: ApeData) => ape
Merges the array with the given data by the given key or keys. For the given mergeData
an index is created before the merge is performed.
Example
import { ape } from 'ape';
const data = [
{ id: 1, a: 'val 1' },
{ id: 2, a: 'val 2' },
];
const otherData = [
{ id: 1, b: 'foo 1' },
{ id: 2, b: 'foo 2' },
];
const newData = ape(data).createIndex('id').mergeByIndex('id', otherData).data;
// → [{ id: 1, a: 'val 1', b: 'foo 1' }, { id: 2, a: 'val 2', b: 'foo 2' }]
Find by index
findByIndex(query: Partial<ApeRecord>) => ApeRecord | undefined
Returns the single record from the array of records that matches the given query or undefined
if no record matches the query. Throws an error if no index exists for the keys present in the query.
Example
import { ape } from 'ape';
const data = [
{ id: 1, a: 'val 1' },
{ id: 2, a: 'val 2' },
];
const result = ape(data).createIndex('id').findByIndex({ id: 1 });
// → { a: 'val 1' }
Code of conduct
See CODE_OF_CONDUCT
Contributing
To contribute to @fec/ape
, follow these steps:
- Fork this repository.
- Create a branch:
git checkout -b <branch_name>
. - Install dependencies:
npm install
- Make your changes (please also update the tests and documentation)
- Don't forgot to run the tests:
npm test
- Commit your changes:
git commit -m '<commit_message>'
- Push to the original branch:
git push origin <project_name>/<location>
- Create the pull request.
Alternatively see the GitHub documentation on creating a pull request.
Change log
See CHANGE_LOG
License
See LICENSE