util.js 807 B

123456789101112131415161718192021222324252627282930313233
  1. /** Returns a regular expression quantifier with an upper and lower limit. */
  2. export function limit(lower, upper)
  3. {
  4. if ((lower < 0) || (upper <= 0) || (upper < lower)) {
  5. throw new TypeError()
  6. }
  7. return `{${lower},${upper}}`
  8. }
  9. /**
  10. * Trims away any characters after the first match of {@code pattern} in {@code candidate},
  11. * returning the trimmed version.
  12. */
  13. export function trimAfterFirstMatch(regexp, string)
  14. {
  15. const index = string.search(regexp)
  16. if (index >= 0) {
  17. return string.slice(0, index)
  18. }
  19. return string
  20. }
  21. export function startsWith(string, substring)
  22. {
  23. return string.indexOf(substring) === 0
  24. }
  25. export function endsWith(string, substring)
  26. {
  27. return string.indexOf(substring, string.length - substring.length) === string.length - substring.length
  28. }