PageRenderTime 6ms CodeModel.GetById 2ms app.highlight 1ms RepoModel.GetById 1ms app.codeStats 0ms

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

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