UNPKG

2.85 kBJavaScriptView Raw
1(function (global, factory) {
2 typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
3 typeof define === 'function' && define.amd ? define(factory) :
4 (global.umoji = factory());
5}(this, (function () { 'use strict';
6
7 var emojiToUnicode = function (emoji) {
8 var backStr = "";
9 if(emoji&&emoji.length>0){
10 for(var char of emoji){
11 var index = char.codePointAt(0);
12 if(index>65535){
13 var h = '\\u'+(Math.floor((index - 0x10000) / 0x400) + 0xD800).toString(16);
14 var c = '\\u'+((index - 0x10000) % 0x400 + 0xDC00).toString(16);
15 backStr = backStr + h + c;
16 }else{
17 backStr = backStr + char;
18 }
19 }
20 console.log(backStr);
21 }
22 return backStr
23 };
24
25 var toSurrogatePairs = function (index) {
26 var h = '\\u'+(Math.floor((index - 0x10000) / 0x400) + 0xD800).toString(16);
27 var c = '\\u'+((index - 0x10000) % 0x400 + 0xDC00).toString(16);
28 console.log(h+c);
29 return h+c
30 };
31
32 /*! https://mths.be/fromcodepoint v0.2.1 by @mathias */
33 var fromcodepoint = function () {
34 var stringFromCharCode = String.fromCharCode;
35 var floor = Math.floor;
36 var fromCodePoint = function(_) {
37 var MAX_SIZE = 0x4000;
38 var codeUnits = [];
39 var highSurrogate;
40 var lowSurrogate;
41 var index = -1;
42 var length = arguments.length;
43 if (!length) {
44 return '';
45 }
46 var result = '';
47 while (++index < length) {
48 var codePoint = Number(arguments[index]);
49 if (
50 !isFinite(codePoint) || // `NaN`, `+Infinity`, or `-Infinity`
51 codePoint < 0 || // not a valid Unicode code point
52 codePoint > 0x10FFFF || // not a valid Unicode code point
53 floor(codePoint) != codePoint // not an integer
54 ) {
55 throw RangeError('Invalid code point: ' + codePoint);
56 }
57 if (codePoint <= 0xFFFF) { // BMP code point
58 codeUnits.push(codePoint);
59 } else { // Astral code point; split in surrogate halves
60 // https://mathiasbynens.be/notes/javascript-encoding#surrogate-formulae
61 codePoint -= 0x10000;
62 highSurrogate = (codePoint >> 10) + 0xD800;
63 lowSurrogate = (codePoint % 0x400) + 0xDC00;
64 codeUnits.push(highSurrogate, lowSurrogate);
65 }
66 if (index + 1 == length || codeUnits.length > MAX_SIZE) {
67 result += stringFromCharCode.apply(null, codeUnits);
68 codeUnits.length = 0;
69 }
70 }
71 return result;
72 };
73 String.prototype.fromCodePoint = fromCodePoint;
74 };
75
76 var umoji = {
77 emojiToUnicode:emojiToUnicode,
78 toSurrogatePairs:toSurrogatePairs,
79 fromcodepoint:fromcodepoint
80 };
81 var umoji_1 = umoji;
82
83 return umoji_1;
84
85})));