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

spicy-transducers

v1.2.0

Published

Composable algorithmic transformations

Downloads

146

Readme

spicy-transducers

JavaScript library for composable algorithmic transformations.

Heavily inspired by Clojure Transducers, in case it was not obvious enough.

Be warned: Still in very early stage of development. Use at your own risk.

Why

Using transducers decouples data transformations from the actual data being transformed so that they can be easily composed and reused.

Install

npm install --save spicy-transducers
# or
yarn add spicy-transducers

Function Docs

Functions are grouped and exported based on the data type that they operate on.

Any

| Name | Usage | Notes | |-----------|-------|----------------------------------------------------------------------------------------------------------------------| | chain | chain(fn1, fn1, ...fnN)(value) | Chain n functions and provide the initial value to be passed. | | constant | constant(value) | Creates a constant function. | | fallback | fallback(fallbackValue)(testValue) | Enables you to always have a fallback value if testValue is null or undefined. | | identity | identity(value) | An identity function. |

Array

| Name | Usage | Notes | |------------|------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| map | map(mapFunction)(array) | Map function can use the same signature as Array.prototype.map(). | | reduce | reduce(reduceFunction, defaultValue)(array) | Reduce function can have the same signature as Array.prototype.reduce(). | | pop | pop(array) | Returns a new array, with the previous head removed. | | push | push(value)(array) | Returns a new array, with the new value pushed as the head. | | get | get(index)(array) | | | set | set(index, value)(array) | | | update | update(index, updateFn)(array) | The update function will get the current value of the specified index as it’s argument. |

Number

| Name | Usage | |-------------------|-------| | getNthFrom | get(array)(index) |

Object

| Name | Usage | Notes | |-------------------|-----------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------| | callMethod | call(methodName)(argument1, argument2, ...argumentN)(object) | Make sure the method exists, otherwise TypeError: value[methodName] is not a function. | | values | values(object) | Uses Object.keys() | | entries | entries(object) | Uses Object.entries() | | keys | keys(object) | Uses Object.keys() | | get | get(key)(object) | | | set | set(key, value)(object) | | | update | update(key, updateFn)(object) | The update function will get the current value of the specified key as it’s argument. | | | delete | delete(key)(object) | |

Usage Examples

Let’s say you have an object { q1: Number, q2: Number, q3: Number, q4: Number } which represents the number of sales per quarter. You want to:

  1. Flatten this structure
  2. Multiply the number of sales by their price of $99.99
  3. Sum the total profits

Let’s see how some of the functions exported by this module can help you build a reusable transducer for this data.

  import { chain, map, reduce , values } from 'spicy-transducers'; 
  const sales = { q1: 33, q2: 12, q3: 40, q4: 65 }; // our input data
  const toPrice = _ => _ * 99.99; // our multiplier
  const add = (acc, _) => acc + _; // our sum function
  
  const profitsTransducer = chain(
    values,
    map(toPrice),
    reduce(add, 0)
  );
  const result = profitsTransducers(sales); // => 14998.5

Contributors

License

Licensed under the MIT License.

Copyright © 2017-2018 SpiceFactory