@lowentry/utils 0.1.1 → 0.2.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.
Files changed (4) hide show
  1. package/LeTypes.js +121 -0
  2. package/LeUtils.js +1289 -115
  3. package/index.js +2 -2
  4. package/package.json +1 -1
package/LeTypes.js CHANGED
@@ -1,12 +1,73 @@
1
+ import FastDeepEqual from 'fast-deep-equal';
2
+
3
+
4
+ /**
5
+ * A deep equals implementation (npm package "fast-deep-equal").
6
+ *
7
+ * @param {*} value The value to compare.
8
+ * @param {*} other The other value to compare.
9
+ * @returns {boolean} Returns true if the values are equivalent.
10
+ */
11
+ export const EQUAL = FastDeepEqual;
12
+
13
+
14
+ /**
15
+ * Returns true if the value is set (not undefined and not null).
16
+ *
17
+ * @param {*} value
18
+ * @returns {boolean}
19
+ */
1
20
  export const ISSET = (value) => (typeof value !== 'undefined') && (value !== null);
2
21
 
22
+
23
+ /**
24
+ * Returns true if the value is an array.
25
+ *
26
+ * @param {*} value
27
+ * @returns {boolean}
28
+ */
3
29
  export const IS_ARRAY = (value) => Array.isArray(value);
30
+
31
+ /**
32
+ * Ensures the given value is an array (returns the value wrapped in an array if it's not).
33
+ *
34
+ * @param {*} value
35
+ * @returns {*[]}
36
+ */
4
37
  export const ARRAY = (value) => IS_ARRAY(value) ? value : ((typeof value !== 'undefined') ? [value] : []);
5
38
 
39
+
40
+ /**
41
+ * Returns true if the value is an object.
42
+ *
43
+ * @param {*} value
44
+ * @returns {boolean}
45
+ */
6
46
  export const IS_OBJECT = (value) => (typeof value === 'object') && (value !== null) && !Array.isArray(value);
47
+
48
+ /**
49
+ * Ensures the given value is an object (returns an empty object if it's not).
50
+ *
51
+ * @param value
52
+ * @returns {object}
53
+ */
7
54
  export const OBJECT = (value) => IS_OBJECT(value) ? value : {};
8
55
 
56
+
57
+ /**
58
+ * Ensures the given value is a string (casts it to a string if it's not, null and undefined will return an empty string).
59
+ *
60
+ * @param {*} value
61
+ * @returns {string}
62
+ */
9
63
  export const STRING = (value) => ISSET(value) ? ('' + value) : '';
64
+
65
+ /**
66
+ * Returns the first non-null non-undefined value as a string.
67
+ *
68
+ * @param {*} values
69
+ * @returns {string}
70
+ */
10
71
  export const STRING_ANY = (...values) =>
11
72
  {
12
73
  for(let value of values)
@@ -19,9 +80,30 @@ export const STRING_ANY = (...values) =>
19
80
  return '';
20
81
  };
21
82
 
83
+
84
+ /**
85
+ * Ensures the given value is an integer (attempts to cast it to an integer if it's not, null and undefined will return 0).
86
+ *
87
+ * @param {*} value
88
+ * @returns {number}
89
+ */
22
90
  export const INT = (value) => Math.round(FLOAT(value));
91
+
92
+ /**
93
+ * Returns the first non-null non-undefined int-castable value as an integer.
94
+ *
95
+ * @param {*} values
96
+ * @returns {number}
97
+ */
23
98
  export const INT_ANY = (...values) => Math.round(FLOAT_ANY(...values));
24
99
 
100
+
101
+ /**
102
+ * Ensures the given value is a float (attempts to cast it to a float if it's not, null and undefined will return 0).
103
+ *
104
+ * @param {*} value
105
+ * @returns {number}
106
+ */
25
107
  export const FLOAT = (value) =>
26
108
  {
27
109
  const v = +value;
@@ -31,6 +113,13 @@ export const FLOAT = (value) =>
31
113
  }
32
114
  return 0;
33
115
  };
116
+
117
+ /**
118
+ * Returns the first non-null non-undefined float-castable value as a float.
119
+ *
120
+ * @param {*} values
121
+ * @returns {number}
122
+ */
34
123
  export const FLOAT_ANY = (...values) =>
35
124
  {
36
125
  for(let value of values)
@@ -47,9 +136,33 @@ export const FLOAT_ANY = (...values) =>
47
136
  return 0;
48
137
  };
49
138
 
139
+
140
+ /**
141
+ * Ensures the given value is an integer (attempts to cast it to an integer if it's not, null and undefined will return 0).
142
+ * This version is less strict than INT, as it relies on parseFloat instead of on +value, meaning that it will accept strings that contain a number followed by other characters, which +value doesn't.
143
+ *
144
+ * @param {*} value
145
+ * @returns {number}
146
+ */
50
147
  export const INT_LAX = (value) => Math.round(FLOAT_LAX(value));
148
+
149
+ /**
150
+ * Returns the first non-null non-undefined int-castable value as an integer.
151
+ * This version is less strict than INT_ANY, as it relies on parseFloat instead of on +value, meaning that it will accept strings that contain a number followed by other characters, which +value doesn't.
152
+ *
153
+ * @param {*} values
154
+ * @returns {number}
155
+ */
51
156
  export const INT_LAX_ANY = (...values) => Math.round(FLOAT_LAX_ANY(...values));
52
157
 
158
+
159
+ /**
160
+ * Ensures the given value is a float (attempts to cast it to a float if it's not, null and undefined will return 0).
161
+ * This version is less strict than FLOAT, as it relies on parseFloat instead of on +value, meaning that it will accept strings that contain a number followed by other characters, which +value doesn't.
162
+ *
163
+ * @param {*} value
164
+ * @returns {number}
165
+ */
53
166
  export const FLOAT_LAX = (value) =>
54
167
  {
55
168
  const v = parseFloat(value);
@@ -59,6 +172,14 @@ export const FLOAT_LAX = (value) =>
59
172
  }
60
173
  return 0;
61
174
  };
175
+
176
+ /**
177
+ * Returns the first non-null non-undefined float-castable value as a float.
178
+ * This version is less strict than FLOAT_ANY, as it relies on parseFloat instead of on +value, meaning that it will accept strings that contain a number followed by other characters, which +value doesn't.
179
+ *
180
+ * @param {*} values
181
+ * @returns {number}
182
+ */
62
183
  export const FLOAT_LAX_ANY = (...values) =>
63
184
  {
64
185
  for(let value of values)