A lightweight TypeScript utility library providing common type helpers and data conversion functions. Designed for consistent and predictable handling of primitive types, arrays, and objects.
npm install @akb2/types-toolsThis package provides a set of strongly typed utility functions and generic type definitions for working with various data types. It’s intended for use in TypeScript projects that require safe type conversions, validation, and lightweight functional helpers.
Determines whether a value is neither null nor undefined.
isDefined(null); // false
isDefined(undefined); // false
isDefined(0); // true
isDefined(""); // true
isDefined(false); // true
isDefined(NaN); // trueConverts any value to a floating-point number, with optional rounding.
precision = -1→ preserves the number of decimals from the input.
anyToFloat(null); // 0
anyToFloat(undefined); // 0
anyToFloat("", 56); // 56
anyToFloat("42"); // 42
anyToFloat("3.141592653589793", 0, 5); // 3.14159Converts any value to an integer. Falls back to defaultValue if conversion fails.
anyToInt(null); // 0
anyToInt(undefined); // 0
anyToInt("", 56); // 56
anyToInt("42"); // 42
anyToInt("3.141592653589793", 0); // 3Converts any value to a string. Returns defaultTitle if value is null or undefined.
anyToString(null); // ""
anyToString(undefined, "Default title"); // "Default title"
anyToString(NaN); // "NaN"
anyToString(false, "not true"); // "false"
anyToString(3.141592653589793); // "3.141592653589793"Ensures the input is an array.
If the value is already an array, it’s returned as-is.
If it’s a single value — it’s wrapped in an array.
If it’s null or undefined — returns an empty array.
anyToArray(null); // []
anyToArray(undefined); // []
anyToArray([]); // []
anyToArray(["a", "b"]); // ["a", "b"]
anyToArray("String value"); // ["String value"]Converts a value to boolean based on predefined truthy values:
"true", "on", "enabled", "1", 1, and true.
anyToBoolean("true"); // true
anyToBoolean("on"); // true
anyToBoolean("enabled"); // true
anyToBoolean("1"); // true
anyToBoolean(1); // true
anyToBoolean(true); // true
anyToBoolean(null); // false
anyToBoolean("false"); // false
anyToBoolean(false); // false
anyToBoolean("Any string"); // falseConverts any input value into a valid Date instance.
If conversion is not possible, the provided defaultDate is returned (or new Date() by default).
Conversion rules:
stringornumber→ converted vianew Date(value)- valid
Dateinstance → returned as-is - anything else → returns
defaultDate
anyToDate("2025-05-01"); // Date("2025-05-01T00:00:00.000Z")
anyToDate(1700000000000); // Date from timestamp
anyToDate(new Date("2024-01-01")); // same Date instance
anyToDate("invalid"); // returns default Date (now)
anyToDate(null); // returns default Date (now)
anyToDate({}, new Date("2000-01-01")); // returns provided fallback dateCreates an array of the specified length. Optionally fills it using a callback function.
createArray(5); // [0, 1, 2, 3, 4]
createArray(6, i => i * 2); // [0, 2, 4, 6, 8, 10]
createArray(0); // []
createArray(4, i => "Item " + i); // [Item 0, Item 1, Item 2, Item 3]
createArray(4, () => "item"); // [item, item, item, item]Generic object with string, number, or symbol keys.
type CustomObjectKey<K, V> = Record<string | number | symbol, V>;Generic string-keyed object.
type CustomObject<V> = Record<string, V>;A simple string-object mapping.
type SimpleObject = Record<string, string>;Recursive array type allowing nested arrays of any depth.
type MultiArray<T> = T[] | MultiArray<T>[];Recursive object type allowing deeply nested key-value pairs.
type MultiObject<V> = { [key: string]: V | MultiObject<V> };A numeric delta value: -1, 0, or 1.
type Delta = -1 | 0 | 1;Represents a value that can either be of type V or explicitly null.
type Nullable<V> = V | null;Represents a value that can either be of type V or undefined.
type Undefinable<V> = V | undefined;Represents a value that can either be of type V or undefined.
type NotDefinable<V> = V | null | undefined;import { anyToFloat, anyToArray, anyToBoolean, createArray } from "@akb2/types-tools";
const num = anyToFloat("3.14159", 0, 2); // 3.14
const arr = anyToArray("a"); // ["a"]
const flag = anyToBoolean("on"); // true
const seq = createArray(3, i => i + 1); // [1, 2, 3]MIT © 2025 Andrei Kobelev (@akb2)