Skip to content

Composition ​

pipe, flow, and metadata that stays out of the way.

package @smitejs/fp

@smitejs/fp gives you data-first, no-tax functional composition. pipe threads a value through functions left to right; flow builds a reusable pipeline; both leave introspection metadata attached without polluting the visible object.

pipe — thread a value ​

pipe(value, ...fns) applies each function to the previous result. Start with the value, then the steps.

ts
const output = pipe(
  " smite ",
  (value) => value.trim(),
  (value) => value.toUpperCase(),
);
ts
"SMITE"

flow — build a reusable function ​

flow(...fns) returns a single composed function you can name, export, and reuse. Steps run in order when the composed function is called.

ts
const validate = (value: string) => value.trim();
const normalize = (value: string) => value.toLowerCase();
const persist = (value: string) => ({ name: value });

const createUser = flow(validate, normalize, persist);
ts
{ name: "ada" }

identity, constant, noop ​

The trivial combinators round out the toolkit: identity returns its input, constant returns a fixed value regardless of input, noop does nothing.

Composition metadata ​

Composed functions carry a non-enumerable compositionMetadata symbol recording each step's name and index. getCompositionMetadata(fn) reads it, so tooling can inspect a pipeline without executing it — and normal Object.keys, spread, and serialization never see the metadata.

ts
const validate = (value: string) => value.trim();
const normalize = (value: string) => value.toLowerCase();
const persist = (value: string) => ({ name: value });

const createUser = flow(validate, normalize, persist);
const metadata = getCompositionMetadata(createUser);
ts
{
  kind: "fp.flow",
  steps: [
    { index: 0, name: "validate" },
    { index: 1, name: "normalize" },
    { index: 2, name: "persist" },
  ],
}
[]
metadata

Released under the MIT License.

/ search•[ ] prev/next•j/k scroll•h home•t theme•guide/functional/composition