diff --git a/README.md b/README.md index 5ce0c40..970e2f3 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,7 @@ Uses [OpenAI's GPT-3.5](https://platform.openai.com/docs/models/gpt-3-5) `text-d - [Setup](#setup) - [API](#api) - - [`getEmojis(text: string, count?: number | undefined): string[]`](#getemojistext-string-count-number--undefined-string) + - [`getEmojis()`](#getemojis) - [Usage](#usage) ## Setup @@ -32,9 +32,15 @@ export OPENAI_API_KEY=sk-... ## API -### `getEmojis(text: string, count?: number | undefined): string[]` +### `getEmojis()` -Returns an array of emojis that best represent the given text. +| Parameter | Type | Description | +| --- | --- | --- | +| `text` | `string` | The text to get emojis for. | +| `options` | `object` | Optional options for the function. | +| `options.count` | `number` | The number of emojis to represent the text with. | + +Returns a string array of emojis that best represent the given text. Choose how many emojis to use, or let the AI decide how many are needed. @@ -47,7 +53,7 @@ Specify that you want only one emoji: ```js import { getEmojis } from 'gpt-emoji'; -getEmojis('japanese cherry blossom festival', 1); // ['🌸'] +getEmojis('japanese cherry blossom festival', { count: 1 }); // ['🌸'] ``` Allow the AI to decide how many emojis to use: diff --git a/lib/index.ts b/lib/index.ts index 788e9a8..4f76dbf 100644 --- a/lib/index.ts +++ b/lib/index.ts @@ -10,16 +10,18 @@ const openai = new OpenAIApi(configuration); * Generates the emoji(s) that best represent a given string. * This is non-deterministic. * @param text the text/concept to generate emoji(s) for - * @param count the number of emoji(s) to generate. If omitted, the AI will decide how many emojis are needed to best represent the string. + * @param options an object containing optional parameters + * @param options.count the number of emoji(s) to represent the text with. If omitted, the AI will decide how many emojis are needed to best represent the string. * @returns an array of emojis */ export async function getEmojis( text: string, - count?: number | undefined + options: { count?: number } = {} ): Promise { if (typeof text !== 'string' || text.length === 0) { throw new Error('text parameter must be a non-empty string'); } + const count = options.count; if (count && (typeof count !== 'number' || count <= 0)) { throw new Error('optional count parameter must be a positive number'); } diff --git a/test/lib/index-test.ts b/test/lib/index-test.ts index 96de85d..875f17a 100644 --- a/test/lib/index-test.ts +++ b/test/lib/index-test.ts @@ -49,7 +49,9 @@ describe('getEmojis', () => { .stub(OpenAIApi.prototype, 'createCompletion') .resolves(generateCreateCompletionResponse('❗')); - const result = await getEmojis('japanese cherry blossom festival', 1); + const result = await getEmojis('japanese cherry blossom festival', { + count: 1, + }); expect(Array.isArray(result)).toBe(true); expect(result.length).toStrictEqual(1); @@ -63,7 +65,9 @@ describe('getEmojis', () => { .stub(OpenAIApi.prototype, 'createCompletion') .resolves(generateCreateCompletionResponse('❗,😈')); - const result = await getEmojis('japanese cherry blossom festival', 2); + const result = await getEmojis('japanese cherry blossom festival', { + count: 2, + }); expect(Array.isArray(result)).toBe(true); expect(result.length).toStrictEqual(2); @@ -77,7 +81,7 @@ describe('getEmojis', () => { .stub(OpenAIApi.prototype, 'createCompletion') .resolves(generateCreateCompletionResponse(undefined)); - const result = await getEmojis('japanese cherry blossom festival', 2); + const result = await getEmojis('japanese cherry blossom festival'); expect(Array.isArray(result)).toBe(true); expect(result.length).toStrictEqual(0); @@ -95,7 +99,7 @@ describe('getEmojis', () => { it('throws when count is negative', async () => { await expect(() => - getEmojis('foo', -1) + getEmojis('foo', { count: -1 }) ).rejects.toThrowErrorMatchingInlineSnapshot( '"optional count parameter must be a positive number"' );