@imhonglu/format
v1.0.9
Published
String formatting library based on RFC specifications
Downloads
43
Maintainers
Readme
@imhonglu/format
Introduction
- A strongly-typed string formatting library that complies with RFC standards
- Provides an interface similar to the native JSON API
- All Formatters consistently provide
parse,stringify, andsafeParsemethods - Parsed objects are automatically serialized to strings when using JSON.stringify()
Table of Contents
Installation
npm install @imhonglu/formatUsage
For detailed examples, please refer to the API Reference.
import { FullTime } from '@imhonglu/format';
// Parse time string
const time = FullTime.parse('00:00:00.000Z');
// { hour: 0, minute: 0, second: 0, secfrac: '.000', offset: undefined }
// Using parsed results
console.log(time.hour); // 0
console.log(time.toString()); // '00:00:00.000Z'
console.log(JSON.stringify(time)); // '"00:00:00.000Z"'
// Safe parsing with validation
const result = FullTime.safeParse('invalid');
if (result.ok) {
console.log(result.data);
// result.data returns parsed FullTime instance
} else {
console.error(result.error);
// result.error returns InvalidFullTimeError with error information
}API Reference
Date Time Formatter(RFC 3339)
- DateTime - Date and time based on RFC 3339
- FullDate - Date based on RFC 3339
- FullTime - Time based on RFC 3339
- Duration - Duration based on RFC 3339
IP Address Formatter(RFC 2673, RFC 4291, RFC 5954)
- IPv4Address - IPv4 address based on RFC 2673, RFC 5954
- IPv6Address - IPv6 address based on RFC 4291, RFC 5954
Hostname Formatter(RFC 1034, RFC 5890)
- Hostname - Hostname based on RFC 1034
- IdnHostname - Hostname(IDN) based on RFC 1034, RFC 5890
Email Formatter(RFC 5321, RFC 5322)
- Mailbox - Mailbox based on RFC 5321
- IdnMailbox - Mailbox(IDN) based on RFC 5321
- AddressLiteral - Address literal based on RFC 5321
- LocalPart - Local part based on RFC 5321, RFC 5322
URI Formatter(RFC 3986, RFC 3987)
APIs that support IRI commonly provide the { isIri: boolean } option.
- URI - URI / IRI based on RFC 3986, RFC 3987
- URIReference - URI Reference / IRI Reference based on RFC 3986, RFC 3987
- Authority - Authority / IRI Authority based on RFC 3986, RFC 3987
- Path - Path / IRI Path based on RFC 3986, RFC 3987
- Query - Query / IRI Query based on RFC 3986, RFC 3987
- Fragment - Fragment / IRI Fragment based on RFC 3986, RFC 3987
- Scheme - Scheme based on RFC 3986
- IPvFuture - IPvFuture based on RFC 3986
URI Template Formatter(RFC 6570)
- URITemplate - URI template based on RFC 6570
UUID Formatter(RFC 4122)
- UUID - UUID based on RFC 4122
JSON Pointer Formatter(RFC 6901)
- JsonPointer - JSON Pointer based on RFC 6901
