prototypes-array
- a way to implement some extensions to help with array manipulation
Note
- all extensions are strongly typed and included globally.
- ensure the least risk of collision and lack of understanding.
- in order not to risk future implementations of es, all functions start with a capital letter
- You don't need to extend all the functions in this library
npm install prototypes-array
index
usage
- just require in root for a full implementation
require('prototypes-array')
- I just want to implement some array function:
require('prototypes-array/lib/count')
require('prototypes-array/lib/distinct')
require('prototypes-array/lib/first')
require('prototypes-array/lib/last')
require('prototypes-array/lib/max')
require('prototypes-array/lib/min')
require('prototypes-array/lib/pull')
require('prototypes-array/lib/search')
require('prototypes-array/lib/shape')
require('prototypes-array/lib/sum')
examples
const teste1 = [
{ id: 1, name: 'Matheus', code: '013' },
{ id: 2, name: 'Thiffani', code: '313' },
{ id: 3, name: 'Janaina', code: '243' },
{ id: 4, name: 'Juliana', code: '034' },
{ id: 5, name: 'Karine', code: '645' },
{ id: 6, name: 'Thais', code: '423' },
{ id: 7, name: 'Thiffani', code: '033' }
];
Array Count
- Count by element equality from an array by property
- if string returns total found by aggregation
- if by object/function returns number of total found
teste1.Count((e) => e.code.includes('3') ); // 6
teste1.Count({ name: 'Thiffani' }); // 2
teste1.Count('name'); // [{ name: 'Thiffani', _count: 2 }, {...}]
Array Distinct(pk, props?, includes?)
- pk: the key does the group => string|string[]
- props: select as array the output properties => string[]
- includes: object, add objects to output => Object
teste1.Distinct('name', ['name']); // [{ name: 'Thiffani' }, {...}]
teste1.Distinct(['name', 'code'], ['id', 'name'], { observations: null })); // [{ id: 2, name: 'Thiffani', observations: null }, {...}]
Array First
- Return First Element from array
teste1.First() // { id: 1, name: 'Matheus', code: '013' }
{}.First() // { }
Array GroupBy
- Group array objects by key string or callback
const result = array.GroupBy(
// ['id'], by string or
(x) => x.data.substring(0, 7) + x.id,
(current, next) => ({
id: current.id,
qtde_inf: next ? current.qtde_inf + next.qtde_inf : current.qtde_inf,
qtde_pass: next ? current.qtde_pass + next.qtde_pass : current.qtde_pass
})
)
Array Has (iterator)
- checks whether objects and values contain within the array
- iterator: number|string|function|object
'Should return true containing with function'
const test = array.Has(e => e.name === 'teste 001');
expect(test).toBe(true);
'Should return true containing object'
const test = array.Has({id: 1});
expect(test).toBe(true);
'Should return false containing object'
const test = array.Has({name: 'teste 000'});
expect(test).toBe(false);
'Should return true containing in array'
const test = [0,1,2,3].Has(2);
expect(test).toBe(true);
'Should return false not containing in array'
const test = [0,1,2,3].Has(4);
expect(test).toBe(false);
Array Last
- Return Last Element from array
teste1.Last() // { id: 7, name: 'Thiffani', code: '033' }
Array Max
teste1.Max('code') // 645
Array Min
Array PercentBy
- Returns the percentage value assigned by the sum of all values in a property
const array = [
{ id: 1, peso: 4, done: false, task: 'task 001'},
{ id: 2, peso: 5, done: true, task: 'task 002'},
{ id: 3, peso: 10, done: true, task: 'task 002'},
{ id: 4, peso: 1, done: true, task: 'task 002'}
];
array.PercentBy('peso', { done: true }) // '80.00'
array.PercentBy('peso', { done: false }, 0) // '20')
Array Pull
- Removes all elements from an array by property and returns Number pulled items.
teste1.Pull({ name: 'Thiffani' }) // 2
teste1.Pull({ name: 'Test' }) // 0
teste1.Pull({ id: 4 }) // 1
Array Search
- Provides a deep text search within array and array objects within array and arrays...
test1.Search('text or anything you want to fetch') // [ ... ]
Array Sum
- Sums all the numeric property entered and returns a number
teste1.Sum('id') // 28
teste1.Sum(e => e.id) // 28
Array Shape
- Enforces the types and keys of an array or object
'Array only'
const input = [['1', ['2021-10-01 00:00:000', '00']]];
const result = input.Shape({ id: Number, query: [{ created: (x) => new Date(x) }] });
expect(result).toEqual([
{ id: 1, query: [
{ created: new Date("2021-10-01T03:00:00.000Z")}, { created: new Date("2000-01-01T02:00:00.000Z") }
]
}])
const result: {
id: NumberConstructor;
query: {
created: (x: any) => Date;
}[];
}[]
'Object with Array'
const input = { id: '1', query: [{ created: '2021-10-01 00:00:000' }] };
const result = input.Shape({ id: Number, query: [{ created: (x) => new Date(x) }] });
expect(result).toEqual({ id: 1, query: [{ created: new Date("2021-10-01T03:00:00.000Z") }] })
const result: {
id: NumberConstructor;
query: {
created: (x: any) => Date;
}[];
}
})
'Object with Object'
const input = { id: '1', query: { created: '2021-10-01 00:00:000' } }
const result = input.Shape({ id: Number, query: { created: (x) => new Date(x) } });
expect(result).toEqual({ id: 1, query: { created: new Date("2021-10-01T03:00:00.000Z") } })
const result: {
id: NumberConstructor;
query: {
created: (x: any) => Date;
};
}