typof 1.0.0

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/LICENSE.md ADDED
@@ -0,0 +1,9 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Keift
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6
+
7
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8
+
9
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,173 @@
1
+ [String]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String
2
+ [Number]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number
3
+ [Boolean]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean
4
+ [Date]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date
5
+ [Object]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object
6
+ [Array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array
7
+ [Buffer]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer
8
+ [Function]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function
9
+ [Promise]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise
10
+ [Void]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Undefined
11
+ [ReadOptionsDefault]: https://github.com/fabiospampinato/atomically/blob/master/src/constants.ts#L15
12
+ [WriteOptionsDefault]: https://github.com/fabiospampinato/atomically/blob/master/src/constants.ts#L17
13
+ [Types]: ./src/types/Types.type.ts
14
+
15
+ <div align="center">
16
+ <br/>
17
+ <img src="./assets/logo.png" width="350px"/>
18
+ <br/>
19
+ <br/>
20
+ <img src="https://img.shields.io/npm/v/typof?label=version&color=%23633BFF"/>
21
+ <img src="https://img.shields.io/npm/l/typof?label=license&color=%23633BFF"/>
22
+ <img src="https://img.shields.io/npm/dt/typof?label=downloads&color=%2300927F"/>
23
+ <img src="https://img.shields.io/npm/unpacked-size/typof?label=size&color=%2300927F"/>
24
+ </div>
25
+
26
+ ## Contents
27
+
28
+ - [About](#about)
29
+ - [Features](#features)
30
+ - [Installation](#installation)
31
+ - [Documentation](#documentation)
32
+ - [Tree](#tree)
33
+ - [Import](#import)
34
+ - [Methods](#methods)
35
+ - [Types](#types)
36
+ - [Links](#links)
37
+ - [Discord](https://discord.gg/keift)
38
+ - [Telegram](https://t.me/keiftt)
39
+ - [Twitter](https://x.com/keiftttt)
40
+ - [GitHub](https://github.com/keift)
41
+ - [License](#license)
42
+
43
+ ## About
44
+
45
+ Extract the valid types.
46
+
47
+ ## Features
48
+
49
+ - Extract multiple types
50
+ - Extracts integer and float types
51
+ - It infers the type from a string
52
+ - There are simple type converters
53
+
54
+ ## Installation
55
+
56
+ You can install it as follows.
57
+
58
+ ```shell
59
+ // NPM
60
+ npm add typof
61
+
62
+ // PNPM
63
+ pnpm add typof
64
+
65
+ // Yarn
66
+ yarn add typof
67
+
68
+ // Bun
69
+ bun add typof
70
+
71
+ // Deno
72
+ deno add typof
73
+ ```
74
+
75
+ ## Documentation
76
+
77
+ ### Tree
78
+
79
+ Briefly as follows.
80
+
81
+ ```typescript
82
+ typof
83
+
84
+ ├── typof(value)
85
+ ├── string(value)
86
+ ├── number(value)
87
+ ├── integer(value)
88
+ ├── float(value)
89
+ ├── boolean(value)
90
+ ├── object(value)
91
+ ├── array(value)
92
+ ├── date(value)
93
+
94
+ └── type Types
95
+ ```
96
+
97
+ ### Import
98
+
99
+ Briefly as follows.
100
+
101
+ ```typescript
102
+ import { typof } from 'typof';
103
+ ```
104
+
105
+ ### Methods
106
+
107
+ `typof(value)`
108
+
109
+ Extract the valid types.
110
+
111
+ > | Parameter | Default | Description |
112
+ > | --------- | ------- | ----------------------------------- |
113
+ > | value | | Unknown<br/>Value to extract types. |
114
+ >
115
+ > returns [Types]\[]
116
+ >
117
+ > Example:
118
+ >
119
+ > ```typescript
120
+ > typof('test'); // [ 'string' ]
121
+ >
122
+ > typof('0'); // [ 'string', 'number', 'integer' ]
123
+ > typof(0); // [ 'number', 'integer' ]
124
+ >
125
+ > typof('0.5'); // [ 'string', 'number', 'float' ]
126
+ > typof(0.5); // [ 'number', 'float' ]
127
+ >
128
+ > typof('true'); // [ 'string', 'boolean' ]
129
+ > typof(true); // [ 'boolean' ]
130
+ >
131
+ > typof('{"key": "value"}'); // [ 'string', 'object' ]
132
+ > typof({ key: 'value' }); // [ 'object' ]
133
+ >
134
+ > typof('["test"]'); // [ 'string', 'array' ]
135
+ > typof(['test']); // [ 'array' ]
136
+ >
137
+ > typof('2025-01-01'); // [ 'string', 'date' ]
138
+ > typof(new Date('2025-01-01')); // [ 'object', 'date' ]
139
+ >
140
+ > typof('null'); // [ 'string, 'null' ]
141
+ > typof(null); // [ 'null' ]
142
+ >
143
+ > typof('undefined'); // [ 'string, 'undefined' ]
144
+ > typof(undefined); // [ 'undefined' ]
145
+ >
146
+ > // Tests are as follows. (Is this an integer?)
147
+ > if (typof(0).includes('integer')) console.log('This is an integer!');
148
+ > ```
149
+
150
+ ### Types
151
+
152
+ | Type |
153
+ | ------- |
154
+ | [Types] |
155
+
156
+ ## Links
157
+
158
+ - [Discord](https://discord.gg/keift)
159
+ - [Telegram](https://t.me/keiftt)
160
+ - [Twitter](https://x.com/keiftttt)
161
+ - [GitHub](https://github.com/keift)
162
+
163
+ ## License
164
+
165
+ MIT License
166
+
167
+ Copyright (c) 2025 Keift
168
+
169
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
170
+
171
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
172
+
173
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,13 @@
1
+ type Types = 'string' | 'number' | 'integer' | 'float' | 'boolean' | 'object' | 'array' | 'date' | 'null' | 'undefined';
2
+
3
+ declare const typof: (value: unknown) => Types[];
4
+ declare const string: (value: unknown) => string;
5
+ declare const number: (value: unknown) => number;
6
+ declare const integer: (value: unknown) => number;
7
+ declare const float: (value: unknown) => number;
8
+ declare const boolean: <Value>(value: Value) => boolean | Value;
9
+ declare const object: <Value>(value: Value) => object | Value;
10
+ declare const array: <Value>(value: Value) => unknown[] | Value;
11
+ declare const date: (value: unknown) => Date;
12
+
13
+ export { type Types, array, boolean, date, float, integer, number, object, string, typof };
package/dist/main.d.ts ADDED
@@ -0,0 +1,13 @@
1
+ type Types = 'string' | 'number' | 'integer' | 'float' | 'boolean' | 'object' | 'array' | 'date' | 'null' | 'undefined';
2
+
3
+ declare const typof: (value: unknown) => Types[];
4
+ declare const string: (value: unknown) => string;
5
+ declare const number: (value: unknown) => number;
6
+ declare const integer: (value: unknown) => number;
7
+ declare const float: (value: unknown) => number;
8
+ declare const boolean: <Value>(value: Value) => boolean | Value;
9
+ declare const object: <Value>(value: Value) => object | Value;
10
+ declare const array: <Value>(value: Value) => unknown[] | Value;
11
+ declare const date: (value: unknown) => Date;
12
+
13
+ export { type Types, array, boolean, date, float, integer, number, object, string, typof };
package/dist/main.js ADDED
@@ -0,0 +1,142 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/main.ts
21
+ var main_exports = {};
22
+ __export(main_exports, {
23
+ array: () => array,
24
+ boolean: () => boolean,
25
+ date: () => date,
26
+ float: () => float,
27
+ integer: () => integer,
28
+ number: () => number,
29
+ object: () => object,
30
+ string: () => string,
31
+ typof: () => typof
32
+ });
33
+ module.exports = __toCommonJS(main_exports);
34
+
35
+ // src/utils/isObject.util.ts
36
+ var isObject = (value) => {
37
+ if (typeof value === "object" && value !== null) return true;
38
+ if (typeof value === "string") {
39
+ try {
40
+ const parsed = JSON.parse(value);
41
+ return typeof parsed === "object" && parsed !== null;
42
+ } catch {
43
+ return false;
44
+ }
45
+ }
46
+ return false;
47
+ };
48
+
49
+ // src/utils/isISODate.util.ts
50
+ var isISODate = (value) => {
51
+ if (!/^\d{4}-\d{2}-\d{2}.*$/.test(value)) return false;
52
+ return !Number.isNaN(new Date(value).getTime());
53
+ };
54
+
55
+ // src/utils/Typof.util.ts
56
+ var typof = (value) => {
57
+ const types = [];
58
+ if (typeof value === "string") {
59
+ value = value.trim();
60
+ if (typeof value !== "string") return types;
61
+ types.push("string");
62
+ if (Number.isFinite(Number(value))) {
63
+ const number2 = Number(value);
64
+ types.push("number");
65
+ if (Number.isInteger(number2)) {
66
+ types.push("integer");
67
+ } else if (!Number.isInteger(number2)) {
68
+ types.push("float");
69
+ }
70
+ }
71
+ if (value === "true" || value === "false") types.push("boolean");
72
+ if (isObject(value) && !Array.isArray(JSON.parse(value))) types.push("object");
73
+ if (isObject(value) && Array.isArray(JSON.parse(value))) types.push("array");
74
+ if (isISODate(value)) types.push("date");
75
+ if (value === "null") types.push("null");
76
+ if (value === "undefined") types.push("undefined");
77
+ } else if (typeof value === "number") {
78
+ if (!Number.isFinite(value)) return types;
79
+ types.push("number");
80
+ if (Number.isInteger(value)) {
81
+ types.push("integer");
82
+ } else if (!Number.isInteger(value)) {
83
+ types.push("float");
84
+ }
85
+ } else if (typeof value === "boolean") {
86
+ types.push("boolean");
87
+ } else if (isObject(value) && !Array.isArray(value)) {
88
+ types.push("object");
89
+ if (value instanceof Date) types.push("date");
90
+ } else if (isObject(value) && Array.isArray(value)) {
91
+ types.push("array");
92
+ } else if (value === null) {
93
+ types.push("null");
94
+ } else if (value === void 0) {
95
+ types.push("undefined");
96
+ }
97
+ return types;
98
+ };
99
+ var string = (value) => {
100
+ return typof(value).includes("object") || typof(value).includes("array") ? JSON.stringify(value) : String(value);
101
+ };
102
+ var number = (value) => {
103
+ return typof(value).includes("number") ? Number(value) : NaN;
104
+ };
105
+ var integer = (value) => {
106
+ return typof(value).includes("number") ? Number.parseInt(string(value)) : NaN;
107
+ };
108
+ var float = number;
109
+ var boolean = (value) => {
110
+ if (typof(value).includes("boolean") && typof(value).includes("string")) {
111
+ if (value === "true") {
112
+ return true;
113
+ } else if (value === "false") {
114
+ return false;
115
+ } else return value;
116
+ } else return value;
117
+ };
118
+ var object = (value) => {
119
+ if (typof(value).includes("object") && typof(value).includes("string")) {
120
+ return JSON.parse(value);
121
+ } else return value;
122
+ };
123
+ var array = (value) => {
124
+ if (typof(value).includes("array") && typof(value).includes("string")) {
125
+ return JSON.parse(value);
126
+ } else return value;
127
+ };
128
+ var date = (value) => {
129
+ return new Date(value);
130
+ };
131
+ // Annotate the CommonJS export names for ESM import in node:
132
+ 0 && (module.exports = {
133
+ array,
134
+ boolean,
135
+ date,
136
+ float,
137
+ integer,
138
+ number,
139
+ object,
140
+ string,
141
+ typof
142
+ });
package/dist/main.mjs ADDED
@@ -0,0 +1,107 @@
1
+ // src/utils/isObject.util.ts
2
+ var isObject = (value) => {
3
+ if (typeof value === "object" && value !== null) return true;
4
+ if (typeof value === "string") {
5
+ try {
6
+ const parsed = JSON.parse(value);
7
+ return typeof parsed === "object" && parsed !== null;
8
+ } catch {
9
+ return false;
10
+ }
11
+ }
12
+ return false;
13
+ };
14
+
15
+ // src/utils/isISODate.util.ts
16
+ var isISODate = (value) => {
17
+ if (!/^\d{4}-\d{2}-\d{2}.*$/.test(value)) return false;
18
+ return !Number.isNaN(new Date(value).getTime());
19
+ };
20
+
21
+ // src/utils/Typof.util.ts
22
+ var typof = (value) => {
23
+ const types = [];
24
+ if (typeof value === "string") {
25
+ value = value.trim();
26
+ if (typeof value !== "string") return types;
27
+ types.push("string");
28
+ if (Number.isFinite(Number(value))) {
29
+ const number2 = Number(value);
30
+ types.push("number");
31
+ if (Number.isInteger(number2)) {
32
+ types.push("integer");
33
+ } else if (!Number.isInteger(number2)) {
34
+ types.push("float");
35
+ }
36
+ }
37
+ if (value === "true" || value === "false") types.push("boolean");
38
+ if (isObject(value) && !Array.isArray(JSON.parse(value))) types.push("object");
39
+ if (isObject(value) && Array.isArray(JSON.parse(value))) types.push("array");
40
+ if (isISODate(value)) types.push("date");
41
+ if (value === "null") types.push("null");
42
+ if (value === "undefined") types.push("undefined");
43
+ } else if (typeof value === "number") {
44
+ if (!Number.isFinite(value)) return types;
45
+ types.push("number");
46
+ if (Number.isInteger(value)) {
47
+ types.push("integer");
48
+ } else if (!Number.isInteger(value)) {
49
+ types.push("float");
50
+ }
51
+ } else if (typeof value === "boolean") {
52
+ types.push("boolean");
53
+ } else if (isObject(value) && !Array.isArray(value)) {
54
+ types.push("object");
55
+ if (value instanceof Date) types.push("date");
56
+ } else if (isObject(value) && Array.isArray(value)) {
57
+ types.push("array");
58
+ } else if (value === null) {
59
+ types.push("null");
60
+ } else if (value === void 0) {
61
+ types.push("undefined");
62
+ }
63
+ return types;
64
+ };
65
+ var string = (value) => {
66
+ return typof(value).includes("object") || typof(value).includes("array") ? JSON.stringify(value) : String(value);
67
+ };
68
+ var number = (value) => {
69
+ return typof(value).includes("number") ? Number(value) : NaN;
70
+ };
71
+ var integer = (value) => {
72
+ return typof(value).includes("number") ? Number.parseInt(string(value)) : NaN;
73
+ };
74
+ var float = number;
75
+ var boolean = (value) => {
76
+ if (typof(value).includes("boolean") && typof(value).includes("string")) {
77
+ if (value === "true") {
78
+ return true;
79
+ } else if (value === "false") {
80
+ return false;
81
+ } else return value;
82
+ } else return value;
83
+ };
84
+ var object = (value) => {
85
+ if (typof(value).includes("object") && typof(value).includes("string")) {
86
+ return JSON.parse(value);
87
+ } else return value;
88
+ };
89
+ var array = (value) => {
90
+ if (typof(value).includes("array") && typof(value).includes("string")) {
91
+ return JSON.parse(value);
92
+ } else return value;
93
+ };
94
+ var date = (value) => {
95
+ return new Date(value);
96
+ };
97
+ export {
98
+ array,
99
+ boolean,
100
+ date,
101
+ float,
102
+ integer,
103
+ number,
104
+ object,
105
+ string,
106
+ typof
107
+ };
package/package.json ADDED
@@ -0,0 +1,35 @@
1
+ {
2
+ "name": "typof",
3
+ "version": "1.0.0",
4
+ "description": "Extract the valid types.",
5
+ "license": "MIT",
6
+ "homepage": "https://github.com/keift/typof",
7
+ "bugs": "https://github.com/keift/typof/issues",
8
+ "main": "./dist/main.js",
9
+ "module": "./dist/main.mjs",
10
+ "types": "./dist/main.d.ts",
11
+ "scripts": {
12
+ "tests": "bun run lint && bun test",
13
+ "build": "tsup",
14
+ "prettier": "prettier --write ./",
15
+ "lint": "eslint ./"
16
+ },
17
+ "dependencies": {
18
+ "@types/node": "latest"
19
+ },
20
+ "devDependencies": {
21
+ "neatlint": "latest",
22
+ "prettier": "latest",
23
+ "tsup": "latest"
24
+ },
25
+ "repository": {
26
+ "type": "git",
27
+ "url": "https://github.com/keift/typof"
28
+ },
29
+ "keywords": [
30
+ "type",
31
+ "typeof",
32
+ "integer",
33
+ "float"
34
+ ]
35
+ }