UNPKG

1.42 kBJavaScriptView Raw
1'use strict';
2var uncurryThis = require('../internals/function-uncurry-this');
3var toIntegerOrInfinity = require('../internals/to-integer-or-infinity');
4var toString = require('../internals/to-string');
5var requireObjectCoercible = require('../internals/require-object-coercible');
6
7var charAt = uncurryThis(''.charAt);
8var charCodeAt = uncurryThis(''.charCodeAt);
9var stringSlice = uncurryThis(''.slice);
10
11var createMethod = function (CONVERT_TO_STRING) {
12 return function ($this, pos) {
13 var S = toString(requireObjectCoercible($this));
14 var position = toIntegerOrInfinity(pos);
15 var size = S.length;
16 var first, second;
17 if (position < 0 || position >= size) return CONVERT_TO_STRING ? '' : undefined;
18 first = charCodeAt(S, position);
19 return first < 0xD800 || first > 0xDBFF || position + 1 === size
20 || (second = charCodeAt(S, position + 1)) < 0xDC00 || second > 0xDFFF
21 ? CONVERT_TO_STRING
22 ? charAt(S, position)
23 : first
24 : CONVERT_TO_STRING
25 ? stringSlice(S, position, position + 2)
26 : (first - 0xD800 << 10) + (second - 0xDC00) + 0x10000;
27 };
28};
29
30module.exports = {
31 // `String.prototype.codePointAt` method
32 // https://tc39.es/ecma262/#sec-string.prototype.codepointat
33 codeAt: createMethod(false),
34 // `String.prototype.at` method
35 // https://github.com/mathiasbynens/String.prototype.at
36 charAt: createMethod(true)
37};