From 6ab341cd69959a4c6bd5bb6b60ec970084ffe1af Mon Sep 17 00:00:00 2001 From: Jian Weihang Date: Wed, 19 Sep 2018 10:24:20 +0800 Subject: [PATCH] support emoji --- index.js | 3 +++ package.json | 1 + test.js | 4 ++++ 3 files changed, 8 insertions(+) diff --git a/index.js b/index.js index bbc49d2..6dd33ee 100644 --- a/index.js +++ b/index.js @@ -1,8 +1,11 @@ 'use strict'; const stripAnsi = require('strip-ansi'); const isFullwidthCodePoint = require('is-fullwidth-code-point'); +const emojiRegex = require('emoji-regex')(); module.exports = str => { + str = str.replace(emojiRegex, ' '); + if (typeof str !== 'string' || str.length === 0) { return 0; } diff --git a/package.json b/package.json index 89f0b6a..62ca913 100644 --- a/package.json +++ b/package.json @@ -45,6 +45,7 @@ "fixed-width" ], "dependencies": { + "emoji-regex": "^7.0.1", "is-fullwidth-code-point": "^2.0.0", "strip-ansi": "^4.0.0" }, diff --git a/test.js b/test.js index a5e894b..291f799 100644 --- a/test.js +++ b/test.js @@ -10,6 +10,10 @@ test('main', t => { t.is(m('안녕하세요'), 10); t.is(m('A\uD83C\uDE00BC'), 5, 'surrogate'); t.is(m('\u001B[31m\u001B[39m'), 0); + t.is(m('\u{231A}'), 2, '⌚ default emoji presentation character (Emoji_Presentation)'); + t.is(m('\u{2194}\u{FE0F}'), 2, '↔️ default text presentation character rendered as emoji'); + t.is(m('\u{1F469}'), 2, '👩 emoji modifier base (Emoji_Modifier_Base)'); + t.is(m('\u{1F469}\u{1F3FF}'), 2, '👩🏿 emoji modifier base followed by a modifier'); }); test('ignores control characters', t => {