2016-07-12 16:55:06 -04:00
|
|
|
/**
|
|
|
|
* @license
|
2020-05-19 15:08:49 -04:00
|
|
|
* Copyright Google LLC All Rights Reserved.
|
2016-07-12 16:55:06 -04:00
|
|
|
*
|
|
|
|
* Use of this source code is governed by an MIT-style license that can be
|
|
|
|
* found in the LICENSE file at https://angular.io/license
|
|
|
|
*/
|
|
|
|
|
2020-04-03 15:58:20 -04:00
|
|
|
import {escapeRegExp, partitionArray, splitAtColon, stringify, utf8Encode} from '../src/util';
|
2016-07-12 16:55:06 -04:00
|
|
|
|
2017-12-16 17:42:55 -05:00
|
|
|
{
|
2016-07-12 16:55:06 -04:00
|
|
|
describe('util', () => {
|
2016-08-01 14:33:35 -04:00
|
|
|
describe('splitAtColon', () => {
|
|
|
|
it('should split when a single ":" is present', () => {
|
|
|
|
expect(splitAtColon('a:b', [])).toEqual(['a', 'b']);
|
|
|
|
});
|
|
|
|
|
2020-04-08 13:14:18 -04:00
|
|
|
it('should trim parts', () => {
|
|
|
|
expect(splitAtColon(' a : b ', [])).toEqual(['a', 'b']);
|
|
|
|
});
|
2016-08-01 14:33:35 -04:00
|
|
|
|
|
|
|
it('should support multiple ":"', () => {
|
|
|
|
expect(splitAtColon('a:b:c', [])).toEqual(['a', 'b:c']);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should use the default value when no ":" is present', () => {
|
|
|
|
expect(splitAtColon('ab', ['c', 'd'])).toEqual(['c', 'd']);
|
|
|
|
});
|
|
|
|
});
|
2017-03-02 12:37:01 -05:00
|
|
|
|
|
|
|
describe('RegExp', () => {
|
|
|
|
it('should escape regexp', () => {
|
|
|
|
expect(new RegExp(escapeRegExp('b')).exec('abc')).toBeTruthy();
|
|
|
|
expect(new RegExp(escapeRegExp('b')).exec('adc')).toBeFalsy();
|
|
|
|
expect(new RegExp(escapeRegExp('a.b')).exec('a.b')).toBeTruthy();
|
|
|
|
expect(new RegExp(escapeRegExp('a.b')).exec('axb')).toBeFalsy();
|
|
|
|
});
|
|
|
|
});
|
2017-03-14 20:13:39 -04:00
|
|
|
|
|
|
|
describe('utf8encode', () => {
|
|
|
|
// tests from https://github.com/mathiasbynens/wtf-8
|
|
|
|
it('should encode to utf8', () => {
|
|
|
|
const tests = [
|
|
|
|
['abc', 'abc'],
|
|
|
|
// // 1-byte
|
|
|
|
['\0', '\0'],
|
|
|
|
// // 2-byte
|
|
|
|
['\u0080', '\xc2\x80'],
|
|
|
|
['\u05ca', '\xd7\x8a'],
|
|
|
|
['\u07ff', '\xdf\xbf'],
|
|
|
|
// // 3-byte
|
|
|
|
['\u0800', '\xe0\xa0\x80'],
|
|
|
|
['\u2c3c', '\xe2\xb0\xbc'],
|
|
|
|
['\uffff', '\xef\xbf\xbf'],
|
|
|
|
// //4-byte
|
|
|
|
['\uD800\uDC00', '\xF0\x90\x80\x80'],
|
|
|
|
['\uD834\uDF06', '\xF0\x9D\x8C\x86'],
|
|
|
|
['\uDBFF\uDFFF', '\xF4\x8F\xBF\xBF'],
|
|
|
|
// unmatched surrogate halves
|
|
|
|
// high surrogates: 0xD800 to 0xDBFF
|
|
|
|
['\uD800', '\xED\xA0\x80'],
|
|
|
|
['\uD800\uD800', '\xED\xA0\x80\xED\xA0\x80'],
|
|
|
|
['\uD800A', '\xED\xA0\x80A'],
|
|
|
|
['\uD800\uD834\uDF06\uD800', '\xED\xA0\x80\xF0\x9D\x8C\x86\xED\xA0\x80'],
|
|
|
|
['\uD9AF', '\xED\xA6\xAF'],
|
|
|
|
['\uDBFF', '\xED\xAF\xBF'],
|
|
|
|
// low surrogates: 0xDC00 to 0xDFFF
|
|
|
|
['\uDC00', '\xED\xB0\x80'],
|
|
|
|
['\uDC00\uDC00', '\xED\xB0\x80\xED\xB0\x80'],
|
|
|
|
['\uDC00A', '\xED\xB0\x80A'],
|
|
|
|
['\uDC00\uD834\uDF06\uDC00', '\xED\xB0\x80\xF0\x9D\x8C\x86\xED\xB0\x80'],
|
|
|
|
['\uDEEE', '\xED\xBB\xAE'],
|
|
|
|
['\uDFFF', '\xED\xBF\xBF'],
|
|
|
|
];
|
2020-04-08 13:14:18 -04:00
|
|
|
tests.forEach(([input, output]) => {
|
2020-11-15 09:53:05 -05:00
|
|
|
expect(utf8Encode(input).map(byte => String.fromCharCode(byte)).join('')).toEqual(output);
|
2020-04-08 13:14:18 -04:00
|
|
|
});
|
2017-03-14 20:13:39 -04:00
|
|
|
});
|
|
|
|
});
|
2019-03-22 05:42:52 -04:00
|
|
|
|
|
|
|
describe('stringify()', () => {
|
2020-04-08 13:14:18 -04:00
|
|
|
it('should handle objects with no prototype.', () => {
|
|
|
|
expect(stringify(Object.create(null))).toEqual('object');
|
|
|
|
});
|
2019-03-22 05:42:52 -04:00
|
|
|
});
|
2020-04-03 15:58:20 -04:00
|
|
|
|
|
|
|
describe('partitionArray()', () => {
|
|
|
|
it('should handle empty arrays', () => {
|
|
|
|
expect(partitionArray([], () => true)).toEqual([[], []]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should handle arrays with primitive type values', () => {
|
|
|
|
expect(partitionArray([1, 2, 3], (el: number) => el < 2)).toEqual([[1], [2, 3]]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should handle arrays of objects', () => {
|
|
|
|
expect(partitionArray([{id: 1}, {id: 2}, {id: 3}], (el: any) => el.id < 2)).toEqual([
|
|
|
|
[{id: 1}], [{id: 2}, {id: 3}]
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
});
|
2016-07-21 20:12:00 -04:00
|
|
|
});
|
2019-06-14 06:19:09 -04:00
|
|
|
}
|