Skip to content

Commit

Permalink
Merge pull request #4 from bmish/options-object
Browse files Browse the repository at this point in the history
  • Loading branch information
bmish authored Mar 17, 2023
2 parents d1bb9f3 + 9d8f33c commit bb40717
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 10 deletions.
14 changes: 10 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.

Expand All @@ -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:
Expand Down
6 changes: 4 additions & 2 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string[]> {
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');
}
Expand Down
12 changes: 8 additions & 4 deletions test/lib/index-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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"'
);
Expand Down

0 comments on commit bb40717

Please sign in to comment.