string.ts 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. export const stringMethods = new Set([
  2. "toLowerCase",
  3. "toUpperCase",
  4. "trim",
  5. "trimStart",
  6. "trimEnd",
  7. "trimLeft",
  8. "trimRight",
  9. "split",
  10. "slice",
  11. "substring",
  12. "substr",
  13. "includes",
  14. "startsWith",
  15. "endsWith",
  16. "indexOf",
  17. "lastIndexOf",
  18. "replace",
  19. "replaceAll",
  20. "repeat",
  21. "padStart",
  22. "padEnd",
  23. "charAt",
  24. "charCodeAt",
  25. "codePointAt",
  26. "at",
  27. "concat",
  28. "toString",
  29. "match",
  30. "matchAll",
  31. "search",
  32. "localeCompare",
  33. "normalize",
  34. ])
  35. export const stringStatics = new Set(["fromCharCode", "fromCodePoint"])
  36. export const invokeStringStatic = (name: string, args: Array<unknown>, node: AstNode): unknown => {
  37. const codes = args.map((arg) => {
  38. if (typeof arg !== "number") throw new InterpreterRuntimeError(`String.${name} expects number arguments.`, node)
  39. return arg
  40. })
  41. switch (name) {
  42. case "fromCharCode":
  43. return String.fromCharCode(...codes)
  44. case "fromCodePoint":
  45. return String.fromCodePoint(...codes)
  46. default:
  47. throw new InterpreterRuntimeError(`String.${name} is not available in CodeMode.`, node)
  48. }
  49. }
  50. import { type AstNode, InterpreterRuntimeError } from "../interpreter/model.js"