Skip to content

Commit

Permalink
Merge pull request #113 from JoshuaKGoldberg/v2-refactor-refactor
Browse files Browse the repository at this point in the history
v2 Refactor, Refactored
  • Loading branch information
omnidan committed May 15, 2023
2 parents 3536692 + b4cd722 commit 8752c68
Show file tree
Hide file tree
Showing 14 changed files with 5,952 additions and 4,189 deletions.
9 changes: 2 additions & 7 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,2 @@
node_modules
*.log
*.sublime-project
*.sublime-workspace
*.txt
test.js
coverage
coverage/
node_modules/
5 changes: 5 additions & 0 deletions .prettierrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"arrowParens": "avoid",
"semi": false,
"singleQuote": true
}
4 changes: 3 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
language: node_js

node_js:
- "0.10"
- "lts/*"
- "node"
248 changes: 194 additions & 54 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,88 +1,228 @@
# node-emoji

[![NPM version (1.0.3)](https://img.shields.io/npm/v/node-emoji.svg?style=flat-square)](https://www.npmjs.com/package/node-emoji) [![NPM Downloads](https://img.shields.io/npm/dm/node-emoji.svg?style=flat-square)](https://www.npmjs.com/package/node-emoji) [![Build Status](https://img.shields.io/travis/omnidan/node-emoji/master.svg?style=flat-square)](https://travis-ci.org/omnidan/node-emoji) [![Dependencies](https://img.shields.io/david/omnidan/node-emoji.svg?style=flat-square)](https://david-dm.org/omnidan/node-emoji) [![https://paypal.me/DanielBugl/9](https://img.shields.io/badge/donate-paypal-yellow.svg?style=flat-square)](https://paypal.me/DanielBugl/9)
[![NPM Version](https://img.shields.io/npm/v/node-emoji.svg?style=flat-square)](https://www.npmjs.com/package/node-emoji)
[![NPM Downloads](https://img.shields.io/npm/dm/node-emoji.svg?style=flat-square)](https://www.npmjs.com/package/node-emoji)
[![Build Status](https://img.shields.io/travis/omnidan/node-emoji/master.svg?style=flat-square)](https://travis-ci.org/omnidan/node-emoji)
[![https://paypal.me/DanielBugl/9](https://img.shields.io/badge/donate-paypal-yellow.svg?style=flat-square)](https://paypal.me/DanielBugl/9)

_simple emoji support for node.js projects_
_Friendly emoji lookups and parsing utilities for Node.js._ ✨

![node-emoji example](https://i.imgur.com/yIo5Uux.png)
`node-emoji` provides a fun, straightforward interface on top of the following excellent libraries:

**Help wanted:** We are looking for volunteers to maintain this project, if you are interested, feel free to contact me at [me@omnidan.net](mailto:me@omnidan.net)
- [`emojilib`](https://npmjs.org/package/emojilib): provides a list of emojis and keyword search on top of it
- [`skin-tone`](https://npmjs.org/package/skin-tone): parses out base emojis from skin tones

## Installation
To install `node-emoji`, you need [node.js](http://nodejs.org/) and [npm](https://github.com/npm/npm#super-easy-install). :rocket:
> **Help wanted:** We are looking for volunteers to maintain this project.
> If you are interested, feel free to contact me at [me@omnidan.net](mailto:me@omnidan.net).
Once you have that set-up, just run `npm install --save node-emoji` in your project directory. :ship:
## Install

You're now ready to use emoji in your node projects! Awesome! :metal:
```sh
npm install node-emoji
```

## Usage
```javascript
var emoji = require('node-emoji')
emoji.get('coffee') // returns the emoji code for coffee (displays emoji on terminals that support it)
emoji.which(emoji.get('coffee')) // returns the string "coffee"
emoji.get(':fast_forward:') // `.get` also supports github flavored markdown emoji (http://www.emoji-cheat-sheet.com/)
emoji.emojify('I :heart: :coffee:!') // replaces all :emoji: with the actual emoji, in this case: returns "I ❀️ β˜•οΈ!"
emoji.random() // returns a random emoji + key, e.g. `{ emoji: '❀️', key: 'heart' }`
emoji.search('cof') // returns an array of objects with matching emoji's. `[{ emoji: 'β˜•οΈ', key: 'coffee' }, { emoji: ⚰', key: 'coffin'}]`
emoji.unemojify('I ❀️ πŸ•') // replaces the actual emoji with :emoji:, in this case: returns "I :heart: :pizza:"
emoji.find('πŸ•') // Find the `pizza` emoji, and returns `({ emoji: 'πŸ•', key: 'pizza' })`;
emoji.find('pizza') // Find the `pizza` emoji, and returns `({ emoji: 'πŸ•', key: 'pizza' })`;
emoji.hasEmoji('πŸ•') // Validate if this library knows an emoji like `πŸ•`
emoji.hasEmoji('pizza') // Validate if this library knowns a emoji with the name `pizza`
emoji.strip('⚠️ 〰️ 〰️ low disk space') // Strips the string from emoji's, in this case returns: "low disk space".
emoji.replace('⚠️ 〰️ 〰️ low disk space', (emoji) => `${emoji.key}:`) // Replace emoji's by callback method: "warning: low disk space"

```js
const emoji = require('node-emoji')

emoji.emojify('I :heart: :coffee:!') // 'I ❀️ β˜•οΈ!'

emoji.find('heart') // { emoji: '❀', name: 'heart' }
emoji.find('❀️') // { emoji: '❀', name: 'heart' }

emoji.get('unicorn') // πŸ¦„
emoji.get(':unicorn:') // πŸ¦„

emoji.has(':pizza:') // true
emoji.has('πŸ•') // true
emoji.has('unknown') // false

emoji.random() // { name: 'house', emoji: '🏠' }

emoji.replace('I ❀️ coffee!', 'love') // 'I love coffee!'

emoji.search(':uni:') // [ { emoji: 'πŸ¦„', name: 'unicorn' }, ... ]

emoji.strip('I ❀️ coffee!') // 'I coffee!'

emoji.unemojify('πŸ• for πŸ’ƒ') // ':pizza: for :dancer:'

emoji.which('πŸ¦„') // 'unicorn'
```

Note: There are some emojis that have "-" in their name, for example "t-rex" and refrencing them directly like this `(emoji.t-rex)` won't work. In such cases, use the `emoji.get('t-rex')` syntax instead
## API

### emoji.emojify(input, options?)

## Options
Parse all markdown-encoded emojis in a string.

### onMissing
`emoji.emojify(str, onMissing)`
Parameters:

As second argument, `emojify` takes an handler to parse unknown emojis. Provide a function to add your own handler:
1. **`input`** (`string`): The input string containing the markdown-encoding emojis.
1. **`options`** _(optional)_:
- **`fallback`** (`string`; default: `""`): The string to fallback to if an emoji was not found.
- **`format`** (`() => (emoji: string, part: string, string: string) => string`; default: `value => value`): Add a middleware layer to modify each matched emoji after parsing.

```js
var onMissing = function (name) {
return name;
});
const emoji = require('node-emoji')

var emojified = emoji.emojify('I :unknown_emoji: :star: :another_one:', onMissing);
// emojified: I unknown_emoji ⭐️ another_one
console.log(emoji.emojify('The :unicorn: is a fictitious animal.'))
// 'The πŸ¦„ is a fictitious animal.'
```

### format
`emoji.emojify(str, onMissing, format)`
### emoji.find(emoji)

Get the name and character of an emoji.

Parameters:

As third argument, `emojify` takes an handler to wrap parsed emojis. Provide a function to place emojis in custom elements, and to apply your custom styling:
1. **`emoji`** (`string`): The emoji to get the data of.

```js
var format = function (code, name) {
return '<img alt="' + code + '" src="' + name + '.png" />';
});
const emoji = require('node-emoji')

var emojified = emoji.emojify('I :unknown_emoji: :star: :another_one:', null, format);
// emojified: I <img alt="❀️" src="heart.png" /> <img alt="β˜•οΈ" src="coffee.png" />
console.log(emoji.find('πŸ¦„'))
// { name: 'unicorn', emoji: 'πŸ¦„' }
```

## Adding new emoji
Emoji come from js-emoji (Thanks a lot :thumbsup:). You can get a JSON file with all emoji here: https://raw.githubusercontent.com/omnidan/node-emoji/master/lib/emoji.json
### emoji.get(name)

To update the list or add custom emoji, clone this repository and put them into `lib/emojifile.js`.
Then run `npm run-script emojiparse` in the project directory or `node emojiparse` in the lib directory.
This should generate the new emoji.json file and output `Done.`.
Get an emoji from an emoji name.

That's all, you now have more emoji you can use! :clap:
Parameters:

## Support / Donations
If you want to support node-emoji development, please consider donating (it helps me keeping my projects active and alive!):
1. **`name`** (`string`): The name of the emoji to get.

* Paypal: [![daniel.bugl@gmail.com](https://www.paypalobjects.com/en_US/i/btn/btn_donate_SM.gif)](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=YBMS9EKTNPZHJ)
* Bitcoin: [1J5eKsrAcPPLv5gPxSjSUkXnbJpkhndFgA](bitcoin:1J5eKsrAcPPLv5gPxSjSUkXnbJpkhndFgA)

## Special Thanks
```js
const emoji = require('node-emoji')

... to Anand Chowdhary (@AnandChowdhary) and his company Pabio (https://github.com/pabio) for sponsoring this project via [GitHub Sponsors](https://github.com/sponsors/omnidan)!
console.log(emoji.get('unicorn'))
// 'πŸ¦„'
```

### emoji.has(emoji)

Check if this library supports a specific emoji.

Parameters:

1. **`emoji`** (`string`): The emoji to check.

```js
const emoji = require('node-emoji')

console.log(emoji.has('πŸ¦„'))
// true
```

### emoji.random()

Get a random emoji.

```js
const emoji = require('node-emoji')

console.log(emoji.random())
// { name: 'unicorn', emoji: 'πŸ¦„' }
```

### emoji.replace(input, replacement)

Replace the emojis in a string.

Parameters:

- **`input`** (`string`): The input string.
- **`replacement`** (`string | (emoji: string, index: number, string: InputValueType) => string`): The character to replace the emoji with. Can be either a string or a callback that returns a string.

```js
const emoji = require('node-emoji')

console.log(emoji.replace('The πŸ¦„ is a fictitious animal.', 'unicorn'))
// 'The unicorn is a fictitious animal.'
```

### emoji.search(keyword)

Search for emojis containing the provided name in their name.

Parameters:

1. **`keyword`** (`string`): The keyword to search for.

```js
const emoji = require('node-emoji')

console.log(emoji.search('honey'))
// [ { name: 'honeybee', emoji: '🐝' }, { name: 'honey_pot', emoji: '🍯' } ]
```

### emoji.strip(input, options?)

Remove all of the emojis from a string.

Parameters:

1. **`input`** (`string`): The input string to strip the emojis from.
1. **`options`** _(optional)_:

- **`preserveSpaces`** (`boolean`): Whether to keep the extra space after a stripped emoji.

```js
const emoji = require('node-emoji')

console.log(emoji.strip('πŸ¦„ The unicorn is a fictitious animal.'))
// 'The unicorn is a fictitious animal.'

console.log(
emoji.strip('πŸ¦„ The unicorn is a fictitious animal.', {
preserveSpaces: true,
})
)
// ' The unicorn is a fictitious animal.'
```

### emoji.unemojify(input)

Convert all emojis in a string to their markdown-encoded counterparts.

Parameters:

1. **`input`** (`string`): The input string containing the emojis.

```js
const emoji = require('node-emoji')

console.log(emoji.unemojify('The πŸ¦„ is a fictitious animal.'))
// 'The :unicorn: is a fictitious animal.'
```

### emoji.which(emoji)

Get an emoji name from an emoji.

Parameters:

1. **`emoji`** (`string`): The emoji to get the name of.

```js
const emoji = require('node-emoji')

console.log(emoji.which('πŸ¦„'))
// 'unicorn'
```

## License

[![FOSSA Status](https://app.fossa.io/api/projects/git%2Bgithub.com%2Fomnidan%2Fnode-emoji.svg?type=large)](https://app.fossa.io/projects/git%2Bgithub.com%2Fomnidan%2Fnode-emoji?ref=badge_large)

## Sponsorship

- [GitHub Sponsors](https://github.com/sponsors/omnidan)
- Paypal: [![daniel.bugl@gmail.com](https://www.paypalobjects.com/en_US/i/btn/btn_donate_SM.gif)](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=YBMS9EKTNPZHJ)
- Bitcoin: [1J5eKsrAcPPLv5gPxSjSUkXnbJpkhndFgA](bitcoin:1J5eKsrAcPPLv5gPxSjSUkXnbJpkhndFgA)

### Special Thanks

... to Anand Chowdhary (@AnandChowdhary) and his company Pabio (https://github.com/pabio) for sponsoring this project via [GitHub Sponsors](https://github.com/sponsors/omnidan)!
Loading

0 comments on commit 8752c68

Please sign in to comment.