npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

functionallibrary

v2.1.2

Published

funciones a usar en programacion funcional

Downloads

71

Readme

Functional Library

functional programming functions.

(Librería de funciones usadas para programación funcional.)

Install

npm i functionallibrary

Summary

allAreTrue,
atLeastOneTrue,
commonsItemsBetweenArrays,
compose,
createPropertyByOtherOne: curry(createPropertyByOtherOne),
curry,
debounce,
decide: curry(decide),
equality: curry(equality),
every: curry(every),
filter: curry(filter),
find: curry(find),
findIndex: curry(findIndex),
forEach: curry(forEach),
getPropertysValue: curry(getPropertysValue),
identity: curry(identity),
isEmpty,
isNotEmpty,
map: curry(map),
mergeObjects,
reduce: curry(reduce),
removeItemFromArrayByIndex: curry(removeItemFromArrayByIndex),
removeItemFromArrayByProp: curry(removeItemFromArrayByProp),
returnUniqueArraysItems,
round: curry(round),
setNewProperty,
some: curry(some),

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() {
		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);
	}

	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); // 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

every

	const arr = [true, true, true, true];
	const allTrue = (v) => !!v;
	every(allTrue, arr) // true

	const arr = [1, 3, 5, 'a', 10];
	const isNumber = (n) => typeof n === 'number';
	const allNumbers = every(isNumber, arr) // false

filter

	const arr = [1, 3, 5, 8, 10, 12];
	const moreThan5 = (v) => v > 5;
	const item = filter(moreThan5, arr) // [8, 10, 12]

find

	const arr = [1, 3, 5];
	const findFive = (v) => v === 5;
	const item = find(findFive, arr) // 5

findIndex

	const arr = [1, 3, 5];
	const findIndex = (v) => v === 5;
	const itemIndex = find(findIndex, arr) // 2

map

	const arr = [1, 3, 5];
	const duplicate = (v) => v * 2;
	const duplicateValues = map(duplicate, arr) // [2, 6, 10]

reduce

	const arr = [1, 3, 5];
	const total = (acc, v) => acc + v;
	const totalAmount = reduce(total, 0, arr) // 9

some

	const arr = [1, 3, 5, 'a', 10];
	const letter = (n) => typeof n === 'string';
	const anyLetter = some(letter, arr) // true

atLeastOneTrue

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

const a = [1, 2, 3, 4, 5, 6, 7, 8, 9];
const b = [5, 6, 7, 8, 9, 10];
commonsItemsBetweenArrays(null, a, b); // [5, 6, 7, 8, 9]
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('name', a, b) // [{id: 1, name: 'wen'}, {id: 5, name: 'JJ'}]

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,
) // [{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

create code using id value;

	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

/**
 * @param {string, number} prop - propiedad a usar para comparar
 * @param {string, number} val - 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)
 */
equality(prop, val, item);
const a = 2;
const b = 3;
equality(a, null, b) // false

const personA = {id: 1, name: 'wen'};
const personB = {id: 2, name: 'JJ'};
equality('id', 2, personB) // true
equality('id', 2, personA) // false
const Abuelo = {
	name: 'Andres',
	children: {
		name: 'Jose Juan',
		children: {
			name: 'Noah'
		}
	}
}
equality('children.children.name', 'Noah', Abuelo)
// true

getPropertysValue

const grandMother = {
	name: 'ada',
	age: 69,
	child: {
		name: 'jose',
		age: 38,
		child: {
			name: 'juan',
			age: 11,
		}
	}
}
const grandChild = getPropertysValue('child.child.name', grandMother) // 'juan'
const granMothersName = getPropertysValue('name', grandMother) // 'ada'
const grandChildLastname = getPropertysValue('child.child.lastname', grandMother) // 'undefined'

getPropertyValue('child.lastname.name', grandMother) // 'undefined'
getPropertyValue('lastname.child.name', grandMother) // 'undefined'

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) // true
isEmpty(fullArray) // false
isEmpty(emptyObject) // true
isEmpty(fullObject) // false
isEmpty(emptyString) // true
isEmpty(fullString) // false
isEmpty(emptyNumber) // true
isEmpty(fullNumber) // false

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) // false
isNotEmpty(fullArray) // true
isNotEmpty(emptyObject) // false
isNotEmpty(fullObject) // true
isNotEmpty(emptyString) // false
isNotEmpty(fullString) // true
isNotEmpty(emptyNumber) // false
isNotEmpty(fullNumber) // true

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) // {
	// roadName: 'trolcal 33',
	// long: '433 Km',
	// name: 'kaki',
	// age: 33,
	// brand: 'toyota',
	// model: 'forrunner',
// }

const p1 = {
	name: 'jose',
}
const p2 = {
	name: 'juan',
	age: 28,
}
const p3 = {
	name: 'Andres',
	age: 40,
	children: true,
}
mergeObjects(props1, props2, props3) // { name: 'Andres', age: 40, children: true }
mergeObjects(props3, props2, props1) // { name: 'jose', age: 28, children: true }

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)// [
	// { id: 1, name: 'kaki' },
	// { id: 2, name: 'churry' },
	// { id: 4, name: 'javier' },
	// { id: 5, name: 'juan' },
]

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)// [
	// { id: 1, name: 'kaki' },
	// { id: 2, name: 'churry' },
	// { id: 4, name: 'javier' },
	// { id: 5, name: 'juan' },
]

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)// [
	// {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

const num = 34.456789;
const twoDecimals = round(2);
const threeDecimals = round(3)
twoDecimals(num) // 34.46
threeDecimals(num) // 34.457

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 },
];

currying

const persons = [
	{ id: 1, name: 'kaki', age: 33 },
	{ id: 2, name: 'jj', age: 38 },
	{ id: 3, name: 'churry', age: 11 },
	{ id: 4, name: 'nene', age: 6 },
];
const students = [
	{ id: 1, name: 'kaki', age: 13 },
	{ id: 2, name: 'jj', age: 18 },
	{ id: 3, name: 'churry', age: 11 },
	{ id: 4, name: 'nene', age: 6 },
];
const biggerThan30 = v => v > 30;
const getAge = getPropertysValue('age');
const existAnyBodyBiggerThan30 = some(compose(biggerThan30, getAge));
const anyBodyBiggerThan30InPersons = existAnyBodyBiggerThan30(persons); // true
const anyBodyBiggerThan30InStudents = existAnyBodyBiggerThan30(students); // false

Identity

const five = 5;
identity(five) // 5

const arr = [1, 2];
identity(arr) // [1, 2]

const obj = { id: 1, code: 'abc' };
identity(obj) // { id: 1, code: 'abc' }

Decide

/**
 * 
 * @param { function | Boolean } c - Condicional. Decide cuál función ejecutar
 * @param { function } f - Función a ejecutar si "c" es verdadero
 * @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
 */
decide(c, f, g, i)
const list = [
    { id:1, name: 'Pedro' },
    { id:2, name: 'Anyela' },
    { id:3, name: 'Arelis' },
    { id:4, name: 'Tony' },
];
const setAge = setNewProperty('age', 30);
const isTony = equality('id', 4);
const addAgeToTony = decide(isTony, setAge, identity);
const updatedList = map(addAgeToTony, list);
// { id:1, name: 'Pedro' },
// { id:2, name: 'Anyela' },
// { id:3, name: 'Arelis' },
// { id:4, name: 'Tony', age: 30 },
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