@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 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
- import safe from '@zdepot/utils'
24
-
25
- // Or import only what you need
26
- import { safe } from '@zdepot/utils'
27
- ```
28
-
29
- ## API
30
-
31
- ### isSameType(a, b)
32
-
33
- Checks if two values are of the same type, using `Object.prototype.toString` for precise detection.
34
-
35
- ```ts
36
- safe.isSameType([], []) // true
37
- safe.isSameType({}, []) // false
38
- safe.isSameType(1, '1') // false
39
- ```
40
-
41
- ### array(value)
42
-
43
- Ensures an array is returned. Returns the input directly if it's an array; parses JSON strings automatically; otherwise returns an empty array `[]`.
44
-
45
- ```ts
46
- safe.array([1, 2]) // [1, 2]
47
- safe.array('[1,2]') // [1, 2]
48
- safe.array('hello') // []
49
- safe.array(null) // []
50
- ```
51
-
52
- ### string(value, fallback?)
53
-
54
- Ensures a string is returned. Returns the input directly if it's a string, otherwise returns the fallback value (default `""`).
55
-
56
- ```ts
57
- safe.string('hello') // 'hello'
58
- safe.string(123) // ''
59
- safe.string(123, 'default') // 'default'
60
- ```
61
-
62
- ### number(value, fallback?)
63
-
64
- Ensures a number is returned. Uses `Number()` for conversion, returns fallback value (default `0`) if result is NaN or Infinity.
65
-
66
- ```ts
67
- safe.number(123) // 123
68
- safe.number('456') // 456
69
- safe.number('abc') // 0
70
- safe.number('abc', -1) // -1
71
- safe.number(Infinity) // 0
72
- ```
73
-
74
- ### jsonParse(value, fallback)
75
-
76
- `JSON.parse` with fallback value. Returns fallback if parsing fails or result type doesn't match fallback type.
77
-
78
- ```ts
79
- safe.jsonParse('{"a":1}', {}) // { a: 1 }
80
- safe.jsonParse('invalid', {}) // {}
81
- safe.jsonParse('"hello"', []) // [] (type mismatch)
82
- safe.jsonParse(null, {}) // {}
83
- ```
84
-
85
- ### jsonParseObj(value, fallback)
86
-
87
- Enhanced `jsonParse` that only processes strings starting with `[` or `{` (objects/arrays), otherwise returns fallback directly. Built-in length limit prevents DoS attacks.
88
-
89
- ```ts
90
- safe.jsonParseObj('{"a":1}', {}) // { a: 1 }
91
- safe.jsonParseObj('[1,2]', []) // [1, 2]
92
- safe.jsonParseObj('"hello"', {}) // {} (doesn't start with [ or {)
93
- safe.jsonParseObj('invalid', {}) // {}
94
- ```
95
-
96
- ### split(value, splitStr)
97
-
98
- Safe `String.split`. Empty strings return `[]` instead of `['']`, errors also return `[]`.
99
-
100
- ```ts
101
- safe.split('a,b,c', ',') // ['a', 'b', 'c']
102
- safe.split('', ',') // []
103
- safe.split(null, ',') // []
104
- ```
105
-
106
- ### boolean(value, fallback?)
107
-
108
- Ensures a boolean is returned. Supports `true/false` strings (case-insensitive) and `0/1` numbers.
109
-
110
- ```ts
111
- safe.boolean(true) // true
112
- safe.boolean('TRUE') // true
113
- safe.boolean(1) // true
114
- safe.boolean('false') // false
115
- safe.boolean(0) // false
116
- safe.boolean('hello') // false (fallback value)
117
- ```
118
-
119
- ### date(value, fallback?)
120
-
121
- Ensures a Date object is returned. Supports Date objects, timestamps, and date strings.
122
-
123
- ```ts
124
- safe.date(new Date()) // returns as-is
125
- safe.date(1700000000000) // new Date(1700000000000)
126
- safe.date('2023-01-01') // new Date('2023-01-01')
127
- safe.date('invalid') // new Date() (current time)
128
- safe.date('invalid', fallback) // fallback
129
- ```
130
-
131
- ### function(value, fallback?)
132
-
133
- Ensures a function is returned. Returns the input directly if it's a function, otherwise returns the fallback value (default empty function).
134
-
135
- ```ts
136
- safe.function(fn) // fn
137
- safe.function(null) // () => undefined
138
- safe.function(null, () => 'hi') // () => 'hi'
139
- ```
140
-
141
- ### nonEmptyString(value, fallback?)
142
-
143
- Ensures a non-empty string is returned. Returns the input if it's a non-empty string, otherwise returns the fallback value.
144
-
145
- ```ts
146
- safe.nonEmptyString('hello') // 'hello'
147
- safe.nonEmptyString(' ') // '' (whitespace only)
148
- safe.nonEmptyString('') // ''
149
- safe.nonEmptyString(123, 'N/A') // 'N/A'
150
- ```
151
-
152
- ### email(value, fallback?)
153
-
154
- Ensures a valid email string is returned.
155
-
156
- ```ts
157
- safe.email('user@example.com') // 'user@example.com'
158
- safe.email('invalid') // ''
159
- safe.email('bad', 'a@b.com') // 'a@b.com'
160
- ```
161
-
162
- ### timestamp(value, fallback?)
163
-
164
- Ensures a valid timestamp is returned.
165
-
166
- ```ts
167
- safe.timestamp(1700000000000) // 1700000000000
168
- safe.timestamp(new Date()) // date.getTime()
169
- safe.timestamp('2023-01-01') // 1672531200000
170
- safe.timestamp('invalid', 0) // 0
171
- ```
172
-
173
- ### jsonStringify(value, indent?)
174
-
175
- Safe JSON serialization. Automatically handles circular references and functions without throwing.
176
-
177
- ```ts
178
- safe.jsonStringify({ a: 1 }) // '{"a":1}'
179
- safe.jsonStringify({ fn: () => {} }) // '{"fn":"[Function]"}'
180
-
181
- const obj: any = { name: 'test' }
182
- obj.self = obj
183
- safe.jsonStringify(obj) // '{"name":"test","self":"[Circular]"}'
184
- ```
185
-
186
- ## Use Cases
187
-
188
- ```ts
189
- // API response handling
190
- const data = safe.jsonParse(response.data, defaultValue)
191
- const items = safe.array(data.items)
192
-
193
- // Config file parsing
194
- const config = safe.jsonParseObj(fs.readFileSync('config.json', 'utf-8'), {})
195
-
196
- // User input processing
197
- const username = safe.string(input.username, 'guest')
198
- const age = safe.number(input.age, 0)
199
-
200
- // String splitting
201
- const parts = safe.split(input.tags, ',') // avoids split(',a', ',') returning ['', 'a']
202
- ```
203
-
204
- ## License
205
-
206
- MIT
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: any[]) => any>(value: unknown, fallback?: T) => T;
70
+ function: <T extends (...args: unknown[]) => unknown>(value: unknown, fallback?: T) => T;
71
71
  /**
72
72
  * 确保返回非空字符串。如果是非空字符串则返回,否则返回兜底值。
73
73
  * @param value 输入值
@@ -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:()=>p});var k=/^[^\s@]+@[^\s@]+\.[^\s@]+$/,a=/^[\[{]/,h=()=>{},c=n=>Object.prototype.toString.call(n),o=(n,t)=>c(n)===c(t),g=(n,t)=>{if(n==null)return t;try{let r=JSON.parse(n);return o(r,t)?r:t}catch{return t}},x=(n,t)=>{if(typeof n!="string"||n.length>10485760||n.length<2)return t;try{if(!a.test(n))throw new Error;let r=JSON.parse(n);return o(r,t)?r:t}catch{return t}},E=n=>o(n,[])?n:o(n,"")?g(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},O=(n,t)=>typeof n!="string"||n===""?[]:n.split(t),D=(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},w=(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},_=(n,t=h)=>typeof n=="function"?n:t,b=(n,t="")=>{let r=i(n,t);return r.trim()!==""?r:t},J=(n,t="")=>{let r=i(n,t);return k.test(r)?r:t},j=(n,t=Date.now())=>w(n,new Date(t)).getTime(),F=(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=f(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}},p={isSameType:o,array:E,string:i,number:f,jsonParse:g,jsonParseObj:x,split:O,boolean:D,date:w,function:_,nonEmptyString:b,email:J,timestamp:j,jsonStringify:F,positive:L,url:M},P=p;return S(R);})();
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:()=>p});module.exports=S(R);var k=/^[^\s@]+@[^\s@]+\.[^\s@]+$/,a=/^[\[{]/,h=()=>{},c=n=>Object.prototype.toString.call(n),o=(n,t)=>c(n)===c(t),g=(n,t)=>{if(n==null)return t;try{let r=JSON.parse(n);return o(r,t)?r:t}catch{return t}},x=(n,t)=>{if(typeof n!="string"||n.length>10485760||n.length<2)return t;try{if(!a.test(n))throw new Error;let r=JSON.parse(n);return o(r,t)?r:t}catch{return t}},E=n=>o(n,[])?n:o(n,"")?g(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},O=(n,t)=>typeof n!="string"||n===""?[]:n.split(t),D=(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},w=(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},_=(n,t=h)=>typeof n=="function"?n:t,b=(n,t="")=>{let r=i(n,t);return r.trim()!==""?r:t},J=(n,t="")=>{let r=i(n,t);return k.test(r)?r:t},j=(n,t=Date.now())=>w(n,new Date(t)).getTime(),F=(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=f(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}},p={isSameType:o,array:E,string:i,number:f,jsonParse:g,jsonParseObj:x,split:O,boolean:D,date:w,function:_,nonEmptyString:b,email:J,timestamp:j,jsonStringify:F,positive:L,url:M},P=p;0&&(module.exports={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 w=/^[^\s@]+@[^\s@]+\.[^\s@]+$/,p=/^[\[{]/,T=()=>{},u=n=>Object.prototype.toString.call(n),s=(n,t)=>u(n)===u(t),c=(n,t)=>{if(n==null)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)return t;try{if(!p.test(n))throw new Error;let r=JSON.parse(n);return s(r,t)?r:t}catch{return t}},m=n=>s(n,[])?n:s(n,"")?c(n,[]):[],o=(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},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},f=(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=o(n,t);return r.trim()!==""?r:t},a=(n,t="")=>{let r=o(n,t);return w.test(r)?r:t},h=(n,t=Date.now())=>f(n,new Date(t)).getTime(),x=(n,t)=>{let r=new WeakSet;try{return JSON.stringify(n,(i,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"{}"}},E=(n,t=1)=>{let r=g(n,NaN);return r>0?r:t},O=(n,t="")=>{let r=o(n,t);try{let i=new URL(r);return i.protocol==="http:"||i.protocol==="https:"?r:t}catch{return t}},D={isSameType:s,array:m,string:o,number:g,jsonParse:c,jsonParseObj:y,split:d,boolean:N,date:f,function:S,nonEmptyString:k,email:a,timestamp:h,jsonStringify:x,positive:E,url:O},_=D;export{_ as default,D as safe};
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.0",
4
- "description": "安全类型转换工具库,提供带兜底值的类型判断与转换方法",
5
- "main": "dist/index.js",
6
- "module": "dist/index.mjs",
7
- "browser": "dist/index.iife.js",
8
- "types": "dist/index.d.ts",
9
- "exports": {
10
- ".": {
11
- "browser": "./dist/index.iife.js",
12
- "types": "./dist/index.d.ts",
13
- "import": "./dist/index.mjs",
14
- "require": "./dist/index.js"
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": "dalongzhu",
45
- "license": "MIT",
46
- "repository": {
47
- "type": "git",
48
- "url": "https://gitee.com/DLwebweb/zdepot-utils",
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
+ }