|
| 1 | +/** |
| 2 | +* @license Apache-2.0 |
| 3 | +* |
| 4 | +* Copyright (c) 2019 The Stdlib Authors. |
| 5 | +* |
| 6 | +* Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +* you may not use this file except in compliance with the License. |
| 8 | +* You may obtain a copy of the License at |
| 9 | +* |
| 10 | +* http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +* |
| 12 | +* Unless required by applicable law or agreed to in writing, software |
| 13 | +* distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +* See the License for the specific language governing permissions and |
| 16 | +* limitations under the License. |
| 17 | +*/ |
| 18 | + |
| 19 | +'use strict'; |
| 20 | + |
| 21 | +// MODULES // |
| 22 | + |
| 23 | +var replace = require( '@stdlib/string/replace' ); |
| 24 | +var hasOwnProp = require( '@stdlib/assert/has-own-property' ); |
| 25 | +var table = require( '@stdlib/datasets/emoji-code-picto' ); |
| 26 | + |
| 27 | + |
| 28 | +// VARIABLES // |
| 29 | + |
| 30 | +var TABLE = table(); |
| 31 | + |
| 32 | + |
| 33 | +// MAIN // |
| 34 | + |
| 35 | +/** |
| 36 | +* Replaces emoji codes with rendered emoji in a line of slide text. |
| 37 | +* |
| 38 | +* ## Notes |
| 39 | +* |
| 40 | +* - The input line object is mutated. |
| 41 | +* |
| 42 | +* @private |
| 43 | +* @param {Object} line - line |
| 44 | +* @returns {Object} rendered line |
| 45 | +*/ |
| 46 | +function emoji( line ) { |
| 47 | + var RE_EMOJI_CODE; |
| 48 | + var len; |
| 49 | + |
| 50 | + // Create a regular expression for matching emoji codes (code schema: `:<short_name>[::<modifier>[, <modifier>[, ...]]]`): |
| 51 | + RE_EMOJI_CODE = /:([\w+\-]+)(?::{2}([\w+\-,\s]+))?:/g; // eslint-disable-line no-useless-escape |
| 52 | + |
| 53 | + // Initialize a length adjustment factor: |
| 54 | + len = 0; |
| 55 | + |
| 56 | + // Replace emoji codes with rendered emojis: |
| 57 | + line.text = replace( line.text, RE_EMOJI_CODE, replacer ); |
| 58 | + |
| 59 | + // Update the effective line length: |
| 60 | + line.length += len; |
| 61 | + |
| 62 | + // Return the rendered line: |
| 63 | + return line; |
| 64 | + |
| 65 | + /** |
| 66 | + * Callback invoked for each match. |
| 67 | + * |
| 68 | + * @private |
| 69 | + * @param {string} match - matched substring |
| 70 | + * @param {string} p1 - first string found by a parenthesized capture group |
| 71 | + * @param {(string|void)} p2 - second string found by a parenthesized capture group |
| 72 | + * @returns {string} replacement string |
| 73 | + */ |
| 74 | + function replacer( match, p1, p2 ) { |
| 75 | + var code; |
| 76 | + var out; |
| 77 | + |
| 78 | + // Reconstitute the emoji code in order to normalize the code string: |
| 79 | + code = ':' + p1 + ':'; |
| 80 | + if ( p2 ) { |
| 81 | + p2 = p2.split( /,\s*/ ).join( ', ' ); // normalize any modifiers by ensuring consistent modifier spacing |
| 82 | + code += ':' + p2 + ':'; |
| 83 | + } |
| 84 | + if ( hasOwnProp( TABLE, code ) ) { |
| 85 | + out = TABLE[ code ]; |
| 86 | + |
| 87 | + // Note: we limit ourselves to emojis that are two columns wide--emoji being considered wide characters--hence, minus two. Depending on Unicode support, other emoji may not render as a single glyph. |
| 88 | + if ( out.length === 2 ) { |
| 89 | + len -= match.length - 2; |
| 90 | + return out; |
| 91 | + } |
| 92 | + } |
| 93 | + // If unable to convert an emoji code to a rendered emoji, return the matched substring unchanged: |
| 94 | + return match; |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | + |
| 99 | +// EXPORTS // |
| 100 | + |
| 101 | +module.exports = emoji; |
0 commit comments