Skip to content

Instantly share code, notes, and snippets.

@JoseJuan81
Last active November 28, 2019 11:15
Show Gist options
  • Save JoseJuan81/b4e857101ccdb56dace2ca1dd93a9c29 to your computer and use it in GitHub Desktop.
Save JoseJuan81/b4e857101ccdb56dace2ca1dd93a9c29 to your computer and use it in GitHub Desktop.
Libreria propia de funciones
// function que cree un nuevo objecto con solo las propiedades que quiero a partir de otro objeto
/* eslint-disable max-len */
export const compose = (...fns) => (...args) => fns.reduceRight((res, fn) => [fn.call(null, ...res)], args)[0];
export const notCommonItemsArray = (arr1, arr2) => { // retorna los valores unicos entre dos arreglos.
const len1 = arr1.length;
const len2 = arr2.length;
const minorArr = len1 < len2 ? arr1 : arr2;
const majorArr = minorArr.length === len1 ? arr2: arr1;
const newMajorArr = majorArr
minorArr.forEach(m => {
const index = newMajorArr.findIndex(i => i === m);
newMajorArr.splice(index, 1);
});
return newMajorArr;
},
export const debounce = (fn, wait = 200) => { // retarda la ejecucion de una funcion
let timeOut = null;
return function inner(...args) {
clearTimeout(timeOut);
timeOut = setTimeout(fn.bind(this, args), wait);
};
}
export const merge = (obj1, obj2) => { // mezcla las propiedades de dos objetos
const newObj = { ...obj2 };
Object.keys(obj2).forEach((key) => {
/* eslint-disable */
const propExist = obj1.hasOwnProperty(key);
if (propExist) {
newObj[key] = obj1[key];
}
});
return newObj;
}
export const isEmpty = (arg) => { // evalua si la variable esta vacia
if (Array.isArray(arg)) {
return arg.length === 0;
}
if (typeof arg === 'object' && arg !== null) {
return Object.keys(arg).length === 0;
}
if (typeof arg === 'string' || typeof arg === 'number') {
return !arg;
}
return !arg;
}
// verifica si un {}, [], string o número es nulo o está vacio
export const isNotEmpty = arg =>
export const setNewProperty = (property, value) => obj => {
const newObj = { ...obj };
newObj[property] = typeOf value === 'function' ? value(obj) : value;
return newObj;
}
// const obj = {};
// obj = setNewProperty('name', 'Jose Juan')(obj); --> obj = { name: 'Jose Juan' }
// const user = setNewProperty('name', 'Noah')({}); --> user = { name: 'Noah' }
export const getPropertysValue = prop => obj => (obj || {})[prop];
// const user = { name: 'Noah' };
// const userName = getPropertysvalue('name')(user); --> userName = 'Noah';
export const updateItemInCollection = (matchProp, el, collection) => collection.reduce((list, item) => {
if (el[matchProp] === item[matchProp]) {
return list.concat(el);
}
return list.concat(item);
}, []);
function minorAnEqualTo(val, prop) {
return function inner(obj) {
if (typeof obj === 'object') {
return val >= obj[prop];
}
return val >= obj;
};
}
export const removeItemFromArrayByIndex = (index, arr) => arr.reduce((list, item, ind) => {
const newList = [...list];
if (index === ind) {
return newList;
}
return newList.concat(item);
}, []);
// let persons = [{ id: 1, name: 'Noah' }, { id: 2, name; 'Pepe' }, { id: 3, name: 'Luciana' }];
// persons = removeItemFromArrayByIndex(1, persons); --> persons = [{ id: 1, name: 'Noah' }, { id: 3, name: 'Luciana' }];
function matchWithProperty(prop, val) {
return function inner(item) {
if (item[prop] && item[prop] === val) {
return item;
}
return {};
};
}
function getItemByIndex(index) {
return function inner(arr) {
return arr[index] || null;
};
}
// let persons = [{ id: 1, name: 'Noah' }, { id: 2, name; 'Pepe' }, { id: 3, name: 'Luciana' }];
// const noah = getItemByIndex(1)(persons); --> noah = { id: 1, name: 'Noah' };
function listByNoMatch(prop, collection) {
return function inner(list, item) {
const newList = [...list];
const exist = !!collection.find((c) => {
if (typeof c === 'object') {
return c[prop] === item[prop];
}
return c === item[prop];
});
if (!exist) {
return newList.concat(item);
}
return newList;
};
}
// const persons = [{ id: 1, name: 'Noah' }, { id: 2, name; 'Pepe' }, { id: 3, name: 'Luciana' }];
// const people = [{ id: 1, name: 'Noah' }, { id: 2, name; 'Pepe' }, { id: 3, name: 'Luciana' }, { id: 4, name: 'Jose Juan' }];
// const notCommon = people.map(listByNoMatch(name, persons)); --> notCommon = [{ id: 4, name: 'Jose Juan' }]
/* eslint-disable prefer-const */
export const equality = (...args) => {
let [prop1, prop2] = args;
if (!prop2) {
prop2 = prop1;
}
return function inner(item) {
return typeof item === 'object' ? item[prop1] === prop2 : item === prop1;
};
}
// const people = [{ id: 1, name: 'Noah' }, { id: 2, name; 'Pepe' }, { id: 3, name: 'Luciana' }, { id: 4, name: 'Jose Juan' }];
// const noah = people.find(equality('name', 'Noah')); --> noah = { id: 1, name: 'Noah' }
function createPropertyByOtherOne(...args) {
const [prop1, prop2] = args;
return function inner(item) {
let newItem = null;
if (item.length) {
[newItem] = item;
} else {
newItem = { ...item };
}
newItem[prop1] = getPropertysValue(prop2)(newItem);
return newItem;
};
}
// const product = { id: 23, color: 'red', size: 'M' }
// const newProduct = createPropertyByOtherOne('num', 'id')(product); --> newProduct = { id: 23, color: 'red', size: 'M', num: 23 }
export const getDeeper = (...args) => {
const propFlow = args.split('.');
return function inner(item) {
let newItem = { ...item };
propFlow.forEach((el) => {
newItem = (newItem || {})[el];
});
return newItem;
};
}
// const product = { id: 2, features: { size: 'L', color: 'Azul', tax: { pe: 18, ecu: 12 } }, name: 'Polo' }
// const peTaxes = getDeeper('features', 'tax', 'pe')(product); --> peTaxes = 18
function validateObject(obj) {
return obj || {};
}
export const reduce = (fn, col, acc = []) => col.reduce(fn, acc);
export const map = (fn, col) => col.map(fn);
export const find = (fn, col) => col.find(fn);
export const forEach = (fn, col) => col.forEach(fn);
export const matchBetweenArrays = (collection1, collection2) => {
var [arr1, prop1] = collection1;
var [arr2, prop2] = collection2;
var list = [];
if (prop2) {
arr1.forEach((a) => {
var val = prop1 ? a[prop1] : a;
var match = find(equality(prop2, val), arr2);
list = list.concat(match);
});
}
if(
arr1.every(a => typeof a === 'string' || typeof a === 'number') &&
arr2.every(a => typeof a === 'string' || typeof a === 'number')
) {
arr1.forEach((a) => {
var match = arr2.find(b => a === b);
list = match ? list.concat(match) : list;
});
}
return list;
}
// 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]
export const decimals = decimal => value => Math.round(value * (10 ** decimal)) / (10 ** decimal);
export const identity = item => item;
export const atLeastOneTrue = (...args) => args.some(a => a),
// al menos un item es true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment