Skip to content

Commit 7046cbb

Browse files
committed
feat: allow RegExp objects to be passed to search that are evaluated for each emoji
1 parent 7f1ce60 commit 7046cbb

2 files changed

Lines changed: 47 additions & 6 deletions

File tree

src/search.test.ts

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ describe('search', () => {
77
expect(search('100')).toEqual([{ emoji: '💯', name: '100' }])
88
})
99

10+
it('returns a single pair when given one-of emoji name as regular expression', () => {
11+
expect(search(/100/)).toEqual([{ emoji: '💯', name: '100' }])
12+
})
13+
1014
it('returns multiple emojis when given a common substring', () => {
1115
expect(search('cartwheel')).toEqual([
1216
{
@@ -20,6 +24,19 @@ describe('search', () => {
2024
])
2125
})
2226

27+
it('returns multiple emojis when given a common regular expression', () => {
28+
expect(search(/cartwheel/)).toEqual([
29+
{
30+
emoji: '🤸‍♀️',
31+
name: 'woman_cartwheeling',
32+
},
33+
{
34+
emoji: '🤸‍♂️',
35+
name: 'man_cartwheeling',
36+
},
37+
])
38+
})
39+
2340
it('should match when you include the colon', () => {
2441
expect(search(':cartwheel:')).toEqual([
2542
{
@@ -33,7 +50,24 @@ describe('search', () => {
3350
])
3451
})
3552

36-
it('returns an empty array when no matching emojis are found', () => {
53+
it('should match when you include the colon in the regular expression', () => {
54+
expect(search(/:cartwheel:/)).toEqual([
55+
{
56+
emoji: '🤸‍♀️',
57+
name: 'woman_cartwheeling',
58+
},
59+
{
60+
emoji: '🤸‍♂️',
61+
name: 'man_cartwheeling',
62+
},
63+
])
64+
})
65+
66+
it('returns an empty array when no matching emojis are found for a string search', () => {
3767
expect(search('notAnEmoji')).toEqual([])
3868
})
69+
70+
it('returns an empty array when no matching emojis are found for a regular expression search', () => {
71+
expect(search(/notAnEmoji/)).toEqual([])
72+
})
3973
})

src/search.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,21 @@
1-
import { assert } from '@sindresorhus/is'
1+
import is, { assert } from '@sindresorhus/is'
22

33
import { emojiData } from './data.js'
44
import { normalizeName } from './utils.js'
55

6-
export const search = (keyword: string) => {
7-
assert.string(keyword)
6+
export const search = (keyword: RegExp | string) => {
7+
assert.any([is.default.string, is.default.regExp], keyword)
88

9-
keyword = normalizeName(keyword)
9+
if (is.default.string(keyword)) {
10+
keyword = normalizeName(keyword)
11+
}
12+
13+
if (is.default.regExp(keyword)) {
14+
const normalizedPattern = normalizeName(keyword.source)
15+
keyword = new RegExp(normalizedPattern)
16+
}
1017

1118
return emojiData
12-
.filter(([name]) => name.includes(keyword))
19+
.filter(([name]) => name.match(keyword))
1320
.map(([name, emoji]) => ({ emoji, name }))
1421
}

0 commit comments

Comments
 (0)