functionallibrary
Advanced tools
Comparing version
@@ -6,2 +6,3 @@ const { equality } = require('./equality'); | ||
const everyCurried = curry(every); | ||
const equalityCurried = curry(equality); | ||
@@ -15,3 +16,5 @@ const commonsItemsBetweenArrays = (prop, collection1, collection2) => { | ||
const val = a[prop]; | ||
const match = find(equality(prop, val), bigger); | ||
const equal = equalityCurried(prop, val); | ||
console.log('equal', equal); | ||
const match = find(equal, bigger); | ||
if (match) { | ||
@@ -18,0 +21,0 @@ list = list.concat(match); |
@@ -5,3 +5,3 @@ /** | ||
* @param { function } f - Función a ejecutar si "c" es verdadero | ||
* @param { function } g - Función a ejecutar si "c" es falso | ||
* @param { function, any } g - Función a ejecutar si "c" es falso. Si no es función, es retornado tal cual. | ||
* @param { object } i - Objeto que es argumento de todas las funciones anteriores | ||
@@ -11,5 +11,6 @@ */ | ||
const conditional = typeof c === 'function' ? c(i) : c; | ||
return conditional ? f(i) : g(i); | ||
console.log('item', i, conditional); | ||
return conditional ? f(i) : typeof g === 'function' ? g(i) : g; | ||
}; | ||
module.exports.decide = decide; |
@@ -1,10 +0,33 @@ | ||
const equality = (...args) => { | ||
let [prop1, prop2] = args; | ||
if (!prop2) { | ||
prop2 = prop1; | ||
const { curry } = require('./curry'); | ||
const { getPropertysValue } = require('./getPropertysValue'); | ||
const getProp = curry(getPropertysValue); | ||
/** | ||
* @param {string, number} prop1 - propiedad a usar para comparar | ||
* @param {string, number} val2 - valor a comprar cuando se usa un item del tipo objeto | ||
* @param {object, string, number} item - cuando es un objeto se usa con val2, cuando es string o number se usa solo (val2 === null) | ||
*/ | ||
const equality = (prop1, val2, item) => { | ||
const findPropIn = typeof prop1 === 'string' ? getProp(prop1) : prop1; | ||
const val = val2 || getProp; | ||
if (typeof item === 'object' && typeof val !== 'function') { | ||
return findPropIn(item) === val; | ||
} | ||
return function inner(item) { | ||
return typeof item === 'object' ? item[prop1] === prop2 : item === prop1; | ||
}; | ||
if (typeof item === 'object' && typeof val === 'function') { | ||
return findPropIn(item) === val(item); | ||
} | ||
return item === prop1; | ||
} | ||
module.exports.equality = equality; | ||
// const equality = (...args) => { | ||
// let [prop1, prop2] = args; | ||
// if (!prop2) { | ||
// prop2 = prop1; | ||
// } | ||
// return function inner(item) { | ||
// return typeof item === 'object' ? item[prop1] === prop2 : item === prop1; | ||
// }; | ||
// } | ||
// module.exports.equality = equality; |
@@ -1,6 +0,1 @@ | ||
// const setNewProperty = (property, v) => (item, index) => { | ||
// const a = { ...item }; | ||
// a[property] = typeof v === 'function' ? v(item, index) : v; | ||
// return a; | ||
// } | ||
const setNewProperty = (property, v, item) => { | ||
@@ -7,0 +2,0 @@ const a = { ...item }; |
{ | ||
"name": "functionallibrary", | ||
"version": "2.0.4", | ||
"version": "2.0.5", | ||
"description": "funciones a usar en programacion funcional", | ||
"main": "dist/functionallibrary.umd.js", | ||
"main": "lib/functionallibrary.umd.js", | ||
"scripts": { | ||
"build": "npm run build:umd", | ||
"build:umd": "rollup --config build/rollup.config.js --format umd --file dist/functionallibrary.umd.js", | ||
"build:umd": "rollup --config build/rollup.config.js --format umd --file lib/functionallibrary.umd.js", | ||
"test": "node ./test" | ||
@@ -10,0 +10,0 @@ }, |
@@ -64,9 +64,7 @@ # Functional Library | ||
function updateDocuments() { | ||
this.documents = map( | ||
compose( | ||
setNewProperty('createdAt', new Date()), | ||
setNewProperty('customer', this.customer), | ||
), | ||
this.documents, | ||
) | ||
const setCreateAt = setNewProperty('createdAt', new Date()); | ||
const setCustomer = setNewProperty('customer', this.customer); | ||
const updatingPropsInDocuments = compose(setCreateAt, setCustomer); | ||
const update = map(updatingPropsInDocuments); | ||
this.documents = update(this.documents); | ||
} | ||
@@ -524,2 +522,14 @@ | ||
// { id:4, name: 'Tony', age: 30 }, | ||
``` | ||
```js | ||
const list2 = [ | ||
{ id:1, name: 'TOny', age: 2 }, | ||
{ id:2, name: 'Tony', age: 12 }, | ||
{ id:3, name: 'Tony', age: 20 }, | ||
{ id:4, name: 'Tony', age: 30 }, | ||
]; | ||
const is30 = equality('age', 30); | ||
const findTonyIndex = decide(isTony, is30, false); | ||
const tonyIndex = findIndex(findTonyIndex, list2); // tonyIndex = 3 | ||
``` |
const assert = require('assert').strict; | ||
const { map, decide, setNewProperty, equality, identity } = require('./../wrapper'); | ||
const { map, decide, setNewProperty, equality, identity, findIndex } = require('./../wrapper'); | ||
@@ -28,2 +28,17 @@ const list = [ | ||
); | ||
const list2 = [ | ||
{ id:1, name: 'TOny', age: 2 }, | ||
{ id:2, name: 'Tony', age: 12 }, | ||
{ id:3, name: 'Tony', age: 20 }, | ||
{ id:4, name: 'Tony', age: 30 }, | ||
]; | ||
const is30 = equality('age', 30); | ||
const findTonyIndex = decide(isTony, is30, false); | ||
const tonyIndex = findIndex(findTonyIndex, list2); | ||
assert.deepEqual( | ||
3, | ||
tonyIndex, | ||
'Lo índices no son iguales', | ||
); | ||
console.log('==> decide function Ok!') |
@@ -8,3 +8,3 @@ const assert = require('assert').strict; | ||
assert.deepEqual( | ||
equality(a)(b), | ||
equality(a, null, b), | ||
false, | ||
@@ -17,3 +17,3 @@ 'No existe igualdad', | ||
assert.deepEqual( | ||
equality(a)(b), | ||
equality(a, null, b), | ||
true, | ||
@@ -27,3 +27,3 @@ 'Existe igualdad', | ||
assert.deepEqual( | ||
equality('id', 2)(personB), | ||
equality('id', 2, personB), | ||
true, | ||
@@ -35,3 +35,3 @@ 'No Existe igualdad', | ||
assert.deepEqual( | ||
equality('id', 1)(personB), | ||
equality('id', 1, personB), | ||
false, | ||
@@ -38,0 +38,0 @@ 'Existe igualdad', |
@@ -31,3 +31,3 @@ const { allAreTrue } = require("./functions/allAreTrue"); | ||
decide: curry(decide), | ||
equality, | ||
equality: curry(equality), | ||
every: curry(every), | ||
@@ -34,0 +34,0 @@ filter: curry(filter), |
61055
4.52%1531
3.17%533
1.91%