typof 1.0.5 → 1.0.7
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 +12 -8
- package/dist/main.js +11 -17
- package/dist/main.mjs +11 -17
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -108,7 +108,7 @@ import { typof } from 'typof';
|
|
|
108
108
|
|
|
109
109
|
`typof(value)`
|
|
110
110
|
|
|
111
|
-
|
|
111
|
+
Infer types.
|
|
112
112
|
|
|
113
113
|
> | Parameter | Type | Default | Description |
|
|
114
114
|
> | --------- | ------- | ------- | --------------------- |
|
|
@@ -119,6 +119,12 @@ Infers types.
|
|
|
119
119
|
> Example:
|
|
120
120
|
>
|
|
121
121
|
> ```typescript
|
|
122
|
+
> // Tests are as follows. (Is this an integer?)
|
|
123
|
+
> if (typof(0).includes('integer')) console.log('This is an integer.');
|
|
124
|
+
>
|
|
125
|
+
> // Index zero always ensures reliable type checking. As the index increases, species depth also increases.
|
|
126
|
+
> if (typof('0.5')[0] === 'string') console.log('This is a string.');
|
|
127
|
+
>
|
|
122
128
|
> typof('test'); // [ "string" ]
|
|
123
129
|
>
|
|
124
130
|
> typof('0'); // [ "string", "number", "integer" ]
|
|
@@ -130,7 +136,7 @@ Infers types.
|
|
|
130
136
|
> typof('true'); // [ "string", "boolean" ]
|
|
131
137
|
> typof(true); // [ "boolean" ]
|
|
132
138
|
>
|
|
133
|
-
> typof('{"key":
|
|
139
|
+
> typof('{"key":"value"}'); // [ "string", "object" ]
|
|
134
140
|
> typof({ key: 'value' }); // [ "object" ]
|
|
135
141
|
>
|
|
136
142
|
> typof('["test"]'); // [ "string", "array" ]
|
|
@@ -144,12 +150,6 @@ Infers types.
|
|
|
144
150
|
>
|
|
145
151
|
> typof('undefined'); // [ "string", "undefined" ]
|
|
146
152
|
> typof(undefined); // [ "undefined" ]
|
|
147
|
-
>
|
|
148
|
-
> // Tests are as follows. (Is this an integer?)
|
|
149
|
-
> if (typof(0).includes('integer')) console.log('This is an integer.');
|
|
150
|
-
>
|
|
151
|
-
> // Index zero always ensures reliable type checking. As the index increases, species depth also increases.
|
|
152
|
-
> if (typof('0')[0] === 'string') console.log('This is a string.');
|
|
153
153
|
> ```
|
|
154
154
|
|
|
155
155
|
<br/>
|
|
@@ -192,6 +192,8 @@ Convert to number.
|
|
|
192
192
|
>
|
|
193
193
|
> ```typescript
|
|
194
194
|
> number('0.5'); // 0.5
|
|
195
|
+
> number(0.5); // 0.5
|
|
196
|
+
> number('test'); // NaN
|
|
195
197
|
> ```
|
|
196
198
|
|
|
197
199
|
<br/>
|
|
@@ -211,6 +213,7 @@ Convert to integer.
|
|
|
211
213
|
> ```typescript
|
|
212
214
|
> integer('0.5'); // 0
|
|
213
215
|
> integer(0.5); // 0
|
|
216
|
+
> integer('test'); // NaN
|
|
214
217
|
> ```
|
|
215
218
|
|
|
216
219
|
<br/>
|
|
@@ -287,6 +290,7 @@ Convert to date.
|
|
|
287
290
|
>
|
|
288
291
|
> ```typescript
|
|
289
292
|
> date('2025-01-01'); // 2025-01-01T00:00:00.000Z
|
|
293
|
+
> date(new Date('2025-01-01')); // 2025-01-01T00:00:00.000Z
|
|
290
294
|
> date('test'); // "test"
|
|
291
295
|
> ```
|
|
292
296
|
|
package/dist/main.js
CHANGED
|
@@ -66,9 +66,7 @@ var typof = (value) => {
|
|
|
66
66
|
types.push("number");
|
|
67
67
|
if (Number.isInteger(number2)) {
|
|
68
68
|
types.push("integer");
|
|
69
|
-
} else
|
|
70
|
-
types.push("float");
|
|
71
|
-
}
|
|
69
|
+
} else types.push("float");
|
|
72
70
|
}
|
|
73
71
|
if (value === "true" || value === "false") types.push("boolean");
|
|
74
72
|
if (isObject(value) && !Array.isArray(JSON.parse(value))) types.push("object");
|
|
@@ -81,9 +79,7 @@ var typof = (value) => {
|
|
|
81
79
|
types.push("number");
|
|
82
80
|
if (Number.isInteger(value)) {
|
|
83
81
|
types.push("integer");
|
|
84
|
-
} else
|
|
85
|
-
types.push("float");
|
|
86
|
-
}
|
|
82
|
+
} else types.push("float");
|
|
87
83
|
} else if (typeof value === "boolean") {
|
|
88
84
|
types.push("boolean");
|
|
89
85
|
} else if (isObject(value) && !Array.isArray(value)) {
|
|
@@ -97,7 +93,8 @@ var typof = (value) => {
|
|
|
97
93
|
return types;
|
|
98
94
|
};
|
|
99
95
|
var string = (value) => {
|
|
100
|
-
|
|
96
|
+
const types = typof(value);
|
|
97
|
+
return types.includes("object") || types.includes("array") ? JSON.stringify(value) : String(value);
|
|
101
98
|
};
|
|
102
99
|
var number = (value) => {
|
|
103
100
|
return typof(value).includes("number") ? Number(value) : NaN;
|
|
@@ -106,7 +103,8 @@ var integer = (value) => {
|
|
|
106
103
|
return typof(value).includes("number") ? Math.trunc(Number(value)) : NaN;
|
|
107
104
|
};
|
|
108
105
|
var boolean = (value) => {
|
|
109
|
-
|
|
106
|
+
const types = typof(value);
|
|
107
|
+
if (types.includes("boolean") && types.includes("string")) {
|
|
110
108
|
if (value === "true") {
|
|
111
109
|
return true;
|
|
112
110
|
} else if (value === "false") {
|
|
@@ -115,19 +113,15 @@ var boolean = (value) => {
|
|
|
115
113
|
} else return value;
|
|
116
114
|
};
|
|
117
115
|
var object = (value) => {
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
} else return value;
|
|
116
|
+
const types = typof(value);
|
|
117
|
+
return types.includes("object") && types.includes("string") ? JSON.parse(value) : value;
|
|
121
118
|
};
|
|
122
119
|
var array = (value) => {
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
} else return value;
|
|
120
|
+
const types = typof(value);
|
|
121
|
+
return types.includes("array") && types.includes("string") ? JSON.parse(value) : value;
|
|
126
122
|
};
|
|
127
123
|
var date = (value) => {
|
|
128
|
-
|
|
129
|
-
return new Date(value);
|
|
130
|
-
} else return value;
|
|
124
|
+
return typof(value).includes("date") ? new Date(value) : value;
|
|
131
125
|
};
|
|
132
126
|
var _null = (value) => {
|
|
133
127
|
return typof(value).includes("null") ? null : value;
|
package/dist/main.mjs
CHANGED
|
@@ -31,9 +31,7 @@ var typof = (value) => {
|
|
|
31
31
|
types.push("number");
|
|
32
32
|
if (Number.isInteger(number2)) {
|
|
33
33
|
types.push("integer");
|
|
34
|
-
} else
|
|
35
|
-
types.push("float");
|
|
36
|
-
}
|
|
34
|
+
} else types.push("float");
|
|
37
35
|
}
|
|
38
36
|
if (value === "true" || value === "false") types.push("boolean");
|
|
39
37
|
if (isObject(value) && !Array.isArray(JSON.parse(value))) types.push("object");
|
|
@@ -46,9 +44,7 @@ var typof = (value) => {
|
|
|
46
44
|
types.push("number");
|
|
47
45
|
if (Number.isInteger(value)) {
|
|
48
46
|
types.push("integer");
|
|
49
|
-
} else
|
|
50
|
-
types.push("float");
|
|
51
|
-
}
|
|
47
|
+
} else types.push("float");
|
|
52
48
|
} else if (typeof value === "boolean") {
|
|
53
49
|
types.push("boolean");
|
|
54
50
|
} else if (isObject(value) && !Array.isArray(value)) {
|
|
@@ -62,7 +58,8 @@ var typof = (value) => {
|
|
|
62
58
|
return types;
|
|
63
59
|
};
|
|
64
60
|
var string = (value) => {
|
|
65
|
-
|
|
61
|
+
const types = typof(value);
|
|
62
|
+
return types.includes("object") || types.includes("array") ? JSON.stringify(value) : String(value);
|
|
66
63
|
};
|
|
67
64
|
var number = (value) => {
|
|
68
65
|
return typof(value).includes("number") ? Number(value) : NaN;
|
|
@@ -71,7 +68,8 @@ var integer = (value) => {
|
|
|
71
68
|
return typof(value).includes("number") ? Math.trunc(Number(value)) : NaN;
|
|
72
69
|
};
|
|
73
70
|
var boolean = (value) => {
|
|
74
|
-
|
|
71
|
+
const types = typof(value);
|
|
72
|
+
if (types.includes("boolean") && types.includes("string")) {
|
|
75
73
|
if (value === "true") {
|
|
76
74
|
return true;
|
|
77
75
|
} else if (value === "false") {
|
|
@@ -80,19 +78,15 @@ var boolean = (value) => {
|
|
|
80
78
|
} else return value;
|
|
81
79
|
};
|
|
82
80
|
var object = (value) => {
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
} else return value;
|
|
81
|
+
const types = typof(value);
|
|
82
|
+
return types.includes("object") && types.includes("string") ? JSON.parse(value) : value;
|
|
86
83
|
};
|
|
87
84
|
var array = (value) => {
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
} else return value;
|
|
85
|
+
const types = typof(value);
|
|
86
|
+
return types.includes("array") && types.includes("string") ? JSON.parse(value) : value;
|
|
91
87
|
};
|
|
92
88
|
var date = (value) => {
|
|
93
|
-
|
|
94
|
-
return new Date(value);
|
|
95
|
-
} else return value;
|
|
89
|
+
return typof(value).includes("date") ? new Date(value) : value;
|
|
96
90
|
};
|
|
97
91
|
var _null = (value) => {
|
|
98
92
|
return typof(value).includes("null") ? null : value;
|