typof 1.0.4 → 1.0.6
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 +48 -42
- package/dist/main.d.mts +1 -1
- package/dist/main.d.ts +1 -1
- package/dist/main.js +10 -12
- package/dist/main.mjs +10 -12
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -2,7 +2,6 @@
|
|
|
2
2
|
[Number]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number
|
|
3
3
|
[Boolean]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean
|
|
4
4
|
[Object]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object
|
|
5
|
-
[Array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array
|
|
6
5
|
[Date]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date
|
|
7
6
|
[Buffer]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer
|
|
8
7
|
[Promise]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise
|
|
@@ -58,19 +57,19 @@ Infer types.
|
|
|
58
57
|
You can install it as follows.
|
|
59
58
|
|
|
60
59
|
```shell
|
|
61
|
-
|
|
60
|
+
# NPM
|
|
62
61
|
npm add typof
|
|
63
62
|
|
|
64
|
-
|
|
63
|
+
# PNPM
|
|
65
64
|
pnpm add typof
|
|
66
65
|
|
|
67
|
-
|
|
66
|
+
# Yarn
|
|
68
67
|
yarn add typof
|
|
69
68
|
|
|
70
|
-
|
|
69
|
+
# Bun
|
|
71
70
|
bun add typof
|
|
72
71
|
|
|
73
|
-
|
|
72
|
+
# Deno
|
|
74
73
|
deno add typof
|
|
75
74
|
```
|
|
76
75
|
|
|
@@ -109,17 +108,23 @@ import { typof } from 'typof';
|
|
|
109
108
|
|
|
110
109
|
`typof(value)`
|
|
111
110
|
|
|
112
|
-
|
|
111
|
+
Infer types.
|
|
113
112
|
|
|
114
|
-
> | Parameter | Default | Description
|
|
115
|
-
> | --------- | ------- |
|
|
116
|
-
> | value | |
|
|
113
|
+
> | Parameter | Type | Default | Description |
|
|
114
|
+
> | --------- | ------- | ------- | --------------------- |
|
|
115
|
+
> | value | Unknown | | Value to infer types. |
|
|
117
116
|
>
|
|
118
117
|
> returns [Types]\[]
|
|
119
118
|
>
|
|
120
119
|
> Example:
|
|
121
120
|
>
|
|
122
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
|
+
>
|
|
123
128
|
> typof('test'); // [ "string" ]
|
|
124
129
|
>
|
|
125
130
|
> typof('0'); // [ "string", "number", "integer" ]
|
|
@@ -131,7 +136,7 @@ Infers types.
|
|
|
131
136
|
> typof('true'); // [ "string", "boolean" ]
|
|
132
137
|
> typof(true); // [ "boolean" ]
|
|
133
138
|
>
|
|
134
|
-
> typof('{"key":
|
|
139
|
+
> typof('{"key":"value"}'); // [ "string", "object" ]
|
|
135
140
|
> typof({ key: 'value' }); // [ "object" ]
|
|
136
141
|
>
|
|
137
142
|
> typof('["test"]'); // [ "string", "array" ]
|
|
@@ -145,9 +150,6 @@ Infers types.
|
|
|
145
150
|
>
|
|
146
151
|
> typof('undefined'); // [ "string", "undefined" ]
|
|
147
152
|
> typof(undefined); // [ "undefined" ]
|
|
148
|
-
>
|
|
149
|
-
> // Tests are as follows. (Is this an integer?)
|
|
150
|
-
> if (typof(0).includes('integer')) console.log('This is an integer!');
|
|
151
153
|
> ```
|
|
152
154
|
|
|
153
155
|
<br/>
|
|
@@ -156,9 +158,9 @@ Infers types.
|
|
|
156
158
|
|
|
157
159
|
Convert to string.
|
|
158
160
|
|
|
159
|
-
> | Parameter | Default | Description
|
|
160
|
-
> | --------- | ------- |
|
|
161
|
-
> | value | |
|
|
161
|
+
> | Parameter | Type | Default | Description |
|
|
162
|
+
> | --------- | ------- | ------- | ----------------- |
|
|
163
|
+
> | value | Unknown | | Value to convert. |
|
|
162
164
|
>
|
|
163
165
|
> returns [String]
|
|
164
166
|
>
|
|
@@ -180,9 +182,9 @@ Convert to string.
|
|
|
180
182
|
|
|
181
183
|
Convert to number.
|
|
182
184
|
|
|
183
|
-
> | Parameter | Default | Description
|
|
184
|
-
> | --------- | ------- |
|
|
185
|
-
> | value | |
|
|
185
|
+
> | Parameter | Type | Default | Description |
|
|
186
|
+
> | --------- | ------- | ------- | ----------------- |
|
|
187
|
+
> | value | Unknown | | Value to convert. |
|
|
186
188
|
>
|
|
187
189
|
> returns [Number]
|
|
188
190
|
>
|
|
@@ -190,6 +192,8 @@ Convert to number.
|
|
|
190
192
|
>
|
|
191
193
|
> ```typescript
|
|
192
194
|
> number('0.5'); // 0.5
|
|
195
|
+
> number(0.5); // 0.5
|
|
196
|
+
> number('test'); // NaN
|
|
193
197
|
> ```
|
|
194
198
|
|
|
195
199
|
<br/>
|
|
@@ -198,9 +202,9 @@ Convert to number.
|
|
|
198
202
|
|
|
199
203
|
Convert to integer.
|
|
200
204
|
|
|
201
|
-
> | Parameter | Default | Description
|
|
202
|
-
> | --------- | ------- |
|
|
203
|
-
> | value | |
|
|
205
|
+
> | Parameter | Type | Default | Description |
|
|
206
|
+
> | --------- | ------- | ------- | ----------------- |
|
|
207
|
+
> | value | Unknown | | Value to convert. |
|
|
204
208
|
>
|
|
205
209
|
> returns [Number]
|
|
206
210
|
>
|
|
@@ -209,6 +213,7 @@ Convert to integer.
|
|
|
209
213
|
> ```typescript
|
|
210
214
|
> integer('0.5'); // 0
|
|
211
215
|
> integer(0.5); // 0
|
|
216
|
+
> integer('test'); // NaN
|
|
212
217
|
> ```
|
|
213
218
|
|
|
214
219
|
<br/>
|
|
@@ -217,9 +222,9 @@ Convert to integer.
|
|
|
217
222
|
|
|
218
223
|
Convert to boolean.
|
|
219
224
|
|
|
220
|
-
> | Parameter | Default | Description
|
|
221
|
-
> | --------- | ------- |
|
|
222
|
-
> | value | |
|
|
225
|
+
> | Parameter | Type | Default | Description |
|
|
226
|
+
> | --------- | ------- | ------- | ----------------- |
|
|
227
|
+
> | value | Unknown | | Value to convert. |
|
|
223
228
|
>
|
|
224
229
|
> returns [Boolean] | Value
|
|
225
230
|
>
|
|
@@ -237,9 +242,9 @@ Convert to boolean.
|
|
|
237
242
|
|
|
238
243
|
Convert to object.
|
|
239
244
|
|
|
240
|
-
> | Parameter | Default | Description
|
|
241
|
-
> | --------- | ------- |
|
|
242
|
-
> | value | |
|
|
245
|
+
> | Parameter | Type | Default | Description |
|
|
246
|
+
> | --------- | ------- | ------- | ----------------- |
|
|
247
|
+
> | value | Unknown | | Value to convert. |
|
|
243
248
|
>
|
|
244
249
|
> returns [Object] | Value
|
|
245
250
|
>
|
|
@@ -256,11 +261,11 @@ Convert to object.
|
|
|
256
261
|
|
|
257
262
|
Convert to array.
|
|
258
263
|
|
|
259
|
-
> | Parameter | Default | Description
|
|
260
|
-
> | --------- | ------- |
|
|
261
|
-
> | value | |
|
|
264
|
+
> | Parameter | Type | Default | Description |
|
|
265
|
+
> | --------- | ------- | ------- | ----------------- |
|
|
266
|
+
> | value | Unknown | | Value to convert. |
|
|
262
267
|
>
|
|
263
|
-
> returns [
|
|
268
|
+
> returns Unknown\[] | Value
|
|
264
269
|
>
|
|
265
270
|
> Example:
|
|
266
271
|
>
|
|
@@ -275,9 +280,9 @@ Convert to array.
|
|
|
275
280
|
|
|
276
281
|
Convert to date.
|
|
277
282
|
|
|
278
|
-
> | Parameter | Default | Description
|
|
279
|
-
> | --------- | ------- |
|
|
280
|
-
> | value | |
|
|
283
|
+
> | Parameter | Type | Default | Description |
|
|
284
|
+
> | --------- | ------- | ------- | ----------------- |
|
|
285
|
+
> | value | Unknown | | Value to convert. |
|
|
281
286
|
>
|
|
282
287
|
> returns [Date] | Value
|
|
283
288
|
>
|
|
@@ -285,6 +290,7 @@ Convert to date.
|
|
|
285
290
|
>
|
|
286
291
|
> ```typescript
|
|
287
292
|
> date('2025-01-01'); // 2025-01-01T00:00:00.000Z
|
|
293
|
+
> date(new Date('2025-01-01')); // 2025-01-01T00:00:00.000Z
|
|
288
294
|
> date('test'); // "test"
|
|
289
295
|
> ```
|
|
290
296
|
|
|
@@ -294,9 +300,9 @@ Convert to date.
|
|
|
294
300
|
|
|
295
301
|
Convert to null.
|
|
296
302
|
|
|
297
|
-
> | Parameter | Default | Description
|
|
298
|
-
> | --------- | ------- |
|
|
299
|
-
> | value | |
|
|
303
|
+
> | Parameter | Type | Default | Description |
|
|
304
|
+
> | --------- | ------- | ------- | ----------------- |
|
|
305
|
+
> | value | Unknown | | Value to convert. |
|
|
300
306
|
>
|
|
301
307
|
> returns [Null] | Value
|
|
302
308
|
>
|
|
@@ -314,9 +320,9 @@ Convert to null.
|
|
|
314
320
|
|
|
315
321
|
Convert to undefined.
|
|
316
322
|
|
|
317
|
-
> | Parameter | Default | Description
|
|
318
|
-
> | --------- | ------- |
|
|
319
|
-
> | value | |
|
|
323
|
+
> | Parameter | Type | Default | Description |
|
|
324
|
+
> | --------- | ------- | ------- | ----------------- |
|
|
325
|
+
> | value | Unknown | | Value to convert. |
|
|
320
326
|
>
|
|
321
327
|
> returns [Undefined] | Value
|
|
322
328
|
>
|
package/dist/main.d.mts
CHANGED
|
@@ -7,7 +7,7 @@ declare const integer: (value: unknown) => number;
|
|
|
7
7
|
declare const boolean: <Value>(value: Value) => boolean | Value;
|
|
8
8
|
declare const object: <Value>(value: Value) => object | Value;
|
|
9
9
|
declare const array: <Value>(value: Value) => unknown[] | Value;
|
|
10
|
-
declare const date: (value:
|
|
10
|
+
declare const date: <Value>(value: Value) => Date | Value;
|
|
11
11
|
declare const _null: <Value>(value: Value) => null | Value;
|
|
12
12
|
declare const _undefined: <Value>(value: Value) => undefined | Value;
|
|
13
13
|
|
package/dist/main.d.ts
CHANGED
|
@@ -7,7 +7,7 @@ declare const integer: (value: unknown) => number;
|
|
|
7
7
|
declare const boolean: <Value>(value: Value) => boolean | Value;
|
|
8
8
|
declare const object: <Value>(value: Value) => object | Value;
|
|
9
9
|
declare const array: <Value>(value: Value) => unknown[] | Value;
|
|
10
|
-
declare const date: (value:
|
|
10
|
+
declare const date: <Value>(value: Value) => Date | Value;
|
|
11
11
|
declare const _null: <Value>(value: Value) => null | Value;
|
|
12
12
|
declare const _undefined: <Value>(value: Value) => undefined | Value;
|
|
13
13
|
|
package/dist/main.js
CHANGED
|
@@ -93,13 +93,12 @@ var typof = (value) => {
|
|
|
93
93
|
types.push("array");
|
|
94
94
|
} else if (value === null) {
|
|
95
95
|
types.push("null");
|
|
96
|
-
} else if (value === void 0)
|
|
97
|
-
types.push("undefined");
|
|
98
|
-
}
|
|
96
|
+
} else if (value === void 0) types.push("undefined");
|
|
99
97
|
return types;
|
|
100
98
|
};
|
|
101
99
|
var string = (value) => {
|
|
102
|
-
|
|
100
|
+
const types = typof(value);
|
|
101
|
+
return types.includes("object") || types.includes("array") ? JSON.stringify(value) : String(value);
|
|
103
102
|
};
|
|
104
103
|
var number = (value) => {
|
|
105
104
|
return typof(value).includes("number") ? Number(value) : NaN;
|
|
@@ -108,7 +107,8 @@ var integer = (value) => {
|
|
|
108
107
|
return typof(value).includes("number") ? Math.trunc(Number(value)) : NaN;
|
|
109
108
|
};
|
|
110
109
|
var boolean = (value) => {
|
|
111
|
-
|
|
110
|
+
const types = typof(value);
|
|
111
|
+
if (types.includes("boolean") && types.includes("string")) {
|
|
112
112
|
if (value === "true") {
|
|
113
113
|
return true;
|
|
114
114
|
} else if (value === "false") {
|
|
@@ -117,17 +117,15 @@ var boolean = (value) => {
|
|
|
117
117
|
} else return value;
|
|
118
118
|
};
|
|
119
119
|
var object = (value) => {
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
} else return value;
|
|
120
|
+
const types = typof(value);
|
|
121
|
+
return types.includes("object") && types.includes("string") ? JSON.parse(value) : value;
|
|
123
122
|
};
|
|
124
123
|
var array = (value) => {
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
} else return value;
|
|
124
|
+
const types = typof(value);
|
|
125
|
+
return types.includes("array") && types.includes("string") ? JSON.parse(value) : value;
|
|
128
126
|
};
|
|
129
127
|
var date = (value) => {
|
|
130
|
-
return new Date(value);
|
|
128
|
+
return typof(value).includes("date") ? new Date(value) : value;
|
|
131
129
|
};
|
|
132
130
|
var _null = (value) => {
|
|
133
131
|
return typof(value).includes("null") ? null : value;
|
package/dist/main.mjs
CHANGED
|
@@ -58,13 +58,12 @@ var typof = (value) => {
|
|
|
58
58
|
types.push("array");
|
|
59
59
|
} else if (value === null) {
|
|
60
60
|
types.push("null");
|
|
61
|
-
} else if (value === void 0)
|
|
62
|
-
types.push("undefined");
|
|
63
|
-
}
|
|
61
|
+
} else if (value === void 0) types.push("undefined");
|
|
64
62
|
return types;
|
|
65
63
|
};
|
|
66
64
|
var string = (value) => {
|
|
67
|
-
|
|
65
|
+
const types = typof(value);
|
|
66
|
+
return types.includes("object") || types.includes("array") ? JSON.stringify(value) : String(value);
|
|
68
67
|
};
|
|
69
68
|
var number = (value) => {
|
|
70
69
|
return typof(value).includes("number") ? Number(value) : NaN;
|
|
@@ -73,7 +72,8 @@ var integer = (value) => {
|
|
|
73
72
|
return typof(value).includes("number") ? Math.trunc(Number(value)) : NaN;
|
|
74
73
|
};
|
|
75
74
|
var boolean = (value) => {
|
|
76
|
-
|
|
75
|
+
const types = typof(value);
|
|
76
|
+
if (types.includes("boolean") && types.includes("string")) {
|
|
77
77
|
if (value === "true") {
|
|
78
78
|
return true;
|
|
79
79
|
} else if (value === "false") {
|
|
@@ -82,17 +82,15 @@ var boolean = (value) => {
|
|
|
82
82
|
} else return value;
|
|
83
83
|
};
|
|
84
84
|
var object = (value) => {
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
} else return value;
|
|
85
|
+
const types = typof(value);
|
|
86
|
+
return types.includes("object") && types.includes("string") ? JSON.parse(value) : value;
|
|
88
87
|
};
|
|
89
88
|
var array = (value) => {
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
} else return value;
|
|
89
|
+
const types = typof(value);
|
|
90
|
+
return types.includes("array") && types.includes("string") ? JSON.parse(value) : value;
|
|
93
91
|
};
|
|
94
92
|
var date = (value) => {
|
|
95
|
-
return new Date(value);
|
|
93
|
+
return typof(value).includes("date") ? new Date(value) : value;
|
|
96
94
|
};
|
|
97
95
|
var _null = (value) => {
|
|
98
96
|
return typof(value).includes("null") ? null : value;
|