Skip to content

Commit 0e518c6

Browse files
Implement the es-shim-api interface
1 parent 97ab3d3 commit 0e518c6

File tree

11 files changed

+178
-123
lines changed

11 files changed

+178
-123
lines changed

.editorconfig

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
root = true
2+
3+
[*]
4+
indent_style = tab
5+
end_of_line = lf
6+
insert_final_newline = true

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ coverage
33

44
# Installed npm modules
55
node_modules
6+
package-lock.json
67

78
# Folder view configuration files
89
.DS_Store

.npmrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package-lock=false

auto.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/*! https://mths.be/codepointat v1.0.0 by @mathias */
2+
3+
require('./shim')();

codepointat.js

Lines changed: 0 additions & 54 deletions
This file was deleted.

implementation.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*! https://mths.be/codepointat v1.0.0 by @mathias */
2+
3+
'use strict';
4+
5+
var callBound = require('es-abstract/helpers/callBound');
6+
var RequireObjectCoercible = require('es-abstract/2019/RequireObjectCoercible');
7+
var ToString = require('es-abstract/2019/ToString');
8+
var ToInteger = require('es-abstract/2019/ToInteger');
9+
var StringCharCodeAt = callBound('String.prototype.charCodeAt');
10+
11+
module.exports = function codePointAt(position) {
12+
var O = RequireObjectCoercible(this);
13+
var string = ToString(O);
14+
var size = string.length;
15+
var index = ToInteger(position);
16+
// Account for out-of-bounds indices:
17+
if (index < 0 || index >= size) {
18+
return undefined;
19+
}
20+
// Get the first code unit
21+
var first = StringCharCodeAt(string, index);
22+
var second;
23+
if ( // check if it’s the start of a surrogate pair
24+
first >= 0xD800 && first <= 0xDBFF && // high surrogate
25+
size > index + 1 // there is a next code unit
26+
) {
27+
second = StringCharCodeAt(string, index + 1);
28+
if (second >= 0xDC00 && second <= 0xDFFF) { // low surrogate
29+
// https://mathiasbynens.be/notes/javascript-encoding#surrogate-formulae
30+
return (first - 0xD800) * 0x400 + second - 0xDC00 + 0x10000;
31+
}
32+
}
33+
return first;
34+
};

index.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*! https://mths.be/codepointat v1.0.0 by @mathias */
2+
3+
'use strict';
4+
5+
var callBind = require('es-abstract/helpers/callBind');
6+
var define = require('define-properties');
7+
8+
var implementation = require('./implementation');
9+
var getPolyfill = require('./polyfill');
10+
var shim = require('./shim');
11+
12+
var boundCodePointAt = callBind(implementation);
13+
14+
define(boundCodePointAt, {
15+
getPolyfill: getPolyfill,
16+
implementation: implementation,
17+
shim: shim
18+
});
19+
20+
module.exports = boundCodePointAt;

package.json

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,15 @@
33
"version": "0.2.1",
44
"description": "A robust & optimized `String.prototype.codePointAt` polyfill, based on the ECMAScript 6 specification.",
55
"homepage": "https://mths.be/codepointat",
6-
"main": "codepointat.js",
6+
"main": "index.js",
7+
"exports": {
8+
".": "./index.js",
9+
"./auto": "./auto.js",
10+
"./shim": "./shim.js",
11+
"./getPolyfill": "./getPolyfill.js",
12+
"./implementation": "./implementation.js",
13+
"./package.json": "./package.json"
14+
},
715
"keywords": [
816
"string",
917
"unicode",
@@ -28,5 +36,12 @@
2836
"scripts": {
2937
"test": "node tests/tests.js",
3038
"cover": "istanbul cover --report html --verbose --dir coverage tests/tests.js"
39+
},
40+
"dependencies": {
41+
"define-properties": "^1.1.3",
42+
"es-abstract": "^1.17.5"
43+
},
44+
"devDependencies": {
45+
"istanbul": "^0.4.5"
3146
}
3247
}

polyfill.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/*! https://mths.be/codepointat v1.0.0 by @mathias */
2+
3+
'use strict';
4+
5+
var implementation = require('./implementation');
6+
7+
module.exports = function getPolyfill() {
8+
return String.prototype.codePointAt || implementation;
9+
};

shim.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/*! https://mths.be/codepointat v1.0.0 by @mathias */
2+
3+
'use strict';
4+
5+
var define = require('define-properties');
6+
7+
var getPolyfill = require('./polyfill');
8+
9+
module.exports = function shimCodePointAt() {
10+
var polyfill = getPolyfill();
11+
12+
if (String.prototype.codePointAt !== polyfill) {
13+
define(String.prototype, { codePointAt: polyfill });
14+
}
15+
16+
return polyfill;
17+
}

0 commit comments

Comments
 (0)