Functional Library
functional programming functions.
(Librería de funciones usadas para programación funcional.)
Install
npm i functionallibrary
Use
import functionallibrary in your .js
file
import { map, setNewProperty, compose } from 'functionallibrary';
Vuejs use
in your .vue
file
<script>
import { map, setNewProperty, compose } from 'functionallibrary';
function updateDocuments() {
this.documents = map(
compose(
setNewProperty('createdAt', new Date()),
setNewProperty('customer', this.customer),
),
this.documents,
)
}
function data() {
return {
customer: {
name: 'jhon Doe',
id: '00001',
},
documents: [
{
id: 1,
num: '1234',
description: 'new shoes',
type: 'sale',
},
{
id: 2,
num: '2345',
description: 'new t-shirt',
type: 'sale',
},
],
},
}
export default {
name: 'vue-component',
data,
methods: {
updateDocuments,
},
}
</script>
the updateDocument
function result:
this.documents = [
{
id: 1,
num: '1234',
description: 'new shoes',
type: 'sale',
createdAt: '2019-12-28',
customer: {
name: 'jhon Doe',
id: '00001',
},
},
{
id: 2,
num: '2345',
description: 'new t-shirt',
type: 'sale',
createdAt: '2019-12-28',
customer: {
name: 'jhon Doe',
id: '00001',
},
},
];
Functions
allAreTrue
const flag1 = true;
const a = 'string';
const n = 10;
allAreTrue(flag1, a, n);
const flag1 = true;
const a = 'string';
const n = 0;
allAreTrue(flag1, a, n);
const flag1 = true;
const a = '';
const n = 4;
allAreTrue(flag1, a, n);
arrayPrototypes
find
const arr = [1, 3, 5];
const findFive = (v) => v === 5;
const item = find(findFive, arr)
findIndex
const arr = [1, 3, 5];
const findIndex = (v) => v === 5;
const itemIndex = find(findIndex, arr)
map
const arr = [1, 3, 5];
const duplicate = (v) => v * 2;
const duplicateValues = map(duplicate, arr)
reduce
const arr = [1, 3, 5];
const total = (acc, v) => acc + v;
const totalAmount = reduce(total, arr, 0)
atLeastOneTrue
const flag1 = true;
const a = 'string';
const n = 10;
atLeastOneTrue(flag1, a, n);
const flag1 = true;
const a = '';
const n = 0;
atLeastOneTrue(flag1, a, n);
const flag1 = false;
const a = '';
const n = 0;
atLeastOneTrue(flag1, a, n);
commonsIntemsBetweenArrays
const a = [1, 2, 3, 4, 5, 6, 7, 8, 9];
const b = [5, 6, 7, 8, 9, 10];
commonsIntemsBetweenArrays([a], [b]);
var a = [
{id: 1, name: 'wen'},
{id: 2, name: 'are'},
{id: 3, name: 'Tony'},
{id: 4, name: 'Ada'},
{id: 5, name: 'JJ'},
];
var b = [
{id: 1, name: 'wen'},
{id: 5, name: 'JJ'},
{id: 6, name: 'nene'},
];
commonsItemsBetweenArrays([a, 'name'], [b, 'name'])
compose
var persons = [
{id: 1, name: 'wen'},
{id: 5, name: 'JJ'},
{id: 6, name: 'nene'},
];
const newPersons = map(
compose(
setNewProperty('flagActive', true),
setNewProperty('createdAt', new Date()),
setNewProperty('code', { id, name } => `${name-id}`),
),
persons,
)
createPropertyByOtherOne
var persons = [
{id: 1, name: 'wen'},
{id: 5, name: 'JJ'},
{id: 6, name: 'nene'},
];
const newPersons = map(
compose(createPropertyByOtherOne('code', 'id')),
persons,
)
equality
const a = 2;
const b = 3;
equality(a)(b)
const personA = {id: 1, name: 'wen'};
const personB = {id: 2, name: 'JJ'};
equality('id', 2)(personB)
equality('id', 2)(personA)
getPropertysValue
const grandMother = {
name: 'ada',
age: 69,
child: {
name: 'jose',
age: 38,
child: {
name: 'juan',
age: 11,
}
}
}
const grandChild = getPropertysValue('child.child.name')(grandMother)
const granMothersName = getPropertysValue('name')(grandMother)
const grandChildLastname = getPropertysValue('child.child.lastname')(grandMother)
getPropertyValue('child.lastname.name')(grandMother)
getPropertyValue('lastname.child.name')(grandMother)
isEmpty
const emptyObject = {};
const fullObject = { age: 24 };
const emptyArray = [];
const fullArray = [5];
const emptyString = '';
const fullString = 'hola mundo';
const emptyNumber = 0;
const fullNumber = 3;
isEmpty(emptyArray)
isEmpty(fullArray)
isEmpty(emptyObject)
isEmpty(fullObject)
isEmpty(emptyString)
isEmpty(fullString)
isEmpty(emptyNumber)
isEmpty(fullNumber)
isNotEmpty
const emptyObject = {};
const fullObject = { age: 24 };
const emptyArray = [];
const fullArray = [5];
const emptyString = '';
const fullString = 'hola mundo';
const emptyNumber = 0;
const fullNumber = 3;
isNotEmpty(emptyArray)
isNotEmpty(fullArray)
isNotEmpty(emptyObject)
isNotEmpty(fullObject)
isNotEmpty(emptyString)
isNotEmpty(fullString)
isNotEmpty(emptyNumber)
isNotEmpty(fullNumber)
mergeObjects
const car = {
brand: 'toyota',
model: 'forrunner',
}
const driver = {
name: 'kaki',
age: 33,
}
const road = {
roadName: 'trolcal 33',
long: '433 Km',
}
mergerObjects(car, driver, road)
removeItemFromArrayByIndex
const persons = [
{ id: 1, name: 'kaki' },
{ id: 2, name: 'churry' },
{ id: 3, name: 'ada' },
{ id: 4, name: 'javier' },
{ id: 5, name: 'juan' },
];
const adaIndex = findIndex(p => p.name === 'ada', persons);
removeItemFromArrayByIndex(adaIndex, persons)
]
removeItemFromArrayByProp
const persons = [
{ id: 1, name: 'kaki' },
{ id: 2, name: 'churry' },
{ id: 3, name: 'ada' },
{ id: 4, name: 'javier' },
{ id: 5, name: 'juan' },
];
removeItemFromArrayByProp('name', 'ada')(persons)
]
returnUniqueArraysItems
var a = [
{id: 1, name: 'wen'},
{id: 2, name: 'are'},
{id: 3, name: 'Tony'},
{id: 4, name: 'Ada'},
{id: 5, name: 'JJ'},
];
var b = [
{id: 1, name: 'wen'},
{id: 5, name: 'JJ'},
{id: 6, name: 'nene'},
];
var c = [
{ id: 2, name: 'are' },
{ id: 6, name: 'nene'},
{ id: 7, name: 'jaime' },
]
returnUniqueArraysItems(a, b, c)
]
round
const num = 34.456789;
const twoDecimals = round(2);
const threeDecimals = round(3)
twoDecimals(num)
threeDecimals(num)
setNewProperty
Basic
const person = {
id: 1,
name: 'Andres',
};
setNewProperty('age', 69)(person)
result: person = { id: 1, name: 'Andres', age: 69 }
in Arrays
const persons = [
{ id: 1, name: 'Andres' },
{ id: 2, name: 'Ada' },
];
const personsUpdates = persons.map(setNewProperty('age', 69))
result:
persons = [
{ id: 1, name: 'Andres', age: 69 },
{ id: 2, name: 'Ada', age: 69 },
];