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

f-box-core

v0.1.7

Published

Put your data in a box, and suddenly it's easier to handle, transform, and use however you need.

Downloads

452

Readme

F-Box

F-Box is a utility library for functional programming in TypeScript. It provides abstractions such as Box, RBox, Maybe, Either, and Task to simplify handling values with contexts, side effects, and asynchronous computations.

F-Boxは、TypeScript における関数型プログラミング向けのユーティリティライブラリです。BoxRBoxMaybeEitherTaskといった抽象化を提供し、文脈を持つ値、副作用、非同期計算の取り扱いを簡素化します。

| Type | Description | | -------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | Box | Represents a simple container for a value, supporting functional transformations and chaining. / 値を包むシンプルなコンテナで、関数型の変換や連結をサポートします。 | | RBox | A reactive version of Box that supports reactivity and state management. / 状態管理とリアクティビティをサポートするBoxのリアクティブ版です。 | | Maybe | Represents a value that may or may not exist (Just or Nothing). / 値が存在するかしないかを表現する型(Just または Nothing)。 | | Either | Represents a value that is either an error (Left) or a valid result (Right). / エラー(Left)または有効な結果(Right)を表す型。 | | Task | Represents an asynchronous computation that produces a result. / 非同期の計算を表現し、結果を生成する型。 |


Installation

Install via npm:

npm install f-box

Usage

Box

A container for encapsulating values, enabling functional transformations with map, flatMap, and more.

値をカプセル化し、mapflatMapなどでの関数的な変換を可能にするコンテナです。

Example

import { Box } from "f-box";

const value = Box.pack(10);
const result = value["<$>"]((x) => x * 2)["<$>"]((x) => x + 5);

console.log(result.getValue()); // Outputs: 25

RBox

A reactive container for managing state, ideal for applications requiring reactivity like React or Vue.

リアクティブな状態管理用コンテナで、React や Vue のようなリアクティブ性を必要とするアプリケーションに最適です。

Example

import { RBox } from "f-box";

const state = RBox.pack(0);

state.subscribe(console.log); // Outputs: 1
state.setValue(1);

Maybe

Represents optional values, preventing null or undefined errors with a Just or Nothing abstraction.

オプショナルな値を表現し、JustまたはNothingの抽象化によってnullundefinedのエラーを防ぎます。

Example

import { Maybe } from "f-box";

const maybeValue = Maybe.just(42);
const result = maybeValue["<$>"]((x) => x * 2).getOrElse(0);

console.log(result); // Outputs: 84

Either

Encapsulates computations that may succeed (Right) or fail (Left).

成功(Right)または失敗(Left)する可能性のある計算をカプセル化します。

Example

import { Either } from "f-box";

const divide = (a: number, b: number): Either<string, number> =>
  b === 0 ? Either.left("Division by zero") : Either.right(a / b);

const result = divide(10, 2)
  ["<$>"]((x) => x * 3)
  .getOrElse(0);

console.log(result); // Outputs: 15

Task

Handles asynchronous computations while maintaining functional style.

非同期計算を扱いつつ、関数型のスタイルを維持します。

Example

import { Task } from "f-box";

const asyncTask = Task.pack(() => Promise.resolve(10));

asyncTask["<$>"]((x) => x * 2)
  .run()
  .then((result) => {
    console.log(result); // Outputs: 20
  });

Supported Operators

Operators like <$>, <*>, and >>= are designed to make functional programming intuitive, allowing you to compose, transform, and chain operations seamlessly.

<$><*>>>=のような演算子は、関数型プログラミングを直感的にし、操作の合成、変換、および連結をシームレスに行えるように設計されています。

| Operator | Name | Description | | -------- | -------------- | --------------------------------------------------------------------------------------- | | <$> | Map | Transforms the value inside the container. Useful for applying functions to encapsulated values. / コンテナ内の値を変換します。カプセル化された値に関数を適用するのに便利です。 | | <*> | Apply | Applies a wrapped function to a wrapped value. Useful for computations involving multiple contexts. / 包まれた関数を包まれた値に適用します。複数の文脈を持つ計算に便利です。 | | >>= | FlatMap (Bind) | Chains computations while flattening results. Useful for dependent computations where the result of one step influences the next. / 計算を連結し、結果を平坦化します。ある計算の結果が次のステップに影響する依存型計算に便利です。 |


Operator Usage Examples

<$> - Map

What it does:

Applies a function to the value inside the container. This is particularly useful when you want to modify or transform the encapsulated value without unwrapping it.

用途:

コンテナ内の値に関数を適用します。値を取り出さずに変換や加工を行いたい場合に便利です。

import { Box } from "f-box";

const box = Box.pack(5);
const result = box["<$>"]((x) => x * 2);
console.log(result.getValue()); // Outputs: 10

<*> - Apply

What it does:

Allows applying a function wrapped in a container to a value wrapped in another container. This is useful in scenarios where both the function and the value are produced in separate contexts (e.g., computations or asynchronous tasks).

用途:

包まれた関数を包まれた値に適用します。関数と値がそれぞれ異なる文脈で生成される場合(例:計算や非同期タスク)に便利です。

import { Box } from "f-box";

const boxFn = Box.pack((x: number) => x + 3);
const boxValue = Box.pack(7);

const result = boxFn["<*>"](boxValue);
console.log(result.getValue()); // Outputs: 10

>>= - FlatMap

What it does:

Chains computations while handling the context. This is essential when the result of one computation determines the next step, such as when working with asynchronous tasks, error handling, or computations that may produce no result.

用途:

文脈を維持しながら計算を連結します。例えば、非同期タスク、エラー処理、結果が存在しない可能性のある計算を扱う場合に役立ちます。

import { Either } from "f-box";

const divide = (a: number, b: number): Either<string, number> =>
  b === 0 ? Either.left("Division by zero") : Either.right(a / b);

const result = Either.right(10)
  [">>="]((x) => divide(x, 2))
  [">>="]((x) => divide(x, 5));

console.log(result.getOrElse(0)); // Outputs: 1

License

This project is licensed under the MIT License - see the LICENSE file for details.