typof 1.0.5 → 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 +12 -8
- package/dist/main.js +9 -11
- package/dist/main.mjs +9 -11
- 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
|
@@ -97,7 +97,8 @@ var typof = (value) => {
|
|
|
97
97
|
return types;
|
|
98
98
|
};
|
|
99
99
|
var string = (value) => {
|
|
100
|
-
|
|
100
|
+
const types = typof(value);
|
|
101
|
+
return types.includes("object") || types.includes("array") ? JSON.stringify(value) : String(value);
|
|
101
102
|
};
|
|
102
103
|
var number = (value) => {
|
|
103
104
|
return typof(value).includes("number") ? Number(value) : NaN;
|
|
@@ -106,7 +107,8 @@ var integer = (value) => {
|
|
|
106
107
|
return typof(value).includes("number") ? Math.trunc(Number(value)) : NaN;
|
|
107
108
|
};
|
|
108
109
|
var boolean = (value) => {
|
|
109
|
-
|
|
110
|
+
const types = typof(value);
|
|
111
|
+
if (types.includes("boolean") && types.includes("string")) {
|
|
110
112
|
if (value === "true") {
|
|
111
113
|
return true;
|
|
112
114
|
} else if (value === "false") {
|
|
@@ -115,19 +117,15 @@ var boolean = (value) => {
|
|
|
115
117
|
} else return value;
|
|
116
118
|
};
|
|
117
119
|
var object = (value) => {
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
} else return value;
|
|
120
|
+
const types = typof(value);
|
|
121
|
+
return types.includes("object") && types.includes("string") ? JSON.parse(value) : value;
|
|
121
122
|
};
|
|
122
123
|
var array = (value) => {
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
} else return value;
|
|
124
|
+
const types = typof(value);
|
|
125
|
+
return types.includes("array") && types.includes("string") ? JSON.parse(value) : value;
|
|
126
126
|
};
|
|
127
127
|
var date = (value) => {
|
|
128
|
-
|
|
129
|
-
return new Date(value);
|
|
130
|
-
} else return 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
|
@@ -62,7 +62,8 @@ var typof = (value) => {
|
|
|
62
62
|
return types;
|
|
63
63
|
};
|
|
64
64
|
var string = (value) => {
|
|
65
|
-
|
|
65
|
+
const types = typof(value);
|
|
66
|
+
return types.includes("object") || types.includes("array") ? JSON.stringify(value) : String(value);
|
|
66
67
|
};
|
|
67
68
|
var number = (value) => {
|
|
68
69
|
return typof(value).includes("number") ? Number(value) : NaN;
|
|
@@ -71,7 +72,8 @@ var integer = (value) => {
|
|
|
71
72
|
return typof(value).includes("number") ? Math.trunc(Number(value)) : NaN;
|
|
72
73
|
};
|
|
73
74
|
var boolean = (value) => {
|
|
74
|
-
|
|
75
|
+
const types = typof(value);
|
|
76
|
+
if (types.includes("boolean") && types.includes("string")) {
|
|
75
77
|
if (value === "true") {
|
|
76
78
|
return true;
|
|
77
79
|
} else if (value === "false") {
|
|
@@ -80,19 +82,15 @@ var boolean = (value) => {
|
|
|
80
82
|
} else return value;
|
|
81
83
|
};
|
|
82
84
|
var object = (value) => {
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
} else return value;
|
|
85
|
+
const types = typof(value);
|
|
86
|
+
return types.includes("object") && types.includes("string") ? JSON.parse(value) : value;
|
|
86
87
|
};
|
|
87
88
|
var array = (value) => {
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
} else return value;
|
|
89
|
+
const types = typof(value);
|
|
90
|
+
return types.includes("array") && types.includes("string") ? JSON.parse(value) : value;
|
|
91
91
|
};
|
|
92
92
|
var date = (value) => {
|
|
93
|
-
|
|
94
|
-
return new Date(value);
|
|
95
|
-
} else return 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;
|