@lowentry/utils 1.24.2 → 1.25.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.d.ts +4 -0
- package/index.js +67 -9
- package/index.js.map +1 -1
- package/package.json +1 -1
- package/src/LeTypes.js +67 -0
- package/src/index.js +1 -1
package/package.json
CHANGED
package/src/LeTypes.js
CHANGED
|
@@ -71,6 +71,73 @@ export const STRING_ANY = (...values) =>
|
|
|
71
71
|
};
|
|
72
72
|
|
|
73
73
|
|
|
74
|
+
/**
|
|
75
|
+
* Ensures the given value is a boolean.
|
|
76
|
+
*
|
|
77
|
+
* This will work differently than !!value, as it will try to figure out the most logical boolean value from the given value.
|
|
78
|
+
*
|
|
79
|
+
* @param {*} value
|
|
80
|
+
* @returns {boolean}
|
|
81
|
+
*/
|
|
82
|
+
export const BOOL = (value) =>
|
|
83
|
+
{
|
|
84
|
+
return BOOL_ANY(value);
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Returns the first non-null non-undefined boolean-castable value as a boolean.
|
|
89
|
+
*
|
|
90
|
+
* @param {*} values
|
|
91
|
+
* @returns {boolean}
|
|
92
|
+
*/
|
|
93
|
+
export const BOOL_ANY = (...values) =>
|
|
94
|
+
{
|
|
95
|
+
for(let value of values)
|
|
96
|
+
{
|
|
97
|
+
if(!ISSET(value))
|
|
98
|
+
{
|
|
99
|
+
continue;
|
|
100
|
+
}
|
|
101
|
+
if(typeof value === 'boolean')
|
|
102
|
+
{
|
|
103
|
+
return value;
|
|
104
|
+
}
|
|
105
|
+
if(typeof value === 'number')
|
|
106
|
+
{
|
|
107
|
+
if(!isNaN(value))
|
|
108
|
+
{
|
|
109
|
+
return (value !== 0);
|
|
110
|
+
}
|
|
111
|
+
return false;
|
|
112
|
+
}
|
|
113
|
+
if(typeof value === 'string')
|
|
114
|
+
{
|
|
115
|
+
value = value.toLowerCase().trim();
|
|
116
|
+
if((value === '') || (value === 'false') || (value === 'no') || (value === 'off') || (value === '0'))
|
|
117
|
+
{
|
|
118
|
+
return false;
|
|
119
|
+
}
|
|
120
|
+
if((value === 'true') || (value === 'yes') || (value === 'on') || (value === '1'))
|
|
121
|
+
{
|
|
122
|
+
return true;
|
|
123
|
+
}
|
|
124
|
+
const float = +value;
|
|
125
|
+
if(!isNaN(float))
|
|
126
|
+
{
|
|
127
|
+
return (float !== 0);
|
|
128
|
+
}
|
|
129
|
+
// unrecognized string, let's try the next value, else we return false (after the loop)
|
|
130
|
+
}
|
|
131
|
+
if(IS_ARRAY(value) || IS_OBJECT(value))
|
|
132
|
+
{
|
|
133
|
+
return !!value;
|
|
134
|
+
}
|
|
135
|
+
// unrecognized type, let's try the next value, else we return false (after the loop)
|
|
136
|
+
}
|
|
137
|
+
return false;
|
|
138
|
+
};
|
|
139
|
+
|
|
140
|
+
|
|
74
141
|
/**
|
|
75
142
|
* Ensures the given value is an integer (attempts to cast it to an integer if it's not, null and undefined will return 0).
|
|
76
143
|
*
|
package/src/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
export {LeUtils} from './LeUtils.js';
|
|
2
|
-
export {ISSET, IS_ARRAY, ARRAY, IS_OBJECT, OBJECT, STRING, STRING_ANY, INT, INT_ANY, FLOAT, FLOAT_ANY, INT_LAX, INT_LAX_ANY, FLOAT_LAX, FLOAT_LAX_ANY} from './LeTypes.js';
|
|
2
|
+
export {ISSET, IS_ARRAY, ARRAY, IS_OBJECT, OBJECT, STRING, STRING_ANY, BOOL, BOOL_ANY, INT, INT_ANY, FLOAT, FLOAT_ANY, INT_LAX, INT_LAX_ANY, FLOAT_LAX, FLOAT_LAX_ANY} from './LeTypes.js';
|
|
3
3
|
export {EventEmitter} from './classes/EventEmitter.js';
|
|
4
4
|
export {LinkedList} from './classes/LinkedList.js';
|
|
5
5
|
export {SerializableMap} from './classes/SerializableMap.js';
|