import { createOptionalEmojiRegexItem, createSetEmojiRegexItem, createUTF16EmojiRegexItem, } from '../lib/emoji/regex/base'; import { createEmojiRegexItemForNumbers, createRegexForNumbersSequence, optimiseNumbersSet, } from '../lib/emoji/regex/numbers'; describe('Creating chunks of regex for numbers', () => { it('Numbers', () => { // UTF-16 expect(createEmojiRegexItemForNumbers([0x2763])).toEqual({ type: 'utf16', regex: '\\u2763', numbers: [0x2763], group: true, }); expect( createEmojiRegexItemForNumbers([0x2761, 0x2765, 0x2764, 0x2763]) ).toEqual({ type: 'utf16', regex: '[\\u2761\\u2763-\\u2765]', numbers: [0x2761, 0x2763, 0x2764, 0x2765], group: true, }); // UTF-32 expect(createEmojiRegexItemForNumbers([0x1f49a])).toEqual({ type: 'sequence', regex: '\\uD83D\\uDC9A', items: [ { type: 'utf16', regex: '\\uD83D', numbers: [0xd83d], group: true, }, { type: 'utf16', regex: '\\uDC9A', numbers: [0xdc9a], group: true, }, ], numbers: [0x1f49a], group: false, }); // Similar ranges const items1 = createEmojiRegexItemForNumbers([ 0x1f49a, 0x1f49c, 0x1f49b, 0x1f89a, 0x1f89b, 0x1f89c, ]); delete (items1 as unknown as Record).items; expect(items1).toEqual({ type: 'sequence', regex: '[\\uD83D\\uD83E][\\uDC9A-\\uDC9C]', numbers: [0x1f49a, 0x1f49b, 0x1f49c, 0x1f89a, 0x1f89b, 0x1f89c], group: false, }); // Mismatched ranges const items2 = createEmojiRegexItemForNumbers([ 0x1f49a, 0x1f49c, 0x1f49b, 0x1f89a, 0x1f89b, 0x1f89e, ]); delete (items2 as unknown as Record).sets; expect(items2).toEqual({ type: 'set', regex: '\\uD83D[\\uDC9A-\\uDC9C]|\\uD83E[\\uDC9A\\uDC9B\\uDC9E]', numbers: [0x1f49a, 0x1f49b, 0x1f49c, 0x1f89a, 0x1f89b, 0x1f89e], group: false, }); // Mix const items3 = createEmojiRegexItemForNumbers([ 0x2763, 0x2765, 0x1f49a, 0x1f49c, 0x1f49b, 0x1f89a, 0x1f89b, 0x1f89e, 0x2764, ]); delete (items3 as unknown as Record).sets; expect(items3).toEqual({ type: 'set', regex: '[\\u2763-\\u2765]|\\uD83D[\\uDC9A-\\uDC9C]|\\uD83E[\\uDC9A\\uDC9B\\uDC9E]', numbers: [ 0x2763, 0x2764, 0x2765, 0x1f49a, 0x1f49b, 0x1f49c, 0x1f89a, 0x1f89b, 0x1f89e, ], group: false, }); }); it('Numbers sequence', () => { // UTF-16: cannot be sequence expect(createRegexForNumbersSequence([0x2763])).toEqual( createUTF16EmojiRegexItem([0x2763]) ); // UTF-32 expect(createRegexForNumbersSequence([0x1f49a])).toEqual({ type: 'sequence', regex: '\\uD83D\\uDC9A', numbers: [0x1f49a], items: [ createUTF16EmojiRegexItem([0xd83d]), createUTF16EmojiRegexItem([0xdc9a]), ], group: false, }); // Variation expect(createRegexForNumbersSequence([0x1f49a, 0xfe0f])).toEqual({ type: 'sequence', regex: '\\uD83D\\uDC9A\\uFE0F?', items: [ createUTF16EmojiRegexItem([0xd83d]), createUTF16EmojiRegexItem([0xdc9a]), createOptionalEmojiRegexItem( createUTF16EmojiRegexItem([0xfe0f]) ), ], group: false, }); expect(createRegexForNumbersSequence([0x1f49a, 0xfe0f], false)).toEqual( { type: 'sequence', regex: '\\uD83D\\uDC9A\\uFE0F', items: [ createUTF16EmojiRegexItem([0xd83d]), createUTF16EmojiRegexItem([0xdc9a]), createUTF16EmojiRegexItem([0xfe0f]), ], group: false, } ); // Variation only expect(createRegexForNumbersSequence([0xfe0f])).toEqual( createOptionalEmojiRegexItem(createUTF16EmojiRegexItem([0xfe0f])) ); }); it('Optimising set', () => { // Mix of numbers expect( optimiseNumbersSet( createSetEmojiRegexItem([ // Mandatory createUTF16EmojiRegexItem([0x2000]), createUTF16EmojiRegexItem([0x2001]), createEmojiRegexItemForNumbers([0x1f932]), // Optional createOptionalEmojiRegexItem( createUTF16EmojiRegexItem([0x2100]) ), createOptionalEmojiRegexItem( createEmojiRegexItemForNumbers([0x1f91d]) ), ]) ) ).toEqual( createSetEmojiRegexItem([ createOptionalEmojiRegexItem( createEmojiRegexItemForNumbers([0x1f91d, 0x2100]) ), createEmojiRegexItemForNumbers([0x2000, 0x2001, 0x1f932]), ]) ); // Duplicate optional and mandatory numbers expect( optimiseNumbersSet( createSetEmojiRegexItem([ // Mandatory createUTF16EmojiRegexItem([0x2000]), createUTF16EmojiRegexItem([0x2001]), createEmojiRegexItemForNumbers([0x1f932]), // Optional createOptionalEmojiRegexItem( createUTF16EmojiRegexItem([0x2001, 0x2002]) ), createOptionalEmojiRegexItem( createEmojiRegexItemForNumbers([0x1f91d]) ), ]) ) ).toEqual( createSetEmojiRegexItem([ createOptionalEmojiRegexItem( createEmojiRegexItemForNumbers([0x1f91d, 0x2001, 0x2002]) ), createEmojiRegexItemForNumbers([0x2000, 0x1f932]), ]) ); }); });