Files
dockerhub-description/src/utils.ts
Peter Evans 4b1a4bb484
Some checks failed
Publish Docker Image / publish (push) Has been cancelled
feat: truncate short description exceeding the byte limit (#143)
* feat: truncate short description exceeding the byte limit

* fix tests
2023-04-07 09:16:46 +09:00

15 lines
353 B
TypeScript

import unicodeSubstring = require('unicode-substring')
export function getErrorMessage(error: unknown) {
if (error instanceof Error) return error.message
return String(error)
}
export function truncateToBytes(s: string, n: number): string {
let len = n
while (Buffer.byteLength(s) > n) {
s = unicodeSubstring(s, 0, len--)
}
return s
}