Skip to content

Commit 42c873e

Browse files
committed
Utf8Tools: return result only in same type as input in truncateToUtf8ByteLength
1 parent 8e110a7 commit 42c873e

File tree

2 files changed

+25
-19
lines changed

2 files changed

+25
-19
lines changed

src/utf8-tools/Utf8Tools.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -209,8 +209,13 @@ export class Utf8Tools {
209209
return i === bytes.length;
210210
}
211211

212+
/* eslint-disable lines-between-class-members, no-dupe-class-members */
213+
public static truncateToUtf8ByteLength(input: string, length: number, applyEllipsis?: boolean)
214+
: { result: string, didTruncate: boolean };
215+
public static truncateToUtf8ByteLength(input: Uint8Array, length: number, applyEllipsis?: boolean)
216+
: { result: Uint8Array, didTruncate: boolean };
212217
public static truncateToUtf8ByteLength(input: string | Uint8Array, length: number, applyEllipsis: boolean = true)
213-
: { truncatedString: string, truncatedBytes: Uint8Array, didTruncate: boolean } {
218+
: { result: string | Uint8Array, didTruncate: boolean } {
214219
if (length < 0) {
215220
throw new Error('Invalid byte length');
216221
}
@@ -224,8 +229,7 @@ export class Utf8Tools {
224229

225230
if (bytes.length <= length) {
226231
return {
227-
truncatedString: typeof input === 'string' ? input : Utf8Tools.utf8ByteArrayToString(input),
228-
truncatedBytes: bytes,
232+
result: input,
229233
didTruncate: false,
230234
};
231235
}
@@ -249,9 +253,9 @@ export class Utf8Tools {
249253
}
250254

251255
return {
252-
truncatedString: Utf8Tools.utf8ByteArrayToString(bytes),
253-
truncatedBytes: bytes,
256+
result: typeof input === 'string' ? Utf8Tools.utf8ByteArrayToString(bytes) : bytes,
254257
didTruncate: true,
255258
};
256259
}
260+
/* eslint-enable lines-between-class-members, no-dupe-class-members */
257261
}

tests/Utf8Tools.spec.ts

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -82,42 +82,44 @@ describe('Utf8Tools', () => {
8282
it('can truncate to utf8 byte lengths', () => {
8383
expect(() => Utf8Tools.truncateToUtf8ByteLength(asciiString, -1)).toThrow();
8484

85-
const expected = {
85+
const expected: {
86+
didTruncate: boolean,
87+
result: string | Uint8Array,
88+
} = {
8689
didTruncate: false,
87-
truncatedString: hanziString,
88-
truncatedBytes: hanziBytes,
90+
result: hanziString,
8991
};
9092
expect(Utf8Tools.truncateToUtf8ByteLength(hanziString, hanziBytes.length, true)).toEqual(expected);
9193
expect(Utf8Tools.truncateToUtf8ByteLength(hanziString, hanziBytes.length, false)).toEqual(expected);
9294
expect(Utf8Tools.truncateToUtf8ByteLength(hanziString, hanziBytes.length + 1, true)).toEqual(expected);
9395
expect(Utf8Tools.truncateToUtf8ByteLength(hanziString, hanziBytes.length + 1, false)).toEqual(expected);
9496

9597
expected.didTruncate = true;
96-
expected.truncatedString = asciiString.substring(0, asciiString.length - 1);
97-
expected.truncatedBytes = Utf8Tools.stringToUtf8ByteArray(expected.truncatedString);
98+
expected.result = asciiString.substring(0, asciiString.length - 1);
9899
expect(Utf8Tools.truncateToUtf8ByteLength(asciiString, asciiBytes.length - 1, false)).toEqual(expected);
99-
expect(Utf8Tools.truncateToUtf8ByteLength(asciiBytes, asciiBytes.length - 1, false)).toEqual(expected);
100100
expect(Utf8Tools.truncateToUtf8ByteLength(asciiString, asciiBytes.length - 1, true)).toEqual(expected);
101+
expected.result = Utf8Tools.stringToUtf8ByteArray(expected.result);
102+
expect(Utf8Tools.truncateToUtf8ByteLength(asciiBytes, asciiBytes.length - 1, false)).toEqual(expected);
101103
expect(Utf8Tools.truncateToUtf8ByteLength(asciiBytes, asciiBytes.length - 1, true)).toEqual(expected);
102104

103-
expected.truncatedString = hanziString.substring(0, hanziString.length - 1);
104-
expected.truncatedBytes = Utf8Tools.stringToUtf8ByteArray(expected.truncatedString);
105+
expected.result = hanziString.substring(0, hanziString.length - 1);
105106
expect(Utf8Tools.truncateToUtf8ByteLength(hanziString, hanziBytes.length - 1, false)).toEqual(expected);
107+
expected.result = Utf8Tools.stringToUtf8ByteArray(expected.result);
106108
expect(Utf8Tools.truncateToUtf8ByteLength(hanziBytes, hanziBytes.length - 1, false)).toEqual(expected);
107109

108-
expected.truncatedString = `${hanziString.substring(0, hanziString.length - 2)}…`;
109-
expected.truncatedBytes = Utf8Tools.stringToUtf8ByteArray(expected.truncatedString);
110+
expected.result = `${hanziString.substring(0, hanziString.length - 2)}…`;
110111
expect(Utf8Tools.truncateToUtf8ByteLength(hanziString, hanziBytes.length - 1, true)).toEqual(expected);
112+
expected.result = Utf8Tools.stringToUtf8ByteArray(expected.result);
111113
expect(Utf8Tools.truncateToUtf8ByteLength(hanziBytes, hanziBytes.length - 1, true)).toEqual(expected);
112114

113-
expected.truncatedString = '';
114-
expected.truncatedBytes = Utf8Tools.stringToUtf8ByteArray(expected.truncatedString);
115+
expected.result = '';
115116
expect(Utf8Tools.truncateToUtf8ByteLength(astralString, astralBytes.length - 1, false)).toEqual(expected);
117+
expected.result = Utf8Tools.stringToUtf8ByteArray(expected.result);
116118
expect(Utf8Tools.truncateToUtf8ByteLength(astralBytes, astralBytes.length - 1, false)).toEqual(expected);
117119

118-
expected.truncatedString = '…';
119-
expected.truncatedBytes = Utf8Tools.stringToUtf8ByteArray(expected.truncatedString);
120+
expected.result = '…';
120121
expect(Utf8Tools.truncateToUtf8ByteLength(astralString, astralBytes.length - 1, true)).toEqual(expected);
122+
expected.result = Utf8Tools.stringToUtf8ByteArray(expected.result);
121123
expect(Utf8Tools.truncateToUtf8ByteLength(astralBytes, astralBytes.length - 1, true)).toEqual(expected);
122124
});
123125
});

0 commit comments

Comments
 (0)