A small TypeScript utility library containing reusable functions, types, predicates, mappers, builders, and value-check helpers.
TyFun is intended to make common TypeScript development tasks a little easier by providing simple, typed helpers for object building, filtering, mapping, predicate composition, and null/undefined checks.
WARNING
This is a library undergoing active development. Use with care, as there may be incompatible changes between versions.
npm install --save korimsoft-tyfun@latestImport utilities directly from the package root:
import { aBuilder, byId, notNil, extractId, isNil } from 'korimsoft-tyfun';- Builders - fluent builders for plain TypeScript objects
- Filters / Predicates - reusable predicate functions for
filter,find, and similar operations - Mappers - helpers for extracting and transforming values
- Operators - predicate combinators such as
notandand - Value Checks - utilities for null and undefined checks
- Custom Types - reusable generic TypeScript types
Creates a fluent builder for plain TypeScript objects.
import { aBuilder } from 'korimsoft-tyfun';
type User = {
id: string;
name: string;
age: number;
};
const user = aBuilder<User>()
.withId('user-1')
.withName('Alice')
.withAge(30)
.build();You can also provide a partial template:
import { aBuilder } from 'korimsoft-tyfun';
type User = {
id: string;
name: string;
active: boolean;
};
const activeUserBuilder = aBuilder<User>({
active: true
});
const user = activeUserBuilder
.withId('user-1')
.withName('Alice')
.build();For properties that start with an uppercase letter, the generated builder method uses with_:
import { aBuilder } from 'korimsoft-tyfun';
type Config = {
URL: string;
};
const config = aBuilder<Config>()
.with_URL('https://example.com')
.build();TyFun provides predicate helpers that can be used with array methods such as filter, find, and some.
Creates a predicate that matches objects with a specific id.
import { byId } from 'korimsoft-tyfun';
const users = [
{ id: 'user-1', name: 'Alice' },
{ id: 'user-2', name: 'Bob' }
];
const user = users.find(byId('user-1'));Creates a predicate that matches objects by a property value.
import { byProperty } from 'korimsoft-tyfun';
const users = [
{ id: 'user-1', role: 'admin' },
{ id: 'user-2', role: 'user' }
];
const admins = users.filter(byProperty('role', 'admin'));Creates a predicate that matches values using deep equality.
import { byValue } from 'korimsoft-tyfun';
const values = [
{ enabled: true },
{ enabled: false },
{ enabled: true }
];
const enabledValues = values.filter(byValue({ enabled: true }));Creates a predicate that excludes null and undefined.
import { notNil } from 'korimsoft-tyfun';
const values = ['hello', null, 'world', undefined];
const definedValues = values.filter(notNil());Creates a predicate that matches null or undefined.
import { itemIsNil } from 'korimsoft-tyfun';
const values = ['hello', null, 'world', undefined];
const emptyValues = values.filter(itemIsNil());Returns a mapper that returns the input value unchanged.
import { identity } from 'korimsoft-tyfun';
const values = [1, 2, 3];
const sameValues = values.map(identity<number>());Creates a mapper that extracts a property from an object.
import { extractAProperty } from 'korimsoft-tyfun';
const users = [
{ id: 'user-1', name: 'Alice' },
{ id: 'user-2', name: 'Bob' }
];
const names = users.map(extractAProperty('name'));Extracts the id property from an object.
import { extractId } from 'korimsoft-tyfun';
const users = [
{ id: 'user-1', name: 'Alice' },
{ id: 'user-2', name: 'Bob' }
];
const ids = users.map(extractId);Creates a predicate that negates another predicate.
import { not, byProperty } from 'korimsoft-tyfun';
const users = [
{ id: 'user-1', active: true },
{ id: 'user-2', active: false }
];
const inactiveUsers = users.filter(not(byProperty('active', true)));Creates a predicate that returns true only when all provided predicates match.
import { and, byProperty } from 'korimsoft-tyfun';
const users = [
{ id: 'user-1', role: 'admin', active: true },
{ id: 'user-2', role: 'admin', active: false },
{ id: 'user-3', role: 'user', active: true }
];
const activeAdmins = users.filter(
and(
byProperty('role', 'admin'),
byProperty('active', true)
)
);Checks whether a value is null or undefined.
import { isNil } from 'korimsoft-tyfun';
isNil(null); // true
isNil(undefined); // true
isNil('hello'); // false
isNil(0); // falseTyFun exports several reusable TypeScript types.
Represents an object with an id property.
import type { ObjectWithId } from 'korimsoft-tyfun';
type User = ObjectWithId<string> & {
name: string;
};Represents an object indexed by string or number keys.
import type { ObjectMap } from 'korimsoft-tyfun';
const usersById: ObjectMap<{ name: string }> = {
'user-1': { name: 'Alice' },
'user-2': { name: 'Bob' }
};Represents a function that receives an item and returns a boolean.
import type { Predicate } from 'korimsoft-tyfun';
const isLongString: Predicate<string> = value => value.length > 10;Represents a function that maps one value to another.
import type { Mapper } from 'korimsoft-tyfun';
const stringLength: Mapper<string, number> = value => value.length;The package currently exports:
aBuilderBuilder
byIdbyPropertybyValuenotNilitemIsNil
identityextractAPropertyextractIdMapper
notand
isNil
ObjectWithIdObjectMapPredicate
MIT