CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-formatjs--ecma402-abstract

A collection of implementation for ECMAScript abstract operations

Pending
Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

SecuritybySnyk

Pending

The risk profile of this skill

Overview
Eval results
Files

mathematical.mddocs/

Mathematical Operations

ECMA-262 abstract operations implemented with Decimal.js for high-precision arithmetic, type conversion, and mathematical utilities. These functions provide the mathematical foundation for internationalization operations requiring precise numeric calculations.

Note: The Decimal type used throughout these functions comes from the decimal.js library, which provides high-precision decimal arithmetic to avoid floating-point precision issues.

Capabilities

Type Conversion Operations

Core type conversion functions following ECMA-262 specification.

/**
 * Converts input to string representation according to ECMA-262
 * @param o - Value to convert to string
 * @returns String representation of the value
 * @throws TypeError for Symbol values
 */
function ToString(o: unknown): string;

/**
 * Converts input to Decimal number representation for precise arithmetic
 * @param arg - Value to convert to number
 * @returns Decimal representation of the numeric value
 * @throws TypeError for BigInt and Symbol values
 */
function ToNumber(arg: any): Decimal;

/**
 * Converts input to object representation
 * @param arg - Value to convert to object
 * @returns Object representation of the value
 * @throws TypeError for null and undefined values
 */
function ToObject<T>(arg: T): T extends null ? never : T extends undefined ? never : T;

/**
 * Converts input to primitive value with optional type preference
 * @param input - Value to convert to primitive
 * @param preferredType - Preferred conversion type ('string' or 'number')
 * @returns Primitive representation of the value
 */
function ToPrimitive<T extends 'string' | 'number' = 'string' | 'number'>(
  input: any,
  preferredType: T
): string | number | boolean | undefined | null;

Usage Examples:

import { ToString, ToNumber, ToObject, ToPrimitive } from "@formatjs/ecma402-abstract";

// String conversion
console.log(ToString(123)); // '123'
console.log(ToString(true)); // 'true'
console.log(ToString(null)); // 'null'

// Number conversion with Decimal precision
const num1 = ToNumber('123.456');
console.log(num1.toString()); // '123.456'

const num2 = ToNumber(true);
console.log(num2.toString()); // '1'

// Object conversion
const obj = ToObject('hello');
console.log(typeof obj); // 'object'

// Primitive conversion
const prim1 = ToPrimitive({ valueOf: () => 42 }, 'number');
console.log(prim1); // 42

const prim2 = ToPrimitive({ toString: () => 'text' }, 'string');
console.log(prim2); // 'text'

Internationalization Mathematical Value

Converts input to Decimal mathematical value specifically for Intl operations.

/**
 * Converts input to Decimal mathematical value for Intl operations
 * @param input - Value to convert to mathematical value
 * @returns Decimal representation suitable for internationalization operations
 */
function ToIntlMathematicalValue(input: unknown): Decimal;

Usage Examples:

import { ToIntlMathematicalValue } from "@formatjs/ecma402-abstract";

// Convert various types for Intl operations
const val1 = ToIntlMathematicalValue('123.45');
console.log(val1.toString()); // '123.45'

const val2 = ToIntlMathematicalValue(999.999);
console.log(val2.toString()); // '999.999'

const val3 = ToIntlMathematicalValue(BigInt(12345));
console.log(val3.toString()); // '12345'

Comparison and Equality Operations

Value comparison functions following ECMA-262 specification.

/**
 * Performs SameValue comparison according to ECMA-262
 * @param x - First value to compare
 * @param y - Second value to compare
 * @returns true if values are the same, false otherwise
 */
function SameValue(x: any, y: any): boolean;

Usage Examples:

import { SameValue } from "@formatjs/ecma402-abstract";

// Normal equality
console.log(SameValue(1, 1)); // true
console.log(SameValue('a', 'a')); // true

// Special cases
console.log(SameValue(NaN, NaN)); // true (unlike ===)
console.log(SameValue(+0, -0)); // false (unlike ===)
console.log(SameValue(-0, -0)); // true

Time and Date Operations

Date and time manipulation functions following ECMA-262 specification.

/**
 * Clips time value to valid range according to ECMA-262
 * @param time - Time value in milliseconds as Decimal
 * @returns Clipped time value or NaN if out of range
 */
function TimeClip(time: Decimal): Decimal;

/**
 * Calculates day number from time value
 * @param t - Time value in milliseconds
 * @returns Day number
 */
function Day(t: number): number;

/**
 * Calculates weekday from time value (0 = Sunday, 6 = Saturday)
 * @param t - Time value in milliseconds
 * @returns Weekday number (0-6)
 */
function WeekDay(t: number): number;

