Skip to content

Commit ad05df8

Browse files
kamilogorekHazAT
authored andcommitted
ref: Change ellipsis to 3 dots and update serializeKeysToEventMessage method (#1829)
1 parent b73d9e9 commit ad05df8

File tree

4 files changed

+42
-7
lines changed

4 files changed

+42
-7
lines changed

packages/utils/src/object.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ export function serializeKeysToEventMessage(keys: string[], maxLength: number =
163163
}
164164

165165
if (keys[0].length >= maxLength) {
166-
return keys[0];
166+
return truncate(keys[0], maxLength);
167167
}
168168

169169
for (let includedKeys = keys.length; includedKeys > 0; includedKeys--) {
@@ -174,7 +174,7 @@ export function serializeKeysToEventMessage(keys: string[], maxLength: number =
174174
if (includedKeys === keys.length) {
175175
return serialized;
176176
}
177-
return `${serialized}\u2026`;
177+
return truncate(serialized, maxLength);
178178
}
179179

180180
return '';

packages/utils/src/string.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
11
import { isString } from './is';
22

33
/**
4-
* Encodes given object into url-friendly format
4+
* Truncates given string to the maximum characters count
55
*
66
* @param str An object that contains serializable values
77
* @param max Maximum number of characters in truncated string
88
* @returns string Encoded
99
*/
10-
1110
export function truncate(str: string, max: number = 0): string {
1211
if (max === 0 || !isString(str)) {
1312
return str;
1413
}
15-
return str.length <= max ? str : `${str.substr(0, max)}\u2026`;
14+
return str.length <= max ? str : `${str.substr(0, max)}...`;
1615
}
1716

1817
/**

packages/utils/test/object.test.ts

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
1-
import { clone, deserialize, fill, safeNormalize, serialize, urlEncode } from '../src/object';
1+
import {
2+
clone,
3+
deserialize,
4+
fill,
5+
safeNormalize,
6+
serialize,
7+
serializeKeysToEventMessage,
8+
urlEncode,
9+
} from '../src/object';
210

311
const MATRIX = [
412
{ name: 'boolean', object: true, serialized: 'true' },
@@ -142,6 +150,34 @@ describe('deserialize()', () => {
142150
}
143151
});
144152

153+
describe('serializeKeysToEventMessage()', () => {
154+
test('no keys', () => {
155+
expect(serializeKeysToEventMessage([], 10)).toEqual('[object has no keys]');
156+
});
157+
158+
test('one key should be returned as a whole if not over the length limit', () => {
159+
expect(serializeKeysToEventMessage(['foo'], 10)).toEqual('foo');
160+
expect(serializeKeysToEventMessage(['foobarbazx'], 10)).toEqual('foobarbazx');
161+
});
162+
163+
test('one key should be appended with ... and truncated when over the limit', () => {
164+
expect(serializeKeysToEventMessage(['foobarbazqux'], 10)).toEqual('foobarbazq...');
165+
});
166+
167+
test('multiple keys should be joined as a whole if not over the length limit', () => {
168+
expect(serializeKeysToEventMessage(['foo', 'bar'], 10)).toEqual('foo, bar');
169+
});
170+
171+
test('multiple keys should include only as much keys as can fit into the limit', () => {
172+
expect(serializeKeysToEventMessage(['foo', 'bar', 'baz'], 10)).toEqual('foo, bar');
173+
expect(serializeKeysToEventMessage(['foo', 'verylongkey', 'baz'], 10)).toEqual('foo');
174+
});
175+
176+
test('multiple keys should truncate first key if its too long', () => {
177+
expect(serializeKeysToEventMessage(['foobarbazqux', 'bar', 'baz'], 10)).toEqual('foobarbazq...');
178+
});
179+
});
180+
145181
describe('fill()', () => {
146182
test('wraps a method by calling a replacement function on it', () => {
147183
const source = {

packages/utils/test/string.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { truncate } from '../src/string';
22

33
describe('truncate()', () => {
44
test('it works as expected', () => {
5-
expect(truncate('lolol', 3)).toEqual('lol\u2026');
5+
expect(truncate('lolol', 3)).toEqual('lol...');
66
expect(truncate('lolol', 10)).toEqual('lolol');
77
expect(truncate('lol', 3)).toEqual('lol');
88
expect(truncate(new Array(1000).join('f'), 0)).toEqual(new Array(1000).join('f'));

0 commit comments

Comments
 (0)