@wener/utils 1.1.48 → 1.1.50
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/lib/arrays/MaybeArray.js.map +1 -1
- package/lib/arrays/arrayFromAsync.js.map +1 -1
- package/lib/asyncs/AsyncInterval.js.map +1 -1
- package/lib/asyncs/MaybePromise.js.map +1 -1
- package/lib/asyncs/Promises.js +9 -1
- package/lib/asyncs/Promises.js.map +1 -1
- package/lib/asyncs/createAsyncIterator.js.map +1 -1
- package/lib/asyncs/createLazyPromise.js +52 -51
- package/lib/asyncs/createLazyPromise.js.map +1 -1
- package/lib/asyncs/firstOfAsyncIterator.js.map +1 -1
- package/lib/asyncs/generatorOfStream.js.map +1 -1
- package/lib/asyncs/isIterator.js.map +1 -1
- package/lib/asyncs/isPromise.js.map +1 -1
- package/lib/asyncs/isThenable.js.map +1 -1
- package/lib/asyncs/nextOfAsyncIterator.js.map +1 -1
- package/lib/asyncs/promiseOfCallback.js.map +1 -1
- package/lib/asyncs/timeout.js +4 -4
- package/lib/asyncs/timeout.js.map +1 -1
- package/lib/browsers/copy.js.map +1 -1
- package/lib/browsers/download.js.map +1 -1
- package/lib/browsers/getFileFromDataTransfer.js.map +1 -1
- package/lib/browsers/loaders.js.map +1 -1
- package/lib/cn/division/DivisionCode.js.map +1 -1
- package/lib/cn/division/binarySearch.js.map +1 -1
- package/lib/cn/formatChineseAmount.js.map +1 -1
- package/lib/cn/id/Mod11.js.map +1 -1
- package/lib/cn/id/ResidentIdNumber.js.map +1 -1
- package/lib/cn/id/types.js.map +1 -1
- package/lib/cn/index.js.map +1 -1
- package/lib/cn/parseChineseNumber.js.map +1 -1
- package/lib/cn/pinyin/cartesianProduct.js.map +1 -1
- package/lib/cn/pinyin/loader.js.map +1 -1
- package/lib/cn/pinyin/preload.js.map +1 -1
- package/lib/cn/pinyin/toPinyinPure.js.map +1 -1
- package/lib/cn/pinyin/transform.js.map +1 -1
- package/lib/cn/types.js +3 -0
- package/lib/cn/types.js.map +1 -0
- package/lib/cn/uscc/Mod31.js.map +1 -1
- package/lib/cn/uscc/USCC.js.map +1 -1
- package/lib/cn/uscc/isUSCC.js.map +1 -1
- package/lib/crypto/base.js.map +1 -1
- package/lib/crypto/getNodeCrypto.js +1 -0
- package/lib/crypto/getNodeCrypto.js.map +1 -1
- package/lib/crypto/getRandomValues.js.map +1 -1
- package/lib/crypto/hashing.js.map +1 -1
- package/lib/crypto/md5.bench.js.map +1 -1
- package/lib/crypto/md5.d.js.map +1 -1
- package/lib/crypto/md5.js.map +1 -1
- package/lib/crypto/pem/pem.js.map +1 -1
- package/lib/crypto/randomUUID.js.map +1 -1
- package/lib/crypto/randomUUIDv7.js.map +1 -1
- package/lib/crypto/ulid.js.map +1 -1
- package/lib/emitter/types.js.map +1 -1
- package/lib/errors/Errors.js +38 -17
- package/lib/errors/Errors.js.map +1 -1
- package/lib/fetch/HttpStatus.js.map +1 -1
- package/lib/fetch/createFetchWith.js.map +1 -1
- package/lib/fetch/createFetchWithLogging.js.map +1 -1
- package/lib/fetch/createFetchWithRetry.js.map +1 -1
- package/lib/fetch/dumpRequest.js.map +1 -1
- package/lib/fetch/dumpResponse.js.map +1 -1
- package/lib/fetch/http.types.js.map +1 -1
- package/lib/fetch/index.js.map +1 -1
- package/lib/fetch/types.js.map +1 -1
- package/lib/i18n/createTranslate.js.map +1 -1
- package/lib/index.js +2 -0
- package/lib/index.js.map +1 -1
- package/lib/io/AbstractEncoding.js.map +1 -1
- package/lib/io/ArrayBuffer.test-d.js.map +1 -1
- package/lib/io/ArrayBuffers.js +217 -173
- package/lib/io/ArrayBuffers.js.map +1 -1
- package/lib/io/Buffer.js.map +1 -1
- package/lib/io/ByteBuffer.js +233 -34
- package/lib/io/ByteBuffer.js.map +1 -1
- package/lib/io/base64.js +2 -2
- package/lib/io/base64.js.map +1 -1
- package/lib/io/dump.js.map +1 -1
- package/lib/io/isBuffer.js.map +1 -1
- package/lib/io/isTransferable.js.map +1 -1
- package/lib/io/parseDataUri.js.map +1 -1
- package/lib/langs/MaybeFunction.js.map +1 -1
- package/lib/langs/classOf.js.map +1 -1
- package/lib/langs/deepEqual.js.map +1 -1
- package/lib/langs/deepFreeze.js.map +1 -1
- package/lib/langs/getGlobalStates.js +17 -0
- package/lib/langs/getGlobalStates.js.map +1 -0
- package/lib/langs/getObjectId.js +18 -0
- package/lib/langs/getObjectId.js.map +1 -0
- package/lib/langs/isClass.js.map +1 -1
- package/lib/langs/isDefined.js.map +1 -1
- package/lib/langs/isEmptyObject.js.map +1 -1
- package/lib/langs/isFunction.js.map +1 -1
- package/lib/langs/isNullish.js +5 -0
- package/lib/langs/isNullish.js.map +1 -0
- package/lib/langs/isPlainObject.js.map +1 -1
- package/lib/langs/memoize.js.map +1 -1
- package/lib/langs/mixin.js.map +1 -1
- package/lib/langs/parseBoolean.js.map +1 -1
- package/lib/langs/shallowClone.js.map +1 -1
- package/lib/langs/shallowEqual.js.map +1 -1
- package/lib/libs/ms.js.map +1 -1
- package/lib/logging/Logger.js.map +1 -1
- package/lib/logging/createChildLogger.js.map +1 -1
- package/lib/logging/createLogger.js.map +1 -1
- package/lib/logging/createNoopLogger.js.map +1 -1
- package/lib/logging/slog.js.map +1 -1
- package/lib/maths/clamp.js +7 -6
- package/lib/maths/clamp.js.map +1 -1
- package/lib/maths/random.js.map +1 -1
- package/lib/mitt/index.js.map +1 -1
- package/lib/modules/isModule.js.map +1 -1
- package/lib/modules/parseModuleId.js.map +1 -1
- package/lib/objects/computeIfAbsent.js.map +1 -1
- package/lib/objects/get.js.map +1 -1
- package/lib/objects/get.test-d.js.map +1 -1
- package/lib/objects/merge/index.js.map +1 -1
- package/lib/objects/merge/isMergeableObject.js.map +1 -1
- package/lib/objects/merge/merge.js.map +1 -1
- package/lib/objects/parseObjectPath.js.map +1 -1
- package/lib/objects/set.js.map +1 -1
- package/lib/runtime/AsyncCloser.js.map +1 -1
- package/lib/runtime/Closer.js.map +1 -1
- package/lib/runtime/getGlobalThis.js.map +1 -1
- package/lib/runtime/structuredClone.js.map +1 -1
- package/lib/schema/typebox/index.js.map +1 -1
- package/lib/schema/typebox/typebox.js.map +1 -1
- package/lib/scripts/getGenerateContext.js.map +1 -1
- package/lib/server/crypto/md5.js.map +1 -1
- package/lib/server/fetch/createFetchWithProxy.js.map +1 -1
- package/lib/server/fetch/createFetchWithProxyByNodeFetch.js.map +1 -1
- package/lib/server/fetch/createFetchWithProxyByUndici.js.map +1 -1
- package/lib/server/getPackageDir.js.map +1 -1
- package/lib/server/index.js.map +1 -1
- package/lib/server/jsdom.js.map +1 -1
- package/lib/server/node-fetch.js.map +1 -1
- package/lib/server/polyfill/polyfillBrowser.js.map +1 -1
- package/lib/server/polyfill/polyfillCrypto.js.map +1 -1
- package/lib/server/polyfill/polyfillFetch.js.map +1 -1
- package/lib/server/polyfill/polyfillJsDom.js.map +1 -1
- package/lib/server/polyfill/polyfillWebSocket.js.map +1 -1
- package/lib/server/ws.js.map +1 -1
- package/lib/strings/camelCase.js.map +1 -1
- package/lib/strings/formatBytes.js.map +1 -1
- package/lib/strings/renderTemplate.js.map +1 -1
- package/lib/typedoc.js.map +1 -1
- package/lib/types.d.js.map +1 -1
- package/lib/validations/asserts.js.map +1 -1
- package/lib/validations/isUUID.js.map +1 -1
- package/lib/validations/parseTimestamp.js.map +1 -1
- package/package.json +4 -4
- package/src/asyncs/Promises.ts +12 -2
- package/src/asyncs/createLazyPromise.test.ts +52 -13
- package/src/asyncs/createLazyPromise.ts +66 -61
- package/src/asyncs/firstOfAsyncIterator.ts +1 -1
- package/src/asyncs/nextOfAsyncIterator.ts +1 -1
- package/src/asyncs/timeout.ts +4 -4
- package/src/cn/types.ts +11 -0
- package/src/crypto/getNodeCrypto.ts +1 -0
- package/src/errors/Errors.ts +37 -16
- package/src/fetch/createFetchWith.ts +1 -1
- package/src/fetch/createFetchWithRetry.ts +1 -1
- package/src/index.ts +2 -0
- package/src/io/ArrayBuffers.base64.test.ts +7 -0
- package/src/io/ArrayBuffers.ts +276 -271
- package/src/io/ByteBuffer.test.ts +26 -14
- package/src/io/ByteBuffer.ts +270 -38
- package/src/io/base64.ts +2 -2
- package/src/langs/getGlobalStates.ts +21 -0
- package/src/langs/getObjectId.ts +20 -0
- package/src/langs/isNullish.ts +3 -0
- package/src/langs/mixin.test.ts +4 -0
- package/src/logging/slog.ts +1 -1
- package/src/maths/clamp.test.ts +5 -1
- package/src/maths/clamp.ts +7 -7
- package/src/objects/merge/merge.test.ts +1 -1
- package/src/schema/typebox/typebox.ts +1 -1
- package/src/server/fetch/createFetchWithProxyByUndici.ts +2 -2
- package/tsconfig.json +4 -1
- package/lib/schema/typebox/gen/codegen/common/encoder.js +0 -94
- package/lib/schema/typebox/gen/codegen/common/encoder.js.map +0 -1
- package/lib/schema/typebox/gen/codegen/common/formatter.js +0 -33
- package/lib/schema/typebox/gen/codegen/common/formatter.js.map +0 -1
- package/lib/schema/typebox/gen/codegen/common/index.js +0 -29
- package/lib/schema/typebox/gen/codegen/common/index.js.map +0 -1
- package/lib/schema/typebox/gen/codegen/common/jsdoc.js +0 -117
- package/lib/schema/typebox/gen/codegen/common/jsdoc.js.map +0 -1
- package/lib/schema/typebox/gen/codegen/expression/compiler.js +0 -364
- package/lib/schema/typebox/gen/codegen/expression/compiler.js.map +0 -1
- package/lib/schema/typebox/gen/codegen/expression/errors.js +0 -259
- package/lib/schema/typebox/gen/codegen/expression/errors.js.map +0 -1
- package/lib/schema/typebox/gen/codegen/expression/evaluator.js +0 -254
- package/lib/schema/typebox/gen/codegen/expression/evaluator.js.map +0 -1
- package/lib/schema/typebox/gen/codegen/expression/expression.js +0 -381
- package/lib/schema/typebox/gen/codegen/expression/expression.js.map +0 -1
- package/lib/schema/typebox/gen/codegen/expression/index.js +0 -32
- package/lib/schema/typebox/gen/codegen/expression/index.js.map +0 -1
- package/lib/schema/typebox/gen/codegen/index.js +0 -29
- package/lib/schema/typebox/gen/codegen/index.js.map +0 -1
- package/lib/schema/typebox/gen/codegen/model/index.js +0 -40
- package/lib/schema/typebox/gen/codegen/model/index.js.map +0 -1
- package/lib/schema/typebox/gen/codegen/model/model-to-arktype.js +0 -260
- package/lib/schema/typebox/gen/codegen/model/model-to-arktype.js.map +0 -1
- package/lib/schema/typebox/gen/codegen/model/model-to-expression.js +0 -383
- package/lib/schema/typebox/gen/codegen/model/model-to-expression.js.map +0 -1
- package/lib/schema/typebox/gen/codegen/model/model-to-grpc.js +0 -238
- package/lib/schema/typebox/gen/codegen/model/model-to-grpc.js.map +0 -1
- package/lib/schema/typebox/gen/codegen/model/model-to-io-ts.js +0 -274
- package/lib/schema/typebox/gen/codegen/model/model-to-io-ts.js.map +0 -1
- package/lib/schema/typebox/gen/codegen/model/model-to-javascript.js +0 -47
- package/lib/schema/typebox/gen/codegen/model/model-to-javascript.js.map +0 -1
- package/lib/schema/typebox/gen/codegen/model/model-to-json-schema.js +0 -192
- package/lib/schema/typebox/gen/codegen/model/model-to-json-schema.js.map +0 -1
- package/lib/schema/typebox/gen/codegen/model/model-to-typebox.js +0 -33
- package/lib/schema/typebox/gen/codegen/model/model-to-typebox.js.map +0 -1
- package/lib/schema/typebox/gen/codegen/model/model-to-typescript.js +0 -188
- package/lib/schema/typebox/gen/codegen/model/model-to-typescript.js.map +0 -1
- package/lib/schema/typebox/gen/codegen/model/model-to-valibot.js +0 -239
- package/lib/schema/typebox/gen/codegen/model/model-to-valibot.js.map +0 -1
- package/lib/schema/typebox/gen/codegen/model/model-to-value.js +0 -43
- package/lib/schema/typebox/gen/codegen/model/model-to-value.js.map +0 -1
- package/lib/schema/typebox/gen/codegen/model/model-to-yrel.js +0 -227
- package/lib/schema/typebox/gen/codegen/model/model-to-yrel.js.map +0 -1
- package/lib/schema/typebox/gen/codegen/model/model-to-yup.js +0 -225
- package/lib/schema/typebox/gen/codegen/model/model-to-yup.js.map +0 -1
- package/lib/schema/typebox/gen/codegen/model/model-to-zod.js +0 -248
- package/lib/schema/typebox/gen/codegen/model/model-to-zod.js.map +0 -1
- package/lib/schema/typebox/gen/codegen/model/model.js +0 -27
- package/lib/schema/typebox/gen/codegen/model/model.js.map +0 -1
- package/lib/schema/typebox/gen/codegen/typescript/index.js +0 -28
- package/lib/schema/typebox/gen/codegen/typescript/index.js.map +0 -1
- package/lib/schema/typebox/gen/codegen/typescript/typescript-to-model.js +0 -72
- package/lib/schema/typebox/gen/codegen/typescript/typescript-to-model.js.map +0 -1
- package/lib/schema/typebox/gen/codegen/typescript/typescript-to-typebox.js +0 -620
- package/lib/schema/typebox/gen/codegen/typescript/typescript-to-typebox.js.map +0 -1
- package/lib/schema/typebox/gen/index.js +0 -3
- package/lib/schema/typebox/gen/index.js.map +0 -1
- package/src/schema/typebox/gen/codegen/common/encoder.ts +0 -99
- package/src/schema/typebox/gen/codegen/common/formatter.ts +0 -31
- package/src/schema/typebox/gen/codegen/common/index.ts +0 -29
- package/src/schema/typebox/gen/codegen/common/jsdoc.ts +0 -93
- package/src/schema/typebox/gen/codegen/expression/compiler.ts +0 -377
- package/src/schema/typebox/gen/codegen/expression/errors.ts +0 -302
- package/src/schema/typebox/gen/codegen/expression/evaluator.ts +0 -268
- package/src/schema/typebox/gen/codegen/expression/expression.ts +0 -538
- package/src/schema/typebox/gen/codegen/expression/index.ts +0 -32
- package/src/schema/typebox/gen/codegen/index.ts +0 -29
- package/src/schema/typebox/gen/codegen/model/index.ts +0 -40
- package/src/schema/typebox/gen/codegen/model/model-to-arktype.ts +0 -266
- package/src/schema/typebox/gen/codegen/model/model-to-expression.ts +0 -378
- package/src/schema/typebox/gen/codegen/model/model-to-grpc.ts +0 -244
- package/src/schema/typebox/gen/codegen/model/model-to-io-ts.ts +0 -294
- package/src/schema/typebox/gen/codegen/model/model-to-javascript.ts +0 -42
- package/src/schema/typebox/gen/codegen/model/model-to-json-schema.ts +0 -166
- package/src/schema/typebox/gen/codegen/model/model-to-typebox.ts +0 -32
- package/src/schema/typebox/gen/codegen/model/model-to-typescript.ts +0 -189
- package/src/schema/typebox/gen/codegen/model/model-to-valibot.ts +0 -236
- package/src/schema/typebox/gen/codegen/model/model-to-value.ts +0 -42
- package/src/schema/typebox/gen/codegen/model/model-to-yrel.ts +0 -232
- package/src/schema/typebox/gen/codegen/model/model-to-yup.ts +0 -226
- package/src/schema/typebox/gen/codegen/model/model-to-zod.ts +0 -251
- package/src/schema/typebox/gen/codegen/model/model.ts +0 -33
- package/src/schema/typebox/gen/codegen/typescript/index.ts +0 -28
- package/src/schema/typebox/gen/codegen/typescript/typescript-to-model.ts +0 -61
- package/src/schema/typebox/gen/codegen/typescript/typescript-to-typebox.ts +0 -647
- package/src/schema/typebox/gen/gen.test.ts +0 -12
- package/src/schema/typebox/gen/index.ts +0 -1
- /package/src/schema/{typebox/gen/README.md → README.md} +0 -0
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/server/polyfill/polyfillWebSocket.ts"],"sourcesContent":["import type { MaybePromise } from '../../asyncs/MaybePromise';\nimport { getGlobalThis } from '../../runtime/getGlobalThis';\n\nexport function polyfillWebSocket(ws: any): boolean;\nexport function polyfillWebSocket(ws?: undefined): Promise<boolean>;\nexport function polyfillWebSocket(ws?: any): MaybePromise<boolean> {\n const globalThis = getGlobalThis();\n if ('WebSocket' in globalThis) {\n return false;\n }\n if (ws && 'then' in ws) {\n return ws.then((v: any) => {\n return polyfillWebSocket(v?.default || v);\n });\n }\n if (ws) {\n const { WebSocket } = ws;\n Object.assign(globalThis, { WebSocket });\n }\n return import('ws').then((ws) => polyfillWebSocket(ws));\n}\n"],"names":["getGlobalThis","polyfillWebSocket","ws","globalThis","then","v","default","WebSocket","Object","assign"],"
|
|
1
|
+
{"version":3,"sources":["../../../src/server/polyfill/polyfillWebSocket.ts"],"sourcesContent":["import type { MaybePromise } from '../../asyncs/MaybePromise';\nimport { getGlobalThis } from '../../runtime/getGlobalThis';\n\nexport function polyfillWebSocket(ws: any): boolean;\nexport function polyfillWebSocket(ws?: undefined): Promise<boolean>;\nexport function polyfillWebSocket(ws?: any): MaybePromise<boolean> {\n const globalThis = getGlobalThis();\n if ('WebSocket' in globalThis) {\n return false;\n }\n if (ws && 'then' in ws) {\n return ws.then((v: any) => {\n return polyfillWebSocket(v?.default || v);\n });\n }\n if (ws) {\n const { WebSocket } = ws;\n Object.assign(globalThis, { WebSocket });\n }\n return import('ws').then((ws) => polyfillWebSocket(ws));\n}\n"],"names":["getGlobalThis","polyfillWebSocket","ws","globalThis","then","v","default","WebSocket","Object","assign"],"mappings":"AACA,SAASA,aAAa,QAAQ,8BAA8B;AAI5D,OAAO,SAASC,kBAAkBC,EAAQ;IACxC,MAAMC,aAAaH;IACnB,IAAI,eAAeG,YAAY;QAC7B,OAAO;IACT;IACA,IAAID,MAAM,UAAUA,IAAI;QACtB,OAAOA,GAAGE,IAAI,CAAC,CAACC;YACd,OAAOJ,kBAAkBI,GAAGC,WAAWD;QACzC;IACF;IACA,IAAIH,IAAI;QACN,MAAM,EAAEK,SAAS,EAAE,GAAGL;QACtBM,OAAOC,MAAM,CAACN,YAAY;YAAEI;QAAU;IACxC;IACA,OAAO,MAAM,CAAC,MAAMH,IAAI,CAAC,CAACF,KAAOD,kBAAkBC;AACrD"}
|
package/lib/server/ws.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/server/ws.ts"],"sourcesContent":["export * from './polyfill/polyfillWebSocket';\n"],"names":[],"
|
|
1
|
+
{"version":3,"sources":["../../src/server/ws.ts"],"sourcesContent":["export * from './polyfill/polyfillWebSocket';\n"],"names":[],"mappings":"AAAA,cAAc,+BAA+B"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/strings/camelCase.ts"],"sourcesContent":["// https://github.com/sindresorhus/camelcase/blob/master/index.js\n\nfunction preserveCamelCase(string: string) {\n let isLastCharLower = false;\n let isLastCharUpper = false;\n let isLastLastCharUpper = false;\n\n for (let i = 0; i < string.length; i++) {\n const character = string[i];\n\n if (isLastCharLower && /[a-zA-Z]/.test(character) && character.toUpperCase() === character) {\n string = string.slice(0, i) + '-' + string.slice(i);\n isLastCharLower = false;\n isLastLastCharUpper = isLastCharUpper;\n isLastCharUpper = true;\n i++;\n } else if (\n isLastCharUpper &&\n isLastLastCharUpper &&\n /[a-zA-Z]/.test(character) &&\n character.toLowerCase() === character\n ) {\n string = string.slice(0, i - 1) + '-' + string.slice(i - 1);\n isLastLastCharUpper = isLastCharUpper;\n isLastCharUpper = false;\n isLastCharLower = true;\n } else {\n isLastCharLower = character.toLowerCase() === character && character.toUpperCase() !== character;\n isLastLastCharUpper = isLastCharUpper;\n isLastCharUpper = character.toUpperCase() === character && character.toLowerCase() !== character;\n }\n }\n\n return string;\n}\n\nexport function pascalCase(input: string | string[]) {\n return camelCase(input, { pascalCase: true });\n}\n\nexport function camelCase(\n input: string | string[],\n options: { pascalCase: boolean } = {\n pascalCase: false,\n },\n) {\n if (!(typeof input === 'string' || Array.isArray(input))) {\n throw new TypeError('Expected the input to be `string | string[]`');\n }\n\n const postProcess = (x: string) => (options.pascalCase ? x.charAt(0).toUpperCase() + x.slice(1) : x);\n\n if (Array.isArray(input)) {\n input = input\n .map((x) => x.trim())\n .filter((x) => x.length)\n .join('-');\n } else {\n input = input.trim();\n }\n\n if (input.length === 0) {\n return '';\n }\n\n if (input.length === 1) {\n return options.pascalCase ? input.toUpperCase() : input.toLowerCase();\n }\n\n const hasUpperCase = input !== input.toLowerCase();\n\n if (hasUpperCase) {\n input = preserveCamelCase(input);\n }\n\n input = input\n .replace(/^[_.\\- ]+/, '')\n .toLowerCase()\n .replace(/[_.\\- ]+(\\w|$)/g, (_, p1) => p1.toUpperCase())\n .replace(/\\d+(\\w|$)/g, (m) => m.toUpperCase());\n\n return postProcess(input);\n}\n"],"names":["preserveCamelCase","string","isLastCharLower","isLastCharUpper","isLastLastCharUpper","i","length","character","test","toUpperCase","slice","toLowerCase","pascalCase","input","camelCase","options","Array","isArray","TypeError","postProcess","x","charAt","map","trim","filter","join","hasUpperCase","replace","_","p1","m"],"
|
|
1
|
+
{"version":3,"sources":["../../src/strings/camelCase.ts"],"sourcesContent":["// https://github.com/sindresorhus/camelcase/blob/master/index.js\n\nfunction preserveCamelCase(string: string) {\n let isLastCharLower = false;\n let isLastCharUpper = false;\n let isLastLastCharUpper = false;\n\n for (let i = 0; i < string.length; i++) {\n const character = string[i];\n\n if (isLastCharLower && /[a-zA-Z]/.test(character) && character.toUpperCase() === character) {\n string = string.slice(0, i) + '-' + string.slice(i);\n isLastCharLower = false;\n isLastLastCharUpper = isLastCharUpper;\n isLastCharUpper = true;\n i++;\n } else if (\n isLastCharUpper &&\n isLastLastCharUpper &&\n /[a-zA-Z]/.test(character) &&\n character.toLowerCase() === character\n ) {\n string = string.slice(0, i - 1) + '-' + string.slice(i - 1);\n isLastLastCharUpper = isLastCharUpper;\n isLastCharUpper = false;\n isLastCharLower = true;\n } else {\n isLastCharLower = character.toLowerCase() === character && character.toUpperCase() !== character;\n isLastLastCharUpper = isLastCharUpper;\n isLastCharUpper = character.toUpperCase() === character && character.toLowerCase() !== character;\n }\n }\n\n return string;\n}\n\nexport function pascalCase(input: string | string[]) {\n return camelCase(input, { pascalCase: true });\n}\n\nexport function camelCase(\n input: string | string[],\n options: { pascalCase: boolean } = {\n pascalCase: false,\n },\n) {\n if (!(typeof input === 'string' || Array.isArray(input))) {\n throw new TypeError('Expected the input to be `string | string[]`');\n }\n\n const postProcess = (x: string) => (options.pascalCase ? x.charAt(0).toUpperCase() + x.slice(1) : x);\n\n if (Array.isArray(input)) {\n input = input\n .map((x) => x.trim())\n .filter((x) => x.length)\n .join('-');\n } else {\n input = input.trim();\n }\n\n if (input.length === 0) {\n return '';\n }\n\n if (input.length === 1) {\n return options.pascalCase ? input.toUpperCase() : input.toLowerCase();\n }\n\n const hasUpperCase = input !== input.toLowerCase();\n\n if (hasUpperCase) {\n input = preserveCamelCase(input);\n }\n\n input = input\n .replace(/^[_.\\- ]+/, '')\n .toLowerCase()\n .replace(/[_.\\- ]+(\\w|$)/g, (_, p1) => p1.toUpperCase())\n .replace(/\\d+(\\w|$)/g, (m) => m.toUpperCase());\n\n return postProcess(input);\n}\n"],"names":["preserveCamelCase","string","isLastCharLower","isLastCharUpper","isLastLastCharUpper","i","length","character","test","toUpperCase","slice","toLowerCase","pascalCase","input","camelCase","options","Array","isArray","TypeError","postProcess","x","charAt","map","trim","filter","join","hasUpperCase","replace","_","p1","m"],"mappings":"AAAA,iEAAiE;AAEjE,SAASA,kBAAkBC,MAAc;IACvC,IAAIC,kBAAkB;IACtB,IAAIC,kBAAkB;IACtB,IAAIC,sBAAsB;IAE1B,IAAK,IAAIC,IAAI,GAAGA,IAAIJ,OAAOK,MAAM,EAAED,IAAK;QACtC,MAAME,YAAYN,MAAM,CAACI,EAAE;QAE3B,IAAIH,mBAAmB,WAAWM,IAAI,CAACD,cAAcA,UAAUE,WAAW,OAAOF,WAAW;YAC1FN,SAASA,OAAOS,KAAK,CAAC,GAAGL,KAAK,MAAMJ,OAAOS,KAAK,CAACL;YACjDH,kBAAkB;YAClBE,sBAAsBD;YACtBA,kBAAkB;YAClBE;QACF,OAAO,IACLF,mBACAC,uBACA,WAAWI,IAAI,CAACD,cAChBA,UAAUI,WAAW,OAAOJ,WAC5B;YACAN,SAASA,OAAOS,KAAK,CAAC,GAAGL,IAAI,KAAK,MAAMJ,OAAOS,KAAK,CAACL,IAAI;YACzDD,sBAAsBD;YACtBA,kBAAkB;YAClBD,kBAAkB;QACpB,OAAO;YACLA,kBAAkBK,UAAUI,WAAW,OAAOJ,aAAaA,UAAUE,WAAW,OAAOF;YACvFH,sBAAsBD;YACtBA,kBAAkBI,UAAUE,WAAW,OAAOF,aAAaA,UAAUI,WAAW,OAAOJ;QACzF;IACF;IAEA,OAAON;AACT;AAEA,OAAO,SAASW,WAAWC,KAAwB;IACjD,OAAOC,UAAUD,OAAO;QAAED,YAAY;IAAK;AAC7C;AAEA,OAAO,SAASE,UACdD,KAAwB,EACxBE,UAAmC;IACjCH,YAAY;AACd,CAAC;IAED,IAAI,CAAE,CAAA,OAAOC,UAAU,YAAYG,MAAMC,OAAO,CAACJ,MAAK,GAAI;QACxD,MAAM,IAAIK,UAAU;IACtB;IAEA,MAAMC,cAAc,CAACC,IAAeL,QAAQH,UAAU,GAAGQ,EAAEC,MAAM,CAAC,GAAGZ,WAAW,KAAKW,EAAEV,KAAK,CAAC,KAAKU;IAElG,IAAIJ,MAAMC,OAAO,CAACJ,QAAQ;QACxBA,QAAQA,MACLS,GAAG,CAAC,CAACF,IAAMA,EAAEG,IAAI,IACjBC,MAAM,CAAC,CAACJ,IAAMA,EAAEd,MAAM,EACtBmB,IAAI,CAAC;IACV,OAAO;QACLZ,QAAQA,MAAMU,IAAI;IACpB;IAEA,IAAIV,MAAMP,MAAM,KAAK,GAAG;QACtB,OAAO;IACT;IAEA,IAAIO,MAAMP,MAAM,KAAK,GAAG;QACtB,OAAOS,QAAQH,UAAU,GAAGC,MAAMJ,WAAW,KAAKI,MAAMF,WAAW;IACrE;IAEA,MAAMe,eAAeb,UAAUA,MAAMF,WAAW;IAEhD,IAAIe,cAAc;QAChBb,QAAQb,kBAAkBa;IAC5B;IAEAA,QAAQA,MACLc,OAAO,CAAC,aAAa,IACrBhB,WAAW,GACXgB,OAAO,CAAC,mBAAmB,CAACC,GAAGC,KAAOA,GAAGpB,WAAW,IACpDkB,OAAO,CAAC,cAAc,CAACG,IAAMA,EAAErB,WAAW;IAE7C,OAAOU,YAAYN;AACrB"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/strings/formatBytes.ts"],"sourcesContent":["/**\n * Format bytes as human-readable text.\n *\n * @param bytes Number of bytes.\n * @param si True to use metric (SI) units, aka powers of 1000. False to use\n * binary (IEC), aka powers of 1024.\n * @param dp Number of decimal places to display.\n *\n * @return Formatted string.\n */\nexport function formatBytes(bytes: number, si = false, dp = 1) {\n const thresh = si ? 1000 : 1024;\n\n if (Math.abs(bytes) < thresh) {\n return `${bytes} B`;\n }\n\n const units = si\n ? ['kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']\n : ['KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB'];\n let u = -1;\n const r = 10 ** dp;\n\n do {\n bytes /= thresh;\n ++u;\n } while (Math.round(Math.abs(bytes) * r) / r >= thresh && u < units.length - 1);\n\n return bytes.toFixed(dp) + ' ' + units[u];\n}\n"],"names":["formatBytes","bytes","si","dp","thresh","Math","abs","units","u","r","round","length","toFixed"],"
|
|
1
|
+
{"version":3,"sources":["../../src/strings/formatBytes.ts"],"sourcesContent":["/**\n * Format bytes as human-readable text.\n *\n * @param bytes Number of bytes.\n * @param si True to use metric (SI) units, aka powers of 1000. False to use\n * binary (IEC), aka powers of 1024.\n * @param dp Number of decimal places to display.\n *\n * @return Formatted string.\n */\nexport function formatBytes(bytes: number, si = false, dp = 1) {\n const thresh = si ? 1000 : 1024;\n\n if (Math.abs(bytes) < thresh) {\n return `${bytes} B`;\n }\n\n const units = si\n ? ['kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']\n : ['KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB'];\n let u = -1;\n const r = 10 ** dp;\n\n do {\n bytes /= thresh;\n ++u;\n } while (Math.round(Math.abs(bytes) * r) / r >= thresh && u < units.length - 1);\n\n return bytes.toFixed(dp) + ' ' + units[u];\n}\n"],"names":["formatBytes","bytes","si","dp","thresh","Math","abs","units","u","r","round","length","toFixed"],"mappings":"AAAA;;;;;;;;;CASC,GACD,OAAO,SAASA,YAAYC,KAAa,EAAEC,KAAK,KAAK,EAAEC,KAAK,CAAC;IAC3D,MAAMC,SAASF,KAAK,OAAO;IAE3B,IAAIG,KAAKC,GAAG,CAACL,SAASG,QAAQ;QAC5B,OAAO,GAAGH,MAAM,EAAE,CAAC;IACrB;IAEA,MAAMM,QAAQL,KACV;QAAC;QAAM;QAAM;QAAM;QAAM;QAAM;QAAM;QAAM;KAAK,GAChD;QAAC;QAAO;QAAO;QAAO;QAAO;QAAO;QAAO;QAAO;KAAM;IAC5D,IAAIM,IAAI,CAAC;IACT,MAAMC,IAAI,MAAMN;IAEhB,GAAG;QACDF,SAASG;QACT,EAAEI;IACJ,QAASH,KAAKK,KAAK,CAACL,KAAKC,GAAG,CAACL,SAASQ,KAAKA,KAAKL,UAAUI,IAAID,MAAMI,MAAM,GAAG,EAAG;IAEhF,OAAOV,MAAMW,OAAO,CAACT,MAAM,MAAMI,KAAK,CAACC,EAAE;AAC3C"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/strings/renderTemplate.ts"],"sourcesContent":["import { get } from '../objects/get';\n\n/**\n * render template like js template string\n *\n * @example\n * renderTemplate('My name is ${name}',{name:'wener'})\n * // 'My name is wener'\n *\n * @param match `js` for `${name}`, common for `{{name}}`\n */\nexport function renderTemplate(\n template: string,\n data: ((v: string) => any) | object | undefined,\n match: 'js' | 'common' | RegExp = 'js',\n) {\n let getter: Function;\n if (!data) {\n // todo warning in dev\n getter = () => '';\n } else if (typeof data === 'function') {\n getter = data;\n } else {\n getter = (v: string) => get(data, v);\n }\n if (typeof match === 'string') {\n match = Matches[match] || Matches.js;\n }\n return template.replace(match, (_, g) => {\n return getter(g.trim());\n });\n}\n\nconst Matches: Record<string, RegExp> = {\n js: /\\${(.*?)}/g,\n common: /{{(.*?)}}/g,\n};\n"],"names":["get","renderTemplate","template","data","match","getter","v","Matches","js","replace","_","g","trim","common"],"
|
|
1
|
+
{"version":3,"sources":["../../src/strings/renderTemplate.ts"],"sourcesContent":["import { get } from '../objects/get';\n\n/**\n * render template like js template string\n *\n * @example\n * renderTemplate('My name is ${name}',{name:'wener'})\n * // 'My name is wener'\n *\n * @param match `js` for `${name}`, common for `{{name}}`\n */\nexport function renderTemplate(\n template: string,\n data: ((v: string) => any) | object | undefined,\n match: 'js' | 'common' | RegExp = 'js',\n) {\n let getter: Function;\n if (!data) {\n // todo warning in dev\n getter = () => '';\n } else if (typeof data === 'function') {\n getter = data;\n } else {\n getter = (v: string) => get(data, v);\n }\n if (typeof match === 'string') {\n match = Matches[match] || Matches.js;\n }\n return template.replace(match, (_, g) => {\n return getter(g.trim());\n });\n}\n\nconst Matches: Record<string, RegExp> = {\n js: /\\${(.*?)}/g,\n common: /{{(.*?)}}/g,\n};\n"],"names":["get","renderTemplate","template","data","match","getter","v","Matches","js","replace","_","g","trim","common"],"mappings":"AAAA,SAASA,GAAG,QAAQ,iBAAiB;AAErC;;;;;;;;CAQC,GACD,OAAO,SAASC,eACdC,QAAgB,EAChBC,IAA+C,EAC/CC,QAAkC,IAAI;IAEtC,IAAIC;IACJ,IAAI,CAACF,MAAM;QACT,sBAAsB;QACtBE,SAAS,IAAM;IACjB,OAAO,IAAI,OAAOF,SAAS,YAAY;QACrCE,SAASF;IACX,OAAO;QACLE,SAAS,CAACC,IAAcN,IAAIG,MAAMG;IACpC;IACA,IAAI,OAAOF,UAAU,UAAU;QAC7BA,QAAQG,OAAO,CAACH,MAAM,IAAIG,QAAQC,EAAE;IACtC;IACA,OAAON,SAASO,OAAO,CAACL,OAAO,CAACM,GAAGC;QACjC,OAAON,OAAOM,EAAEC,IAAI;IACtB;AACF;AAEA,MAAML,UAAkC;IACtCC,IAAI;IACJK,QAAQ;AACV"}
|
package/lib/typedoc.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/typedoc.ts"],"sourcesContent":["export * from './index';\n// fixme md5 conflict\n// export * from './server';\n"],"names":[],"
|
|
1
|
+
{"version":3,"sources":["../src/typedoc.ts"],"sourcesContent":["export * from './index';\n// fixme md5 conflict\n// export * from './server';\n"],"names":[],"mappings":"AAAA,cAAc,UAAU,CACxB,qBAAqB;CACrB,4BAA4B"}
|
package/lib/types.d.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/types.d.ts"],"sourcesContent":["declare var __DEV__: boolean;\n\nnamespace NodeJS {\n interface Process {\n // webpack check\n readonly browser?: boolean;\n }\n interface ProcessEnv {\n readonly NODE_ENV: 'development' | 'production' | 'test';\n }\n}\n\nexport type AbstractConstructor<T = {}> = abstract new (...args: any[]) => T;\nexport type Constructor<T = {}> = new (...args: any[]) => T;\nexport type Optional<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>;\nexport type PartialRequired<T, K extends keyof T> = Partial<Omit<T, K>> & Required<Pick<T, K>>;\n\n/*\nhttps://github.com/total-typescript/ts-reset/blob/main/src/entrypoints/filter-boolean.d.ts\nhttps://github.com/sindresorhus/type-fest\n */\n"],"names":[],"
|
|
1
|
+
{"version":3,"sources":["../src/types.d.ts"],"sourcesContent":["declare var __DEV__: boolean;\n\nnamespace NodeJS {\n interface Process {\n // webpack check\n readonly browser?: boolean;\n }\n interface ProcessEnv {\n readonly NODE_ENV: 'development' | 'production' | 'test';\n }\n}\n\nexport type AbstractConstructor<T = {}> = abstract new (...args: any[]) => T;\nexport type Constructor<T = {}> = new (...args: any[]) => T;\nexport type Optional<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>;\nexport type PartialRequired<T, K extends keyof T> = Partial<Omit<T, K>> & Required<Pick<T, K>>;\n\n/*\nhttps://github.com/total-typescript/ts-reset/blob/main/src/entrypoints/filter-boolean.d.ts\nhttps://github.com/sindresorhus/type-fest\n */\n"],"names":[],"mappings":"AAeA,WAA+F,CAE/F;;;CAGC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/validations/asserts.ts"],"sourcesContent":["export function assert(condition: any, msg?: string): asserts condition {\n // if (!condition) {\n // throw new AssertionError(msg);\n // }\n console.assert(!condition, msg);\n}\n\nexport function assertIsDefined<T>(val: T): asserts val is NonNullable<T> {\n // if (val === undefined || val === null) {\n // throw new AssertionError(`Expected 'val' to be defined, but received ${val}`);\n // }\n console.assert(val === undefined || val === null, 'Expected defined');\n}\n"],"names":["assert","condition","msg","console","assertIsDefined","val","undefined"],"
|
|
1
|
+
{"version":3,"sources":["../../src/validations/asserts.ts"],"sourcesContent":["export function assert(condition: any, msg?: string): asserts condition {\n // if (!condition) {\n // throw new AssertionError(msg);\n // }\n console.assert(!condition, msg);\n}\n\nexport function assertIsDefined<T>(val: T): asserts val is NonNullable<T> {\n // if (val === undefined || val === null) {\n // throw new AssertionError(`Expected 'val' to be defined, but received ${val}`);\n // }\n console.assert(val === undefined || val === null, 'Expected defined');\n}\n"],"names":["assert","condition","msg","console","assertIsDefined","val","undefined"],"mappings":"AAAA,OAAO,SAASA,OAAOC,SAAc,EAAEC,GAAY;IACjD,oBAAoB;IACpB,mCAAmC;IACnC,IAAI;IACJC,QAAQH,MAAM,CAAC,CAACC,WAAWC;AAC7B;AAEA,OAAO,SAASE,gBAAmBC,GAAM;IACvC,2CAA2C;IAC3C,mFAAmF;IACnF,IAAI;IACJF,QAAQH,MAAM,CAACK,QAAQC,aAAaD,QAAQ,MAAM;AACpD"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/validations/isUUID.ts"],"sourcesContent":["export function isUUID(str: string | undefined | null) {\n return Boolean(str) && /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i.test(String(str));\n}\n"],"names":["isUUID","str","Boolean","test","String"],"
|
|
1
|
+
{"version":3,"sources":["../../src/validations/isUUID.ts"],"sourcesContent":["export function isUUID(str: string | undefined | null) {\n return Boolean(str) && /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i.test(String(str));\n}\n"],"names":["isUUID","str","Boolean","test","String"],"mappings":"AAAA,OAAO,SAASA,OAAOC,GAA8B;IACnD,OAAOC,QAAQD,QAAQ,yEAAyEE,IAAI,CAACC,OAAOH;AAC9G"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/validations/parseTimestamp.ts"],"sourcesContent":["export function parseTimestamp(raw?: string | number | Date): Date | undefined {\n if (!raw) {\n return undefined;\n }\n if (raw instanceof Date) {\n return raw;\n }\n\n if (typeof raw === 'string' && /^[0-9.]+$/.test(raw)) {\n let n = parseFloat(raw);\n const len = Math.floor(n).toString().length;\n /*\n 最常见的是 10 位和 13 位\n 9999999999 - 10*9 - 1970-04-26\n 99999999999 - 11*9 - 1973\n 999999999999 - 12*9 - 2001\n */\n if (len <= 11) {\n n *= 1000;\n }\n return new Date(n);\n } else if (typeof raw === 'string') {\n const date = new Date(raw);\n if (!isNaN(+date)) {\n return date;\n }\n }\n throw new Error(`parseTimestamp: invalid \"${raw}\"`);\n}\n"],"names":["parseTimestamp","raw","undefined","Date","test","n","parseFloat","len","Math","floor","toString","length","date","isNaN","Error"],"
|
|
1
|
+
{"version":3,"sources":["../../src/validations/parseTimestamp.ts"],"sourcesContent":["export function parseTimestamp(raw?: string | number | Date): Date | undefined {\n if (!raw) {\n return undefined;\n }\n if (raw instanceof Date) {\n return raw;\n }\n\n if (typeof raw === 'string' && /^[0-9.]+$/.test(raw)) {\n let n = parseFloat(raw);\n const len = Math.floor(n).toString().length;\n /*\n 最常见的是 10 位和 13 位\n 9999999999 - 10*9 - 1970-04-26\n 99999999999 - 11*9 - 1973\n 999999999999 - 12*9 - 2001\n */\n if (len <= 11) {\n n *= 1000;\n }\n return new Date(n);\n } else if (typeof raw === 'string') {\n const date = new Date(raw);\n if (!isNaN(+date)) {\n return date;\n }\n }\n throw new Error(`parseTimestamp: invalid \"${raw}\"`);\n}\n"],"names":["parseTimestamp","raw","undefined","Date","test","n","parseFloat","len","Math","floor","toString","length","date","isNaN","Error"],"mappings":"AAAA,OAAO,SAASA,eAAeC,GAA4B;IACzD,IAAI,CAACA,KAAK;QACR,OAAOC;IACT;IACA,IAAID,eAAeE,MAAM;QACvB,OAAOF;IACT;IAEA,IAAI,OAAOA,QAAQ,YAAY,YAAYG,IAAI,CAACH,MAAM;QACpD,IAAII,IAAIC,WAAWL;QACnB,MAAMM,MAAMC,KAAKC,KAAK,CAACJ,GAAGK,QAAQ,GAAGC,MAAM;QAC3C;;;;;KAKC,GACD,IAAIJ,OAAO,IAAI;YACbF,KAAK;QACP;QACA,OAAO,IAAIF,KAAKE;IAClB,OAAO,IAAI,OAAOJ,QAAQ,UAAU;QAClC,MAAMW,OAAO,IAAIT,KAAKF;QACtB,IAAI,CAACY,MAAM,CAACD,OAAO;YACjB,OAAOA;QACT;IACF;IACA,MAAM,IAAIE,MAAM,CAAC,yBAAyB,EAAEb,IAAI,CAAC,CAAC;AACpD"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wener/utils",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.50",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Utils for daily use",
|
|
6
6
|
"repository": {
|
|
@@ -70,14 +70,14 @@
|
|
|
70
70
|
"lodash"
|
|
71
71
|
],
|
|
72
72
|
"devDependencies": {
|
|
73
|
-
"@sinclair/typebox": "^0.
|
|
74
|
-
"@types/lodash": "^4.17.
|
|
73
|
+
"@sinclair/typebox": "^0.33.15",
|
|
74
|
+
"@types/lodash": "^4.17.10",
|
|
75
75
|
"@types/ws": "^8.5.12",
|
|
76
76
|
"@wener/utils": "link:",
|
|
77
77
|
"https-proxy-agent": "^7.0.5",
|
|
78
78
|
"lodash": "^4.17.21",
|
|
79
79
|
"node-fetch": "^3.3.2",
|
|
80
|
-
"undici": "^6.
|
|
80
|
+
"undici": "^6.20.0",
|
|
81
81
|
"zod": "^3.23.8"
|
|
82
82
|
},
|
|
83
83
|
"publishConfig": {
|
package/src/asyncs/Promises.ts
CHANGED
|
@@ -40,8 +40,18 @@ export class Promises {
|
|
|
40
40
|
* Returns `true` if the given value is a Promise.
|
|
41
41
|
* @param v The value to check.
|
|
42
42
|
*/
|
|
43
|
-
static isPromise<T>(v: any): v is
|
|
44
|
-
return v && (v instanceof Promise ||
|
|
43
|
+
static isPromise<T>(v: any): v is PromiseLike<T> {
|
|
44
|
+
return v && (v instanceof Promise || typeof v.then === 'function');
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Returns a Promise that resolves when the given signal is aborted.
|
|
49
|
+
*/
|
|
50
|
+
static aborted(signal: AbortSignal): Promise<void> {
|
|
51
|
+
// https://nodejs.org/api/util.html#utilabortedsignal-resource
|
|
52
|
+
return new Promise((_, reject) => {
|
|
53
|
+
signal.addEventListener('abort', () => reject(new DOMException('Aborted', 'AbortError')));
|
|
54
|
+
});
|
|
45
55
|
}
|
|
46
56
|
}
|
|
47
57
|
|
|
@@ -1,18 +1,56 @@
|
|
|
1
|
-
import { expect, test } from 'vitest';
|
|
1
|
+
import { describe, expect, test } from 'vitest';
|
|
2
2
|
import { createLazyPromise } from './createLazyPromise';
|
|
3
3
|
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
4
|
+
describe('basic', async () => {
|
|
5
|
+
test('no executor', async () => {
|
|
6
|
+
const promise = createLazyPromise();
|
|
7
|
+
let r = 0;
|
|
8
|
+
void promise.then((v) => (r = v));
|
|
9
|
+
expect(r).toBe(0);
|
|
10
|
+
const { resolve, reject } = promise;
|
|
11
|
+
expect(resolve).toBeTruthy();
|
|
12
|
+
expect(reject).toBeTruthy();
|
|
13
|
+
|
|
14
|
+
// peek
|
|
15
|
+
expect(promise.value).toBeUndefined();
|
|
16
|
+
expect(promise.status).toBe('pending');
|
|
17
|
+
resolve(1);
|
|
18
|
+
expect(promise.value).toBe(1);
|
|
19
|
+
expect(promise.status).toBe('resolved');
|
|
20
|
+
|
|
21
|
+
expect(r).toBe(0);
|
|
22
|
+
|
|
23
|
+
await promise;
|
|
24
|
+
|
|
25
|
+
expect(r).toBe(1);
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
test('can resolve to lazy', async () => {
|
|
29
|
+
const a = createLazyPromise(() => {
|
|
30
|
+
return createLazyPromise(() => {
|
|
31
|
+
return 10;
|
|
32
|
+
});
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
expect(a.value).toBeUndefined();
|
|
36
|
+
expect(a.status).toBe('pending');
|
|
37
|
+
expect(await a).toBe(10);
|
|
38
|
+
expect(a.status).toBe('resolved');
|
|
39
|
+
expect(a.value).toBe(10);
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
test('can resolve to async lazy', async () => {
|
|
43
|
+
const a = createLazyPromise(async () => {
|
|
44
|
+
return createLazyPromise(async () => {
|
|
45
|
+
return 10;
|
|
46
|
+
});
|
|
47
|
+
});
|
|
48
|
+
expect(a.value).toBeUndefined();
|
|
49
|
+
expect(a.status).toBe('pending');
|
|
50
|
+
expect(await a).toBe(10);
|
|
51
|
+
expect(a.status).toBe('resolved');
|
|
52
|
+
expect(a.value).toBe(10);
|
|
53
|
+
});
|
|
16
54
|
});
|
|
17
55
|
|
|
18
56
|
test('manual resolve skip exec', async () => {
|
|
@@ -21,6 +59,7 @@ test('manual resolve skip exec', async () => {
|
|
|
21
59
|
});
|
|
22
60
|
promise.resolve(-1);
|
|
23
61
|
expect(await promise).toBe(-1);
|
|
62
|
+
expect(String(promise)).toBe('[object LazyPromise]');
|
|
24
63
|
});
|
|
25
64
|
|
|
26
65
|
test('lazy exec resolve by manual', async () => {
|
|
@@ -1,9 +1,13 @@
|
|
|
1
1
|
import { isPromise } from './isPromise';
|
|
2
2
|
import type { MaybePromise } from './MaybePromise';
|
|
3
|
+
import { Promises } from './Promises';
|
|
3
4
|
|
|
4
5
|
export type LazyPromise<T> = Promise<T> & {
|
|
5
6
|
reject(reason?: any): void;
|
|
6
7
|
resolve(v?: MaybePromise<T>): void;
|
|
8
|
+
readonly status: 'pending' | 'resolved' | 'rejected';
|
|
9
|
+
readonly value: T | undefined;
|
|
10
|
+
readonly error: any;
|
|
7
11
|
};
|
|
8
12
|
|
|
9
13
|
/**
|
|
@@ -11,77 +15,78 @@ export type LazyPromise<T> = Promise<T> & {
|
|
|
11
15
|
* if you pass a function to it, it will be executed when the promise try to resolve.
|
|
12
16
|
*/
|
|
13
17
|
export function createLazyPromise<T = any>(
|
|
14
|
-
executor?:
|
|
15
|
-
| ((resolve: LazyPromise<T>['resolve'], reject: LazyPromise<T>['reject']) => void)
|
|
16
|
-
| ((resolve: LazyPromise<T>['resolve'], reject: LazyPromise<T>['reject']) => MaybePromise<T>),
|
|
18
|
+
executor?: (resolve: LazyPromise<T>['resolve'], reject: LazyPromise<T>['reject']) => MaybePromise<T> | void,
|
|
17
19
|
): LazyPromise<T> {
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
}) as LazyPromise<T>;
|
|
24
|
-
}
|
|
20
|
+
const { promise, resolve, reject } = Promises.withResolvers();
|
|
21
|
+
const lazy = Object.assign(promise, {
|
|
22
|
+
resolve,
|
|
23
|
+
reject,
|
|
24
|
+
}) as LazyPromise<T>;
|
|
25
25
|
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
26
|
+
let executed = false;
|
|
27
|
+
let pending = true;
|
|
28
|
+
let status: 'pending' | 'resolved' | 'rejected' = 'pending';
|
|
29
|
+
let value: T | undefined;
|
|
30
|
+
let error: any;
|
|
31
|
+
|
|
32
|
+
const _resolve = (v: T) => {
|
|
33
|
+
resolve(v);
|
|
34
|
+
if (!pending) return;
|
|
35
|
+
pending = false;
|
|
36
|
+
// do not delay this for sync status peek
|
|
37
|
+
status = 'resolved';
|
|
38
|
+
value = v;
|
|
39
|
+
};
|
|
40
|
+
const _reject = (v: any) => {
|
|
41
|
+
reject(v);
|
|
42
|
+
if (!pending) return;
|
|
43
|
+
pending = false;
|
|
44
|
+
status = 'rejected';
|
|
45
|
+
error = v;
|
|
33
46
|
};
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
holder.reject = reject;
|
|
37
|
-
holder.resolve = resolve;
|
|
38
|
-
}),
|
|
47
|
+
|
|
48
|
+
const like = Object.assign(
|
|
39
49
|
{
|
|
40
|
-
|
|
41
|
-
|
|
50
|
+
get status() {
|
|
51
|
+
return status;
|
|
42
52
|
},
|
|
43
|
-
|
|
44
|
-
|
|
53
|
+
get value() {
|
|
54
|
+
return value;
|
|
55
|
+
},
|
|
56
|
+
get error() {
|
|
57
|
+
return error;
|
|
45
58
|
},
|
|
46
59
|
},
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
} else if (result !== undefined) {
|
|
66
|
-
holder.resolve(result);
|
|
60
|
+
{
|
|
61
|
+
cache: lazy.catch.bind(lazy),
|
|
62
|
+
finally: lazy.finally?.bind(lazy),
|
|
63
|
+
resolve: _resolve,
|
|
64
|
+
reject: _reject,
|
|
65
|
+
then: (...args: any[]) => {
|
|
66
|
+
if (executor && !executed) {
|
|
67
|
+
executed = true;
|
|
68
|
+
try {
|
|
69
|
+
const result = executor(_resolve, _reject);
|
|
70
|
+
// ensure resolve/reject is called
|
|
71
|
+
if (isPromise(result)) {
|
|
72
|
+
result.then(_resolve, _reject);
|
|
73
|
+
} else if (result !== undefined) {
|
|
74
|
+
_resolve(result);
|
|
75
|
+
}
|
|
76
|
+
} catch (e) {
|
|
77
|
+
_reject(e);
|
|
67
78
|
}
|
|
68
|
-
} catch (e) {
|
|
69
|
-
holder.reject(e);
|
|
70
79
|
}
|
|
71
|
-
|
|
72
|
-
return then(...args);
|
|
73
|
-
};
|
|
74
|
-
const like = holder as LazyPromise<T>;
|
|
75
|
-
like.then = future.then;
|
|
76
|
-
like.catch = future.catch.bind(future);
|
|
77
|
-
if (future.finally) like.finally = future.finally;
|
|
78
|
-
|
|
79
|
-
void Object.defineProperty(like, Symbol.species, {
|
|
80
|
-
get() {
|
|
81
|
-
return Promise;
|
|
80
|
+
return lazy.then(...args);
|
|
82
81
|
},
|
|
82
|
+
},
|
|
83
|
+
) as any as LazyPromise<T>;
|
|
84
|
+
|
|
85
|
+
if ('toStringTag' in Symbol) {
|
|
86
|
+
Object.defineProperty(like, Symbol.toStringTag, {
|
|
87
|
+
value: 'LazyPromise',
|
|
83
88
|
});
|
|
84
|
-
return like;
|
|
85
89
|
}
|
|
86
|
-
|
|
90
|
+
|
|
91
|
+
return like;
|
|
87
92
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { isPromise } from './isPromise';
|
|
2
|
-
import { MaybePromise } from './MaybePromise';
|
|
2
|
+
import type { MaybePromise } from './MaybePromise';
|
|
3
3
|
import { nextOfAsyncIterator } from './nextOfAsyncIterator';
|
|
4
4
|
|
|
5
5
|
export function firstOfAsyncIterator<T>(it: MaybePromise<AsyncIterator<T> | Iterator<T> | T>): MaybePromise<T> {
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { isIterator } from './isIterator';
|
|
2
2
|
import { isPromise } from './isPromise';
|
|
3
|
-
import { MaybePromise } from './MaybePromise';
|
|
3
|
+
import type { MaybePromise } from './MaybePromise';
|
|
4
4
|
|
|
5
5
|
type IteratorLike<T> = Iterable<T> | Iterator<T>;
|
|
6
6
|
type AsyncIteratorLike<T> = AsyncIterable<T> | AsyncIterator<T> | AsyncIterableIterator<T> | IteratorLike<T>;
|
package/src/asyncs/timeout.ts
CHANGED
|
@@ -1,17 +1,17 @@
|
|
|
1
1
|
export function timeout<T = any>(
|
|
2
|
-
|
|
2
|
+
pending: Promise<T> | ((args: { signal: AbortSignal }) => Promise<T>),
|
|
3
3
|
ms: number,
|
|
4
4
|
): Promise<T> {
|
|
5
5
|
const error = new TimeoutError();
|
|
6
6
|
let timeout: any;
|
|
7
7
|
let ac: AbortController | undefined;
|
|
8
|
-
if (typeof
|
|
8
|
+
if (typeof pending === 'function') {
|
|
9
9
|
ac = new AbortController();
|
|
10
|
-
|
|
10
|
+
pending = pending({ signal: ac.signal });
|
|
11
11
|
}
|
|
12
12
|
|
|
13
13
|
return Promise.race<T>([
|
|
14
|
-
|
|
14
|
+
pending,
|
|
15
15
|
new Promise((_resolve, reject) => {
|
|
16
16
|
timeout = setTimeout(() => {
|
|
17
17
|
ac?.abort(error);
|
package/src/cn/types.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export interface StringFormat<GenerateOptions, ParsedObject> {
|
|
2
|
+
regex?: RegExp;
|
|
3
|
+
validate: (s: string) => boolean;
|
|
4
|
+
generate: (opts?: GenerateOptions) => ParsedObject;
|
|
5
|
+
parse: (s: string) => ParsedObject | undefined;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export interface StringChecksum {
|
|
9
|
+
validate: (s: string) => boolean;
|
|
10
|
+
generate: (s: string) => string;
|
|
11
|
+
}
|
package/src/errors/Errors.ts
CHANGED
|
@@ -152,26 +152,47 @@ export interface ErrorDetail {
|
|
|
152
152
|
check<T>(v: T | undefined | null, o?: Partial<ErrorDetailInit> | string): asserts v is NonNullable<T>;
|
|
153
153
|
}
|
|
154
154
|
|
|
155
|
-
export
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
155
|
+
export namespace Errors {
|
|
156
|
+
/*
|
|
157
|
+
https://github.com/tc39/proposal-error-cause
|
|
158
|
+
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/cause
|
|
159
|
+
Stage 3
|
|
160
|
+
Chrome 93, Safari 15, Node 16.9
|
|
161
|
+
|
|
162
|
+
https://www.npmjs.com/package/pony-cause
|
|
163
|
+
*/
|
|
164
|
+
|
|
165
|
+
export const BadRequest: ErrorDetail = create({ status: 400 });
|
|
166
|
+
export const Unauthorized: ErrorDetail = create({ status: 403 });
|
|
167
|
+
export const Forbidden: ErrorDetail = create({ status: 403 });
|
|
168
|
+
export const NotFound: ErrorDetail = create({ status: 404 });
|
|
169
|
+
export const InternalServerError: ErrorDetail = create({ status: 500 });
|
|
170
|
+
export const NotImplemented: ErrorDetail = create({ status: 501 });
|
|
171
|
+
export const ServiceUnavailable: ErrorDetail = create({ status: 503 });
|
|
172
|
+
|
|
173
|
+
// known errors
|
|
174
|
+
// TypeError when an operation could not be performed, typically (but not exclusively) when a value is not of the expected type.
|
|
175
|
+
// RangeError when a number is not within the correct range allowed.
|
|
176
|
+
// AggregateError when multiple errors need to be reported by an operation, for example by Promise.all().
|
|
177
|
+
// 例如 Promise.any() 会返回一个 AggregateError,其中包含所有 rejected 的 Promise 的错误。
|
|
178
|
+
// EvalError when an error occurs during the evaluation of JavaScript code.
|
|
179
|
+
// ReferenceError when a non-existent variable is referenced.
|
|
180
|
+
// SyntaxError when a syntax error occurs while parsing code in eval().
|
|
181
|
+
// URIError when encodeURI() or decodeURI() are passed invalid parameters.
|
|
182
|
+
// InternalError when an internal error in the JavaScript engine is thrown. E.g. "too much recursion".
|
|
183
|
+
// DOMException when an error occurs in the DOM.
|
|
184
|
+
|
|
185
|
+
export const resolvers: ((e: any) => ErrorDetail | void)[] = [];
|
|
186
|
+
|
|
187
|
+
export function create(init: ErrorDetailInit): ErrorDetail {
|
|
167
188
|
return new DetailHolder(init);
|
|
168
189
|
}
|
|
169
190
|
|
|
170
|
-
|
|
191
|
+
export function ok(res: Response, o?: Partial<ErrorDetailInit>) {
|
|
171
192
|
if (res.ok) {
|
|
172
193
|
return res;
|
|
173
194
|
}
|
|
174
|
-
throw
|
|
195
|
+
throw create({
|
|
175
196
|
description: res.statusText,
|
|
176
197
|
status: res.status,
|
|
177
198
|
...o,
|
|
@@ -182,7 +203,7 @@ export class Errors {
|
|
|
182
203
|
}).asError();
|
|
183
204
|
}
|
|
184
205
|
|
|
185
|
-
|
|
206
|
+
export function resolve(e: any): ErrorDetail {
|
|
186
207
|
if (e instanceof DetailHolder) {
|
|
187
208
|
return e;
|
|
188
209
|
}
|
|
@@ -191,7 +212,7 @@ export class Errors {
|
|
|
191
212
|
return e.detail;
|
|
192
213
|
}
|
|
193
214
|
|
|
194
|
-
for (const resolver of
|
|
215
|
+
for (const resolver of resolvers) {
|
|
195
216
|
const r = resolver(e);
|
|
196
217
|
if (r) {
|
|
197
218
|
return r;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { getGlobalThis } from '../runtime/getGlobalThis';
|
|
2
|
-
import { FetchLike } from './types';
|
|
2
|
+
import type { FetchLike } from './types';
|
|
3
3
|
|
|
4
4
|
type RequestDelayFunction = (attempt: number, error: Error | null, response: Response | null) => number;
|
|
5
5
|
type RequestRetryOnFunction = (
|
package/src/index.ts
CHANGED
|
@@ -47,6 +47,8 @@ export { maybeFunction, type MaybeFunction } from './langs/MaybeFunction';
|
|
|
47
47
|
export { memoize } from './langs/memoize';
|
|
48
48
|
export { mixin } from './langs/mixin';
|
|
49
49
|
export type { MixinFunction, MixinInstance, MixinReturnValue } from './langs/mixin';
|
|
50
|
+
export { getObjectId } from './langs/getObjectId';
|
|
51
|
+
export { getGlobalStates, setGlobalStates } from './langs/getGlobalStates';
|
|
50
52
|
|
|
51
53
|
export { AsyncCloser } from './runtime/AsyncCloser';
|
|
52
54
|
export { Closer } from './runtime/Closer';
|
|
@@ -84,3 +84,10 @@ test('toBase64', () => {
|
|
|
84
84
|
const buf = ArrayBuffers.from(text, 'utf8', Uint8Array);
|
|
85
85
|
expect(ArrayBuffers.toBase64(buf)).toEqual('YW9ldQ==');
|
|
86
86
|
});
|
|
87
|
+
|
|
88
|
+
test('base64: utf8 string', () => {
|
|
89
|
+
const text = '你好';
|
|
90
|
+
// latin1 only
|
|
91
|
+
expect(() => btoa(text)).toThrow('Invalid character');
|
|
92
|
+
expect(ArrayBuffers.toBase64(text)).toEqual('5L2g5aW9');
|
|
93
|
+
});
|