PageRenderTime 867ms CodeModel.GetById 765ms app.highlight 2ms RepoModel.GetById 99ms app.codeStats 0ms

/website/source/php/strings/chr.html

http://github.com/kvz/phpjs
HTML | 53 lines | 52 code | 1 blank | 0 comment | 0 complexity | aa32a820a1e49c2e94625e9d30b624b8 MD5 | raw file
 1---
 2warning: 'This file is auto generated by `npm run web:inject`, do not edit by hand'
 3examples:
 4  - |-
 5    chr(75) === 'K'
 6    chr(65536) === '\uD800\uDC00'
 7estarget: es5
 8returns:
 9  - |-
10    true
11    true
12dependencies: []
13authors:
14  original by:
15    - 'Kevin van Zonneveld (https://kvz.io)'
16  improved by:
17    - 'Brett Zamir (https://brett-zamir.me)'
18notes: []
19type: function
20layout: function
21title: PHP's chr in JavaScript
22description: >-
23  Here’s what our current JavaScript equivalent to <a
24  href="https://php.net/manual/en/function.chr.php">PHP's chr</a> looks like.
25function: chr
26category: strings
27language: php
28permalink: php/strings/chr/
29alias:
30  - /functions/php/chr/
31  - /functions/strings/chr/
32  - /php/chr/
33  - /functions/chr/
34---
35{% codeblock lang:javascript %}module.exports = function chr (codePt) {
36  //  discuss at: https://locutus.io/php/chr/
37  // original by: Kevin van Zonneveld (https://kvz.io)
38  // improved by: Brett Zamir (https://brett-zamir.me)
39  //   example 1: chr(75) === 'K'
40  //   example 1: chr(65536) === '\uD800\uDC00'
41  //   returns 1: true
42  //   returns 1: true
43
44  if (codePt > 0xFFFF) { // Create a four-byte string (length 2) since this code point is high
45    //   enough for the UTF-16 encoding (JavaScript internal use), to
46    //   require representation with two surrogates (reserved non-characters
47    //   used for building other characters; the first is "high" and the next "low")
48    codePt -= 0x10000
49    return String.fromCharCode(0xD800 + (codePt >> 10), 0xDC00 + (codePt & 0x3FF))
50  }
51  return String.fromCharCode(codePt)
52}
53{% endcodeblock %}