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

@sullux/fp-light-function

v0.0.1

Published

A lightweight functional library containing function helpers such as curry, memoize and pipe.

Downloads

5

Readme

fp-light/function

From the fp-light library.

npm i @sullux/fp-light-function

curry

Given a function, returns a version of the function that allows partial application of arguments.

Signature

curry(fn: Function, arity?: number = fn.length): Function
  • fn: The function to which to allow partial argument application.
  • arity: optional. The number of arguments to wait for before invoking.
  • returns: The curried function.

Examples

Uncurried vs curried:

const { curry } = require('@sullux/fp-light-function')
const { log } = console

// uncurried
const add = (first, second) => first + second

const firstValue = 2
const secondValue = 3

log(add(firstValue, secondValue)) // 5
log(add(firstValue)) // NaN

// curried
const progressiveAdd = curry(add)
const addToFirstValue = progressiveAdd(firstValue)

log(addToFirstValue()) // [Function]
log(addToFirstValue(secondValue)) // 5
log(addToFirstValue(40)) // 42

With explicit arity:

const { curry } = require('@sullux/fp-light-function')

const log = curry(console.log, 2) // wait for at least 2 args before executing
log('something') // [Function]
log('something', 'else') // something else

const debug = log('DEBUG:')

debug() // [Function]
debug('foo') // DEBUG: foo
debug('foo', 'bar') // DEBUG: foo bar

Copy/Paste

const curry = (fn, arity) =>
  (arity > -1
    ? (...args) =>
      (args.length >= arity
        ? fn(...args)
        : curry(fn.bind(null, ...args), arity - args.length))
    : curry(fn, fn.length))

memoize

Given a pure function, returns a function that will remember the result of the given function for the given arguments. In essence, given the same arguments, this will return the original value that was returned the first time this function was called with those arguments.

Signature

memoize(fn: Function): Function
  • fn: The function that caches results for a unique list of arguments.
  • returns: The curried function.

Examples

This example is intentionally bad, but illustrates the caching properties of memoization.

const { memoize } = require('@sullux/fp-light-function')

const mutableThing = 1
const increaseMutableThing = memoize(count => (mutableThing += count))

increaseMutableThing(1) // 2
increaseMutableThing(2) // 4
increaseMutableThing(38) // 42
increaseMutableThing(1) // 2
increaseMutableThing(2) // 4
increaseMutableThing(3) // 45

Copy/Paste

const memoize = (fn) => {
  const cache = {}
  const serialize = value =>
    (typeof value === 'function'
      ? value.toString()
      : JSON.stringify(value))
  return (...args) =>
    (key => (cache.hasOwnProperty(key)
      ? cache[key]
      : (cache[key] = fn(...args))))(serialize(args))
}

pipe

Given arguments of functions (and/or arrays of functions), returns a function that will pass its argument(s) to the first function; then will pass the result of that to the second function; finally returning the value returned by the last function. If any function returns a thennable, will chain the remaining function calls as .then(nextFunction) to make the async behavior transparent.

Signature

pipe(...steps: Function | Array<Function | Array<Function>>): Function
  • steps: each argument can be a function or an array of functions that will be flattened into a single array of steps (internally).
  • returns: The pipeline function.

Examples

Saving a record to DynamoDB:

const { pipe, curry } = require('@sullux/fp-light-function')
const dynamodb = new require('aws-sdk').DynamoDB()

const ddbParams = (record) => ({
  Item: {
    Title: { S: record.title },
    Artist: { S: record.artist },
    Data: { S: JSON.stringify(record) }
  },
  TableName: process.env.DDB_TABLE_NAME
})

const saveRecord = curry((ddb, params) =>
  ddb.putItem(params).promise())

const consumedCapacity = ({
  ConsumedCapacity: { TableName, WriteCapacityUnits }
}) => ({
  table: TableName,
  writeCapacityUnits: WriteCapacityUnits,
})

const debug = curry(console.log, 2)('DEBUG')

const saveToDynamo = pipe(
  ddbParams,
  saveRecord,
  consumedCapacity,
  debug
)

saveToDynamo({ title: 'Smells Like Teen Spirit', artist: 'Nirvana' })
  .then(() => console.log('done'))

Copy/Paste

const flatten = values =>
  values.reduce((flattened, value) =>
    (Array.isArray(value)
      ? flattened.concat(flatten(value))
      : flattened.concat([value])), [])

const isThennable = value =>
  value && value.then && (typeof value.then === 'function')

const pipe = (...steps) => initialValue =>
  flatten(steps).reduce((value, step) => (step
    ? isThennable(value)
      ? value.then(step)
      : step(value)
    : value), initialValue)