forked from mathiasbynens/String.prototype.codePointAt
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodepointat.js
More file actions
30 lines (30 loc) · 936 Bytes
/
codepointat.js
File metadata and controls
30 lines (30 loc) · 936 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/*! http://mths.be/codepointat v0.1.0 by @mathias */
if (!String.prototype.codePointAt) {
String.prototype.codePointAt = function(position) {
var string = String(this);
var size = string.length;
// `ToInteger`
var index = position ? Number(position) : 0;
if (isNaN(index)) {
index = 0;
}
// Account for out-of-bounds indices:
if (index < 0 || index >= size) {
return undefined;
}
// Get the first code unit
var first = string.charCodeAt(index);
var second;
if ( // check if it’s the start of a surrogate pair
first >= 0xD800 && first <= 0xDBFF && // high surrogate
size > index + 1 // there is a next code unit
) {
second = string.charCodeAt(index + 1);
if (second >= 0xDC00 && second <= 0xDFFF) { // low surrogate
// http://mathiasbynens.be/notes/javascript-encoding#surrogate-formulae
return (first - 0xD800) * 0x400 + second - 0xDC00 + 0x10000;
}
}
return first;
};
}