* Returns a string containing as many code units as necessary * to represent the Unicode code points given by the first argument. * @author Norbert Lindenberg * @see http://norbertlindenberg.com/2012/05/ecmascript-supplementary-characters/ * @param {number[]} codePoints Array of Unicode
(codePoints)
| 43 | * @returns {String} String (UCS-2) |
| 44 | */ |
| 45 | static stringFromCodePoints (codePoints) { |
| 46 | // In the worst case every code point needs to be translated to two |
| 47 | // surrogates each |
| 48 | // Create a fixed size array that gets sliced at the end |
| 49 | const codeUnits = new Array(codePoints.length * 2) |
| 50 | let j = 0 |
| 51 | let i, codePoint |
| 52 | |
| 53 | for (i = 0; i < codePoints.length; i++) { |
| 54 | codePoint = codePoints[i] |
| 55 | |
| 56 | if (codePoint < 0x10000) { |
| 57 | // Basic Multilingual Plane (BMP) character |
| 58 | codeUnits[j++] = String.fromCharCode(codePoint) |
| 59 | } else { |
| 60 | // Character with surrogates |
| 61 | codePoint -= 0x10000 |
| 62 | codeUnits[j++] = String.fromCharCode((codePoint >> 10) + 0xD800) |
| 63 | codeUnits[j++] = String.fromCharCode((codePoint % 0x400) + 0xDC00) |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | // Slice the fixed size array to the portion actually in use and concatenate |
| 68 | // it to a string |
| 69 | return codeUnits.slice(0, j).join('') |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Creates an array containing the numeric code points of each Unicode |
no test coverage detected