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

@chocolatey/box

v1.2.0

Published

Put a value in a box

Downloads

23

Readme

box

Build Status NPM Version

NAME

Box - put a value in a box

FEATURES

  • no dependencies
  • < 170 B minified + gzipped
  • fully typed (TypeScript)
  • CDN builds (UMD) - jsDelivr, unpkg

INSTALLATION

$ npm install @chocolatey/box

SYNOPSIS

import $ from '@chocolatey/box'

$(42)                    // Box<42>
$(42).value()            // 42
$(42).map(it => it + 1)  // Box<43>
$(42).tap(console.log)   // Box<42>
$(42).then(it => it + 1) // 43
$(42, it => it + 1)      // 43

// "*.tar.gz" -> "*.gz"
const untar = name => $(name)
    .map(it => it.split('.'))
    .tap(it => it.splice(1, 1))
    .then(it => it.join('.'))

untar('package.tar.gz') // "package.gz"

DESCRIPTION

Box puts a value in a container which exposes a handful of methods to facilitate piping values through a series of functions.

It provides a lightweight implementation of the box pattern, which allows the right-to-left flow of function composition to be expressed via the left-to-right syntax of method chaining familiar from jQuery, Lodash, promises etc.

compose

import R from 'ramda'

const fn1 = value => baz(bar(foo(value)))
const fn2 = R.compose(baz, bar, foo)

box

import $ from '@chocolatey/box'

const fn = value => $(value).map(foo).map(bar).then(baz)

Why?

Because:

composition and dot chaining are the same, and dot chaining is more ergonomic in JavaScript

Brian Lonsdorf

Why not?

If you're using Babel, pipelines can be written natively with features such as the (smart) pipeline operator, do expressions and partial application, e.g.:

import { tap } from 'lodash'

const untar = name =>
    name.split('.')
        |> tap(#, it => it.splice(1, 1))
        |> #.join('.')

If you're already using Lodash/Underscore or similar, you can use their built-in methods to implement pipelines, e.g.:

import _ from 'lodash'

const untar = name =>
    _(name)
        .split('.')
        .tap(it => it.splice(1, 1))
        .join('.')

EXPORTS

default

  • Type:
    • <T, R>(value: T, fn: (value: T) => R): R
    • <T>(value: T): Box<T>
  • Aliases: $, box
import $ from '@chocolatey/box'

$(42)               // Box<42>
$(42, it => it + 1) // 43

The default export is a function which either takes a value and puts it in a box (via Box.of) or takes a value and a function and applies the function to the value.

The latter provides a convenient shorthand for passing an argument to an IIFE, e.g.:

imperative

const counter = (function () {
    let count = 0
    return () => ++count
})()

counter() // 1
counter() // 2
counter() // 3

IIFE

const counter = (function (count) { return () => ++count })(0)

Box

const counter = $(0, count => () => ++count)

Box<T>

Static Methods

constructor

  • Type: new <T>(value: T) => Box<T>
import { Box } from '@chocolatey/box'

const box = new Box(42) // Box<42>

Creates a new Box instance containing the supplied value.

Box.of

  • Type: <T>(value: T) => Box<T>
import { Box } from '@chocolatey/box'

const box = Box.of(42)              // Box<42>
const boxes = [1, 2, 3].map(Box.of) // [Box<1>, Box<2>, Box<3>]

Returns a new Box instance containing the supplied value.

Note that of is a function which returns a Box instance rather than a method which returns an instance of its invocant, so the following are equivalent:

class MyBox extends Box {} // XXX missing `of` override

const array = [1, 2]

array.map(it => Box.of(it))   // [Box<1>, Box<2>]
array.map(it => MyBox.of(it)) // [Box<1>, Box<2>]
array.map(Box.of)             // [Box<1>, Box<2>]
array.map(MyBox.of)           // [Box<1>, Box<2>]

Instance Methods

map

  • Type: <U>(fn: (value: T) => U): Box<U>
import $ from '@chocolatey/box'

$(42).map(it => it + 1) // Box<43>

Applies the supplied function to the value and returns a new box containing the result.

tap

  • Type: <U>(fn: (value: T) => U): this
import $ from '@chocolatey/box'

$(42).tap(console.log) // Box<42>

Applies the supplied function to the value and returns the original box (the invocant). Useful to insert side effects, logging etc. into a pipeline without changing the value.

then

  • Type: <U>(fn: (value: T) => U): U
import $ from '@chocolatey/box'

$(42).then(it => it + 1) // 43

Returns the result of applying the supplied function to the value.

value

  • Type: (fn?: (value: T) => void): T
import $ from '@chocolatey/box'

$(42).value()            // 42
$(42).value(console.log) // 42

Returns the value. If an optional function is supplied, it is applied to the value before the value is returned. This is similar to tap, except the value is returned rather than the box.

DEVELOPMENT

NPM Scripts

The following NPM scripts are available:

  • build - compile the library for testing and save to the target directory
  • build:doc - generate the README's TOC (table of contents)
  • build:release - compile the library for release and save to the target directory
  • clean - remove the target directory and its contents
  • rebuild - clean the target directory and recompile the library
  • repl - launch a node REPL with the library loaded
  • test - recompile the library and run the test suite
  • test:run - run the test suite
  • typecheck - sanity check the library's type definitions

COMPATIBILITY

SEE ALSO

Libraries

  • fcf - a functional alternative to control-flow statements such as if, switch and while
  • fp-ts - functional programming in TypeScript

Videos

VERSION

1.2.0

AUTHOR

chocolateboy

COPYRIGHT AND LICENSE

Copyright © 2021 by chocolateboy.

This is free software; you can redistribute it and/or modify it under the terms of the MIT license.