PageRenderTime 59ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

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

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