@anjianshi/utils 2.7.0 → 2.8.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.
Files changed (153) hide show
  1. package/env-browser/device.d.ts +24 -0
  2. package/env-browser/device.js +50 -0
  3. package/env-browser/global.d.ts +10 -0
  4. package/env-browser/global.js +15 -0
  5. package/env-browser/load-script.d.ts +5 -0
  6. package/env-browser/load-script.js +13 -0
  7. package/env-browser/logging.d.ts +18 -0
  8. package/env-browser/logging.js +49 -0
  9. package/env-browser/manage-vconsole.d.ts +16 -0
  10. package/env-browser/manage-vconsole.js +38 -0
  11. package/env-node/crypto-random.d.ts +13 -0
  12. package/env-node/crypto-random.js +28 -0
  13. package/env-node/fs.d.ts +19 -0
  14. package/env-node/fs.js +48 -0
  15. package/env-node/index.d.ts +6 -0
  16. package/env-node/index.js +6 -0
  17. package/env-node/logging/handlers.d.ts +58 -0
  18. package/env-node/logging/handlers.js +154 -0
  19. package/env-node/logging/index.d.ts +11 -0
  20. package/env-node/logging/index.js +14 -0
  21. package/env-node/safe-request.d.ts +26 -0
  22. package/env-node/safe-request.js +40 -0
  23. package/env-react/emotion.d.ts +20 -0
  24. package/env-react/emotion.jsx +34 -0
  25. package/env-service/controllers.d.ts +30 -0
  26. package/env-service/controllers.js +41 -0
  27. package/env-service/env-reader.d.ts +55 -0
  28. package/env-service/env-reader.js +79 -0
  29. package/env-service/index.d.ts +6 -0
  30. package/env-service/index.js +6 -0
  31. package/env-service/prisma/adapt-logging.d.ts +21 -0
  32. package/env-service/prisma/adapt-logging.js +30 -0
  33. package/env-service/prisma/extensions/exist.d.ts +10 -0
  34. package/env-service/prisma/extensions/exist.js +16 -0
  35. package/env-service/prisma/extensions/find-and-count.d.ts +7 -0
  36. package/env-service/prisma/extensions/find-and-count.js +19 -0
  37. package/env-service/prisma/extensions/soft-delete.d.ts +52 -0
  38. package/env-service/prisma/extensions/soft-delete.js +123 -0
  39. package/env-service/prisma/extensions/with-transaction.d.ts +9 -0
  40. package/env-service/prisma/extensions/with-transaction.js +54 -0
  41. package/env-service/prisma/index.d.ts +6 -0
  42. package/env-service/prisma/index.js +6 -0
  43. package/env-service/prisma/transaction-contexted.d.ts +11 -0
  44. package/env-service/prisma/transaction-contexted.js +52 -0
  45. package/env-service/redis-cache.d.ts +39 -0
  46. package/env-service/redis-cache.js +116 -0
  47. package/env-service/tasks.d.ts +12 -0
  48. package/env-service/tasks.js +37 -0
  49. package/index.d.ts +3 -0
  50. package/index.js +3 -0
  51. package/init-dayjs.d.ts +2 -0
  52. package/init-dayjs.js +7 -0
  53. package/lang/async.d.ts +19 -0
  54. package/lang/async.js +34 -0
  55. package/lang/color.d.ts +37 -0
  56. package/lang/color.js +111 -0
  57. package/lang/index.d.ts +8 -0
  58. package/lang/index.js +8 -0
  59. package/lang/may-success.d.ts +40 -0
  60. package/lang/may-success.js +27 -0
  61. package/lang/object.d.ts +12 -0
  62. package/lang/object.js +41 -0
  63. package/lang/random.d.ts +13 -0
  64. package/lang/random.js +24 -0
  65. package/lang/string.d.ts +29 -0
  66. package/lang/string.js +92 -0
  67. package/lang/time.d.ts +10 -0
  68. package/lang/time.js +18 -0
  69. package/{src/lang/types.ts → lang/types.d.ts} +23 -43
  70. package/lang/types.js +28 -0
  71. package/logging/adapt.d.ts +10 -0
  72. package/logging/adapt.js +43 -0
  73. package/logging/formatters.d.ts +10 -0
  74. package/logging/formatters.js +22 -0
  75. package/logging/index.d.ts +45 -0
  76. package/logging/index.js +90 -0
  77. package/md5.d.ts +30 -0
  78. package/md5.js +308 -0
  79. package/package.json +14 -20
  80. package/url.d.ts +77 -0
  81. package/url.js +149 -0
  82. package/validators/array.d.ts +30 -0
  83. package/validators/array.js +47 -0
  84. package/validators/base.d.ts +82 -0
  85. package/validators/base.js +42 -0
  86. package/validators/boolean.d.ts +3 -0
  87. package/validators/boolean.js +22 -0
  88. package/validators/datetime.d.ts +12 -0
  89. package/validators/datetime.js +30 -0
  90. package/validators/factory.d.ts +70 -0
  91. package/validators/factory.js +121 -0
  92. package/validators/index.d.ts +9 -0
  93. package/validators/index.js +9 -0
  94. package/validators/number.d.ts +19 -0
  95. package/validators/number.js +26 -0
  96. package/validators/object.d.ts +28 -0
  97. package/validators/object.js +49 -0
  98. package/validators/one-of.d.ts +10 -0
  99. package/validators/one-of.js +15 -0
  100. package/validators/string.d.ts +22 -0
  101. package/validators/string.js +35 -0
  102. package/README.md +0 -10
  103. package/eslint.config.cjs +0 -33
  104. package/publish-prepare.cjs +0 -16
  105. package/src/env-browser/device.ts +0 -62
  106. package/src/env-browser/global.ts +0 -21
  107. package/src/env-browser/load-script.ts +0 -13
  108. package/src/env-browser/logging.ts +0 -58
  109. package/src/env-browser/manage-vconsole.ts +0 -54
  110. package/src/env-node/crypto-random.ts +0 -30
  111. package/src/env-node/fs.ts +0 -50
  112. package/src/env-node/index.ts +0 -6
  113. package/src/env-node/logging/handlers.ts +0 -190
  114. package/src/env-node/logging/index.ts +0 -16
  115. package/src/env-node/safe-request.ts +0 -66
  116. package/src/env-react/emotion.tsx +0 -42
  117. package/src/env-service/controllers.ts +0 -93
  118. package/src/env-service/env-reader.ts +0 -141
  119. package/src/env-service/index.ts +0 -6
  120. package/src/env-service/prisma/adapt-logging.ts +0 -39
  121. package/src/env-service/prisma/extensions/exist.ts +0 -21
  122. package/src/env-service/prisma/extensions/find-and-count.ts +0 -24
  123. package/src/env-service/prisma/extensions/soft-delete.ts +0 -162
  124. package/src/env-service/prisma/extensions/with-transaction.ts +0 -65
  125. package/src/env-service/prisma/index.ts +0 -6
  126. package/src/env-service/prisma/transaction-contexted.ts +0 -80
  127. package/src/env-service/redis-cache.ts +0 -142
  128. package/src/env-service/tasks.ts +0 -45
  129. package/src/index.ts +0 -3
  130. package/src/init-dayjs.ts +0 -8
  131. package/src/lang/async.ts +0 -47
  132. package/src/lang/color.ts +0 -119
  133. package/src/lang/index.ts +0 -7
  134. package/src/lang/may-success.ts +0 -57
  135. package/src/lang/object.ts +0 -39
  136. package/src/lang/random.ts +0 -25
  137. package/src/lang/string.ts +0 -95
  138. package/src/lang/time.ts +0 -19
  139. package/src/logging/adapt.ts +0 -49
  140. package/src/logging/formatters.ts +0 -23
  141. package/src/logging/index.ts +0 -106
  142. package/src/md5.ts +0 -318
  143. package/src/url.ts +0 -185
  144. package/src/validators/array.ts +0 -97
  145. package/src/validators/base.ts +0 -145
  146. package/src/validators/boolean.ts +0 -21
  147. package/src/validators/datetime.ts +0 -39
  148. package/src/validators/factory.ts +0 -244
  149. package/src/validators/index.ts +0 -9
  150. package/src/validators/number.ts +0 -54
  151. package/src/validators/object.ts +0 -101
  152. package/src/validators/one-of.ts +0 -33
  153. package/src/validators/string.ts +0 -72
