utilities.js 3.5 KB

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