string-multibyte.js 1.2 KB

12345678910111213141516171819202122232425262728
  1. var toInteger = require('../internals/to-integer');
  2. var toString = require('../internals/to-string');
  3. var requireObjectCoercible = require('../internals/require-object-coercible');
  4. // `String.prototype.codePointAt` methods implementation
  5. var createMethod = function (CONVERT_TO_STRING) {
  6. return function ($this, pos) {
  7. var S = toString(requireObjectCoercible($this));
  8. var position = toInteger(pos);
  9. var size = S.length;
  10. var first, second;
  11. if (position < 0 || position >= size) return CONVERT_TO_STRING ? '' : undefined;
  12. first = S.charCodeAt(position);
  13. return first < 0xD800 || first > 0xDBFF || position + 1 === size
  14. || (second = S.charCodeAt(position + 1)) < 0xDC00 || second > 0xDFFF
  15. ? CONVERT_TO_STRING ? S.charAt(position) : first
  16. : CONVERT_TO_STRING ? S.slice(position, position + 2) : (first - 0xD800 << 10) + (second - 0xDC00) + 0x10000;
  17. };
  18. };
  19. module.exports = {
  20. // `String.prototype.codePointAt` method
  21. // https://tc39.es/ecma262/#sec-string.prototype.codepointat
  22. codeAt: createMethod(false),
  23. // `String.prototype.at` method
  24. // https://github.com/mathiasbynens/String.prototype.at
  25. charAt: createMethod(true)
  26. };