@jhqn/utils-core 0.0.1-beta.10 → 0.0.1-beta.12

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/dist/index.cjs CHANGED
@@ -1,5 +1,7 @@
1
1
  'use strict';
2
2
 
3
+ const detectBrowser = require('detect-browser');
4
+
3
5
  const snake2Kebab = (str) => str.replace(/_([a-z])/g, (_, letter) => `-${letter.toLowerCase()}`);
4
6
  const snake2Pascal = (str) => str.replace(/_([a-z])/g, (_, letter) => letter.toUpperCase()).replace(/^[a-z]/, (letter) => letter.toUpperCase());
5
7
  const snake2Camel = (str) => str.replace(/(_([a-z]))/g, (match) => match[1].toUpperCase());
@@ -83,6 +85,9 @@ const isTel = (val) => {
83
85
  return false;
84
86
  return cnTelRE.test(val.toString());
85
87
  };
88
+ const isValidRes = (res) => res.code === 200 && res.data;
89
+ const isValidArrRes = (res) => res.code === 200 && Array.isArray(res.data);
90
+ const isValidPageRes = (res) => res.code === 200 && res.data && Array.isArray(res.data.list || res.data.records);
86
91
 
87
92
  const hidePhone = (str) => {
88
93
  if (str) {
@@ -138,6 +143,60 @@ const hideThird = (str) => {
138
143
  return str.slice(0, partLen) + "*".repeat(len - 2 * partLen) + str.slice(len - partLen);
139
144
  };
140
145
 
146
+ const waitTime = (time = 100) => {
147
+ return new Promise((resolve) => {
148
+ setTimeout(() => {
149
+ resolve(true);
150
+ }, time);
151
+ });
152
+ };
153
+ const fixiOSInputAutoZoomIn = () => {
154
+ if (CSS.supports("(font:-apple-system-body) and (-webkit-touch-callout:none) and (-webkit-tap-highlight-color:hotpink)")) {
155
+ const viewport = document.querySelector('meta[name="viewport"]');
156
+ if (viewport !== null) {
157
+ viewport.content = `${viewport.content}, user-scalable=no`;
158
+ }
159
+ }
160
+ };
161
+ const { userAgent } = window.navigator;
162
+ const getHostEnv = () => {
163
+ const ua = userAgent.toLowerCase();
164
+ const baseInfo = detectBrowser.detect();
165
+ const zlb = ua.includes("@zlb");
166
+ const zyd = ua.includes("saas");
167
+ const zzd = ua.includes("zhejiang") || ua.includes("tauruszjd");
168
+ const wx = ua.includes("micromessenger");
169
+ const zfb = ua.includes("alipay");
170
+ const mini = ua.includes("miniprogram");
171
+ return {
172
+ ...baseInfo,
173
+ /**
174
+ * 是否是浙里办
175
+ */
176
+ zlb,
177
+ /**
178
+ * 是否是专有钉
179
+ */
180
+ zyd,
181
+ /**
182
+ * 是否是浙政钉
183
+ */
184
+ zzd,
185
+ /**
186
+ * 是否是微信
187
+ */
188
+ wx,
189
+ /**
190
+ * 是否是支付宝
191
+ */
192
+ zfb,
193
+ /**
194
+ * 是否是小程序
195
+ */
196
+ mini
197
+ };
198
+ };
199
+
141
200
  const parseToJSON = (str, reviver2) => isJSONStr(str) ? JSON.parse(str, reviver2) : null;
142
201
  const replacer = (_, value) => {
143
202
  if (value instanceof Map) {
@@ -164,6 +223,8 @@ exports.camel2Snake = camel2Snake;
164
223
  exports.camel2Title = camel2Title;
165
224
  exports.checkImg = checkImg;
166
225
  exports.filterObj = filterObj;
226
+ exports.fixiOSInputAutoZoomIn = fixiOSInputAutoZoomIn;
227
+ exports.getHostEnv = getHostEnv;
167
228
  exports.hideCardNo = hideCardNo;
168
229
  exports.hideMiddle = hideMiddle;
169
230
  exports.hideName = hideName;
@@ -175,7 +236,10 @@ exports.isJSONStr = isJSONStr;
175
236
  exports.isNumeric = isNumeric;
176
237
  exports.isPhone = isPhone;
177
238
  exports.isTel = isTel;
239
+ exports.isValidArrRes = isValidArrRes;
178
240
  exports.isValidKey = isValidKey;
241
+ exports.isValidPageRes = isValidPageRes;
242
+ exports.isValidRes = isValidRes;
179
243
  exports.jClear = jClear;
180
244
  exports.jDebug = jDebug;
181
245
  exports.jError = jError;
@@ -206,3 +270,5 @@ exports.title2Camel = title2Camel;
206
270
  exports.title2Kebab = title2Kebab;
207
271
  exports.title2Pascal = title2Pascal;
208
272
  exports.title2Snake = title2Snake;
273
+ exports.userAgent = userAgent;
274
+ exports.waitTime = waitTime;
package/dist/index.d.ts CHANGED
@@ -1,3 +1,5 @@
1
+ import * as detect_browser from 'detect-browser';
2
+
1
3
  /**
2
4
  * snake2Kebab
3
5
  * @param {string} str 字符串
@@ -212,6 +214,14 @@ declare type Undefinable<T> = T | undefined;
212
214
  * 可 Null 或 Undefined
213
215
  */
214
216
  declare type NullOrUndefinable<T> = Nullable<Undefinable<T>>;
217
+ /**
218
+ * 请求结果
219
+ */
220
+ declare type Res<T = any> = Service.Response<T>;
221
+ /**
222
+ * 分页请求结果
223
+ */
224
+ declare type PageRes<T = any> = Service.PageResponse<T>;
215
225
 
216
226
  /**
217
227
  * 隐藏手机号
@@ -239,6 +249,195 @@ declare const hideMiddle: (str: NullOrUndefinable<string>) => string;
239
249
  */
240
250
  declare const hideThird: (str: NullOrUndefinable<string>) => string;
241
251
 
252
+ /**
253
+ * 等待一段时间
254
+ * @param time
255
+ */
256
+ declare const waitTime: (time?: number) => Promise<unknown>;
257
+ /**
258
+ * 防止 iOS 因输入框字体小于16px 而在 focus 时页面缩放变大需要手动回正的情况。
259
+ * iOS 10+ 允许 pinch-zoom, 即使 viewport 是不可伸缩的。
260
+ * 只对这些浏览器添加额外规则
261
+ */
262
+ declare const fixiOSInputAutoZoomIn: () => void;
263
+ declare const userAgent: string;
264
+ /**
265
+ * 获取宿主环境信息
266
+ */
267
+ declare const getHostEnv: () => {
268
+ /**
269
+ * 是否是浙里办
270
+ */
271
+ zlb: boolean;
272
+ /**
273
+ * 是否是专有钉
274
+ */
275
+ zyd: boolean;
276
+ /**
277
+ * 是否是浙政钉
278
+ */
279
+ zzd: boolean;
280
+ /**
281
+ * 是否是微信
282
+ */
283
+ wx: boolean;
284
+ /**
285
+ * 是否是支付宝
286
+ */
287
+ zfb: boolean;
288
+ /**
289
+ * 是否是小程序
290
+ */
291
+ mini: boolean;
292
+ } | {
293
+ /**
294
+ * 是否是浙里办
295
+ */
296
+ zlb: boolean;
297
+ /**
298
+ * 是否是专有钉
299
+ */
300
+ zyd: boolean;
301
+ /**
302
+ * 是否是浙政钉
303
+ */
304
+ zzd: boolean;
305
+ /**
306
+ * 是否是微信
307
+ */
308
+ wx: boolean;
309
+ /**
310
+ * 是否是支付宝
311
+ */
312
+ zfb: boolean;
313
+ /**
314
+ * 是否是小程序
315
+ */
316
+ mini: boolean;
317
+ name: detect_browser.Browser;
318
+ version: string;
319
+ os: detect_browser.OperatingSystem | null;
320
+ type: "browser";
321
+ } | {
322
+ /**
323
+ * 是否是浙里办
324
+ */
325
+ zlb: boolean;
326
+ /**
327
+ * 是否是专有钉
328
+ */
329
+ zyd: boolean;
330
+ /**
331
+ * 是否是浙政钉
332
+ */
333
+ zzd: boolean;
334
+ /**
335
+ * 是否是微信
336
+ */
337
+ wx: boolean;
338
+ /**
339
+ * 是否是支付宝
340
+ */
341
+ zfb: boolean;
342
+ /**
343
+ * 是否是小程序
344
+ */
345
+ mini: boolean;
346
+ name: detect_browser.Browser;
347
+ version: string;
348
+ os: detect_browser.OperatingSystem | null;
349
+ bot: string;
350
+ type: "bot-device";
351
+ } | {
352
+ /**
353
+ * 是否是浙里办
354
+ */
355
+ zlb: boolean;
356
+ /**
357
+ * 是否是专有钉
358
+ */
359
+ zyd: boolean;
360
+ /**
361
+ * 是否是浙政钉
362
+ */
363
+ zzd: boolean;
364
+ /**
365
+ * 是否是微信
366
+ */
367
+ wx: boolean;
368
+ /**
369
+ * 是否是支付宝
370
+ */
371
+ zfb: boolean;
372
+ /**
373
+ * 是否是小程序
374
+ */
375
+ mini: boolean;
376
+ type: "bot";
377
+ bot: true;
378
+ name: "bot";
379
+ version: null;
380
+ os: null;
381
+ } | {
382
+ /**
383
+ * 是否是浙里办
384
+ */
385
+ zlb: boolean;
386
+ /**
387
+ * 是否是专有钉
388
+ */
389
+ zyd: boolean;
390
+ /**
391
+ * 是否是浙政钉
392
+ */
393
+ zzd: boolean;
394
+ /**
395
+ * 是否是微信
396
+ */
397
+ wx: boolean;
398
+ /**
399
+ * 是否是支付宝
400
+ */
401
+ zfb: boolean;
402
+ /**
403
+ * 是否是小程序
404
+ */
405
+ mini: boolean;
406
+ version: string;
407
+ type: "node";
408
+ name: "node";
409
+ os: NodeJS.Platform;
410
+ } | {
411
+ /**
412
+ * 是否是浙里办
413
+ */
414
+ zlb: boolean;
415
+ /**
416
+ * 是否是专有钉
417
+ */
418
+ zyd: boolean;
419
+ /**
420
+ * 是否是浙政钉
421
+ */
422
+ zzd: boolean;
423
+ /**
424
+ * 是否是微信
425
+ */
426
+ wx: boolean;
427
+ /**
428
+ * 是否是支付宝
429
+ */
430
+ zfb: boolean;
431
+ /**
432
+ * 是否是小程序
433
+ */
434
+ mini: boolean;
435
+ type: "react-native";
436
+ name: "react-native";
437
+ version: null;
438
+ os: null;
439
+ };
440
+
242
441
  /**
243
442
  * Json字符串转对象
244
443
  * @param str
@@ -303,5 +502,20 @@ declare const isPhone: (val: NullOrUndefinable<Numeric>) => boolean;
303
502
  * @param val
304
503
  */
305
504
  declare const isTel: (val: NullOrUndefinable<Numeric>) => boolean;
505
+ /**
506
+ * 校验接口返回内容
507
+ * @param res
508
+ */
509
+ declare const isValidRes: (res: Res) => any;
510
+ /**
511
+ * 校验接口数组返回内容
512
+ * @param res
513
+ */
514
+ declare const isValidArrRes: (res: Res<any[]>) => boolean;
515
+ /**
516
+ * 校验接口分页返回内容
517
+ * @param res
518
+ */
519
+ declare const isValidPageRes: (res: PageRes) => boolean;
306
520
 
307
- export { NullOrUndefinable, Nullable, Numeric, Undefinable, camel2Kebab, camel2Pascal, camel2Snake, camel2Title, checkImg, filterObj, hideCardNo, hideMiddle, hideName, hidePhone, hideThird, isArrStr, isDef, isJSONStr, isNumeric, isPhone, isTel, isValidKey, jClear, jDebug, jError, jGroup, jGroupC, jGroupE, jInfo, jLog, jTime, jTimeEnd, jWarn, kebab2Camel, kebab2Pascal, kebab2Snake, kebab2Title, parseToJSON, pascal2Camel, pascal2Kebab, pascal2Snake, pascal2Title, replacer, reviver, snake2Camel, snake2Kebab, snake2Pascal, snake2Title, title2Camel, title2Kebab, title2Pascal, title2Snake };
521
+ export { NullOrUndefinable, Nullable, Numeric, Undefinable, camel2Kebab, camel2Pascal, camel2Snake, camel2Title, checkImg, filterObj, fixiOSInputAutoZoomIn, getHostEnv, hideCardNo, hideMiddle, hideName, hidePhone, hideThird, isArrStr, isDef, isJSONStr, isNumeric, isPhone, isTel, isValidArrRes, isValidKey, isValidPageRes, isValidRes, jClear, jDebug, jError, jGroup, jGroupC, jGroupE, jInfo, jLog, jTime, jTimeEnd, jWarn, kebab2Camel, kebab2Pascal, kebab2Snake, kebab2Title, parseToJSON, pascal2Camel, pascal2Kebab, pascal2Snake, pascal2Title, replacer, reviver, snake2Camel, snake2Kebab, snake2Pascal, snake2Title, title2Camel, title2Kebab, title2Pascal, title2Snake, userAgent, waitTime };
package/dist/index.mjs CHANGED
@@ -1,3 +1,5 @@
1
+ import { detect } from 'detect-browser';
2
+
1
3
  const snake2Kebab = (str) => str.replace(/_([a-z])/g, (_, letter) => `-${letter.toLowerCase()}`);
2
4
  const snake2Pascal = (str) => str.replace(/_([a-z])/g, (_, letter) => letter.toUpperCase()).replace(/^[a-z]/, (letter) => letter.toUpperCase());
3
5
  const snake2Camel = (str) => str.replace(/(_([a-z]))/g, (match) => match[1].toUpperCase());
@@ -81,6 +83,9 @@ const isTel = (val) => {
81
83
  return false;
82
84
  return cnTelRE.test(val.toString());
83
85
  };
86
+ const isValidRes = (res) => res.code === 200 && res.data;
87
+ const isValidArrRes = (res) => res.code === 200 && Array.isArray(res.data);
88
+ const isValidPageRes = (res) => res.code === 200 && res.data && Array.isArray(res.data.list || res.data.records);
84
89
 
85
90
  const hidePhone = (str) => {
86
91
  if (str) {
@@ -136,6 +141,60 @@ const hideThird = (str) => {
136
141
  return str.slice(0, partLen) + "*".repeat(len - 2 * partLen) + str.slice(len - partLen);
137
142
  };
138
143
 
144
+ const waitTime = (time = 100) => {
145
+ return new Promise((resolve) => {
146
+ setTimeout(() => {
147
+ resolve(true);
148
+ }, time);
149
+ });
150
+ };
151
+ const fixiOSInputAutoZoomIn = () => {
152
+ if (CSS.supports("(font:-apple-system-body) and (-webkit-touch-callout:none) and (-webkit-tap-highlight-color:hotpink)")) {
153
+ const viewport = document.querySelector('meta[name="viewport"]');
154
+ if (viewport !== null) {
155
+ viewport.content = `${viewport.content}, user-scalable=no`;
156
+ }
157
+ }
158
+ };
159
+ const { userAgent } = window.navigator;
160
+ const getHostEnv = () => {
161
+ const ua = userAgent.toLowerCase();
162
+ const baseInfo = detect();
163
+ const zlb = ua.includes("@zlb");
164
+ const zyd = ua.includes("saas");
165
+ const zzd = ua.includes("zhejiang") || ua.includes("tauruszjd");
166
+ const wx = ua.includes("micromessenger");
167
+ const zfb = ua.includes("alipay");
168
+ const mini = ua.includes("miniprogram");
169
+ return {
170
+ ...baseInfo,
171
+ /**
172
+ * 是否是浙里办
173
+ */
174
+ zlb,
175
+ /**
176
+ * 是否是专有钉
177
+ */
178
+ zyd,
179
+ /**
180
+ * 是否是浙政钉
181
+ */
182
+ zzd,
183
+ /**
184
+ * 是否是微信
185
+ */
186
+ wx,
187
+ /**
188
+ * 是否是支付宝
189
+ */
190
+ zfb,
191
+ /**
192
+ * 是否是小程序
193
+ */
194
+ mini
195
+ };
196
+ };
197
+
139
198
  const parseToJSON = (str, reviver2) => isJSONStr(str) ? JSON.parse(str, reviver2) : null;
140
199
  const replacer = (_, value) => {
141
200
  if (value instanceof Map) {
@@ -156,4 +215,4 @@ const reviver = (_, value) => {
156
215
  };
157
216
  const filterObj = (obj, keys, reverse = false) => Object.fromEntries(Object.entries(obj).filter(([key]) => reverse ? !keys.includes(key) : keys.includes(key)));
158
217
 
159
- export { camel2Kebab, camel2Pascal, camel2Snake, camel2Title, checkImg, filterObj, hideCardNo, hideMiddle, hideName, hidePhone, hideThird, isArrStr, isDef, isJSONStr, isNumeric, isPhone, isTel, isValidKey, jClear, jDebug, jError, jGroup, jGroupC, jGroupE, jInfo, jLog, jTime, jTimeEnd, jWarn, kebab2Camel, kebab2Pascal, kebab2Snake, kebab2Title, parseToJSON, pascal2Camel, pascal2Kebab, pascal2Snake, pascal2Title, replacer, reviver, snake2Camel, snake2Kebab, snake2Pascal, snake2Title, title2Camel, title2Kebab, title2Pascal, title2Snake };
218
+ export { camel2Kebab, camel2Pascal, camel2Snake, camel2Title, checkImg, filterObj, fixiOSInputAutoZoomIn, getHostEnv, hideCardNo, hideMiddle, hideName, hidePhone, hideThird, isArrStr, isDef, isJSONStr, isNumeric, isPhone, isTel, isValidArrRes, isValidKey, isValidPageRes, isValidRes, jClear, jDebug, jError, jGroup, jGroupC, jGroupE, jInfo, jLog, jTime, jTimeEnd, jWarn, kebab2Camel, kebab2Pascal, kebab2Snake, kebab2Title, parseToJSON, pascal2Camel, pascal2Kebab, pascal2Snake, pascal2Title, replacer, reviver, snake2Camel, snake2Kebab, snake2Pascal, snake2Title, title2Camel, title2Kebab, title2Pascal, title2Snake, userAgent, waitTime };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@jhqn/utils-core",
3
3
  "type": "module",
4
- "version": "0.0.1-beta.10",
4
+ "version": "0.0.1-beta.12",
5
5
  "author": "jade-gjz <jiangang.zhang@jaderd.com>",
6
6
  "license": "MIT",
7
7
  "homepage": "https://github.com/jaderd-jh/utils/tree/main/packages/core#readme",
@@ -31,6 +31,9 @@
31
31
  "files": [
32
32
  "dist"
33
33
  ],
34
+ "dependencies": {
35
+ "detect-browser": "^5.3.0"
36
+ },
34
37
  "scripts": {
35
38
  "test": "vitest",
36
39
  "//build": "tsup",