Skip to content

ref: Change ellipsis to 3 dots and update serializeKeysToEventMessage method #1829

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/utils/src/object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ export function serializeKeysToEventMessage(keys: string[], maxLength: number =
}

if (keys[0].length >= maxLength) {
return keys[0];
return truncate(keys[0], maxLength);
}

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

return '';
Expand Down
5 changes: 2 additions & 3 deletions packages/utils/src/string.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
import { isString } from './is';

/**
* Encodes given object into url-friendly format
* Truncates given string to the maximum characters count
*
* @param str An object that contains serializable values
* @param max Maximum number of characters in truncated string
* @returns string Encoded
*/

export function truncate(str: string, max: number = 0): string {
if (max === 0 || !isString(str)) {
return str;
}
return str.length <= max ? str : `${str.substr(0, max)}\u2026`;
return str.length <= max ? str : `${str.substr(0, max)}...`;
}

/**
Expand Down
38 changes: 37 additions & 1 deletion packages/utils/test/object.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
import { clone, deserialize, fill, safeNormalize, serialize, urlEncode } from '../src/object';
import {
clone,
deserialize,
fill,
safeNormalize,
serialize,
serializeKeysToEventMessage,
urlEncode,
} from '../src/object';

const MATRIX = [
{ name: 'boolean', object: true, serialized: 'true' },
Expand Down Expand Up @@ -142,6 +150,34 @@ describe('deserialize()', () => {
}
});

describe('serializeKeysToEventMessage()', () => {
test('no keys', () => {
expect(serializeKeysToEventMessage([], 10)).toEqual('[object has no keys]');
});

test('one key should be returned as a whole if not over the length limit', () => {
expect(serializeKeysToEventMessage(['foo'], 10)).toEqual('foo');
expect(serializeKeysToEventMessage(['foobarbazx'], 10)).toEqual('foobarbazx');
});

test('one key should be appended with ... and truncated when over the limit', () => {
expect(serializeKeysToEventMessage(['foobarbazqux'], 10)).toEqual('foobarbazq...');
});

test('multiple keys should be joined as a whole if not over the length limit', () => {
expect(serializeKeysToEventMessage(['foo', 'bar'], 10)).toEqual('foo, bar');
});

test('multiple keys should include only as much keys as can fit into the limit', () => {
expect(serializeKeysToEventMessage(['foo', 'bar', 'baz'], 10)).toEqual('foo, bar');
expect(serializeKeysToEventMessage(['foo', 'verylongkey', 'baz'], 10)).toEqual('foo');
});

test('multiple keys should truncate first key if its too long', () => {
expect(serializeKeysToEventMessage(['foobarbazqux', 'bar', 'baz'], 10)).toEqual('foobarbazq...');
});
});

describe('fill()', () => {
test('wraps a method by calling a replacement function on it', () => {
const source = {
Expand Down
2 changes: 1 addition & 1 deletion packages/utils/test/string.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { truncate } from '../src/string';

describe('truncate()', () => {
test('it works as expected', () => {
expect(truncate('lolol', 3)).toEqual('lol\u2026');
expect(truncate('lolol', 3)).toEqual('lol...');
expect(truncate('lolol', 10)).toEqual('lolol');
expect(truncate('lol', 3)).toEqual('lol');
expect(truncate(new Array(1000).join('f'), 0)).toEqual(new Array(1000).join('f'));
Expand Down