2023-01-03 19:55:46 +00:00
|
|
|
|
// Copyright 2021 Signal Messenger, LLC
|
2021-03-08 22:31:19 +00:00
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
|
|
import { assert } from 'chai';
|
|
|
|
|
|
2022-08-22 23:31:35 +00:00
|
|
|
|
import { getGraphemes, count, isSingleGrapheme } from '../../util/grapheme';
|
2021-03-08 22:31:19 +00:00
|
|
|
|
|
|
|
|
|
describe('grapheme utilities', () => {
|
2021-04-07 17:04:42 +00:00
|
|
|
|
describe('getGraphemes', () => {
|
|
|
|
|
it('returns extended graphemes in a string', () => {
|
|
|
|
|
assert.deepEqual([...getGraphemes('')], []);
|
|
|
|
|
assert.deepEqual([...getGraphemes('hello')], [...'hello']);
|
|
|
|
|
assert.deepEqual(
|
|
|
|
|
[...getGraphemes('Bokmål')],
|
|
|
|
|
['B', 'o', 'k', 'm', 'å', 'l']
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
assert.deepEqual([...getGraphemes('💩💩💩')], ['💩', '💩', '💩']);
|
|
|
|
|
assert.deepEqual([...getGraphemes('👩❤️👩')], ['👩❤️👩']);
|
|
|
|
|
assert.deepEqual([...getGraphemes('👌🏽👌🏾👌🏿')], ['👌🏽', '👌🏾', '👌🏿']);
|
|
|
|
|
|
|
|
|
|
assert.deepEqual([...getGraphemes('L̷̳͔̲͝Ģ̵̮̯̤̩̙͍̬̟͉̹̘̹͍͈̮̦̰̣͟͝O̶̴̮̻̮̗͘͡!̴̷̟͓͓')], ['L̷̳͔̲͝', 'Ģ̵̮̯̤̩̙͍̬̟͉̹̘̹͍͈̮̦̰̣͟͝', 'O̶̴̮̻̮̗͘͡', '!̴̷̟͓͓']);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2021-03-08 22:31:19 +00:00
|
|
|
|
describe('count', () => {
|
|
|
|
|
it('returns the number of extended graphemes in a string (not necessarily the length)', () => {
|
|
|
|
|
// These tests modified [from iOS][0].
|
|
|
|
|
// [0]: https://github.com/signalapp/Signal-iOS/blob/800930110b0386a4c351716c001940a3e8fac942/Signal/test/util/DisplayableTextFilterTest.swift#L40-L71
|
|
|
|
|
|
|
|
|
|
// Plain text
|
|
|
|
|
assert.strictEqual(count(''), 0);
|
|
|
|
|
assert.strictEqual(count('boring text'), 11);
|
|
|
|
|
assert.strictEqual(count('Bokmål'), 6);
|
|
|
|
|
|
|
|
|
|
// Emojis
|
|
|
|
|
assert.strictEqual(count('💩💩💩'), 3);
|
|
|
|
|
assert.strictEqual(count('👩❤️👩'), 1);
|
|
|
|
|
assert.strictEqual(count('🇹🇹🌼🇹🇹🌼🇹🇹'), 5);
|
|
|
|
|
assert.strictEqual(count('🇹🇹'), 1);
|
|
|
|
|
assert.strictEqual(count('🇹🇹 '), 2);
|
|
|
|
|
assert.strictEqual(count('👌🏽👌🏾👌🏿'), 3);
|
|
|
|
|
assert.strictEqual(count('😍'), 1);
|
|
|
|
|
assert.strictEqual(count('👩🏽'), 1);
|
|
|
|
|
assert.strictEqual(count('👾🙇💁🙅🙆🙋🙎🙍'), 8);
|
|
|
|
|
assert.strictEqual(count('🐵🙈🙉🙊'), 4);
|
|
|
|
|
assert.strictEqual(count('❤️💔💌💕💞💓💗💖💘💝💟💜💛💚💙'), 15);
|
|
|
|
|
assert.strictEqual(count('✋🏿💪🏿👐🏿🙌🏿👏🏿🙏🏿'), 6);
|
|
|
|
|
assert.strictEqual(count('🚾🆒🆓🆕🆖🆗🆙🏧'), 8);
|
|
|
|
|
assert.strictEqual(count('0️⃣1️⃣2️⃣3️⃣4️⃣5️⃣6️⃣7️⃣8️⃣9️⃣🔟'), 11);
|
|
|
|
|
assert.strictEqual(count('🇺🇸🇷🇺🇦🇫🇦🇲'), 4);
|
|
|
|
|
assert.strictEqual(count('🇺🇸🇷🇺🇸 🇦🇫🇦🇲🇸'), 7);
|
|
|
|
|
assert.strictEqual(count('🇺🇸🇷🇺🇸🇦🇫🇦🇲'), 5);
|
|
|
|
|
assert.strictEqual(count('🇺🇸🇷🇺🇸🇦'), 3);
|
|
|
|
|
assert.strictEqual(count('123'), 3);
|
|
|
|
|
|
|
|
|
|
// Normal diacritic usage
|
|
|
|
|
assert.strictEqual(count('Příliš žluťoučký kůň úpěl ďábelské ódy.'), 39);
|
|
|
|
|
|
|
|
|
|
// Excessive diacritics
|
|
|
|
|
assert.strictEqual(count('Z͑ͫ̓ͪ̂ͫ̽͏̴̙̤̞͉͚̯̞̠͍A̴̵̜̰͔ͫ͗͢L̠ͨͧͩ͘G̴̻͈͍͔̹̑͗̎̅͛́Ǫ̵̹̻̝̳͂̌̌͘'), 5);
|
|
|
|
|
assert.strictEqual(count('H҉̸̧͘͠A͢͞V̛̛I̴̸N͏̕͏G҉̵͜͏͢ ̧̧́T̶̛͘͡R̸̵̨̢̀O̷̡U͡҉B̶̛͢͞L̸̸͘͢͟É̸ ̸̛͘͏R͟È͠͞A̸͝Ḑ̕͘͜I̵͘҉͜͞N̷̡̢͠G̴͘͠ ͟͞T͏̢́͡È̀X̕҉̢̀T̢͠?̕͏̢͘͢'), 28);
|
|
|
|
|
assert.strictEqual(count('L̷̳͔̲͝Ģ̵̮̯̤̩̙͍̬̟͉̹̘̹͍͈̮̦̰̣͟͝O̶̴̮̻̮̗͘͡!̴̷̟͓͓'), 4);
|
|
|
|
|
});
|
|
|
|
|
});
|
2022-08-22 23:31:35 +00:00
|
|
|
|
|
|
|
|
|
describe('isSingleGrapheme', () => {
|
|
|
|
|
it('returns false for the empty string', () => {
|
|
|
|
|
assert.isFalse(isSingleGrapheme(''));
|
|
|
|
|
});
|
|
|
|
|
it('returns true for single graphemes', () => {
|
|
|
|
|
assert.isTrue(isSingleGrapheme('a'));
|
|
|
|
|
assert.isTrue(isSingleGrapheme('å'));
|
|
|
|
|
assert.isTrue(isSingleGrapheme('😍'));
|
|
|
|
|
});
|
|
|
|
|
it('returns false for multiple graphemes', () => {
|
|
|
|
|
assert.isFalse(isSingleGrapheme('ab'));
|
|
|
|
|
assert.isFalse(isSingleGrapheme('a😍'));
|
|
|
|
|
assert.isFalse(isSingleGrapheme('😍a'));
|
|
|
|
|
});
|
|
|
|
|
});
|
2021-03-08 22:31:19 +00:00
|
|
|
|
});
|