Skip to content

Commit

Permalink
feat: allow RegExp objects to be passed to search that are evaluated …
Browse files Browse the repository at this point in the history
…for each emoji
  • Loading branch information
ikelax committed Sep 30, 2024
1 parent 7f1ce60 commit 7046cbb
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 6 deletions.
36 changes: 35 additions & 1 deletion src/search.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ describe('search', () => {
expect(search('100')).toEqual([{ emoji: '💯', name: '100' }])
})

it('returns a single pair when given one-of emoji name as regular expression', () => {
expect(search(/100/)).toEqual([{ emoji: '💯', name: '100' }])
})

it('returns multiple emojis when given a common substring', () => {
expect(search('cartwheel')).toEqual([
{
Expand All @@ -20,6 +24,19 @@ describe('search', () => {
])
})

it('returns multiple emojis when given a common regular expression', () => {
expect(search(/cartwheel/)).toEqual([
{
emoji: '🤸‍♀️',
name: 'woman_cartwheeling',
},
{
emoji: '🤸‍♂️',
name: 'man_cartwheeling',
},
])
})

it('should match when you include the colon', () => {
expect(search(':cartwheel:')).toEqual([
{
Expand All @@ -33,7 +50,24 @@ describe('search', () => {
])
})

it('returns an empty array when no matching emojis are found', () => {
it('should match when you include the colon in the regular expression', () => {
expect(search(/:cartwheel:/)).toEqual([
{
emoji: '🤸‍♀️',
name: 'woman_cartwheeling',
},
{
emoji: '🤸‍♂️',
name: 'man_cartwheeling',
},
])
})

it('returns an empty array when no matching emojis are found for a string search', () => {
expect(search('notAnEmoji')).toEqual([])
})

it('returns an empty array when no matching emojis are found for a regular expression search', () => {
expect(search(/notAnEmoji/)).toEqual([])
})
})
17 changes: 12 additions & 5 deletions src/search.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
import { assert } from '@sindresorhus/is'
import is, { assert } from '@sindresorhus/is'

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

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

keyword = normalizeName(keyword)
if (is.default.string(keyword)) {
keyword = normalizeName(keyword)
}

if (is.default.regExp(keyword)) {
const normalizedPattern = normalizeName(keyword.source)
keyword = new RegExp(normalizedPattern)
}

return emojiData
.filter(([name]) => name.includes(keyword))
.filter(([name]) => name.match(keyword))
.map(([name, emoji]) => ({ emoji, name }))
}

0 comments on commit 7046cbb

Please sign in to comment.