MCPcopy Index your code
hub / github.com/ulid/javascript / encodeTime

Function encodeTime

source/ulid.ts:93–120  ·  view source on GitHub ↗
(now: number, len: number = TIME_LEN)

Source from the content-addressed store, hash-verified

91 * @returns The encoded time
92 */
93export function encodeTime(now: number, len: number = TIME_LEN): string {
94 if (isNaN(now)) {
95 throw new ULIDError(
96 ULIDErrorCode.EncodeTimeValueMalformed,
97 `Time must be a number: ${now}`
98 );
99 } else if (now > TIME_MAX) {
100 throw new ULIDError(
101 ULIDErrorCode.EncodeTimeSizeExceeded,
102 `Cannot encode a time larger than ${TIME_MAX}: ${now}`
103 );
104 } else if (now < 0) {
105 throw new ULIDError(ULIDErrorCode.EncodeTimeNegative, `Time must be positive: ${now}`);
106 } else if (Number.isInteger(now) === false) {
107 throw new ULIDError(
108 ULIDErrorCode.EncodeTimeValueMalformed,
109 `Time must be an integer: ${now}`
110 );
111 }
112 let mod: number,
113 str: string = "";
114 for (let currentLen = len; currentLen > 0; currentLen--) {
115 mod = now % ENCODING_LEN;
116 str = ENCODING.charAt(mod) + str;
117 now = (now - mod) / ENCODING_LEN;
118 }
119 return str;
120}
121
122function inWebWorker(): boolean {
123 // @ts-ignore

Callers 3

monotonicFactoryFunction · 0.85
ulidFunction · 0.85
ulid.spec.tsFile · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected