PageRenderTime 28ms CodeModel.GetById 21ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

/src/php/strings/chr.js

https://github.com/andriuspetrauskis/phpjs
JavaScript | 18 lines | 7 code | 1 blank | 10 comment | 1 complexity | 78e82392ea14a94b7f6e81a6f76b80e7 MD5 | raw file
 1module.exports = function chr (codePt) {
 2  //  discuss at: http://locutus.io/php/chr/
 3  // original by: Kevin van Zonneveld (http://kvz.io)
 4  // improved by: Brett Zamir (http://brett-zamir.me)
 5  //   example 1: chr(75) === 'K'
 6  //   example 1: chr(65536) === '\uD800\uDC00'
 7  //   returns 1: true
 8  //   returns 1: true
 9
10  if (codePt > 0xFFFF) { // Create a four-byte string (length 2) since this code point is high
11    //   enough for the UTF-16 encoding (JavaScript internal use), to
12    //   require representation with two surrogates (reserved non-characters
13    //   used for building other characters; the first is "high" and the next "low")
14    codePt -= 0x10000
15    return String.fromCharCode(0xD800 + (codePt >> 10), 0xDC00 + (codePt & 0x3FF))
16  }
17  return String.fromCharCode(codePt)
18}