/**
 * Calculates year from time value
 * @param t - Time value in milliseconds
 * @returns Year number
 */
function YearFromTime(t: number): number;

/**
 * Calculates month from time value (0-11)
 * @param t - Time value in milliseconds
 * @returns Month number (0-11)
 */
function MonthFromTime(t: number): 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11;

/**
 * Calculates date from time value (1-31)
 * @param t - Time value in milliseconds
 * @returns Date number (1-31)
 */
function DateFromTime(t: number): number;

/**
 * Calculates hour from time value (0-23)
 * @param t - Time value in milliseconds
 * @returns Hour number (0-23)
 */
function HourFromTime(t: number): number;

/**
 * Calculates minute from time value (0-59)
 * @param t - Time value in milliseconds
 * @returns Minute number (0-59)
 */
function MinFromTime(t: number): number;

/**
 * Calculates second from time value (0-59)
 * @param t - Time value in milliseconds
 * @returns Second number (0-59)
 */
function SecFromTime(t: number): number;

/**
 * Calculates millisecond from time value (0-999)
 * @param t - Time value in milliseconds
 * @returns Millisecond number (0-999)
 */
function msFromTime(t: number): number;

Usage Examples:

import { 
  TimeClip, 
  Day, 
  WeekDay, 
  YearFromTime, 
  MonthFromTime,
  DateFromTime,
  HourFromTime,
  MinFromTime,
  SecFromTime 
} from "@formatjs/ecma402-abstract";

const now = Date.now();

// Time clipping
const validTime = TimeClip(new Decimal(now));
console.log(validTime.toString()); // Valid time value

// Date components
console.log(Day(now)); // Day number since epoch
console.log(WeekDay(now)); // Day of week (0-6)
console.log(YearFromTime(now)); // Current year
console.log(MonthFromTime(now)); // Current month (0-11)
console.log(DateFromTime(now)); // Current date (1-31)
console.log(HourFromTime(now)); // Current hour (0-23)
console.log(MinFromTime(now)); // Current minute (0-59)
console.log(SecFromTime(now)); // Current second (0-59)

Utility Date Functions

Additional date utility functions for leap year calculations and day counting.

/**
 * Calculates number of days from year start
 * @param y - Year number
 * @returns Day number from start of year
 */
function DayFromYear(y: number): number;

/**
 * Calculates time from year start
 * @param y - Year number
 * @returns Time in milliseconds from start of year
 */
function TimeFromYear(y: number): number;

/**
 * Calculates number of days in year (365 or 366)
 * @param y - Year number
 * @returns Number of days in the year
 */
function DaysInYear(y: number): 365 | 366;

/**
 * Calculates day within year (0-365)
 * @param t - Time value in milliseconds
 * @returns Day number within the year
 */
function DayWithinYear(t: number): number;

/**
 * Determines if year is leap year (0 or 1)
 * @param t - Time value in milliseconds
 * @returns 1 if leap year, 0 if not
 */
function InLeapYear(t: number): 0 | 1;

Object and Property Operations

Object manipulation functions following ECMA-262 specification.

/**
 * Creates array with specified length
 * @param len - Length of array to create
 * @returns New array with specified length
 */
function ArrayCreate<T = any>(len: number): T[];

/**
 * Checks if object has own property
 * @param o - Object to check
 * @param prop - Property name to check
 * @returns true if object has own property, false otherwise
 */
function HasOwnProperty(o: object, prop: string): boolean;

/**
 * Determines ECMAScript type of value
 * @param x - Value to check type of
 * @returns ECMAScript type name
 */
function Type(x: any): 
  | 'Null'
  | 'Undefined'
  | 'Object'
  | 'Number'
  | 'Boolean'
  | 'String'
  | 'Symbol'
  | 'BigInt'
  | undefined;

/**
 * Implements ordinary instanceof behavior
 * @param C - Constructor function
 * @param O - Object to test
 * @param internalSlots - Optional internal slots for bound functions
 * @returns true if O is instance of C, false otherwise
 */
function OrdinaryHasInstance(
  C: Object,
  O: any,
  internalSlots?: { boundTargetFunction: any }
): boolean;

Constants

/**
 * Decimal representation of zero
 */
const ZERO: Decimal;

Usage Examples:

import { ZERO } from "@formatjs/ecma402-abstract";

// Using mathematical constants
console.log(ZERO.toString()); // '0'

// Mathematical operations
const result = new Decimal(10).plus(ZERO);
console.log(result.toString()); // '10'

// Comparison with zero
const isZero = ZERO.equals(new Decimal(0));
console.log(isZero); // true

docs

index.md

locale-options.md

mathematical.md

number-formatting.md

utilities.md

validation.md

tile.json