Skip to content

Open-source document signature platform. Not affiliated with DocuSign.

TypeScript

Apply initials limits after uppercasing and to the typed default

Claude8d37db3aec91
diff --git a/src/components/signing/signing-live-shell.tsx b/src/components/signing/signing-live-shell.tsx
index ed661a6..c55fe62 100644
--- a/src/components/signing/signing-live-shell.tsx
+++ b/src/components/signing/signing-live-shell.tsx
@@ -1988,8 +1988,10 @@ function SignatureModal({
return existingEntry.value;
}
+ // The same whole-character cut as typing, so applying the default
+ // submits what editing it would.
return fieldType === "initials"
- ? getSignerInitials(signerName)
+ ? truncateToWholeCharacters(getSignerInitials(signerName), 4)
: signerName;
});
const drawPreviewMessage =
diff --git a/src/lib/initials.test.ts b/src/lib/initials.test.ts
index 9d37a7b..564931d 100644
--- a/src/lib/initials.test.ts
+++ b/src/lib/initials.test.ts
@@ -48,3 +48,8 @@ test("max and single-word length match each avatar", () => {
assert.equal(getInitials("Robin 😀", { max: 2, singleWordLength: 2 }), "RO");
assert.equal(getInitials(" ", { max: 2 }), "");
});
+
+test("uppercasing that lengthens a letter still respects max", () => {
+ assert.equal(getInitials("ßeta Roy", { max: 2 }), "SS");
+ assert.equal(getInitials("Anna ßeta Roy", { max: 2 }), "AS");
+});
diff --git a/src/lib/initials.ts b/src/lib/initials.ts
index 6c2a414..6a12304 100644
--- a/src/lib/initials.ts
+++ b/src/lib/initials.ts
@@ -28,16 +28,18 @@ export function getInitials(name: string, options: InitialsOptions): string {
const namedWords = words.filter((word) => startsWithLetterOrDigit.test(word));
const sourceWords = namedWords.length > 0 ? namedWords : words.slice(0, 1);
- if (sourceWords.length === 1 && options.singleWordLength) {
- return splitIntoWholeCharacters(sourceWords[0]!)
- .slice(0, Math.min(options.singleWordLength, options.max))
- .join("")
- .toUpperCase();
- }
+ const initials =
+ sourceWords.length === 1 && options.singleWordLength
+ ? splitIntoWholeCharacters(sourceWords[0]!)
+ .slice(0, options.singleWordLength)
+ .join("")
+ : sourceWords
+ .map((word) => splitIntoWholeCharacters(word)[0] ?? "")
+ .join("");
- return sourceWords
+ // Uppercasing can lengthen a character ("ß" becomes "SS"), so the limit
+ // is applied after it.
+ return splitIntoWholeCharacters(initials.toUpperCase())
.slice(0, options.max)
- .map((word) => splitIntoWholeCharacters(word)[0] ?? "")
- .join("")
- .toUpperCase();
+ .join("");
}