Last change
on this file was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago |
initial commit
|
-
Property mode
set to
100644
|
File size:
1.2 KB
|
Line | |
---|
1 | /*!
|
---|
2 | * has-values <https://github.com/jonschlinkert/has-values>
|
---|
3 | *
|
---|
4 | * Copyright (c) 2014-2015, 2017, Jon Schlinkert.
|
---|
5 | * Released under the MIT License.
|
---|
6 | */
|
---|
7 |
|
---|
8 | 'use strict';
|
---|
9 |
|
---|
10 | var typeOf = require('kind-of');
|
---|
11 | var isNumber = require('is-number');
|
---|
12 |
|
---|
13 | module.exports = function hasValue(val) {
|
---|
14 | // is-number checks for NaN and other edge cases
|
---|
15 | if (isNumber(val)) {
|
---|
16 | return true;
|
---|
17 | }
|
---|
18 |
|
---|
19 | switch (typeOf(val)) {
|
---|
20 | case 'null':
|
---|
21 | case 'boolean':
|
---|
22 | case 'function':
|
---|
23 | return true;
|
---|
24 | case 'string':
|
---|
25 | case 'arguments':
|
---|
26 | return val.length !== 0;
|
---|
27 | case 'error':
|
---|
28 | return val.message !== '';
|
---|
29 | case 'array':
|
---|
30 | var len = val.length;
|
---|
31 | if (len === 0) {
|
---|
32 | return false;
|
---|
33 | }
|
---|
34 | for (var i = 0; i < len; i++) {
|
---|
35 | if (hasValue(val[i])) {
|
---|
36 | return true;
|
---|
37 | }
|
---|
38 | }
|
---|
39 | return false;
|
---|
40 | case 'file':
|
---|
41 | case 'map':
|
---|
42 | case 'set':
|
---|
43 | return val.size !== 0;
|
---|
44 | case 'object':
|
---|
45 | var keys = Object.keys(val);
|
---|
46 | if (keys.length === 0) {
|
---|
47 | return false;
|
---|
48 | }
|
---|
49 | for (var i = 0; i < keys.length; i++) {
|
---|
50 | var key = keys[i];
|
---|
51 | if (hasValue(val[key])) {
|
---|
52 | return true;
|
---|
53 | }
|
---|
54 | }
|
---|
55 | return false;
|
---|
56 | default: {
|
---|
57 | return false;
|
---|
58 | }
|
---|
59 | }
|
---|
60 | };
|
---|
Note:
See
TracBrowser
for help on using the repository browser.