typof 1.0.18 → 1.1.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/README.md CHANGED
@@ -74,6 +74,7 @@ typof
74
74
  ├── string(value)
75
75
  ├── number(value)
76
76
  ├── integer(value)
77
+ ├── float(value)
77
78
  ├── boolean(value)
78
79
  ├── date(value)
79
80
  ├── object(value)
@@ -126,7 +127,7 @@ Infer types.
126
127
  > typof(true); // ['boolean']
127
128
  >
128
129
  > typof('2025-01-01'); // ['string', 'date']
129
- > typof(new Date('2025-01-01')); // ['object', 'date']
130
+ > typof(new Date('2025-01-01')); // ['date', 'object']
130
131
  >
131
132
  > typof('{"key":"value"}'); // ['string', 'object']
132
133
  > typof({ key: 'value' }); // ['object']
@@ -207,6 +208,29 @@ Convert to integer.
207
208
 
208
209
  <br/>
209
210
 
211
+ `float(value, fraction_digits)`
212
+
213
+ Convert to float.
214
+
215
+ > | Parameter | Type | Default | Description |
216
+ > | ----------------- | ------- | ------- | ----------------- |
217
+ > | `value` | Unknown | | Value to convert. |
218
+ > | `fraction_digits` | Number | | Fraction digits. |
219
+ >
220
+ > returns String | Value
221
+ >
222
+ > Example:
223
+ >
224
+ > ```typescript
225
+ > float('0', 2); // '0.00'
226
+ > float(0, 2); // '0.00'
227
+ > float(0.5555, 2); // '0.55'
228
+ > float(0.5555, 4); // '0.5555'
229
+ > float('test', 2); // 'test'
230
+ > ```
231
+
232
+ <br/>
233
+
210
234
  `boolean(value)`
211
235
 
212
236
  Convert to boolean.
package/dist/main.cjs CHANGED
@@ -44,8 +44,8 @@ const typof = (value) => {
44
44
  else types.push("float");
45
45
  } else if (typeof value === "boolean") types.push("boolean");
46
46
  else if (is_object(value) && !Array.isArray(value)) {
47
- types.push("object");
48
47
  if (value instanceof Date) types.push("date");
48
+ types.push("object");
49
49
  } else if (is_object(value) && Array.isArray(value)) types.push("array");
50
50
  else if (value === null) types.push("null");
51
51
  else if (value === void 0) types.push("undefined");
@@ -61,10 +61,13 @@ const number = (value) => {
61
61
  const integer = (value) => {
62
62
  return typof(value).includes("number") ? Math.trunc(Number(value)) : NaN;
63
63
  };
64
+ const float = (value, fraction_digits) => {
65
+ return typof(value).includes("number") ? Number(value).toFixed(fraction_digits) : value;
66
+ };
64
67
  const boolean = (value) => {
65
68
  const types = typof(value);
66
- if (types.includes("boolean") && types.includes("string")) if (value === "true") return true;
67
- else if (value === "false") return false;
69
+ if (types.includes("boolean") && types.includes("string")) if (value.trim() === "true") return true;
70
+ else if (value.trim() === "false") return false;
68
71
  else return value;
69
72
  else return value;
70
73
  };
@@ -91,6 +94,7 @@ exports._undefined = _undefined;
91
94
  exports.array = array;
92
95
  exports.boolean = boolean;
93
96
  exports.date = date;
97
+ exports.float = float;
94
98
  exports.integer = integer;
95
99
  exports.number = number;
96
100
  exports.object = object;
package/dist/main.d.cts CHANGED
@@ -9,6 +9,7 @@ declare const typof: (value: unknown) => Types[];
9
9
  declare const string: (value: unknown) => string;
10
10
  declare const number: (value: unknown) => number;
11
11
  declare const integer: (value: unknown) => number;
12
+ declare const float: <Value>(value: Value, fraction_digits: number) => string | Value;
12
13
  declare const boolean: <Value>(value: Value) => boolean | Value;
13
14
  declare const date: <Value>(value: Value) => Date | Value;
14
15
  declare const object: <Value>(value: Value) => UnknownObject | Value;
@@ -16,4 +17,4 @@ declare const array: <Value>(value: Value) => unknown[] | Value;
16
17
  declare const _null: <Value>(value: Value) => null | Value;
17
18
  declare const _undefined: <Value>(value: Value) => undefined | Value;
18
19
  //#endregion
19
- export { type Types, type UnknownObject, _null, _undefined, array, boolean, date, integer, number, object, string, typof };
20
+ export { type Types, type UnknownObject, _null, _undefined, array, boolean, date, float, integer, number, object, string, typof };
package/dist/main.d.mts CHANGED
@@ -9,6 +9,7 @@ declare const typof: (value: unknown) => Types[];
9
9
  declare const string: (value: unknown) => string;
10
10
  declare const number: (value: unknown) => number;
11
11
  declare const integer: (value: unknown) => number;
12
+ declare const float: <Value>(value: Value, fraction_digits: number) => string | Value;
12
13
  declare const boolean: <Value>(value: Value) => boolean | Value;
13
14
  declare const date: <Value>(value: Value) => Date | Value;
14
15
  declare const object: <Value>(value: Value) => UnknownObject | Value;
@@ -16,4 +17,4 @@ declare const array: <Value>(value: Value) => unknown[] | Value;
16
17
  declare const _null: <Value>(value: Value) => null | Value;
17
18
  declare const _undefined: <Value>(value: Value) => undefined | Value;
18
19
  //#endregion
19
- export { type Types, type UnknownObject, _null, _undefined, array, boolean, date, integer, number, object, string, typof };
20
+ export { type Types, type UnknownObject, _null, _undefined, array, boolean, date, float, integer, number, object, string, typof };
package/dist/main.mjs CHANGED
@@ -43,8 +43,8 @@ const typof = (value) => {
43
43
  else types.push("float");
44
44
  } else if (typeof value === "boolean") types.push("boolean");
45
45
  else if (is_object(value) && !Array.isArray(value)) {
46
- types.push("object");
47
46
  if (value instanceof Date) types.push("date");
47
+ types.push("object");
48
48
  } else if (is_object(value) && Array.isArray(value)) types.push("array");
49
49
  else if (value === null) types.push("null");
50
50
  else if (value === void 0) types.push("undefined");
@@ -60,10 +60,13 @@ const number = (value) => {
60
60
  const integer = (value) => {
61
61
  return typof(value).includes("number") ? Math.trunc(Number(value)) : NaN;
62
62
  };
63
+ const float = (value, fraction_digits) => {
64
+ return typof(value).includes("number") ? Number(value).toFixed(fraction_digits) : value;
65
+ };
63
66
  const boolean = (value) => {
64
67
  const types = typof(value);
65
- if (types.includes("boolean") && types.includes("string")) if (value === "true") return true;
66
- else if (value === "false") return false;
68
+ if (types.includes("boolean") && types.includes("string")) if (value.trim() === "true") return true;
69
+ else if (value.trim() === "false") return false;
67
70
  else return value;
68
71
  else return value;
69
72
  };
@@ -85,4 +88,4 @@ const _undefined = (value) => {
85
88
  return typof(value).includes("undefined") ? void 0 : value;
86
89
  };
87
90
  //#endregion
88
- export { _null, _undefined, array, boolean, date, integer, number, object, string, typof };
91
+ export { _null, _undefined, array, boolean, date, float, integer, number, object, string, typof };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "typof",
3
- "version": "1.0.18",
3
+ "version": "1.1.1",
4
4
  "description": "Infer types.",
5
5
  "license": "MIT",
6
6
  "homepage": "https://github.com/keift/typof",