utilities.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. /**
  4. * @param {number} a The number to test.
  5. * @param {number} min The minimum value in the range, inclusive.
  6. * @param {number} max The maximum value in the range, inclusive.
  7. * @return {boolean} True if a >= min and a <= max.
  8. */
  9. function inRange(a, min, max) {
  10. return min <= a && a <= max;
  11. }
  12. exports.inRange = inRange;
  13. /**
  14. * @param {!Array.<*>} array The array to check.
  15. * @param {*} item The item to look for in the array.
  16. * @return {boolean} True if the item appears in the array.
  17. */
  18. function includes(array, item) {
  19. return array.indexOf(item) !== -1;
  20. }
  21. exports.includes = includes;
  22. /**
  23. * @param {*} o
  24. * @return {Object}
  25. */
  26. function ToDictionary(o) {
  27. if (o === undefined || o === null)
  28. return {};
  29. if (o === Object(o))
  30. return o;
  31. throw TypeError('Could not convert argument to dictionary');
  32. }
  33. exports.ToDictionary = ToDictionary;
  34. /**
  35. * @param {string} string Input string of UTF-16 code units.
  36. * @return {!Array.<number>} Code points.
  37. */
  38. function stringToCodePoints(string) {
  39. // https://heycam.github.io/webidl/#dfn-obtain-unicode
  40. // 1. Let S be the DOMString value.
  41. var s = String(string);
  42. // 2. Let n be the length of S.
  43. var n = s.length;
  44. // 3. Initialize i to 0.
  45. var i = 0;
  46. // 4. Initialize U to be an empty sequence of Unicode characters.
  47. var u = [];
  48. // 5. While i < n:
  49. while (i < n) {
  50. // 1. Let c be the code unit in S at index i.
  51. var c = s.charCodeAt(i);
  52. // 2. Depending on the value of c:
  53. // c < 0xD800 or c > 0xDFFF
  54. if (c < 0xD800 || c > 0xDFFF) {
  55. // Append to U the Unicode character with code point c.
  56. u.push(c);
  57. }
  58. // 0xDC00 ≤ c ≤ 0xDFFF
  59. else if (0xDC00 <= c && c <= 0xDFFF) {
  60. // Append to U a U+FFFD REPLACEMENT CHARACTER.
  61. u.push(0xFFFD);
  62. }
  63. // 0xD800 ≤ c ≤ 0xDBFF
  64. else if (0xD800 <= c && c <= 0xDBFF) {
  65. // 1. If i = n−1, then append to U a U+FFFD REPLACEMENT
  66. // CHARACTER.
  67. if (i === n - 1) {
  68. u.push(0xFFFD);
  69. }
  70. // 2. Otherwise, i < n−1:
  71. else {
  72. // 1. Let d be the code unit in S at index i+1.
  73. var d = s.charCodeAt(i + 1);
  74. // 2. If 0xDC00 ≤ d ≤ 0xDFFF, then:
  75. if (0xDC00 <= d && d <= 0xDFFF) {
  76. // 1. Let a be c & 0x3FF.
  77. var a = c & 0x3FF;
  78. // 2. Let b be d & 0x3FF.
  79. var b = d & 0x3FF;
  80. // 3. Append to U the Unicode character with code point
  81. // 2^16+2^10*a+b.
  82. u.push(0x10000 + (a << 10) + b);
  83. // 4. Set i to i+1.
  84. i += 1;
  85. }
  86. // 3. Otherwise, d < 0xDC00 or d > 0xDFFF. Append to U a
  87. // U+FFFD REPLACEMENT CHARACTER.
  88. else {
  89. u.push(0xFFFD);
  90. }
  91. }
  92. }
  93. // 3. Set i to i+1.
  94. i += 1;
  95. }
  96. // 6. Return U.
  97. return u;
  98. }
  99. exports.stringToCodePoints = stringToCodePoints;
  100. /**
  101. * @param {!Array.<number>} code_points Array of code points.
  102. * @return {string} string String of UTF-16 code units.
  103. */
  104. function codePointsToString(code_points) {
  105. var s = '';
  106. for (var i = 0; i < code_points.length; ++i) {
  107. var cp = code_points[i];
  108. if (cp <= 0xFFFF) {
  109. s += String.fromCharCode(cp);
  110. }
  111. else {
  112. cp -= 0x10000;
  113. s += String.fromCharCode((cp >> 10) + 0xD800, (cp & 0x3FF) + 0xDC00);
  114. }
  115. }
  116. return s;
  117. }
  118. exports.codePointsToString = codePointsToString;
  119. //# sourceMappingURL=utilities.js.map