1c9038b3cb0a7968a9f8b1aff4d30e130dec663a
[platform/framework/web/crosswalk-tizen.git] /
1 // Based on: https://github.com/mathiasbynens/String.prototype.codePointAt
2 // Thanks @mathiasbynens !
3
4 'use strict';
5
6 var toInteger  = require('../../../number/to-integer')
7   , validValue = require('../../../object/valid-value');
8
9 module.exports = function (pos) {
10         var str = String(validValue(this)), l = str.length, first, second;
11         pos = toInteger(pos);
12
13         // Account for out-of-bounds indices:
14         if (pos < 0 || pos >= l) return undefined;
15
16         // Get the first code unit
17         first = str.charCodeAt(pos);
18         if ((first >= 0xD800) && (first <= 0xDBFF) && (l > pos + 1)) {
19                 second = str.charCodeAt(pos + 1);
20                 if (second >= 0xDC00 && second <= 0xDFFF) {
21                         // http://mathiasbynens.be/notes/javascript-encoding#surrogate-formulae
22                         return (first - 0xD800) * 0x400 + second - 0xDC00 + 0x10000;
23                 }
24         }
25         return first;
26 };