PageRenderTime 57ms CodeModel.GetById 35ms RepoModel.GetById 0ms app.codeStats 0ms

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

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