functionallibrary
Advanced tools
Comparing version
@@ -0,18 +1,25 @@ | ||
const { find } = require('./arrayPrototypes'); | ||
const { equality } = require('./equality'); | ||
const commonsItemsBetweenArrays = (collection1, collection2) => { | ||
var [arr1, prop1] = collection1; | ||
var [arr2, prop2] = collection2; | ||
var list = []; | ||
const [arr1, prop1] = collection1; | ||
const [arr2, prop2] = collection2; | ||
const lower = arr1.length <= arr2.length ? arr1 : arr2; | ||
const bigger = lower.length === arr1.length ? arr2 : arr1; | ||
let list = []; | ||
if (prop2) { | ||
arr1.forEach((a) => { | ||
var val = prop1 ? a[prop1] : a; | ||
var match = find(equality(prop2, val), arr2); | ||
list = list.concat(match); | ||
lower.forEach((a) => { | ||
const val = prop1 ? a[prop1] : a; | ||
const match = find(equality(prop2, val), bigger); | ||
if (match) { | ||
list = list.concat(match); | ||
} | ||
}); | ||
} | ||
if( | ||
arr1.every(a => typeof a === 'string' || typeof a === 'number') && | ||
arr2.every(a => typeof a === 'string' || typeof a === 'number') | ||
lower.every(a => typeof a === 'string' || typeof a === 'number') && | ||
bigger.every(a => typeof a === 'string' || typeof a === 'number') | ||
) { | ||
arr1.forEach((a) => { | ||
var match = arr2.find(b => a === b); | ||
lower.forEach((a) => { | ||
const match = bigger.find(b => a === b); | ||
list = match ? list.concat(match) : list; | ||
@@ -23,5 +30,2 @@ }); | ||
} | ||
// var a = [1, 2, 3, 4, 5, 6, 7, 8, 9]; | ||
// var b = [5, 6, 7, 8, 9, 10]; | ||
// matchBetweenArrays([a], [b]); --> [5, 6, 7, 8, 9] | ||
module.exports.commonsItemsBetweenArrays = commonsItemsBetweenArrays; |
@@ -1,2 +0,3 @@ | ||
const { getPropertysValue } = require("./getPropertysValue"); | ||
const { getPropertyValue } = require("./getPropertyValue"); | ||
const createPropertyByOtherOne = (...args) => { | ||
@@ -11,3 +12,3 @@ const [prop1, prop2] = args; | ||
} | ||
newItem[prop1] = getPropertysValue(prop2)(newItem); | ||
newItem[prop1] = getPropertyValue(prop2)(newItem); | ||
return newItem; | ||
@@ -14,0 +15,0 @@ }; |
@@ -5,4 +5,4 @@ const isEmpty = (arg) => { | ||
} | ||
return Object.keys(arg).length === 0; | ||
if (typeof arg === 'object' && arg !== null) { | ||
if (typeof arg === 'object' && arg !== null) { | ||
return Object.keys(arg).length === 0; | ||
} | ||
@@ -9,0 +9,0 @@ if (typeof arg === 'string' || typeof arg === 'number') { |
@@ -1,5 +0,11 @@ | ||
const returnUniqueArraysItems = (...arrs) => { // retorna los valores unicos entre arreglos. | ||
const res = [].concat(...arrs) | ||
return [...new Set(res)]; | ||
const returnUniqueArraysItems = (...arrs) => { | ||
const res = [].concat(...arrs); | ||
const unique = new Set(); | ||
res.forEach(item => { | ||
unique.add(JSON.stringify(item)) | ||
}) | ||
const uniqueParsed = []; | ||
unique.forEach(item => uniqueParsed.push(JSON.parse(item))) | ||
return [].concat(uniqueParsed); | ||
} | ||
module.exports.returnUniqueArraysItems = returnUniqueArraysItems; |
{ | ||
"name": "functionallibrary", | ||
"version": "0.0.2", | ||
"version": "1.0.0", | ||
"description": "funciones a usar en programacion funcional", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
"test": "node ./test" | ||
}, | ||
@@ -9,0 +9,0 @@ "repository": { |
280
README.md
# Functional Library | ||
Librería de funciones usadas para programación funcional. | ||
## allAreTrue | ||
```js | ||
const flag1 = true; | ||
const a = 'string'; | ||
const n = 10; | ||
allAreTrue(flag1, a, n); // true | ||
const flag1 = true; | ||
const a = 'string'; | ||
const n = 0; | ||
allAreTrue(flag1, a, n); // false | ||
const flag1 = true; | ||
const a = ''; | ||
const n = 4; | ||
allAreTrue(flag1, a, n); // false | ||
``` | ||
## arrayPrototypes | ||
***find*** | ||
```js | ||
const arr = [1, 3, 5]; | ||
const findFive = (v) => v === 5; | ||
const item = find(findFive, arr) // 5 | ||
``` | ||
***findIndex*** | ||
```js | ||
const arr = [1, 3, 5]; | ||
const findIndex = (v) => v === 5; | ||
const itemIndex = find(findIndex, arr) // 2 | ||
``` | ||
***map*** | ||
```js | ||
const arr = [1, 3, 5]; | ||
const duplicate = (v) => v * 2; | ||
const duplicateValues = map(duplicate, arr) // [2, 6, 10] | ||
``` | ||
***reduce*** | ||
```js | ||
const arr = [1, 3, 5]; | ||
const total = (acc, v) => acc + v; | ||
const totalAmount = reduce(total, arr, 0) // 9 | ||
``` | ||
## atLeastOneTrue | ||
```js | ||
const flag1 = true; | ||
const a = 'string'; | ||
const n = 10; | ||
atLeastOneTrue(flag1, a, n); // true | ||
const flag1 = true; | ||
const a = ''; | ||
const n = 0; | ||
atLeastOneTrue(flag1, a, n); // true | ||
const flag1 = false; | ||
const a = ''; | ||
const n = 0; | ||
atLeastOneTrue(flag1, a, n); // false | ||
``` | ||
## commonsIntemsBetweenArrays | ||
```js | ||
const a = [1, 2, 3, 4, 5, 6, 7, 8, 9]; | ||
const b = [5, 6, 7, 8, 9, 10]; | ||
commonsIntemsBetweenArrays([a], [b]); // [5, 6, 7, 8, 9] | ||
``` | ||
```js | ||
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']) // [{id: 1, name: 'wen'}, {id: 5, name: 'JJ'}] | ||
``` | ||
## compose | ||
```js | ||
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, | ||
) // [{id: 1, name: 'wen', flagActive: true, createAt: 'fecha de hoy', code: 'wen-1'}, | ||
// {id: 5, name: 'JJ', flagActive: true, createAt: 'fecha de hoy', code: 'JJ-5'}, | ||
// {id: 6, name: 'nene', flagActive: true, createAt: 'fecha de hoy', code: 'nene-6'}]; | ||
``` | ||
## createPropertyByOtherOne | ||
```js | ||
var persons = [ | ||
{id: 1, name: 'wen'}, | ||
{id: 5, name: 'JJ'}, | ||
{id: 6, name: 'nene'}, | ||
]; | ||
const newPersons = map( | ||
compose(createPropertyByOtherOne('code', 'id')), | ||
persons, | ||
) // [ | ||
// { id: 1, name: 'wen', code: 1 }, | ||
// { id: 5, name: 'JJ', code: 5 }, | ||
// { id: 6, name: 'nene', code: 6 }, | ||
// ] | ||
``` | ||
## equality | ||
```js | ||
const a = 2; | ||
const b = 3; | ||
equality(a)(b) // false | ||
const personA = {id: 1, name: 'wen'}; | ||
const personB = {id: 2, name: 'JJ'}; | ||
equality('id', 2)(personB) // true | ||
equality('id', 2)(personA) // false | ||
``` | ||
## getPropertyValue | ||
```js | ||
const grandMother = { | ||
name: 'ada', | ||
age: 69, | ||
child: { | ||
name: 'jose', | ||
age: 38, | ||
child: { | ||
name: 'juan', | ||
age: 11, | ||
} | ||
} | ||
} | ||
const grandChild = getPropertyValue('child.child.name')(grandMother) // 'juan' | ||
const granMothersName = getPropertyValue('name')(grandMother) // 'ada' | ||
const grandChildLastname = getPropertyValue('child.child.lastname')(grandMother) // 'undefined' | ||
getPropertyValue('child.lastname.name')(grandMother) // 'undefined' | ||
getPropertyValue('lastname.child.name')(grandMother) // 'undefined' | ||
``` | ||
## isEmpty | ||
```js | ||
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) // true | ||
isEmpty(fullArray) // false | ||
isEmpty(emptyObject) // true | ||
isEmpty(fullObject) // false | ||
isEmpty(emptyString) // true | ||
isEmpty(fullString) // false | ||
isEmpty(emptyNumber) // true | ||
isEmpty(fullNumber) // false | ||
``` | ||
## isNotEmpty | ||
```js | ||
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) // false | ||
isNotEmpty(fullArray) // true | ||
isNotEmpty(emptyObject) // false | ||
isNotEmpty(fullObject) // true | ||
isNotEmpty(emptyString) // false | ||
isNotEmpty(fullString) // true | ||
isNotEmpty(emptyNumber) // false | ||
isNotEmpty(fullNumber) // true | ||
``` | ||
## mergeObjects | ||
```js | ||
const car = { | ||
brand: 'toyota', | ||
model: 'forrunner', | ||
} | ||
const driver = { | ||
name: 'kaki', | ||
age: 33, | ||
} | ||
const road = { | ||
roadName: 'trolcal 33', | ||
long: '433 Km', | ||
} | ||
mergerObjects(car, driver, road) // { | ||
// roadName: 'trolcal 33', | ||
// long: '433 Km', | ||
// name: 'kaki', | ||
// age: 33, | ||
// brand: 'toyota', | ||
// model: 'forrunner', | ||
// } | ||
``` | ||
## removeItemFromArrayByIndex | ||
```js | ||
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)// [ | ||
// { id: 1, name: 'kaki' }, | ||
// { id: 2, name: 'churry' }, | ||
// { id: 4, name: 'javier' }, | ||
// { id: 5, name: 'juan' }, | ||
] | ||
``` | ||
## removeItemFromArrayByProp | ||
```js | ||
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)// [ | ||
// { id: 1, name: 'kaki' }, | ||
// { id: 2, name: 'churry' }, | ||
// { id: 4, name: 'javier' }, | ||
// { id: 5, name: 'juan' }, | ||
] | ||
``` | ||
## returnUniqueArraysItems | ||
```js | ||
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)// [ | ||
// {id: 1, name: 'wen'}, | ||
// {id: 2, name: 'are'}, | ||
// {id: 3, name: 'Tony'}, | ||
// {id: 4, name: 'Ada'}, | ||
// {id: 5, name: 'JJ'}, | ||
// {id: 6, name: 'nene'}, | ||
// {id: 7, name: 'jaime'}, | ||
] | ||
``` | ||
## round | ||
```js | ||
const num = 34.456789; | ||
const twoDecimals = round(2); | ||
const threeDecimals = round(3) | ||
twoDecimals(num) // 34.46 | ||
threeDecimals(num) // 34.457 | ||
``` | ||
## setNewProperty | ||
@@ -5,0 +285,0 @@ ***Basic*** |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
No tests
QualityPackage does not have any tests. This is a strong signal of a poorly maintained or low quality package.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
27855
254.21%37
76.19%666
326.92%0
-100%307
1037.04%1
Infinity%