safe-from-int.mjs 830 B

123456789101112131415161718192021222324252627282930
  1. export default safeFromInt
  2. import codes from '../character/codes.mjs'
  3. import values from '../character/values.mjs'
  4. import fromCharCode from '../constant/from-char-code.mjs'
  5. function safeFromInt(value, base) {
  6. var code = parseInt(value, base)
  7. if (
  8. // C0 except for HT, LF, FF, CR, space
  9. code < codes.ht ||
  10. code === codes.vt ||
  11. (code > codes.cr && code < codes.space) ||
  12. // Control character (DEL) of the basic block and C1 controls.
  13. (code > codes.tilde && code < 160) ||
  14. // Lone high surrogates and low surrogates.
  15. (code > 55295 && code < 57344) ||
  16. // Noncharacters.
  17. (code > 64975 && code < 65008) ||
  18. (code & 65535) === 65535 ||
  19. (code & 65535) === 65534 ||
  20. // Out of range
  21. code > 1114111
  22. ) {
  23. return values.replacementCharacter
  24. }
  25. return fromCharCode(code)
  26. }