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

Function incrementBase32

source/crockford.ts:62–88  ·  view source on GitHub ↗
(str: string)

Source from the content-addressed store, hash-verified

60}
61
62export function incrementBase32(str: string): string {
63 let done: string | undefined = undefined,
64 index = str.length,
65 char: string,
66 charIndex: number,
67 output = str;
68 const maxCharIndex = ENCODING_LEN - 1;
69 while (!done && index-- >= 0) {
70 char = output[index];
71 charIndex = ENCODING.indexOf(char);
72 if (charIndex === -1) {
73 throw new ULIDError(
74 ULIDErrorCode.Base32IncorrectEncoding,
75 "Incorrectly encoded string"
76 );
77 }
78 if (charIndex === maxCharIndex) {
79 output = replaceCharAt(output, index, ENCODING[0]);
80 continue;
81 }
82 done = replaceCharAt(output, index, ENCODING[charIndex + 1]);
83 }
84 if (typeof done === "string") {
85 return done;
86 }
87 throw new ULIDError(ULIDErrorCode.Base32IncorrectEncoding, "Failed incrementing string");
88}

Callers 2

monotonicFactoryFunction · 0.85
crockford.spec.tsFile · 0.85

Calls 1

replaceCharAtFunction · 0.85

Tested by

no test coverage detected