1.0.0 β€’ Published 6 years ago

@nxmix/emoji-seq-match v1.0.0

Weekly downloads
26
License
MIT
Repository
github
Last release
6 years ago

@nxmix/emoji-seq-match

Build Status Coverage Status npm

Match Emoji combinations according to unicode emoji specification

Why

Different Emoji character combinations may visually produce different widths, for examples:

πŸ‘Ά + 🏼 => πŸ‘ΆπŸΌ  // Base emoji with skin-tone modifier
πŸ‘¨ + πŸ‘© + πŸ‘§ + πŸ‘¦  => πŸ‘¨β€πŸ‘©β€πŸ‘§β€πŸ‘¦  // Emoji characters joined with zero-witdh joiner (\u0200d)

Emoji Sequences, v11.0 defines these combinations.

The reason for creating this module is that I need to get the visual width of a string in terminal application to calculate the cursor's movement distance. To achieve this, I have to first create a method to find out if a string insludes a specification-defined Emoji combination.

Please noe that different terminal apps have different levels of implementation of the specification, including even the latest macOS Terminal.

Usage

Install

npm i @nxmix/emoji-seq-match --save

Typescript definition file is already included.

EXAMPLES

const getMatchedLength = require('@nxmix/emoji-seq-match').default;

getMatchedLength('πŸ‘ΆπŸΌ');
//=> 2

getMatchedLength('🐢🏼'); // puppy does not have skin tone combination" 
//=> 0

getMatchedLength("πŸ‘ΆπŸ½πŸ‘©β€πŸ‘©β€πŸ‘¦β€πŸ‘¦", 2); // from a 'start' postion to match
//=> 7, 'start' is counted by character not visual width

getMatchedLength(['πŸ‘Ά', '🏼']); // also accepts an array of strings
// => 2

Using ES2015w Modules:

import getMatchedLength from '@nxmix/emoji-seq-match';

getMatchedLength('πŸ‘ΆπŸΌ');
//=> 2

Tool

Running npm run parse-spec will download the specification files from http://unicode.org/Public/emoji/11.0/ and place the converted json files in the ./emoji-sequences directory.