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

@b08/functional

v1.1.2

Published

A set of 'functional programming' tools

Downloads

54

Readme

@b08/functional, seeded from @b08/library-seed, library type: dry

A set of 'functional programming' conversions

mapWith

Creates a function that maps array with provided mapper function.

function itemMapper(a: number): string { // return new value … }

// usually is written like this
function arrayMapper(a: number[]): string[] { return a.map(doSmth); }

// can now write it like this
const arrayMapper = mapWith(itemMapper);

When the mapper function has several arguments, first parameter is element of the array, the rest of the parameters are the same for each array element.

function itemMapper(a: number, b: number): string { // return new value … }

// usually is written like this
function arrayMapper(a: number[], b: number): string[] { return a.map(a1 => itemMapper(a1, b)); }

// alternatively
const arrayMapper = mapWith(itemMapper);

// or just call directly
const result: string[] = mapWith(itemMapper)([1, 2, 3], 4); 

filterWith

Creates a function that filters the array with provided predicate.

function predicate(a: number, b: number): boolean { // return bool … }

// usually is written like this
function arrayFilter(a: number[], b: number): number[] { return a.filter(a1 => predicate(a1, b)); }

// alternatively
const arrayFilter = filterWith(predicate);

// or just call directly
const result: number[] = filterWith(predicate)([1, 2, 3], 4);

filterWithNot / rejectWith

Same as filterWith, but inverts predicate result.

partial

Where it is applicable.

Sometimes you have to use functions having 2 arguments in a one-argument context. for example, here employeeMatches is used like that

export function findEmployeeById(employees: Employee[], id: EmployeeId): Employee {
	return employees.find(employee => employeeMatches(employee, id));
}

function employeeMatches(employee: Employee, id: EmployeeId): boolean {
	return employeeIdEquals(employee.id, id);
}

Suggestion

is to shorten partial application a bit, just like that:


export function findEmployeeById(employees: Employee[], id: EmployeeId): Employee {
	// instead of this
	// return employees.find(employee => employeeMatches(employee, id));
	// write this
	   return employee.find(partial(employeeMatches, id));
	// or this
	   return employee.find(partial2(employeeMatches, id));
}

Here "partial2", means that you fix the target function to having 2 parameters exactly. As well as "partial3" is fixed to 3 and "partial4" to 4 correspondingly. And "partial" has overrides for any amount of arguments in target function up to 4, which is a bit less strict, but it has limitation, you can't predefine all arguments. I could not make safe typing for that case.

Alternatively

Can use lodash _.partialRight, which is a bit longer definition, larger package, and yes, has that defect with unsafe typing for no-arguments return function.