String formatting library based on RFC specifications
npm install @imhonglu/format- 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, and safeParse methods
- Parsed objects are automatically serialized to strings when using JSON.stringify()
- Installation
- Usage
- API Reference
``bash`
npm install @imhonglu/format
For detailed examples, please refer to the API Reference.
`ts
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
}
`
- 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
- IPv4Address - IPv4 address based on RFC 2673, RFC 5954
- IPv6Address - IPv6 address based on RFC 4291, RFC 5954
- Hostname - Hostname based on RFC 1034
- IdnHostname - Hostname(IDN) based on RFC 1034, RFC 5890
- 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
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
- URITemplate - URI template based on RFC 6570
- UUID - UUID based on RFC 4122
- JsonPointer - JSON Pointer based on RFC 6901