Skip to content

Commit e1a55c5

Browse files
committed
Initial commit
0 parents  commit e1a55c5

File tree

7 files changed

+365
-0
lines changed

7 files changed

+365
-0
lines changed

.gitattributes

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Automatically normalize line endings for all text-based files
2+
* text=auto

.gitignore

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Coverage report
2+
coverage
3+
4+
# Installed npm modules
5+
node_modules
6+
7+
# Folder view configuration files
8+
.DS_Store
9+
Desktop.ini
10+
11+
# Thumbnail cache files
12+
._*
13+
Thumbs.db
14+
15+
# Files that might appear on external disks
16+
.Spotlight-V100
17+
.Trashes

.travis.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
language: node_js
2+
node_js:
3+
- "0.10"
4+
script:
5+
"node tests/tests.js"

README.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# ES6 `String.prototype.codePointAt` polyfill
2+
3+
A robust & optimized polyfill for [the `String.prototype.codePointAt` method in ECMAScript 6](http://people.mozilla.org/~jorendorff/es6-draft.html#sec-21.1.3.3).
4+
5+
Other polyfills for `String.prototype.codePointAt` are available:
6+
7+
* <http://norbertlindenberg.com/2012/05/ecmascript-supplementary-characters/> by [Norbert Lindenberg](http://norbertlindenberg.com/) (fails 2 tests)
8+
* <https://gist.github.com/slevithan/2290602> by [Steven Levithan](http://stevenlevithan.com/) (fails 6 tests)
9+
10+
## Installation
11+
12+
In a browser:
13+
14+
```html
15+
<script src="codepointat.js"></script>
16+
```
17+
18+
Via [npm](http://npmjs.org/):
19+
20+
```bash
21+
npm install string.prototype.codepointat
22+
```
23+
24+
Then, in [Node.js](http://nodejs.org/):
25+
26+
```js
27+
require('String.prototype.codePointAt');
28+
```
29+
30+
## Author
31+
32+
| [![twitter/mathias](http://gravatar.com/avatar/24e08a9ea84deb17ae121074d0f17125?s=70)](http://twitter.com/mathias "Follow @mathias on Twitter") |
33+
|---|
34+
| [Mathias Bynens](http://mathiasbynens.be/) |
35+
36+
## License
37+
38+
This polyfill is available under the [MIT](http://mths.be/mit) license.

codepointat.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
if (!String.prototype.codePointAt) {
2+
String.prototype.codePointAt = function(position) {
3+
var string = String(this);
4+
var size = string.length;
5+
// `ToInteger`
6+
var index = position ? Number(position) : 0;
7+
if (isNaN(index)) {
8+
index = 0;
9+
}
10+
// Account for out-of-bounds indices:
11+
if (index < 0 || index >= size) {
12+
return undefined;
13+
}
14+
// Get the first code unit
15+
var first = string.charCodeAt(index);
16+
var second;
17+
if ( // check if it’s the start of a surrogate pair
18+
first >= 0xD800 && first <= 0xDBFF && // high surrogate
19+
size > index + 1 // there is a next code unit
20+
) {
21+
second = string.charCodeAt(index + 1);
22+
if (second >= 0xDC00 && second <= 0xDFFF) { // low surrogate
23+
// http://mathiasbynens.be/notes/javascript-encoding#surrogate-formulae
24+
return (first - 0xD800) * 0x400 + second - 0xDC00 + 0x10000;
25+
}
26+
}
27+
return first;
28+
};
29+
}

package.json

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
{
2+
"name": "string.prototype.codepointat",
3+
"version": "0.1.0",
4+
"description": "A robust & optimized `String.prototype.codePointAt` polyfill, based on the ECMAScript 6 specification.",
5+
"homepage": "http://mths.be/codepointat",
6+
"main": "codepointat.js",
7+
"keywords": [
8+
"string",
9+
"unicode",
10+
"es6",
11+
"ecmascript",
12+
"polyfill"
13+
],
14+
"licenses": [
15+
{
16+
"type": "MIT",
17+
"url": "http://mths.be/mit"
18+
}
19+
],
20+
"author": {
21+
"name": "Mathias Bynens",
22+
"url": "http://mathiasbynens.be/"
23+
},
24+
"repository": {
25+
"type": "git",
26+
"url": "https://github.com/mathiasbynens/String.prototype.codePointAt.git"
27+
},
28+
"bugs": {
29+
"url": "https://github.com/mathiasbynens/String.prototype.codePointAt/issues"
30+
},
31+
"files": [
32+
"LICENSE-MIT.txt",
33+
"codepointat.js"
34+
],
35+
"directories": {
36+
"test": "tests"
37+
},
38+
"scripts": {
39+
"test": "node tests/tests.js",
40+
"cover": "istanbul cover --report html --verbose --dir coverage tests/tests.js"
41+
}
42+
}

tests/tests.js

Lines changed: 232 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,232 @@
1+
var assert = require('assert');
2+
3+
require('../codepointat.js');
4+
5+
var tests = [
6+
// String that starts with a BMP symbol
7+
{
8+
'string': 'abc\uD834\uDF06def',
9+
'position': -1,
10+
'result': undefined
11+
},
12+
{
13+
'string': 'abc\uD834\uDF06def',
14+
'position': 0,
15+
'result': 0x61
16+
},
17+
{
18+
'string': 'abc\uD834\uDF06def',
19+
'position': null,
20+
'result': 0x61
21+
},
22+
{
23+
'string': 'abc\uD834\uDF06def',
24+
'position': undefined,
25+
'result': 0x61
26+
},
27+
{
28+
'string': 'abc\uD834\uDF06def',
29+
'position': false,
30+
'result': 0x61
31+
},
32+
{
33+
'string': 'abc\uD834\uDF06def',
34+
'position': NaN,
35+
'result': 0x61
36+
},
37+
{
38+
'string': 'abc\uD834\uDF06def',
39+
'position': '',
40+
'result': 0x61
41+
},
42+
{
43+
'string': 'abc\uD834\uDF06def',
44+
'position': '_',
45+
'result': 0x61
46+
},
47+
{
48+
'string': 'abc\uD834\uDF06def',
49+
'position': 3,
50+
'result': 0x1D306
51+
},
52+
{
53+
'string': 'abc\uD834\uDF06def',
54+
'position': 4,
55+
'result': 0xDF06
56+
},
57+
{
58+
'string': 'abc\uD834\uDF06def',
59+
'position': 5,
60+
'result': 0x64
61+
},
62+
{
63+
'string': 'abc\uD834\uDF06def',
64+
'position': 42,
65+
'result': undefined
66+
},
67+
{
68+
'string': 'abc\uD834\uDF06def',
69+
'position': Infinity,
70+
'result': undefined
71+
},
72+
{
73+
'string': 'abc\uD834\uDF06def',
74+
'position': -Infinity,
75+
'result': undefined
76+
},
77+
// String that starts with an astral symbol
78+
{
79+
'string': '\uD834\uDF06def',
80+
'position': -1,
81+
'result': undefined
82+
},
83+
{
84+
'string': '\uD834\uDF06def',
85+
'position': 0,
86+
'result': 0x1D306
87+
},
88+
{
89+
'string': '\uD834\uDF06def',
90+
'position': null,
91+
'result': 0x1D306
92+
},
93+
{
94+
'string': '\uD834\uDF06def',
95+
'position': undefined,
96+
'result': 0x1D306
97+
},
98+
{
99+
'string': '\uD834\uDF06def',
100+
'position': false,
101+
'result': 0x1D306
102+
},
103+
{
104+
'string': '\uD834\uDF06def',
105+
'position': '',
106+
'result': 0x1D306
107+
},
108+
{
109+
'string': '\uD834\uDF06def',
110+
'position': '_',
111+
'result': 0x1D306
112+
},
113+
{
114+
'string': '\uD834\uDF06def',
115+
'position': 42,
116+
'result': undefined
117+
},
118+
{
119+
'string': '\uD834\uDF06def',
120+
'position': 1,
121+
'result': 0xDF06
122+
},
123+
{
124+
'string': '\uD834\uDF06def',
125+
'position': '1',
126+
'result': 0xDF06
127+
},
128+
// Lone high surrogates
129+
{
130+
'string': '\uD834abc',
131+
'position': -1,
132+
'result': undefined
133+
},
134+
{
135+
'string': '\uD834abc',
136+
'position': 0,
137+
'result': 0xD834
138+
},
139+
{
140+
'string': '\uD834abc',
141+
'position': null,
142+
'result': 0xD834
143+
},
144+
{
145+
'string': '\uD834abc',
146+
'position': undefined,
147+
'result': 0xD834
148+
},
149+
{
150+
'string': '\uD834abc',
151+
'position': false,
152+
'result': 0xD834
153+
},
154+
{
155+
'string': '\uD834abc',
156+
'position': NaN,
157+
'result': 0xD834
158+
},
159+
{
160+
'string': '\uD834abc',
161+
'position': '',
162+
'result': 0xD834
163+
},
164+
{
165+
'string': '\uD834abc',
166+
'position': '_',
167+
'result': 0xD834
168+
},
169+
// Lone low surrogates
170+
{
171+
'string': '\uDF06abc',
172+
'position': -1,
173+
'result': undefined
174+
},
175+
{
176+
'string': '\uDF06abc',
177+
'position': 0,
178+
'result': 0xDF06
179+
},
180+
{
181+
'string': '\uDF06abc',
182+
'position': null,
183+
'result': 0xDF06
184+
},
185+
{
186+
'string': '\uDF06abc',
187+
'position': undefined,
188+
'result': 0xDF06
189+
},
190+
{
191+
'string': '\uDF06abc',
192+
'position': false,
193+
'result': 0xDF06
194+
},
195+
{
196+
'string': '\uDF06abc',
197+
'position': NaN,
198+
'result': 0xDF06
199+
},
200+
{
201+
'string': '\uDF06abc',
202+
'position': '',
203+
'result': 0xDF06
204+
},
205+
{
206+
'string': '\uDF06abc',
207+
'position': '_',
208+
'result': 0xDF06
209+
}
210+
];
211+
212+
var errors = 0;
213+
tests.forEach(function(test, index) {
214+
try {
215+
assert.equal(
216+
test.string.codePointAt(test.position),
217+
test.result,
218+
index
219+
);
220+
} catch(error) {
221+
errors++;
222+
console.log(error.stack);
223+
}
224+
});
225+
226+
console.log(
227+
'Ran %d tests.\n%d assertions failed.\n%d assertions passed.',
228+
tests.length, errors, tests.length - errors
229+
);
230+
if (errors) {
231+
process.exit(1);
232+
}

0 commit comments

Comments
 (0)