PageRenderTime 42ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/website/source/php/var/empty.html

https://github.com/andriuspetrauskis/phpjs
HTML | 93 lines | 89 code | 4 blank | 0 comment | 0 complexity | 05bf4da672b040ef4905a4918dc17b52 MD5 | raw file
  1. ---
  2. warning: 'This file is auto generated by `npm run web:inject`, do not edit by hand'
  3. examples:
  4. - empty(null)
  5. - empty(undefined)
  6. - 'empty([])'
  7. - 'empty({})'
  8. - 'empty({''aFunc'' : function () { alert(''humpty''); } })'
  9. estarget: es5
  10. returns:
  11. - 'true'
  12. - 'true'
  13. - 'true'
  14. - 'true'
  15. - 'false'
  16. dependencies: []
  17. authors:
  18. original by:
  19. - Philippe Baumann
  20. improved by:
  21. - 'Onno Marsman (https://twitter.com/onnomarsman)'
  22. - Francesco
  23. - Marc Jansen
  24. - 'Rafał Kukawski (http://blog.kukawski.pl)'
  25. bugfixed by:
  26. - 'Kevin van Zonneveld (http://kvz.io)'
  27. input by:
  28. - 'Onno Marsman (https://twitter.com/onnomarsman)'
  29. - LH
  30. - 'Stoyan Kyosev (http://www.svest.org/)'
  31. notes: []
  32. type: function
  33. layout: function
  34. title: PHP's empty in JavaScript
  35. description: >-
  36. Heres what our current JavaScript equivalent to <a
  37. href="http://php.net/manual/en/function.empty.php">PHP's empty</a> looks like.
  38. function: empty
  39. category: var
  40. language: php
  41. permalink: php/var/empty/
  42. alias:
  43. - /functions/php/empty/
  44. - /functions/var/empty/
  45. - /php/empty/
  46. - /functions/empty/
  47. ---
  48. {% codeblock lang:javascript %}module.exports = function empty (mixedVar) {
  49. // discuss at: http://locutus.io/php/empty/
  50. // original by: Philippe Baumann
  51. // input by: Onno Marsman (https://twitter.com/onnomarsman)
  52. // input by: LH
  53. // input by: Stoyan Kyosev (http://www.svest.org/)
  54. // bugfixed by: Kevin van Zonneveld (http://kvz.io)
  55. // improved by: Onno Marsman (https://twitter.com/onnomarsman)
  56. // improved by: Francesco
  57. // improved by: Marc Jansen
  58. // improved by: Rafał Kukawski (http://blog.kukawski.pl)
  59. // example 1: empty(null)
  60. // returns 1: true
  61. // example 2: empty(undefined)
  62. // returns 2: true
  63. // example 3: empty([])
  64. // returns 3: true
  65. // example 4: empty({})
  66. // returns 4: true
  67. // example 5: empty({'aFunc' : function () { alert('humpty'); } })
  68. // returns 5: false
  69. var undef
  70. var key
  71. var i
  72. var len
  73. var emptyValues = [undef, null, false, 0, '', '0']
  74. for (i = 0, len = emptyValues.length; i < len; i++) {
  75. if (mixedVar === emptyValues[i]) {
  76. return true
  77. }
  78. }
  79. if (typeof mixedVar === 'object') {
  80. for (key in mixedVar) {
  81. if (mixedVar.hasOwnProperty(key)) {
  82. return false
  83. }
  84. }
  85. return true
  86. }
  87. return false
  88. }
  89. {% endcodeblock %}