@zdepot/utils 1.1.0 → 1.2.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 +207 -206
- package/dist/index.d.ts +1 -1
- package/dist/index.global.js +1 -1
- package/dist/index.js +1 -1
- package/dist/index.mjs +1 -1
- package/package.json +66 -66
package/README.md
CHANGED
|
@@ -1,206 +1,207 @@
|
|
|
1
|
-
# @zdepot/utils
|
|
2
|
-
|
|
3
|
-
Safe type conversion tool library, which provides the method of type judgment and conversion with bottom value, to ensure that your code will not throw exceptions because of type problems.
|
|
4
|
-
|
|
5
|
-
## Features
|
|
6
|
-
|
|
7
|
-
✅ **Type Safety** - Accurate type detection based on `Object.prototype.toString`
|
|
8
|
-
✅ **Fallback Protection** - All conversion methods have default fallback values to prevent runtime errors
|
|
9
|
-
✅ **JSON Parsing** - Smart JSON parsing with automatic type detection
|
|
10
|
-
✅ **Circular References** - Safe JSON serialization with automatic circular reference and function handling
|
|
11
|
-
✅ **TypeScript Support** - Complete TypeScript type definitions
|
|
12
|
-
✅ **Tree-shaking** - Side-effect free, compatible with bundler optimization
|
|
13
|
-
|
|
14
|
-
## Installation
|
|
15
|
-
|
|
16
|
-
```bash
|
|
17
|
-
npm install @zdepot/utils
|
|
18
|
-
```
|
|
19
|
-
|
|
20
|
-
## Usage
|
|
21
|
-
|
|
22
|
-
```ts
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
safe.isSameType(
|
|
38
|
-
safe.isSameType(
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
safe.array(
|
|
48
|
-
safe.array('
|
|
49
|
-
safe.array(
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
safe.string(
|
|
59
|
-
safe.string(123
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
safe.number(
|
|
69
|
-
safe.number('
|
|
70
|
-
safe.number('abc'
|
|
71
|
-
safe.number(
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
safe.jsonParse('
|
|
81
|
-
safe.jsonParse('
|
|
82
|
-
safe.jsonParse(
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
safe.jsonParseObj('
|
|
92
|
-
safe.jsonParseObj('
|
|
93
|
-
safe.jsonParseObj('
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
safe.split('', ',')
|
|
103
|
-
safe.split(
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
safe.boolean(
|
|
113
|
-
safe.boolean(
|
|
114
|
-
safe.boolean(
|
|
115
|
-
safe.boolean(
|
|
116
|
-
safe.boolean(
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
safe.date(
|
|
126
|
-
safe.date(
|
|
127
|
-
safe.date('
|
|
128
|
-
safe.date('invalid'
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
safe.function(
|
|
138
|
-
safe.function(null
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
safe.nonEmptyString('
|
|
148
|
-
safe.nonEmptyString('')
|
|
149
|
-
safe.nonEmptyString(
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
safe.email('
|
|
159
|
-
safe.email('
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
safe.timestamp(
|
|
169
|
-
safe.timestamp(
|
|
170
|
-
safe.timestamp('
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
safe.jsonStringify({
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
obj
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
const
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
const
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
1
|
+
# @zdepot/utils
|
|
2
|
+
|
|
3
|
+
Safe type conversion tool library, which provides the method of type judgment and conversion with bottom value, to ensure that your code will not throw exceptions because of type problems.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
✅ **Type Safety** - Accurate type detection based on `Object.prototype.toString`
|
|
8
|
+
✅ **Fallback Protection** - All conversion methods have default fallback values to prevent runtime errors
|
|
9
|
+
✅ **JSON Parsing** - Smart JSON parsing with automatic type detection
|
|
10
|
+
✅ **Circular References** - Safe JSON serialization with automatic circular reference and function handling
|
|
11
|
+
✅ **TypeScript Support** - Complete TypeScript type definitions
|
|
12
|
+
✅ **Tree-shaking** - Side-effect free, compatible with bundler optimization
|
|
13
|
+
|
|
14
|
+
## Installation
|
|
15
|
+
|
|
16
|
+
```bash
|
|
17
|
+
npm install @zdepot/utils
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
## Usage
|
|
21
|
+
|
|
22
|
+
```ts
|
|
23
|
+
// 默认导入
|
|
24
|
+
import safe from '@zdepot/utils'
|
|
25
|
+
|
|
26
|
+
// 或命名导入
|
|
27
|
+
import { safe } from '@zdepot/utils'
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
## API
|
|
31
|
+
|
|
32
|
+
### isSameType(a, b)
|
|
33
|
+
|
|
34
|
+
Checks if two values are of the same type, using `Object.prototype.toString` for precise detection.
|
|
35
|
+
|
|
36
|
+
```ts
|
|
37
|
+
safe.isSameType([], []) // true
|
|
38
|
+
safe.isSameType({}, []) // false
|
|
39
|
+
safe.isSameType(1, '1') // false
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
### array(value)
|
|
43
|
+
|
|
44
|
+
Ensures an array is returned. Returns the input directly if it's an array; parses JSON strings automatically; otherwise returns an empty array `[]`.
|
|
45
|
+
|
|
46
|
+
```ts
|
|
47
|
+
safe.array([1, 2]) // [1, 2]
|
|
48
|
+
safe.array('[1,2]') // [1, 2]
|
|
49
|
+
safe.array('hello') // []
|
|
50
|
+
safe.array(null) // []
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
### string(value, fallback?)
|
|
54
|
+
|
|
55
|
+
Ensures a string is returned. Returns the input directly if it's a string, otherwise returns the fallback value (default `""`).
|
|
56
|
+
|
|
57
|
+
```ts
|
|
58
|
+
safe.string('hello') // 'hello'
|
|
59
|
+
safe.string(123) // ''
|
|
60
|
+
safe.string(123, 'default') // 'default'
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
### number(value, fallback?)
|
|
64
|
+
|
|
65
|
+
Ensures a number is returned. Uses `Number()` for conversion, returns fallback value (default `0`) if result is NaN or Infinity.
|
|
66
|
+
|
|
67
|
+
```ts
|
|
68
|
+
safe.number(123) // 123
|
|
69
|
+
safe.number('456') // 456
|
|
70
|
+
safe.number('abc') // 0
|
|
71
|
+
safe.number('abc', -1) // -1
|
|
72
|
+
safe.number(Infinity) // 0
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
### jsonParse(value, fallback)
|
|
76
|
+
|
|
77
|
+
`JSON.parse` with fallback value. Returns fallback if parsing fails or result type doesn't match fallback type.
|
|
78
|
+
|
|
79
|
+
```ts
|
|
80
|
+
safe.jsonParse('{"a":1}', {}) // { a: 1 }
|
|
81
|
+
safe.jsonParse('invalid', {}) // {}
|
|
82
|
+
safe.jsonParse('"hello"', []) // [] (type mismatch)
|
|
83
|
+
safe.jsonParse(null, {}) // {}
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
### jsonParseObj(value, fallback)
|
|
87
|
+
|
|
88
|
+
Enhanced `jsonParse` that only processes strings starting with `[` or `{` (objects/arrays), otherwise returns fallback directly. Built-in length limit prevents DoS attacks.
|
|
89
|
+
|
|
90
|
+
```ts
|
|
91
|
+
safe.jsonParseObj('{"a":1}', {}) // { a: 1 }
|
|
92
|
+
safe.jsonParseObj('[1,2]', []) // [1, 2]
|
|
93
|
+
safe.jsonParseObj('"hello"', {}) // {} (doesn't start with [ or {)
|
|
94
|
+
safe.jsonParseObj('invalid', {}) // {}
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
### split(value, splitStr)
|
|
98
|
+
|
|
99
|
+
Safe `String.split`. Empty strings return `[]` instead of `['']`, errors also return `[]`.
|
|
100
|
+
|
|
101
|
+
```ts
|
|
102
|
+
safe.split('a,b,c', ',') // ['a', 'b', 'c']
|
|
103
|
+
safe.split('', ',') // []
|
|
104
|
+
safe.split(null, ',') // []
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
### boolean(value, fallback?)
|
|
108
|
+
|
|
109
|
+
Ensures a boolean is returned. Supports `true/false` strings (case-insensitive) and `0/1` numbers.
|
|
110
|
+
|
|
111
|
+
```ts
|
|
112
|
+
safe.boolean(true) // true
|
|
113
|
+
safe.boolean('TRUE') // true
|
|
114
|
+
safe.boolean(1) // true
|
|
115
|
+
safe.boolean('false') // false
|
|
116
|
+
safe.boolean(0) // false
|
|
117
|
+
safe.boolean('hello') // false (fallback value)
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
### date(value, fallback?)
|
|
121
|
+
|
|
122
|
+
Ensures a Date object is returned. Supports Date objects, timestamps, and date strings.
|
|
123
|
+
|
|
124
|
+
```ts
|
|
125
|
+
safe.date(new Date()) // returns as-is
|
|
126
|
+
safe.date(1700000000000) // new Date(1700000000000)
|
|
127
|
+
safe.date('2023-01-01') // new Date('2023-01-01')
|
|
128
|
+
safe.date('invalid') // new Date() (current time)
|
|
129
|
+
safe.date('invalid', fallback) // fallback
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
### function(value, fallback?)
|
|
133
|
+
|
|
134
|
+
Ensures a function is returned. Returns the input directly if it's a function, otherwise returns the fallback value (default empty function).
|
|
135
|
+
|
|
136
|
+
```ts
|
|
137
|
+
safe.function(fn) // fn
|
|
138
|
+
safe.function(null) // () => undefined
|
|
139
|
+
safe.function(null, () => 'hi') // () => 'hi'
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
### nonEmptyString(value, fallback?)
|
|
143
|
+
|
|
144
|
+
Ensures a non-empty string is returned. Returns the input if it's a non-empty string, otherwise returns the fallback value.
|
|
145
|
+
|
|
146
|
+
```ts
|
|
147
|
+
safe.nonEmptyString('hello') // 'hello'
|
|
148
|
+
safe.nonEmptyString(' ') // '' (whitespace only)
|
|
149
|
+
safe.nonEmptyString('') // ''
|
|
150
|
+
safe.nonEmptyString(123, 'N/A') // 'N/A'
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
### email(value, fallback?)
|
|
154
|
+
|
|
155
|
+
Ensures a valid email string is returned.
|
|
156
|
+
|
|
157
|
+
```ts
|
|
158
|
+
safe.email('user@example.com') // 'user@example.com'
|
|
159
|
+
safe.email('invalid') // ''
|
|
160
|
+
safe.email('bad', 'a@b.com') // 'a@b.com'
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
### timestamp(value, fallback?)
|
|
164
|
+
|
|
165
|
+
Ensures a valid timestamp is returned.
|
|
166
|
+
|
|
167
|
+
```ts
|
|
168
|
+
safe.timestamp(1700000000000) // 1700000000000
|
|
169
|
+
safe.timestamp(new Date()) // date.getTime()
|
|
170
|
+
safe.timestamp('2023-01-01') // 1672531200000
|
|
171
|
+
safe.timestamp('invalid', 0) // 0
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
### jsonStringify(value, indent?)
|
|
175
|
+
|
|
176
|
+
Safe JSON serialization. Automatically handles circular references and functions without throwing.
|
|
177
|
+
|
|
178
|
+
```ts
|
|
179
|
+
safe.jsonStringify({ a: 1 }) // '{"a":1}'
|
|
180
|
+
safe.jsonStringify({ fn: () => {} }) // '{"fn":"[Function]"}'
|
|
181
|
+
|
|
182
|
+
const obj: any = { name: 'test' }
|
|
183
|
+
obj.self = obj
|
|
184
|
+
safe.jsonStringify(obj) // '{"name":"test","self":"[Circular]"}'
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
## Use Cases
|
|
188
|
+
|
|
189
|
+
```ts
|
|
190
|
+
// API response handling
|
|
191
|
+
const data = safe.jsonParse(response.data, defaultValue)
|
|
192
|
+
const items = safe.array(data.items)
|
|
193
|
+
|
|
194
|
+
// Config file parsing
|
|
195
|
+
const config = safe.jsonParseObj(fs.readFileSync('config.json', 'utf-8'), {})
|
|
196
|
+
|
|
197
|
+
// User input processing
|
|
198
|
+
const username = safe.string(input.username, 'guest')
|
|
199
|
+
const age = safe.number(input.age, 0)
|
|
200
|
+
|
|
201
|
+
// String splitting
|
|
202
|
+
const parts = safe.split(input.tags, ',') // avoids split(',a', ',') returning ['', 'a']
|
|
203
|
+
```
|
|
204
|
+
|
|
205
|
+
## License
|
|
206
|
+
|
|
207
|
+
MIT
|
package/dist/index.d.ts
CHANGED
|
@@ -67,7 +67,7 @@ interface SafeUtils {
|
|
|
67
67
|
* @param fallback 兜底值
|
|
68
68
|
* @returns 函数
|
|
69
69
|
*/
|
|
70
|
-
function: <T extends (...args:
|
|
70
|
+
function: <T extends (...args: unknown[]) => unknown>(value: unknown, fallback?: T) => T;
|
|
71
71
|
/**
|
|
72
72
|
* 确保返回非空字符串。如果是非空字符串则返回,否则返回兜底值。
|
|
73
73
|
* @param value 输入值
|
package/dist/index.global.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var safeUtils=(()=>{var u=Object.defineProperty;var T=Object.getOwnPropertyDescriptor;var y=Object.getOwnPropertyNames;var m=Object.prototype.hasOwnProperty;var d=(n,t)=>{for(var r in t)u(n,r,{get:t[r],enumerable:!0})},N=(n,t,r,s)=>{if(t&&typeof t=="object"||typeof t=="function")for(let e of y(t))!m.call(n,e)&&e!==r&&u(n,e,{get:()=>t[e],enumerable:!(s=T(t,e))||s.enumerable});return n};var S=n=>N(u({},"__esModule",{value:!0}),n);var R={};d(R,{default:()=>P,safe:()=>
|
|
1
|
+
"use strict";var safeUtils=(()=>{var u=Object.defineProperty;var T=Object.getOwnPropertyDescriptor;var y=Object.getOwnPropertyNames;var m=Object.prototype.hasOwnProperty;var d=(n,t)=>{for(var r in t)u(n,r,{get:t[r],enumerable:!0})},N=(n,t,r,s)=>{if(t&&typeof t=="object"||typeof t=="function")for(let e of y(t))!m.call(n,e)&&e!==r&&u(n,e,{get:()=>t[e],enumerable:!(s=T(t,e))||s.enumerable});return n};var S=n=>N(u({},"__esModule",{value:!0}),n);var R={};d(R,{default:()=>P,safe:()=>w});var k=/^[^\s@]+@[^\s@]+\.[^\s@]+$/,h=/^[\[{]/,x=()=>{},c=n=>Object.prototype.toString.call(n),o=(n,t)=>c(n)===c(t),f=(n,t)=>{if(n==null||typeof n!="string")return t;try{let r=JSON.parse(n);return o(r,t)?r:t}catch{return t}},D=(n,t)=>{if(typeof n!="string"||n.length>10485760||n.length<2||!h.test(n))return t;try{let r=JSON.parse(n);return o(r,t)?r:t}catch{return t}},O=n=>o(n,[])?n:o(n,"")?f(n,[]):[],i=(n,t="")=>typeof n=="string"?n:t,g=(n,t=0)=>{let r=Number(n);return Number.isFinite(r)?r:Number.isFinite(t)?t:0},E=(n,t)=>typeof n!="string"||n===""?[]:n.split(t),_=(n,t=!1)=>{if(n===!0||n===1)return!0;if(n===!1||n===0)return!1;let r=String(n).toLowerCase();return r==="true"?!0:r==="false"?!1:t},p=(n,t)=>{if(n instanceof Date)return n;if(typeof n=="number"||typeof n=="string"){let r=new Date(n);return isNaN(r.getTime())?t??new Date:r}return t??new Date},F=(n,t=x)=>typeof n=="function"?n:t,J=(n,t="")=>{let r=i(n,t);return r.trim()!==""?r:t},b=(n,t="")=>{let r=i(n,t);return k.test(r)?r:t},j=(n,t=Date.now())=>{let r=Number.isFinite(t)?t:Date.now();return p(n,new Date(r)).getTime()},a=(n,t)=>{let r=new WeakSet;try{return JSON.stringify(n,(s,e)=>{if(typeof e=="function")return"[Function]";if(typeof e=="object"&&e!==null){if(r.has(e))return"[Circular]";r.add(e)}return e},t)}catch{return"{}"}},L=(n,t=1)=>{let r=g(n,NaN);return r>0?r:t},M=(n,t="")=>{let r=i(n,t);try{let s=new URL(r);return s.protocol==="http:"||s.protocol==="https:"?r:t}catch{return t}},w={isSameType:o,array:O,string:i,number:g,jsonParse:f,jsonParseObj:D,split:E,boolean:_,date:p,function:F,nonEmptyString:J,email:b,timestamp:j,jsonStringify:a,positive:L,url:M},P=w;return S(R);})();
|
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var u=Object.defineProperty;var T=Object.getOwnPropertyDescriptor;var y=Object.getOwnPropertyNames;var m=Object.prototype.hasOwnProperty;var d=(n,t)=>{for(var r in t)u(n,r,{get:t[r],enumerable:!0})},N=(n,t,r,s)=>{if(t&&typeof t=="object"||typeof t=="function")for(let e of y(t))!m.call(n,e)&&e!==r&&u(n,e,{get:()=>t[e],enumerable:!(s=T(t,e))||s.enumerable});return n};var S=n=>N(u({},"__esModule",{value:!0}),n);var R={};d(R,{default:()=>P,safe:()=>
|
|
1
|
+
"use strict";var u=Object.defineProperty;var T=Object.getOwnPropertyDescriptor;var y=Object.getOwnPropertyNames;var m=Object.prototype.hasOwnProperty;var d=(n,t)=>{for(var r in t)u(n,r,{get:t[r],enumerable:!0})},N=(n,t,r,s)=>{if(t&&typeof t=="object"||typeof t=="function")for(let e of y(t))!m.call(n,e)&&e!==r&&u(n,e,{get:()=>t[e],enumerable:!(s=T(t,e))||s.enumerable});return n};var S=n=>N(u({},"__esModule",{value:!0}),n);var R={};d(R,{default:()=>P,safe:()=>w});module.exports=S(R);var k=/^[^\s@]+@[^\s@]+\.[^\s@]+$/,h=/^[\[{]/,x=()=>{},c=n=>Object.prototype.toString.call(n),o=(n,t)=>c(n)===c(t),f=(n,t)=>{if(n==null||typeof n!="string")return t;try{let r=JSON.parse(n);return o(r,t)?r:t}catch{return t}},D=(n,t)=>{if(typeof n!="string"||n.length>10485760||n.length<2||!h.test(n))return t;try{let r=JSON.parse(n);return o(r,t)?r:t}catch{return t}},O=n=>o(n,[])?n:o(n,"")?f(n,[]):[],i=(n,t="")=>typeof n=="string"?n:t,g=(n,t=0)=>{let r=Number(n);return Number.isFinite(r)?r:Number.isFinite(t)?t:0},E=(n,t)=>typeof n!="string"||n===""?[]:n.split(t),_=(n,t=!1)=>{if(n===!0||n===1)return!0;if(n===!1||n===0)return!1;let r=String(n).toLowerCase();return r==="true"?!0:r==="false"?!1:t},p=(n,t)=>{if(n instanceof Date)return n;if(typeof n=="number"||typeof n=="string"){let r=new Date(n);return isNaN(r.getTime())?t??new Date:r}return t??new Date},F=(n,t=x)=>typeof n=="function"?n:t,J=(n,t="")=>{let r=i(n,t);return r.trim()!==""?r:t},b=(n,t="")=>{let r=i(n,t);return k.test(r)?r:t},j=(n,t=Date.now())=>{let r=Number.isFinite(t)?t:Date.now();return p(n,new Date(r)).getTime()},a=(n,t)=>{let r=new WeakSet;try{return JSON.stringify(n,(s,e)=>{if(typeof e=="function")return"[Function]";if(typeof e=="object"&&e!==null){if(r.has(e))return"[Circular]";r.add(e)}return e},t)}catch{return"{}"}},L=(n,t=1)=>{let r=g(n,NaN);return r>0?r:t},M=(n,t="")=>{let r=i(n,t);try{let s=new URL(r);return s.protocol==="http:"||s.protocol==="https:"?r:t}catch{return t}},w={isSameType:o,array:O,string:i,number:g,jsonParse:f,jsonParseObj:D,split:E,boolean:_,date:p,function:F,nonEmptyString:J,email:b,timestamp:j,jsonStringify:a,positive:L,url:M},P=w;0&&(module.exports={safe});
|
package/dist/index.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
var
|
|
1
|
+
var p=/^[^\s@]+@[^\s@]+\.[^\s@]+$/,w=/^[\[{]/,T=()=>{},u=n=>Object.prototype.toString.call(n),s=(n,t)=>u(n)===u(t),c=(n,t)=>{if(n==null||typeof n!="string")return t;try{let r=JSON.parse(n);return s(r,t)?r:t}catch{return t}},y=(n,t)=>{if(typeof n!="string"||n.length>10485760||n.length<2||!w.test(n))return t;try{let r=JSON.parse(n);return s(r,t)?r:t}catch{return t}},m=n=>s(n,[])?n:s(n,"")?c(n,[]):[],i=(n,t="")=>typeof n=="string"?n:t,f=(n,t=0)=>{let r=Number(n);return Number.isFinite(r)?r:Number.isFinite(t)?t:0},d=(n,t)=>typeof n!="string"||n===""?[]:n.split(t),N=(n,t=!1)=>{if(n===!0||n===1)return!0;if(n===!1||n===0)return!1;let r=String(n).toLowerCase();return r==="true"?!0:r==="false"?!1:t},g=(n,t)=>{if(n instanceof Date)return n;if(typeof n=="number"||typeof n=="string"){let r=new Date(n);return isNaN(r.getTime())?t??new Date:r}return t??new Date},S=(n,t=T)=>typeof n=="function"?n:t,k=(n,t="")=>{let r=i(n,t);return r.trim()!==""?r:t},h=(n,t="")=>{let r=i(n,t);return p.test(r)?r:t},x=(n,t=Date.now())=>{let r=Number.isFinite(t)?t:Date.now();return g(n,new Date(r)).getTime()},D=(n,t)=>{let r=new WeakSet;try{return JSON.stringify(n,(o,e)=>{if(typeof e=="function")return"[Function]";if(typeof e=="object"&&e!==null){if(r.has(e))return"[Circular]";r.add(e)}return e},t)}catch{return"{}"}},O=(n,t=1)=>{let r=f(n,NaN);return r>0?r:t},E=(n,t="")=>{let r=i(n,t);try{let o=new URL(r);return o.protocol==="http:"||o.protocol==="https:"?r:t}catch{return t}},_={isSameType:s,array:m,string:i,number:f,jsonParse:c,jsonParseObj:y,split:d,boolean:N,date:g,function:S,nonEmptyString:k,email:h,timestamp:x,jsonStringify:D,positive:O,url:E},F=_;export{F as default,_ as safe};
|
package/package.json
CHANGED
|
@@ -1,66 +1,66 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@zdepot/utils",
|
|
3
|
-
"version": "1.1
|
|
4
|
-
"description": "
|
|
5
|
-
"main": "dist/index.js",
|
|
6
|
-
"module": "dist/index.mjs",
|
|
7
|
-
"browser": "dist/index.
|
|
8
|
-
"types": "dist/index.d.ts",
|
|
9
|
-
"exports": {
|
|
10
|
-
".": {
|
|
11
|
-
"
|
|
12
|
-
"
|
|
13
|
-
"
|
|
14
|
-
"
|
|
15
|
-
}
|
|
16
|
-
},
|
|
17
|
-
"files": [
|
|
18
|
-
"dist"
|
|
19
|
-
],
|
|
20
|
-
"sideEffects": false,
|
|
21
|
-
"publishConfig": {
|
|
22
|
-
"access": "public"
|
|
23
|
-
},
|
|
24
|
-
"scripts": {
|
|
25
|
-
"build": "tsup --config tsup.config.ts && node scripts/clean-dmts.js",
|
|
26
|
-
"prepublishOnly": "npm run build",
|
|
27
|
-
"test": "vitest",
|
|
28
|
-
"test:coverage": "vitest run --coverage",
|
|
29
|
-
"test:coverage:report": "vitest run --coverage --reporter=text --reporter=html",
|
|
30
|
-
"lint": "eslint src --ext .ts",
|
|
31
|
-
"lint:fix": "eslint src --ext .ts --fix",
|
|
32
|
-
"format": "prettier --write src/**/*.ts"
|
|
33
|
-
},
|
|
34
|
-
"keywords": [
|
|
35
|
-
"utils",
|
|
36
|
-
"safe",
|
|
37
|
-
"type-check",
|
|
38
|
-
"fallback",
|
|
39
|
-
"json-parse",
|
|
40
|
-
"type-conversion",
|
|
41
|
-
"typescript",
|
|
42
|
-
"ts"
|
|
43
|
-
],
|
|
44
|
-
"author": "
|
|
45
|
-
"license": "MIT",
|
|
46
|
-
"repository": {
|
|
47
|
-
"type": "git",
|
|
48
|
-
"url": "
|
|
49
|
-
"directory": ""
|
|
50
|
-
},
|
|
51
|
-
"devDependencies": {
|
|
52
|
-
"@types/node": "^20.0.0",
|
|
53
|
-
"@typescript-eslint/eslint-plugin": "^6.0.0",
|
|
54
|
-
"@typescript-eslint/parser": "^6.0.0",
|
|
55
|
-
"@vitest/coverage-v8": "^1.0.0",
|
|
56
|
-
"eslint": "^8.0.0",
|
|
57
|
-
"jsdom": "^29.1.1",
|
|
58
|
-
"prettier": "^3.0.0",
|
|
59
|
-
"tsup": "^8.0.0",
|
|
60
|
-
"typescript": "^5.4.0",
|
|
61
|
-
"vitest": "^1.0.0"
|
|
62
|
-
},
|
|
63
|
-
"engines": {
|
|
64
|
-
"node": ">=14.0.0"
|
|
65
|
-
}
|
|
66
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "@zdepot/utils",
|
|
3
|
+
"version": "1.2.1",
|
|
4
|
+
"description": "Safety Type Conversion Utility Library, providing type judgment and conversion methods with fallback values",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"module": "dist/index.mjs",
|
|
7
|
+
"browser": "dist/index.global.js",
|
|
8
|
+
"types": "dist/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"types": "./dist/index.d.ts",
|
|
12
|
+
"import": "./dist/index.mjs",
|
|
13
|
+
"require": "./dist/index.js",
|
|
14
|
+
"default": "./dist/index.mjs"
|
|
15
|
+
}
|
|
16
|
+
},
|
|
17
|
+
"files": [
|
|
18
|
+
"dist"
|
|
19
|
+
],
|
|
20
|
+
"sideEffects": false,
|
|
21
|
+
"publishConfig": {
|
|
22
|
+
"access": "public"
|
|
23
|
+
},
|
|
24
|
+
"scripts": {
|
|
25
|
+
"build": "tsup --config tsup.config.ts && node scripts/clean-dmts.js",
|
|
26
|
+
"prepublishOnly": "npm run build",
|
|
27
|
+
"test": "vitest",
|
|
28
|
+
"test:coverage": "vitest run --coverage",
|
|
29
|
+
"test:coverage:report": "vitest run --coverage --reporter=text --reporter=html",
|
|
30
|
+
"lint": "eslint src --ext .ts",
|
|
31
|
+
"lint:fix": "eslint src --ext .ts --fix",
|
|
32
|
+
"format": "prettier --write src/**/*.ts"
|
|
33
|
+
},
|
|
34
|
+
"keywords": [
|
|
35
|
+
"utils",
|
|
36
|
+
"safe",
|
|
37
|
+
"type-check",
|
|
38
|
+
"fallback",
|
|
39
|
+
"json-parse",
|
|
40
|
+
"type-conversion",
|
|
41
|
+
"typescript",
|
|
42
|
+
"ts"
|
|
43
|
+
],
|
|
44
|
+
"author": "Mr.Z",
|
|
45
|
+
"license": "MIT",
|
|
46
|
+
"repository": {
|
|
47
|
+
"type": "git",
|
|
48
|
+
"url": "",
|
|
49
|
+
"directory": ""
|
|
50
|
+
},
|
|
51
|
+
"devDependencies": {
|
|
52
|
+
"@types/node": "^20.0.0",
|
|
53
|
+
"@typescript-eslint/eslint-plugin": "^6.0.0",
|
|
54
|
+
"@typescript-eslint/parser": "^6.0.0",
|
|
55
|
+
"@vitest/coverage-v8": "^1.0.0",
|
|
56
|
+
"eslint": "^8.0.0",
|
|
57
|
+
"jsdom": "^29.1.1",
|
|
58
|
+
"prettier": "^3.0.0",
|
|
59
|
+
"tsup": "^8.0.0",
|
|
60
|
+
"typescript": "^5.4.0",
|
|
61
|
+
"vitest": "^1.0.0"
|
|
62
|
+
},
|
|
63
|
+
"engines": {
|
|
64
|
+
"node": ">=14.0.0"
|
|
65
|
+
}
|
|
66
|
+
}
|