Skip to content

Commit

Permalink
feat(core): add maxResults to Moji#search
Browse files Browse the repository at this point in the history
  • Loading branch information
ifiokjr committed Feb 15, 2021
1 parent 75b361f commit edd4a29
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 12 deletions.
5 changes: 5 additions & 0 deletions .changeset/unlucky-months-float.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@svgmoji/core': minor
---

Add `maxResults` to `Moji#search` options. The default value is `20`.
51 changes: 39 additions & 12 deletions packages/svgmoji__core/src/base-moji.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,24 +166,27 @@ export abstract class Moji {
* Search for the nearest emoji using the `match-sorter` algorithm.
*/
search(query: string, options: BaseMojiProps = {}): FlatEmoji[] {
const { excludeTone } = { ...DEFAULT_OPTIONS, ...options };
const { excludeTone, maxResults } = { ...DEFAULT_OPTIONS, ...options };
const data = excludeTone ? this.tonelessData : this.data;

if (!query) {
return data;
}

return matchSorter(data, query, {
threshold: rankings.WORD_STARTS_WITH,
keys: [
{ threshold: rankings.STARTS_WITH, key: 'shortcodes' },
(item) => item.shortcodes?.map((shortcode) => shortcode.split('_').join(' ')) ?? [],
'annotation',
'tags',
(item) => (item.subgroup ? subgroups[item.subgroup]?.split('-').join(' ') ?? '' : ''),
(item) => (item.group ? groups[item.group]?.split('-').join(' ') ?? '' : ''),
],
});
return take(
matchSorter(data, query, {
threshold: rankings.WORD_STARTS_WITH,
keys: [
{ threshold: rankings.STARTS_WITH, key: 'shortcodes' },
(item) => item.shortcodes?.map((shortcode) => shortcode.split('_').join(' ')) ?? [],
'annotation',
'tags',
(item) => (item.subgroup ? subgroups[item.subgroup]?.split('-').join(' ') ?? '' : ''),
(item) => (item.group ? groups[item.group]?.split('-').join(' ') ?? '' : ''),
],
}),
maxResults,
);
}

/**
Expand All @@ -206,6 +209,7 @@ export abstract class Moji {

const DEFAULT_OPTIONS: Required<BaseMojiProps> = {
excludeTone: false,
maxResults: 20,
};

interface BaseMojiProps {
Expand All @@ -215,4 +219,27 @@ interface BaseMojiProps {
* @default true;
*/
excludeTone?: boolean;

/**
* The maximum number of results that can be returned by a search. Set to -1 to include all
* results.
*
* @default 20
*/
maxResults?: number;
}

/**
* Takes a number of elements from the provided array starting from the
* zero-index.
*
* Set count to `-1` to include all results.
*
* @param array - the array to take from
* @param count - the number of items to take
*
*/
function take<Type>(array: Type[], count: number): Type[] {
count = Math.max(Math.min(0, count), count === -1 ? array.length : count);
return array.slice(0, count);
}

0 comments on commit edd4a29

Please sign in to comment.