string.ts 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /*************************************************************
  2. *
  3. * Copyright (c) 2017-2022 The MathJax Consortium
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. /**
  18. * @fileoverview Implements some string utility functions
  19. *
  20. * @author dpvc@mathjax.org (Davide Cervone)
  21. */
  22. /**
  23. * Sort strings by length
  24. *
  25. * @param {string} a First string to be compared
  26. * @param {string} b Second string to be compared
  27. * @return {number} -1 id a < b, 0 of a === b, 1 if a > b
  28. */
  29. export function sortLength(a: string, b: string): number {
  30. return a.length !== b.length ? b.length - a.length : a === b ? 0 : a < b ? -1 : 1;
  31. }
  32. /**
  33. * Quote a string for use in regular expressions
  34. *
  35. * @param {string} text The text whose regex characters are to be quoted
  36. * @return {string} The quoted string
  37. */
  38. export function quotePattern(text: string): string {
  39. return text.replace(/([\^$(){}+*?\-|\[\]\:\\])/g, '\\$1');
  40. }
  41. /**
  42. * Convert a UTF-8 string to an array of unicode code points
  43. *
  44. * @param {string} text The string to be turned into unicode positions
  45. * @return {number[]} Array of numbers representing the string's unicode character positions
  46. */
  47. export function unicodeChars(text: string): number[] {
  48. return Array.from(text).map((c) => c.codePointAt(0));
  49. }
  50. /**
  51. * Convert an array of unicode code points to a string
  52. *
  53. * @param {number[]} data The array of unicode code points
  54. * @return {string} The string consisting of the characters at those points
  55. */
  56. export function unicodeString(data: number[]): string {
  57. return String.fromCodePoint(...data);
  58. }
  59. /**
  60. * Test if a value is a percentage
  61. *
  62. * @param {string} x The string to test
  63. * @return {boolean} True if the string ends with a percent sign
  64. */
  65. export function isPercent(x: string): boolean {
  66. return !!x.match(/%\s*$/);
  67. }
  68. /**
  69. * Split a space-separated string of values
  70. *
  71. * @param {string} x The string to be split
  72. * @return {string[]} The list of white-space-separated "words" in the string
  73. */
  74. export function split(x: string): string[] {
  75. return x.trim().split(/\s+/);
  76. }