Skip to content
github-actions[bot] edited this page Aug 28, 2026 · 12 revisions

UMT Main Package

UMT Main Package is written in TypeScript and is a collection of useful functions for various tasks.

Install

npm install umt

# or

yarn add umt

# or

pnpm add umt

# or

bun add umt

v5 is ESM-only. Use import; require("umt") is not supported. Runtime matrix and migration notes: COMPATIBILITY.md.

import { isBetween, addBusinessDays, fromUnix } from "umt";
// or a subpath:
import { weekOfYear } from "umt/Date";

Date helpers

Local-time calendar helpers. Week boundaries are Sunday-start (JavaScript Date#getDay() 0). There are no UTC variants.

Function Behavior
isBetween(date, start, end, unit?, inclusivity?) Default inclusivity is "()" (exclusive both ends, matching dayjs). Ranges are not swapped: if start is after end, the result is false. Omit unit for millisecond timestamps; with a unit, all three dates are truncated via startOf first.
isSame(left, right, unit?) Same truncation rules as isBetween. Omit unit for exact getTime() equality.
addBusinessDays(date, amount, holidays?) / subBusinessDays Walks calendar days until amount weekdays (minus optional holidays compared with isSameDay) have been counted. The start date is not counted: Friday + 1 is Monday, Saturday + 1 is Monday. 0 returns a clone and does not snap to a business day. Does not mutate the input.
getQuarter(date) Local month → 14 (Jan–Mar = 1), matching startOf(..., "quarter").
weekOfYear(date) Sunday-start week index. Week 1 contains January 1 of that local year. Uses day-count rounding so DST does not shift the week number. Not ISO-8601 (Monday-start) week numbering.
fromUnix(value, unit?) / toUnix(date, unit?) Default unit is "s". toUnix(..., "s") is Math.floor(date.getTime() / 1000).

DateInclusivity is "()" | "[]" | "[)" | "(]". UnixTimeUnit is "s" | "ms". DateBoundaryUnit is second | minute | hour | day | week | month | quarter | year.

import {
  addBusinessDays,
  fromUnix,
  getQuarter,
  isBetween,
  toUnix,
  weekOfYear,
} from "umt/Date";

const start = new Date(2025, 3, 10);
const mid = new Date(2025, 3, 15);
const end = new Date(2025, 3, 20);

isBetween(mid, start, end); // true
isBetween(start, start, end); // false (exclusive default)
isBetween(start, start, end, undefined, "[]"); // true

addBusinessDays(new Date(2025, 3, 18), 1); // 2025-04-21 (Monday)
getQuarter(new Date(2025, 3, 15)); // 2
weekOfYear(new Date(2025, 0, 1)); // 1
weekOfYear(new Date(2025, 0, 5)); // 2 (Sunday)

fromUnix(0).getTime(); // 0
toUnix(new Date(1_700_000_000_999)); // 1700000000

Python and Rust ports of these helpers live in package/umt_python and package/umt_rust. Rust treats DateTime<Utc> calendar fields as wall-clock values except fromUnix / toUnix, which use real epoch timestamps. isSame exists in TypeScript only.

IP helpers

IPv4 dotted-decimal only (no IPv6). Numeric results are unsigned 32-bit values. getNetworkAddress returns a number, not a dotted string — pass it through longToIp.

Input validation is the caller's responsibility. These functions do not throw on malformed strings (see COMPATIBILITY.md). Python and Rust ports do validate.

Function Behavior
ipToLong(ip) / longToIp(long) Pack or unpack four octets. Leading zeros in an octet are accepted ("192.168.01.1" equals "192.168.1.1").
cidrToLong(cidr) / cidrToSubnetMask(cidr) Prefix length 032 to a mask number / dotted mask. CIDR 0 is 0 ("0.0.0.0").
subnetMaskToCidr(mask) Counts set bits. Does not require a contiguous mask: "255.0.255.0" returns 16.
isInRange(ip, network, cidr) (ip & mask) === (network & mask). CIDR 0 matches every IPv4 address.
isPrivateIp(ip) RFC 1918 only: 10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16. Loopback (127.0.0.1) and link-local (169.254.0.0/16) are not private.
getIpClass(ip) Classful first-octet lookup (AE). 0.0.0.0 and malformed input return "".
getNetworkAddress(ip, mask) ipToLong(ip) & cidrToLong(subnetMaskToCidr(mask)) as an unsigned 32-bit number.
ipToBinaryString(ip) 32-character 0/1 string, eight bits per octet.
import {
  cidrToSubnetMask,
  getNetworkAddress,
  ipToLong,
  isInRange,
  isPrivateIp,
  longToIp,
} from "umt/IP";

ipToLong("192.168.1.1"); // 3232235777
longToIp(3232235777); // "192.168.1.1"
cidrToSubnetMask(24); // "255.255.255.0"
isInRange("192.168.1.2", "192.168.1.0", 24); // true
isPrivateIp("10.0.0.1"); // true
isPrivateIp("127.0.0.1"); // false
longToIp(getNetworkAddress("192.168.1.1", "255.255.255.0")); // "192.168.1.0"

Python and Rust ports live in package/umt_python and package/umt_rust. Those ports raise / return Err on malformed input and reject non-contiguous subnet masks. They are not exposed through umt-plugin-wasm: Rust IP functions are not named umt_*, so wasm codegen ignores them.

Function List

Advance

Array

Async

Color

Consts

Crypto

DataStructure

Date

Decorator

Error

Function

IP

Iterator

Map

Math

Number

Object

Predicate

Random

Simple

String

Time

Tool

UA

Unit

URL

Validate

API

Classes

Interfaces

Type Aliases

Variables

Functions

Clone this wiki locally