diff --git a/src/react-emojione.js b/src/react-emojione.js index 2ca0681..b04db2c 100644 --- a/src/react-emojione.js +++ b/src/react-emojione.js @@ -1,16 +1,44 @@ -/*! - * react-emojione - * Copyright(c) 2017 Pedro Ladaria - * MIT Licensed - * - * Emoji provided free by http://emojione.com - */ -import React from 'react'; -import ASCII_DATA from './data/ascii-to-unicode'; -import getRenderer from './renderers/renderer-factory'; -import {unicodes, shortToCodepoint, unicodeToCodepoint} from './utils/emoji-format-conversion'; - -const DEFAULT_OPTIONS = { +'use strict'; + +Object.defineProperty(exports, "__esModule", { + value: true +}); +exports.emojify = undefined; + +var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); + +var _slicedToArray = function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }(); /*! + * react-emojione + * Copyright(c) 2017 Pedro Ladaria + * MIT Licensed + * + * Emoji provided free by http://emojione.com + */ + + +var _react = require('react'); + +var _react2 = _interopRequireDefault(_react); + +var _asciiToUnicode = require('./data/ascii-to-unicode'); + +var _asciiToUnicode2 = _interopRequireDefault(_asciiToUnicode); + +var _rendererFactory = require('./renderers/renderer-factory'); + +var _rendererFactory2 = _interopRequireDefault(_rendererFactory); + +var _emojiFormatConversion = require('./utils/emoji-format-conversion'); + +function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } + +function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } + +function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } + +function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } + +var DEFAULT_OPTIONS = { convertShortnames: true, convertUnicode: true, convertAscii: true, @@ -21,58 +49,95 @@ const DEFAULT_OPTIONS = { output: 'emoji' // valid options: 'emoji', 'unicode' }; -const asciiToUnicodeCache = new Map(); -const asciiRegExpToUnicode = new Map(); +var asciiToUnicodeCache = new Map(); +var asciiRegExpToUnicode = new Map(); -ASCII_DATA.forEach(([reStr, unicode]) => asciiRegExpToUnicode.set(RegExp(reStr), unicode)); +_asciiToUnicode2.default.forEach(function (_ref) { + var _ref2 = _slicedToArray(_ref, 2), + reStr = _ref2[0], + unicode = _ref2[1]; -// Escape RegExp code borrowed from lodash -const reRegExpChar = /[\\^$.*+?()[\]{}|]/g; -const reHasRegExpChar = RegExp(reRegExpChar.source); -const escapeRegExp = s => - (s && reHasRegExpChar.test(s)) ? s.replace(reRegExpChar, '\\$&') : s; + return asciiRegExpToUnicode.set(RegExp(reStr), unicode); +}); +// Escape RegExp code borrowed from lodash +var reRegExpChar = /[\\^$.*+?()[\]{}|]/g; +var reHasRegExpChar = RegExp(reRegExpChar.source); +var escapeRegExp = function escapeRegExp(s) { + return s && reHasRegExpChar.test(s) ? s.replace(reRegExpChar, '\\$&') : s; +}; -const convertAsciiToUnicodeOrNull = text => { +var convertAsciiToUnicodeOrNull = function convertAsciiToUnicodeOrNull(text) { if (!text) { return ''; } - const str = String(text); + var str = String(text); if (asciiToUnicodeCache.has(str)) { return asciiToUnicodeCache.get(str); } - for (const [regExp, unicode] of asciiRegExpToUnicode.entries()) { - if (str.replace(regExp, unicode) === unicode) { - asciiToUnicodeCache.set(str, unicode); - return unicode; + var _iteratorNormalCompletion = true; + var _didIteratorError = false; + var _iteratorError = undefined; + + try { + for (var _iterator = asciiRegExpToUnicode.entries()[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) { + var _step$value = _slicedToArray(_step.value, 2), + regExp = _step$value[0], + unicode = _step$value[1]; + + if (str.replace(regExp, unicode) === unicode) { + asciiToUnicodeCache.set(str, unicode); + return unicode; + } + } + } catch (err) { + _didIteratorError = true; + _iteratorError = err; + } finally { + try { + if (!_iteratorNormalCompletion && _iterator.return) { + _iterator.return(); + } + } finally { + if (_didIteratorError) { + throw _iteratorError; + } } } + return null; }; -const asciiRegexStr = ASCII_DATA.map(([reStr, ]) => reStr).join('|'); -const unicodesRegexStr = unicodes.map(escapeRegExp).join('|'); -const shortnamesRegexStr = ':[+-\\d\\w]+:'; +var asciiRegexStr = _asciiToUnicode2.default.map(function (_ref3) { + var _ref4 = _slicedToArray(_ref3, 1), + reStr = _ref4[0]; + + return reStr; +}).join('|'); +var unicodesRegexStr = _emojiFormatConversion.unicodes.map(escapeRegExp).join('|'); +var shortnamesRegexStr = ':[+-\\d\\w]+:'; -const REGEX_CACHE = []; +var REGEX_CACHE = []; -const getRegex = (withUnicode, withAscii, withShortnames) => { - const index = (withUnicode ? 1 : 0) + (withAscii ? 2 : 0) + (withShortnames ? 4 : 0); +var getRegex = function getRegex(withUnicode, withAscii, withShortnames) { + var index = (withUnicode ? 1 : 0) + (withAscii ? 2 : 0) + (withShortnames ? 4 : 0); if (!REGEX_CACHE[index]) { - const parts = [ - withShortnames ? shortnamesRegexStr : '', - withUnicode ? unicodesRegexStr : '', - withAscii ? asciiRegexStr : '', - ].filter(Boolean); - REGEX_CACHE[index] = RegExp(`(${parts.join('|')})`); + var parts = [withShortnames ? shortnamesRegexStr : '', withUnicode ? unicodesRegexStr : '', withAscii ? asciiRegexStr : ''].filter(Boolean); + REGEX_CACHE[index] = RegExp('(' + parts.join('|') + ')'); } return REGEX_CACHE[index]; }; -const startsWithSpace = str => (/^\s/).test(str); -const endsWithSpace = str => (/\s$/).test(str); +var startsWithSpace = function startsWithSpace(str) { + return (/^\s/.test(str) + ); +}; +var endsWithSpace = function endsWithSpace(str) { + return (/\s$/.test(str) + ); +}; -const shouldConvertAscii = (parts, index) => { +var shouldConvertAscii = function shouldConvertAscii(parts, index) { if (parts.length === 1) { return true; } @@ -85,55 +150,117 @@ const shouldConvertAscii = (parts, index) => { return endsWithSpace(parts[index - 1]) && startsWithSpace(parts[index + 1]); }; -export const emojify = (str, options = {}) => { +var emojify = exports.emojify = function emojify(str) { + var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; - const mergedOptions = Object.assign({}, DEFAULT_OPTIONS, options); - const {convertShortnames, convertUnicode, convertAscii} = mergedOptions; + var mergedOptions = Object.assign({}, DEFAULT_OPTIONS, options); - const regExp = getRegex(convertUnicode, convertAscii, convertShortnames); + var convertShortnames = mergedOptions.convertShortnames, + convertUnicode = mergedOptions.convertUnicode, + convertAscii = mergedOptions.convertAscii; - const renderCodepoint = getRenderer(mergedOptions); - const convertedParts = str.split(regExp).filter(Boolean).map((part, index, parts) => { - if (convertAscii && shouldConvertAscii(parts, index)) { - const unicode = convertAsciiToUnicodeOrNull(part); - if (unicode) { - return renderCodepoint(unicodeToCodepoint.get(unicode), `a-${index}`); - } - } - if (convertShortnames && shortToCodepoint.has(part)) { - return renderCodepoint(shortToCodepoint.get(part), `s-${index}`); - } - if (convertUnicode && unicodeToCodepoint.has(part)) { - return renderCodepoint(unicodeToCodepoint.get(part), `u-${index}`); - } - return part; - }); + var regExp = getRegex(convertUnicode, convertAscii, convertShortnames); + + var renderCodepoint = (0, _rendererFactory2.default)(mergedOptions); + if(mergedOptions.output === 'unicode'){ + var convertedParts = str.split(regExp).filter(Boolean).map(function (part, index, parts) { + if (convertAscii && shouldConvertAscii(parts, index)) { + var unicode = convertAsciiToUnicodeOrNull(part); + if (unicode) { + return renderCodepoint(_emojiFormatConversion.unicodeToCodepoint.get(unicode), 'a-' + index); + } + } + if (convertShortnames && _emojiFormatConversion.shortToCodepoint.has(part)) { + return renderCodepoint(_emojiFormatConversion.shortToCodepoint.get(part), 's-' + index); + } + if (convertUnicode && _emojiFormatConversion.unicodeToCodepoint.has(part)) { + return renderCodepoint(_emojiFormatConversion.unicodeToCodepoint.get(part), 'u-' + index); + } + return part; + }); + } + else if(mergedOptions.output === 'emojiShortName'){ + var convertedParts = str.split(regExp).filter(Boolean).map(function (part, index, parts) { + if (convertAscii && shouldConvertAscii(parts, index)) { + var unicode = convertAsciiToUnicodeOrNull(part); + if (unicode) { + var codepoint = _emojiFormatConversion.unicodeToCodepoint.get(unicode); + var shortName = _emojiFormatConversion.codepointToShort.get(codepoint); + return shortName; + } + } + if (convertShortnames && _emojiFormatConversion.shortToCodepoint.has(part)) { + var codepoint = _emojiFormatConversion.shortToCodepoint.get(part); + var shortName = _emojiFormatConversion.codepointToShort.get(codepoint); + return shortName; + + } + if (convertUnicode && _emojiFormatConversion.unicodeToCodepoint.has(part)) { + var codepoint = _emojiFormatConversion.unicodeToCodepoint.get(part); + var shortName = _emojiFormatConversion.codepointToShort.get(codepoint); + return shortName; + } + + return part; + }); + + } + else{ + var convertedParts = str.split(regExp).filter(Boolean).map(function (part, index, parts) { + if (convertAscii && shouldConvertAscii(parts, index)) { + var unicode = convertAsciiToUnicodeOrNull(part); + if (unicode) { + return renderCodepoint(_emojiFormatConversion.unicodeToCodepoint.get(unicode), 'a-' + index); + } + } + if (convertShortnames && _emojiFormatConversion.shortToCodepoint.has(part)) { + return renderCodepoint(_emojiFormatConversion.shortToCodepoint.get(part), 's-' + index); + } + if (convertUnicode && _emojiFormatConversion.unicodeToCodepoint.has(part)) { + return renderCodepoint(_emojiFormatConversion.unicodeToCodepoint.get(part), 'u-' + index); + } + return part; + }); + } - return mergedOptions.output === 'unicode' ? convertedParts.join('') : convertedParts; + return mergedOptions.output === 'unicode'|| mergedOptions.output === 'emojiShortName' ? convertedParts.join('') : convertedParts; }; -class Emojify extends React.Component { +var Emojify = function (_React$Component) { + _inherits(Emojify, _React$Component); - traverse(children, options) { - return React.Children.map(children, child => { - if (React.isValidElement(child)) { - return React.cloneElement(child, {}, this.traverse(child.props.children, options)); - } - if (typeof child === 'string') { - return emojify(child, options); - } - return child; - }); - } + function Emojify() { + _classCallCheck(this, Emojify); - render() { - const children = this.props.children; - return React.Children.count(children) - ? this.traverse(children, this.props) - : null; + return _possibleConstructorReturn(this, (Emojify.__proto__ || Object.getPrototypeOf(Emojify)).apply(this, arguments)); } -} -export default Emojify; + _createClass(Emojify, [{ + key: 'traverse', + value: function traverse(children, options) { + var _this2 = this; + + return _react2.default.Children.map(children, function (child) { + if (_react2.default.isValidElement(child)) { + return _react2.default.cloneElement(child, {}, _this2.traverse(child.props.children, options)); + } + if (typeof child === 'string') { + return emojify(child, options); + } + return child; + }); + } + }, { + key: 'render', + value: function render() { + var children = this.props.children; + return _react2.default.Children.count(children) ? this.traverse(children, this.props) : null; + } + }]); + + return Emojify; +}(_react2.default.Component); + +exports.default = Emojify;