Skip to content

Commit aa59dce

Browse files
Merge pull request #4 from mathiasbynens/nr/es-shim-api
2 parents 97ab3d3 + faaa207 commit aa59dce

15 files changed

+272
-151
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

.travis.yml

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
version: ~> 1.0
12
language: node_js
2-
node_js:
3-
- "0.10"
4-
script:
5-
"node tests/tests.js"
3+
os:
4+
- linux
5+
import:
6+
- ljharb/travis-ci:node/all.yml
7+
- ljharb/travis-ci:node/pretest.yml

README.md

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# ES6 `String.prototype.codePointAt` polyfill [![Build status](https://travis-ci.org/mathiasbynens/String.prototype.codePointAt.svg?branch=master)](https://travis-ci.org/mathiasbynens/String.prototype.codePointAt)
22

3-
A robust & optimized ES3-compatible polyfill for [the `String.prototype.codePointAt` method in ECMAScript 6](http://people.mozilla.org/~jorendorff/es6-draft.html#sec-string.prototype.codepointat).
3+
A robust & optimized polyfill for [the `String.prototype.codePointAt` method in ECMAScript 6](http://people.mozilla.org/~jorendorff/es6-draft.html#sec-string.prototype.codepointat).
4+
5+
This package implements the [es-shim API](https://github.com/es-shims/api) interface. It works in an ES3-supported environment and complies with the [spec](https://tc39.es/ecma262/#sec-string.prototype.codepointat).
46

57
Other polyfills for `String.prototype.codePointAt` are available:
68

@@ -10,12 +12,6 @@ Other polyfills for `String.prototype.codePointAt` are available:
1012

1113
## Installation
1214

13-
In a browser:
14-
15-
```html
16-
<script src="codepointat.js"></script>
17-
```
18-
1915
Via [npm](http://npmjs.org/):
2016

2117
```bash
@@ -32,6 +28,16 @@ require('string.prototype.codepointat');
3228
require('String.prototype.codePointAt');
3329
```
3430

31+
In a browser:
32+
33+
```html
34+
<script src="https://bundle.run/string.prototype.codepointat"></script>
35+
```
36+
37+
> **NOTE**: It's recommended that you install this module using a package manager
38+
> such as `npm`, because loading multiple polyfills from a CDN (such as `bundle.run`)
39+
> will lead to duplicated code.
40+
3541
## Notes
3642

3743
[A polyfill + test suite for `String.fromCodePoint`](https://mths.be/fromcodepoint) is available, too.

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: 24 additions & 7 deletions
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",
@@ -21,12 +29,21 @@
2129
"url": "https://github.com/mathiasbynens/String.prototype.codePointAt.git"
2230
},
2331
"bugs": "https://github.com/mathiasbynens/String.prototype.codePointAt/issues",
24-
"files": [
25-
"LICENSE-MIT.txt",
26-
"codepointat.js"
27-
],
2832
"scripts": {
29-
"test": "node tests/tests.js",
30-
"cover": "istanbul cover --report html --verbose --dir coverage tests/tests.js"
33+
"pretest": "es-shim-api --bound",
34+
"test": "npm run tests-only",
35+
"tests-only": "tape 'tests/*.js'",
36+
"cover": "istanbul cover --report html --verbose --dir coverage tape 'tests/*.js'"
37+
},
38+
"dependencies": {
39+
"es-abstract": "^1.17.5"
40+
},
41+
"devDependencies": {
42+
"@es-shims/api": "^2.1.2",
43+
"define-properties": "^1.1.3",
44+
"function-bind": "^1.1.1",
45+
"functions-have-names": "^1.2.1",
46+
"istanbul": "^0.4.5",
47+
"tape": "^5.0.0"
3148
}
3249
}

0 commit comments

Comments
 (0)