Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow regular expressions to be passed to search #204

Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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([])
})
})
20 changes: 15 additions & 5 deletions src/search.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,24 @@
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)
/**
* Search for emojis containing the provided name or pattern in their name.
*/
export const search = (keyword: RegExp | string) => {
assert.any([is.default.string, is.default.regExp], keyword)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Non-actionable] Hmm, it is unfortunate to have to use the .default before every property of is. But we're on an older version of @sindresorhus/is that doesn't set up ESM defaults properly yet. I think this is reasonable for now, and a followup issue could be to bump @sindresorhus/is to latest.

Copy link
Contributor Author

@ikelax ikelax Oct 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I created an issue #205.

What does [Non-actionable] mean?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh I mean I'm not asking you to take any action in this PR. I'm not requesting changes, just noting something for the future.

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 }))
}
Loading