Skip to content

Common

Cross-cutting utilities to compose functions, handle promises, manipulate wrappers/kinds, and apply runtime conversions shared by the whole library.

How to import?

All functions are exported from the main entry or via direct import (tree-shaking friendly).

typescript
import { pipe, when, clone } from "@duplojs/utils";
import * as DCommon from "@duplojs/utils/common";
import * as C from "@duplojs/utils/common";
import { pipe, when, clone } from "@duplojs/utils/common";

Composition and piping

pipe

Composes synchronous functions by chaining a single input.

innerPipe

Prepares a reusable pipe that returns a function to apply later.

asyncPipe

Composes promises or FutureEither sequentially and returns a Promise.

asyncInnerPipe

Curried version of asyncPipe that returns a function ready to accept a value (sync or async).

forward

Convenient identity function in a chain to pass the value through unchanged.

forwardLog

Logs the value (tap) then returns it to continue the flow.

justReturn

Builds a constant function that always returns the same value.

justExec

Executes a callback immediately and returns its output.

Conditional control and predicates

when

Applies a transformation if the predicate is true (or if the type guard passes), otherwise returns the input.

whenNot

Inverts the condition and runs a function only when the predicate fails.

whenElse

Two typed branches (if/else) that return distinct outputs without losing the input type.

and

Combines multiple type guards / predicates into an intersection.

or

Combines multiple predicates into a union, handy to filter discriminated unions.

equal

Compares to one or multiple literal values with support for type guards on primitives.

isType

Type guard based on typeof, Array.isArray, iterables, etc.

asserts

Throws when a predicate fails and narrows the input type when it passes.

instanceOf

Type guard based on one or more constructors (typed instanceof).

hasKinds

Type guard that checks a value carries all expected kinds.

hasSomeKinds

Type guard that checks a value carries at least one expected kind.

truthy

Type guard that keeps only truthy values.

falsy

Type guard that keeps only falsy values.

Loops

loop

Loop controlled by next / exit callbacks with access to the counter and the previous output.

asyncLoop

Async version of loop that accepts iterations returning promises.

Promises

externalPromise

Creates a promise with its resolve/reject methods exposed to be controlled from the outside.

promiseObject

Transforms an object of promises into a promise of an object with resolved, typed values.

Others

asyncRetry

Retries an async function until a condition is met (max retries, delay, optional logging).

sleep

Async pause to wait for a certain time.

memo

Evaluates a function only once and reuses the result (lazy memoization).

memoPromise

Lazy memoization for functions returning a value or a promise.

stringToMillisecond

Parses durations ("10s", "2h", "1.5d", etc.) into milliseconds, with typed errors.

stringToBytes

Converts sizes ("10mb", "2gb", …) into a number of bytes.

escapeRegExp

Escapes special characters to build a regex from a safe string.

toRegExp

Normalizes a string, string array, or RegExp into a regular expression.

mimeType

Extension-to-MIME map (key without the dot).

interpolation

Generates typed templates with {id} placeholders and a strict mapping of replacements.

wrapValue

Wraps a value in a marked container to identify it or avoid collisions.

isWrappedValue

Type guard to check whether a value is a WrappedValue.

isRuntimeWrappedValueKey

Checks whether a string key matches the runtime marker of a WrappedValue.

toWrappedValue

Ensures a value is wrapped (idempotent if already wrapped).

unwrap

Retrieves the inner value of a wrapper.

unwrapGroup

Unwraps every value of an object.

addWrappedProperties

Dynamically adds derived properties to a wrapped value.

clone

Deep, typed clone of a value (objects, arrays, etc.).

simpleClone

Light clone to quickly duplicate a value without advanced logic.

createEnum

Creates an immutable string enum with has and toTuple helpers.

createTransformer

Creates a recursive transformer from a method name (toNative, toJSON, custom methods).

globalStore

Typed global store (singleton per key) with read/write access.

builder

Helper to create immutable builders with typed accumulation and missing-method checks.

createFormData

Creates an extended FormData from nested objects/arrays and provides helpers to flatten/rebuild deep entries.

override

Defines named overrides (methods or default values) and applies them to an interface.

Path

Path

Mini-domain for POSIX path utilities (resolution, extraction).

Path.isAbsolute

Checks whether a path is absolute.

Path.resolveFrom

Resolves a list of segments from an origin.

Path.getParentFolderPath

Returns the parent folder of a path.

Path.getBaseName

Returns the last segment of a path, with optional extension removal.

Path.getExtensionName

Returns the extension of a path, including the dot.

Released under the MIT license.