@@ -1,101 +0,0 @@
1
- import isPlainObject from 'lodash/isPlainObject.js'
2
- import { success, failed } from '../lang/index.js'
3
- import {
4
- getValidatorGenerator,
5
- type CommonOptions,
6
- type Validator,
7
- type Validated,
8
- type AllowedInputValue,
9
- } from './base.js'
10
-
11
- /** 验证有明确键值对结构的对象 */
12
- export interface StructOptions extends CommonOptions {
13
- /** 定义对象结构,及各个值的验证规则 */
14
- struct: Record<string, Validator<unknown, CommonOptions>>
15
- }
16
-
17
- type StructValues<Options extends StructOptions> = {
18
- [Key in keyof Options['struct']]: Options['struct'][Key] extends Validator<
19
- infer Value,
20
- infer Options
21
- >
22
- ? Validated<Value, Options>
23
- : never
24
- }
25
-
26
- export function getStructValidator<const Options extends StructOptions>(options: Options) {
27
- return getValidatorGenerator<StructValues<Options>, Options>(
28
- function validate(field, value, options) {
29
- if (!isPlainObject(value)) return failed(`${field} should be a plain object`)
30
-
31
- const formatted: Record<string, unknown> = {}
32
- for (const [key, itemValidator] of Object.entries(options.struct)) {
33
- const itemResult = itemValidator(
34
- `${field}["${key}"]`,
35
- (value as Record<string, AllowedInputValue>)[key],
36
- )
37
- if (itemResult.success) {
38
- if (itemResult.data !== undefined) formatted[key] = itemResult.data
39
- } else {
40
- return itemResult
41
- }
42
- }
43
- return success(formatted as StructValues<Options>)
44
- },
45
- )(options)
46
- }
47
-
48
- // ---------------------------------------------------
49
-
50
- /**
51
- * 验证有任意多个 key,但值的类型固定的对象
52
- */
53
- export interface RecordOptions extends CommonOptions {
54
- /** 验证单个值 */
55
- record: Validator<unknown, CommonOptions>
56
-
57
- /** 对象至少要有几项 */
58
- min?: number
59
-
60
- /** 对象最多有几项 */
61
- max?: number
62
- }
63
-
64
- type RecordValues<Options extends RecordOptions> = Record<
65
- string,
66
- Validated<
67
- Options extends { record: Validator<infer T, CommonOptions> } ? T : never,
68
- Options extends { record: Validator<unknown, infer T> } ? T : never
69
- >
70
- >
71
-
72
- export function getRecordValidator<Options extends RecordOptions>(options: Options) {
73
- return getValidatorGenerator<RecordValues<Options>, Options>(
74
- function validate(field, value, options) {
75
- if (!isPlainObject(value)) return failed(`${field} should be a plain object`)
76
-
77
- const formatted: Record<string, unknown> = {}
78
- for (const [key, itemValue] of Object.entries(value as Record<string, AllowedInputValue>)) {
79
- // record 场景下,值为 undefined 的项目视为不存在,不保留在验证结果里,
80
- // 不然一些因为不想赋值而填充了 undefined 值的项目可能意外触发验证失败,或意外得到了默认值。
81
- // (因此 validator 的 required 选项和 defaults 选项也没有意义了)
82
- if (itemValue === undefined) continue
83
-
84
- const itemResult = options.record(`${field}["${key}"]`, itemValue)
85
- if (itemResult.success) {
86
- if (itemResult.data !== undefined) formatted[key] = itemResult.data
87
- } else {
88
- return itemResult
89
- }
90
- }
91
-
92
- const length = Object.keys(formatted).length
93
- if (typeof options.min === 'number' && length < options.min)
94
- return failed(`size of ${field} should >= ${options.min}`)
95
- if (typeof options.max === 'number' && length > options.max)
96
- return failed(`size of ${field} should <= ${options.max}`)
97
-
98
- return success(formatted as RecordValues<Options>)
99
- },
100
- )(options)
101
- }
@@ -1,33 +0,0 @@
1
- import { failed, type MaySuccess } from '../lang/index.js'
2
- import {
3
- getValidatorGenerator,
4
- type CommonOptions,
5
- type Validator,
6
- type AllowedInputValue,
7
- } from './base.js'
8
-
9
- /** 要求返回值通过其中一个验证器的检查 */
10
- export interface OneOfOptions extends CommonOptions {
11
- /** 验证数组各元素 */
12
- validators: Validator<unknown, CommonOptions>[]
13
- }
14
-
15
- export type OneOfValue<Options extends OneOfOptions> = Options extends {
16
- validators: Validator<infer T, CommonOptions>[]
17
- }
18
- ? T
19
- : never
20
-
21
- export function getOneOfValidator<const Options extends OneOfOptions>(
22
- options: Options = {} as Options
23
- ) {
24
- return getValidatorGenerator<OneOfValue<Options>, Options>(function validate(field, value) {
25
- const errors: string[] = []
26
- for (const validator of options.validators) {
27
- const result = validator(field, value as AllowedInputValue)
28
- if (result.success) return result as MaySuccess<OneOfValue<Options>>
29
- else errors.push(result.message)
30
- }
31
- return failed(`${field} do not match any valid format:\n- ` + errors.join('\n- '))
32
- })(options)
33
- }
@@ -1,72 +0,0 @@
1
- import { success, failed } from '../lang/index.js'
2
- import { getValidatorGenerator, type CommonOptions } from './base.js'
3
-
4
- export interface StringOptions extends CommonOptions<string> {
5
- /** 字符串最小长度。defaults='' 时默认为 0,否则默认为 1 */
6
- min?: number
7
-
8
- /** 字符串最大长度。 */
9
- max?: number
10
-
11
- /** 字符串需匹配此正则,也可传入关键词使用预置的正则。正则通常应以 ^ 和 $ 开头结尾。 */
12
- pattern?: RegExp | 'uuid' | 'mobile'
13
-
14
- /**
15
- * 指定一个数组或 TypeScript enum,字段值必须在此 enum 之中。
16
- * 若指定,前几个选项将不再生效。
17
- */
18
- choices?: string[] | Record<string, string>
19
-
20
- /** 验证之前,是否先清除两侧空白字符 @default true */
21
- trim?: boolean
22
- }
23
-
24
- export type StringValueWithChoices<Options extends StringOptions> = Options extends {
25
- choices: (infer T)[]
26
- }
27
- ? T
28
- : Options extends { choices: Record<string, infer T> }
29
- ? T
30
- : string
31
-
32
- export function getStringValidator<const Options extends StringOptions>(
33
- options: Options = {} as Options,
34
- ) {
35
- return getValidatorGenerator<StringValueWithChoices<Options>, Options>(
36
- function validate(field, value) {
37
- if (typeof value !== 'string') return failed(`${field} must be a string`)
38
-
39
- const trim = options.trim ?? true
40
- const formatted = trim ? value.trim() : value
41
-
42
- if ('choices' in options && options.choices) {
43
- const validValues: string[] = Array.isArray(options.choices)
44
- ? options.choices
45
- : Object.values(options.choices)
46
- if (!validValues.includes(formatted))
47
- return failed(`${field} can only be one of ${validValues.join(', ')}.`)
48
- } else {
49
- const { min = options.defaults === '' ? 0 : 1, max, pattern } = options
50
-
51
- if (typeof min === 'number' && formatted.length < min)
52
- return failed(`${field}'s length must >= ${min}`)
53
-
54
- if (typeof max === 'number' && formatted.length > max)
55
- return failed(`${field}'s length must <= ${max}`)
56
-
57
- if (pattern !== undefined) {
58
- if (pattern instanceof RegExp && !pattern.exec(formatted))
59
- return failed(`${field} does not match the pattern`)
60
- if (pattern === 'mobile' && !/^1\d{10}$/.test(formatted))
61
- return failed(`${field} is not a valid mobile number`)
62
- if (pattern === 'uuid') {
63
- if (!/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i.test(formatted))
64
- return failed(`${field} is not a valid uuid`)
65
- }
66
- }
67
- }
68
-
69
- return success(formatted as StringValueWithChoices<Options>)
70
- },
71
- )(options)
72
- }