-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Sendgrid can't create list if name is too long (#669)
* fix: Sendgrid can't create list if name is too long * fix: Lint error
- Loading branch information
1 parent
4c1bfba
commit c5eb0a7
Showing
2 changed files
with
26 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
export function truncateNameToBytes(name: string, maxBytes: number) { | ||
const encoder = new TextEncoder() | ||
const decoder = new TextDecoder() | ||
|
||
const encoded = encoder.encode(name) | ||
if (encoded.length <= maxBytes) { | ||
return name | ||
} | ||
|
||
let truncated = encoded.slice(0, maxBytes) | ||
|
||
for (let i = maxBytes; i > 0; i--) { | ||
try { | ||
return decoder.decode(truncated) | ||
} catch (e) { | ||
truncated = truncated.slice(0, -1) | ||
} | ||
} | ||
|
||
return '' | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters