@luckylillia-dev/luckylillia 0.0.0-stage → 9.0.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/chunks/preload-helper-D5R8FHQt.js +342 -0
- package/chunks/preload-helper-D5R8FHQt.js.map +1 -0
- package/default_config.json +83 -0
- package/llbot-core.js +35013 -0
- package/llbot-core.js.map +1 -0
- package/llbot.js +138 -0
- package/llbot.js.map +1 -0
- package/node_modules/@luckylillia-dev/plugin-sdk/lib/commands.d.ts +86 -0
- package/node_modules/@luckylillia-dev/plugin-sdk/lib/commands.js +1 -0
- package/node_modules/@luckylillia-dev/plugin-sdk/lib/config.d.ts +14 -0
- package/node_modules/@luckylillia-dev/plugin-sdk/lib/config.js +1 -0
- package/node_modules/@luckylillia-dev/plugin-sdk/lib/ffmpeg.d.ts +41 -0
- package/node_modules/@luckylillia-dev/plugin-sdk/lib/ffmpeg.js +1 -0
- package/node_modules/@luckylillia-dev/plugin-sdk/lib/http.d.ts +88 -0
- package/node_modules/@luckylillia-dev/plugin-sdk/lib/http.js +1 -0
- package/node_modules/@luckylillia-dev/plugin-sdk/lib/index.d.ts +17 -0
- package/node_modules/@luckylillia-dev/plugin-sdk/lib/index.js +6 -0
- package/node_modules/@luckylillia-dev/plugin-sdk/lib/internal.d.ts +430 -0
- package/node_modules/@luckylillia-dev/plugin-sdk/lib/internal.js +7 -0
- package/node_modules/@luckylillia-dev/plugin-sdk/lib/manifest.d.ts +68 -0
- package/node_modules/@luckylillia-dev/plugin-sdk/lib/manifest.js +1 -0
- package/node_modules/@luckylillia-dev/plugin-sdk/lib/server.d.ts +23 -0
- package/node_modules/@luckylillia-dev/plugin-sdk/lib/server.js +1 -0
- package/node_modules/@luckylillia-dev/plugin-sdk/lib/storage.d.ts +21 -0
- package/node_modules/@luckylillia-dev/plugin-sdk/lib/storage.js +1 -0
- package/node_modules/@luckylillia-dev/plugin-sdk/lib/testing.d.ts +71 -0
- package/node_modules/@luckylillia-dev/plugin-sdk/lib/testing.js +1123 -0
- package/node_modules/@luckylillia-dev/plugin-sdk/lib/universal.d.ts +299 -0
- package/node_modules/@luckylillia-dev/plugin-sdk/lib/universal.js +1 -0
- package/node_modules/@luckylillia-dev/plugin-sdk/lib/webui.d.ts +762 -0
- package/node_modules/@luckylillia-dev/plugin-sdk/lib/webui.js +9 -0
- package/node_modules/@luckylillia-dev/plugin-sdk/package.json +63 -0
- package/node_modules/@satorijs/element/jsx-dev-runtime.d.ts +3 -0
- package/node_modules/@satorijs/element/jsx-runtime.d.ts +3 -0
- package/node_modules/@satorijs/element/lib/index.cjs +447 -0
- package/node_modules/@satorijs/element/lib/index.d.ts +116 -0
- package/node_modules/@satorijs/element/lib/index.mjs +455 -0
- package/node_modules/@satorijs/element/package.json +60 -0
- package/node_modules/@satorijs/element/src/index.ts +559 -0
- package/node_modules/@satorijs/protocol/lib/index.cjs +187 -0
- package/node_modules/@satorijs/protocol/lib/index.d.ts +328 -0
- package/node_modules/@satorijs/protocol/lib/index.mjs +148 -0
- package/node_modules/@satorijs/protocol/package.json +44 -0
- package/node_modules/@satorijs/protocol/src/index.ts +513 -0
- package/node_modules/@standard-schema/spec/LICENSE +21 -0
- package/node_modules/@standard-schema/spec/README.md +198 -0
- package/node_modules/@standard-schema/spec/dist/index.cjs +18 -0
- package/node_modules/@standard-schema/spec/dist/index.d.cts +119 -0
- package/node_modules/@standard-schema/spec/dist/index.d.ts +119 -0
- package/node_modules/@standard-schema/spec/dist/index.js +0 -0
- package/node_modules/@standard-schema/spec/package.json +52 -0
- package/node_modules/cordis/README.md +10 -0
- package/node_modules/cordis/bin.js +16 -0
- package/node_modules/cordis/lib/context.d.ts +32 -0
- package/node_modules/cordis/lib/events.d.ts +62 -0
- package/node_modules/cordis/lib/fiber.d.ts +85 -0
- package/node_modules/cordis/lib/index.d.ts +7 -0
- package/node_modules/cordis/lib/index.js +1577 -0
- package/node_modules/cordis/lib/logger.d.ts +74 -0
- package/node_modules/cordis/lib/reflect.d.ts +54 -0
- package/node_modules/cordis/lib/registry.d.ts +78 -0
- package/node_modules/cordis/lib/service.d.ts +19 -0
- package/node_modules/cordis/lib/utils.d.ts +50 -0
- package/node_modules/cordis/package.json +49 -0
- package/node_modules/cosmokit/LICENSE +21 -0
- package/node_modules/cosmokit/README.md +24 -0
- package/node_modules/cosmokit/lib/index.cjs +419 -0
- package/node_modules/cosmokit/lib/index.cjs.map +6 -0
- package/node_modules/cosmokit/lib/index.d.ts +142 -0
- package/node_modules/cosmokit/lib/index.mjs +357 -0
- package/node_modules/cosmokit/lib/index.mjs.map +6 -0
- package/node_modules/cosmokit/package.json +57 -0
- package/node_modules/cosmokit/src/array.ts +35 -0
- package/node_modules/cosmokit/src/index.ts +5 -0
- package/node_modules/cosmokit/src/misc.ts +57 -0
- package/node_modules/cosmokit/src/string.ts +101 -0
- package/node_modules/cosmokit/src/time.ts +91 -0
- package/node_modules/cosmokit/src/types.ts +131 -0
- package/node_modules/schemastery/README.md +342 -0
- package/node_modules/schemastery/lib/index.cjs +648 -0
- package/node_modules/schemastery/lib/index.cjs.map +6 -0
- package/node_modules/schemastery/lib/index.d.ts +138 -0
- package/node_modules/schemastery/lib/index.mjs +656 -0
- package/node_modules/schemastery/lib/index.mjs.map +6 -0
- package/node_modules/schemastery/package.json +32 -0
- package/node_modules/schemastery/src/index.ts +841 -0
- package/node_modules/undici/LICENSE +21 -0
- package/node_modules/undici/README.md +741 -0
- package/node_modules/undici/docs/docs/api/Agent.md +84 -0
- package/node_modules/undici/docs/docs/api/BalancedPool.md +99 -0
- package/node_modules/undici/docs/docs/api/CacheStorage.md +30 -0
- package/node_modules/undici/docs/docs/api/CacheStore.md +164 -0
- package/node_modules/undici/docs/docs/api/Client.md +290 -0
- package/node_modules/undici/docs/docs/api/ClientStats.md +27 -0
- package/node_modules/undici/docs/docs/api/Connector.md +115 -0
- package/node_modules/undici/docs/docs/api/ContentType.md +57 -0
- package/node_modules/undici/docs/docs/api/Cookies.md +128 -0
- package/node_modules/undici/docs/docs/api/Debug.md +62 -0
- package/node_modules/undici/docs/docs/api/DiagnosticsChannel.md +317 -0
- package/node_modules/undici/docs/docs/api/Dispatcher.md +1393 -0
- package/node_modules/undici/docs/docs/api/EnvHttpProxyAgent.md +159 -0
- package/node_modules/undici/docs/docs/api/Errors.md +49 -0
- package/node_modules/undici/docs/docs/api/EventSource.md +45 -0
- package/node_modules/undici/docs/docs/api/Fetch.md +60 -0
- package/node_modules/undici/docs/docs/api/GlobalInstallation.md +139 -0
- package/node_modules/undici/docs/docs/api/H2CClient.md +263 -0
- package/node_modules/undici/docs/docs/api/MockAgent.md +603 -0
- package/node_modules/undici/docs/docs/api/MockCallHistory.md +197 -0
- package/node_modules/undici/docs/docs/api/MockCallHistoryLog.md +43 -0
- package/node_modules/undici/docs/docs/api/MockClient.md +81 -0
- package/node_modules/undici/docs/docs/api/MockErrors.md +12 -0
- package/node_modules/undici/docs/docs/api/MockPool.md +555 -0
- package/node_modules/undici/docs/docs/api/Pool.md +84 -0
- package/node_modules/undici/docs/docs/api/PoolStats.md +35 -0
- package/node_modules/undici/docs/docs/api/ProxyAgent.md +229 -0
- package/node_modules/undici/docs/docs/api/RedirectHandler.md +93 -0
- package/node_modules/undici/docs/docs/api/RetryAgent.md +50 -0
- package/node_modules/undici/docs/docs/api/RetryHandler.md +118 -0
- package/node_modules/undici/docs/docs/api/RoundRobinPool.md +145 -0
- package/node_modules/undici/docs/docs/api/SnapshotAgent.md +616 -0
- package/node_modules/undici/docs/docs/api/Socks5ProxyAgent.md +275 -0
- package/node_modules/undici/docs/docs/api/Util.md +25 -0
- package/node_modules/undici/docs/docs/api/WebSocket.md +141 -0
- package/node_modules/undici/docs/docs/api/api-lifecycle.md +91 -0
- package/node_modules/undici/docs/docs/best-practices/client-certificate.md +64 -0
- package/node_modules/undici/docs/docs/best-practices/crawling.md +58 -0
- package/node_modules/undici/docs/docs/best-practices/mocking-request.md +190 -0
- package/node_modules/undici/docs/docs/best-practices/proxy.md +127 -0
- package/node_modules/undici/docs/docs/best-practices/undici-vs-builtin-fetch.md +224 -0
- package/node_modules/undici/docs/docs/best-practices/writing-tests.md +63 -0
- package/node_modules/undici/index-fetch.js +65 -0
- package/node_modules/undici/index.d.ts +3 -0
- package/node_modules/undici/index.js +234 -0
- package/node_modules/undici/lib/api/abort-signal.js +59 -0
- package/node_modules/undici/lib/api/api-connect.js +110 -0
- package/node_modules/undici/lib/api/api-pipeline.js +252 -0
- package/node_modules/undici/lib/api/api-request.js +214 -0
- package/node_modules/undici/lib/api/api-stream.js +209 -0
- package/node_modules/undici/lib/api/api-upgrade.js +111 -0
- package/node_modules/undici/lib/api/index.js +7 -0
- package/node_modules/undici/lib/api/readable.js +580 -0
- package/node_modules/undici/lib/cache/memory-cache-store.js +279 -0
- package/node_modules/undici/lib/cache/sqlite-cache-store.js +467 -0
- package/node_modules/undici/lib/core/connect.js +159 -0
- package/node_modules/undici/lib/core/constants.js +143 -0
- package/node_modules/undici/lib/core/diagnostics.js +227 -0
- package/node_modules/undici/lib/core/errors.js +477 -0
- package/node_modules/undici/lib/core/request.js +485 -0
- package/node_modules/undici/lib/core/socks5-client.js +422 -0
- package/node_modules/undici/lib/core/socks5-utils.js +212 -0
- package/node_modules/undici/lib/core/symbols.js +76 -0
- package/node_modules/undici/lib/core/tree.js +160 -0
- package/node_modules/undici/lib/core/util.js +992 -0
- package/node_modules/undici/lib/dispatcher/agent.js +162 -0
- package/node_modules/undici/lib/dispatcher/balanced-pool.js +221 -0
- package/node_modules/undici/lib/dispatcher/client-h1.js +1744 -0
- package/node_modules/undici/lib/dispatcher/client-h2.js +1056 -0
- package/node_modules/undici/lib/dispatcher/client.js +669 -0
- package/node_modules/undici/lib/dispatcher/dispatcher-base.js +196 -0
- package/node_modules/undici/lib/dispatcher/dispatcher.js +48 -0
- package/node_modules/undici/lib/dispatcher/env-http-proxy-agent.js +146 -0
- package/node_modules/undici/lib/dispatcher/fixed-queue.js +135 -0
- package/node_modules/undici/lib/dispatcher/h2c-client.js +51 -0
- package/node_modules/undici/lib/dispatcher/pool-base.js +214 -0
- package/node_modules/undici/lib/dispatcher/pool.js +118 -0
- package/node_modules/undici/lib/dispatcher/proxy-agent.js +319 -0
- package/node_modules/undici/lib/dispatcher/retry-agent.js +35 -0
- package/node_modules/undici/lib/dispatcher/round-robin-pool.js +137 -0
- package/node_modules/undici/lib/dispatcher/socks5-proxy-agent.js +260 -0
- package/node_modules/undici/lib/encoding/index.js +33 -0
- package/node_modules/undici/lib/global.js +59 -0
- package/node_modules/undici/lib/handler/cache-handler.js +824 -0
- package/node_modules/undici/lib/handler/cache-revalidation-handler.js +124 -0
- package/node_modules/undici/lib/handler/decorator-handler.js +67 -0
- package/node_modules/undici/lib/handler/deduplication-handler.js +504 -0
- package/node_modules/undici/lib/handler/redirect-handler.js +245 -0
- package/node_modules/undici/lib/handler/retry-handler.js +504 -0
- package/node_modules/undici/lib/handler/unwrap-handler.js +106 -0
- package/node_modules/undici/lib/handler/wrap-handler.js +105 -0
- package/node_modules/undici/lib/interceptor/cache.js +621 -0
- package/node_modules/undici/lib/interceptor/decompress.js +561 -0
- package/node_modules/undici/lib/interceptor/deduplicate.js +117 -0
- package/node_modules/undici/lib/interceptor/dns.js +571 -0
- package/node_modules/undici/lib/interceptor/dump.js +99 -0
- package/node_modules/undici/lib/interceptor/redirect.js +21 -0
- package/node_modules/undici/lib/interceptor/response-error.js +95 -0
- package/node_modules/undici/lib/interceptor/retry.js +19 -0
- package/node_modules/undici/lib/llhttp/constants.d.ts +195 -0
- package/node_modules/undici/lib/llhttp/constants.js +531 -0
- package/node_modules/undici/lib/llhttp/llhttp-wasm.js +15 -0
- package/node_modules/undici/lib/llhttp/llhttp_simd-wasm.js +15 -0
- package/node_modules/undici/lib/llhttp/utils.d.ts +2 -0
- package/node_modules/undici/lib/llhttp/utils.js +12 -0
- package/node_modules/undici/lib/mock/mock-agent.js +232 -0
- package/node_modules/undici/lib/mock/mock-call-history.js +248 -0
- package/node_modules/undici/lib/mock/mock-client.js +68 -0
- package/node_modules/undici/lib/mock/mock-errors.js +29 -0
- package/node_modules/undici/lib/mock/mock-interceptor.js +209 -0
- package/node_modules/undici/lib/mock/mock-pool.js +68 -0
- package/node_modules/undici/lib/mock/mock-symbols.js +32 -0
- package/node_modules/undici/lib/mock/mock-utils.js +486 -0
- package/node_modules/undici/lib/mock/pending-interceptors-formatter.js +43 -0
- package/node_modules/undici/lib/mock/snapshot-agent.js +353 -0
- package/node_modules/undici/lib/mock/snapshot-recorder.js +588 -0
- package/node_modules/undici/lib/mock/snapshot-utils.js +158 -0
- package/node_modules/undici/lib/util/cache.js +676 -0
- package/node_modules/undici/lib/util/date.js +670 -0
- package/node_modules/undici/lib/util/promise.js +28 -0
- package/node_modules/undici/lib/util/runtime-features.js +124 -0
- package/node_modules/undici/lib/util/stats.js +32 -0
- package/node_modules/undici/lib/util/timers.js +425 -0
- package/node_modules/undici/lib/web/cache/cache.js +864 -0
- package/node_modules/undici/lib/web/cache/cachestorage.js +152 -0
- package/node_modules/undici/lib/web/cache/util.js +45 -0
- package/node_modules/undici/lib/web/cookies/constants.js +12 -0
- package/node_modules/undici/lib/web/cookies/index.js +201 -0
- package/node_modules/undici/lib/web/cookies/parse.js +315 -0
- package/node_modules/undici/lib/web/cookies/util.js +371 -0
- package/node_modules/undici/lib/web/eventsource/eventsource-stream.js +575 -0
- package/node_modules/undici/lib/web/eventsource/eventsource.js +504 -0
- package/node_modules/undici/lib/web/eventsource/util.js +29 -0
- package/node_modules/undici/lib/web/fetch/LICENSE +21 -0
- package/node_modules/undici/lib/web/fetch/body.js +509 -0
- package/node_modules/undici/lib/web/fetch/constants.js +131 -0
- package/node_modules/undici/lib/web/fetch/data-url.js +596 -0
- package/node_modules/undici/lib/web/fetch/formdata-parser.js +586 -0
- package/node_modules/undici/lib/web/fetch/formdata.js +259 -0
- package/node_modules/undici/lib/web/fetch/global.js +40 -0
- package/node_modules/undici/lib/web/fetch/headers.js +719 -0
- package/node_modules/undici/lib/web/fetch/index.js +2413 -0
- package/node_modules/undici/lib/web/fetch/request.js +1115 -0
- package/node_modules/undici/lib/web/fetch/response.js +641 -0
- package/node_modules/undici/lib/web/fetch/util.js +1522 -0
- package/node_modules/undici/lib/web/infra/index.js +243 -0
- package/node_modules/undici/lib/web/subresource-integrity/Readme.md +9 -0
- package/node_modules/undici/lib/web/subresource-integrity/subresource-integrity.js +307 -0
- package/node_modules/undici/lib/web/webidl/index.js +1006 -0
- package/node_modules/undici/lib/web/websocket/connection.js +339 -0
- package/node_modules/undici/lib/web/websocket/constants.js +126 -0
- package/node_modules/undici/lib/web/websocket/events.js +331 -0
- package/node_modules/undici/lib/web/websocket/frame.js +133 -0
- package/node_modules/undici/lib/web/websocket/permessage-deflate.js +105 -0
- package/node_modules/undici/lib/web/websocket/receiver.js +520 -0
- package/node_modules/undici/lib/web/websocket/sender.js +109 -0
- package/node_modules/undici/lib/web/websocket/stream/websocketerror.js +104 -0
- package/node_modules/undici/lib/web/websocket/stream/websocketstream.js +496 -0
- package/node_modules/undici/lib/web/websocket/util.js +347 -0
- package/node_modules/undici/lib/web/websocket/websocket.js +758 -0
- package/node_modules/undici/package.json +152 -0
- package/node_modules/undici/scripts/strip-comments.js +10 -0
- package/node_modules/undici/types/README.md +6 -0
- package/node_modules/undici/types/agent.d.ts +32 -0
- package/node_modules/undici/types/api.d.ts +43 -0
- package/node_modules/undici/types/balanced-pool.d.ts +30 -0
- package/node_modules/undici/types/cache-interceptor.d.ts +179 -0
- package/node_modules/undici/types/cache.d.ts +36 -0
- package/node_modules/undici/types/client-stats.d.ts +15 -0
- package/node_modules/undici/types/client.d.ts +153 -0
- package/node_modules/undici/types/connector.d.ts +36 -0
- package/node_modules/undici/types/content-type.d.ts +21 -0
- package/node_modules/undici/types/cookies.d.ts +30 -0
- package/node_modules/undici/types/diagnostics-channel.d.ts +74 -0
- package/node_modules/undici/types/dispatcher.d.ts +275 -0
- package/node_modules/undici/types/env-http-proxy-agent.d.ts +22 -0
- package/node_modules/undici/types/errors.d.ts +177 -0
- package/node_modules/undici/types/eventsource.d.ts +66 -0
- package/node_modules/undici/types/fetch.d.ts +231 -0
- package/node_modules/undici/types/formdata.d.ts +114 -0
- package/node_modules/undici/types/global-dispatcher.d.ts +9 -0
- package/node_modules/undici/types/global-origin.d.ts +7 -0
- package/node_modules/undici/types/h2c-client.d.ts +73 -0
- package/node_modules/undici/types/handlers.d.ts +14 -0
- package/node_modules/undici/types/header.d.ts +160 -0
- package/node_modules/undici/types/index.d.ts +91 -0
- package/node_modules/undici/types/interceptors.d.ts +82 -0
- package/node_modules/undici/types/mock-agent.d.ts +68 -0
- package/node_modules/undici/types/mock-call-history.d.ts +111 -0
- package/node_modules/undici/types/mock-client.d.ts +27 -0
- package/node_modules/undici/types/mock-errors.d.ts +12 -0
- package/node_modules/undici/types/mock-interceptor.d.ts +94 -0
- package/node_modules/undici/types/mock-pool.d.ts +27 -0
- package/node_modules/undici/types/patch.d.ts +29 -0
- package/node_modules/undici/types/pool-stats.d.ts +19 -0
- package/node_modules/undici/types/pool.d.ts +41 -0
- package/node_modules/undici/types/proxy-agent.d.ts +29 -0
- package/node_modules/undici/types/readable.d.ts +68 -0
- package/node_modules/undici/types/retry-agent.d.ts +8 -0
- package/node_modules/undici/types/retry-handler.d.ts +125 -0
- package/node_modules/undici/types/round-robin-pool.d.ts +41 -0
- package/node_modules/undici/types/snapshot-agent.d.ts +109 -0
- package/node_modules/undici/types/socks5-proxy-agent.d.ts +25 -0
- package/node_modules/undici/types/util.d.ts +18 -0
- package/node_modules/undici/types/utility.d.ts +7 -0
- package/node_modules/undici/types/webidl.d.ts +347 -0
- package/node_modules/undici/types/websocket.d.ts +188 -0
- package/node_modules/ws/LICENSE +20 -0
- package/node_modules/ws/README.md +548 -0
- package/node_modules/ws/browser.js +8 -0
- package/node_modules/ws/index.js +22 -0
- package/node_modules/ws/lib/buffer-util.js +131 -0
- package/node_modules/ws/lib/constants.js +19 -0
- package/node_modules/ws/lib/event-target.js +292 -0
- package/node_modules/ws/lib/extension.js +203 -0
- package/node_modules/ws/lib/limiter.js +55 -0
- package/node_modules/ws/lib/permessage-deflate.js +530 -0
- package/node_modules/ws/lib/receiver.js +743 -0
- package/node_modules/ws/lib/sender.js +607 -0
- package/node_modules/ws/lib/stream.js +161 -0
- package/node_modules/ws/lib/subprotocol.js +62 -0
- package/node_modules/ws/lib/validation.js +152 -0
- package/node_modules/ws/lib/websocket-server.js +562 -0
- package/node_modules/ws/lib/websocket.js +1407 -0
- package/node_modules/ws/package.json +74 -0
- package/node_modules/ws/wrapper.mjs +21 -0
- package/package.json +17 -4
- package/webui/assets/group-bot-profile-CYK81678.jpg +0 -0
- package/webui/assets/group-bot-settings-BBQBbfgh.jpg +0 -0
- package/webui/assets/index-CuH0UYYo.js +28 -0
- package/webui/assets/index-DjbPiYmB.css +2 -0
- package/webui/favicon.ico +0 -0
- package/webui/index.html +26 -0
- package/webui/logo-128.webp +0 -0
- package/webui/logo-192.webp +0 -0
- package/webui/logo-256.webp +0 -0
- package/webui/logo-64.webp +0 -0
- package/webui/logo.jpg +0 -0
- package/webui/plugin-shared/plugin-sdk-webui.js +3 -0
- package/webui/plugin-shared/react-dom.js +3 -0
- package/webui/plugin-shared/react-jsx-runtime.js +3 -0
- package/webui/plugin-shared/react.js +3 -0
- package//344/275/277/347/224/250/350/257/264/346/230/216.txt +6 -0
- package//346/233/264/346/226/260/346/227/245/345/277/227.txt +1091 -0
- package/README.md +0 -3
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
export namespace Time {
|
|
2
|
+
export const millisecond = 1
|
|
3
|
+
export const second = 1000
|
|
4
|
+
export const minute = second * 60
|
|
5
|
+
export const hour = minute * 60
|
|
6
|
+
export const day = hour * 24
|
|
7
|
+
export const week = day * 7
|
|
8
|
+
|
|
9
|
+
let timezoneOffset = new Date().getTimezoneOffset()
|
|
10
|
+
|
|
11
|
+
export function setTimezoneOffset(offset: number) {
|
|
12
|
+
timezoneOffset = offset
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function getTimezoneOffset() {
|
|
16
|
+
return timezoneOffset
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export function getDateNumber(date: number | Date = new Date(), offset?: number) {
|
|
20
|
+
if (typeof date === 'number') date = new Date(date)
|
|
21
|
+
if (offset === undefined) offset = timezoneOffset
|
|
22
|
+
return Math.floor((date.valueOf() / minute - offset) / 1440)
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export function fromDateNumber(value: number, offset?: number) {
|
|
26
|
+
const date = new Date(value * day)
|
|
27
|
+
if (offset === undefined) offset = timezoneOffset
|
|
28
|
+
return new Date(+date + offset * minute)
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const numeric = /\d+(?:\.\d+)?/.source
|
|
32
|
+
const timeRegExp = new RegExp(`^${[
|
|
33
|
+
'w(?:eek(?:s)?)?',
|
|
34
|
+
'd(?:ay(?:s)?)?',
|
|
35
|
+
'h(?:our(?:s)?)?',
|
|
36
|
+
'm(?:in(?:ute)?(?:s)?)?',
|
|
37
|
+
's(?:ec(?:ond)?(?:s)?)?',
|
|
38
|
+
].map(unit => `(${numeric}${unit})?`).join('')}$`)
|
|
39
|
+
|
|
40
|
+
export function parseTime(source: string) {
|
|
41
|
+
const capture = timeRegExp.exec(source)
|
|
42
|
+
if (!capture) return 0
|
|
43
|
+
return (parseFloat(capture[1]) * week || 0)
|
|
44
|
+
+ (parseFloat(capture[2]) * day || 0)
|
|
45
|
+
+ (parseFloat(capture[3]) * hour || 0)
|
|
46
|
+
+ (parseFloat(capture[4]) * minute || 0)
|
|
47
|
+
+ (parseFloat(capture[5]) * second || 0)
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export function parseDate(date: string) {
|
|
51
|
+
const parsed = parseTime(date)
|
|
52
|
+
if (parsed) {
|
|
53
|
+
date = Date.now() + parsed as any
|
|
54
|
+
} else if (/^\d{1,2}(:\d{1,2}){1,2}$/.test(date)) {
|
|
55
|
+
date = `${new Date().toLocaleDateString()}-${date}`
|
|
56
|
+
} else if (/^\d{1,2}-\d{1,2}-\d{1,2}(:\d{1,2}){1,2}$/.test(date)) {
|
|
57
|
+
date = `${new Date().getFullYear()}-${date}`
|
|
58
|
+
}
|
|
59
|
+
return date ? new Date(date) : new Date()
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export function format(ms: number) {
|
|
63
|
+
const abs = Math.abs(ms)
|
|
64
|
+
if (abs >= day - hour / 2) {
|
|
65
|
+
return Math.round(ms / day) + 'd'
|
|
66
|
+
} else if (abs >= hour - minute / 2) {
|
|
67
|
+
return Math.round(ms / hour) + 'h'
|
|
68
|
+
} else if (abs >= minute - second / 2) {
|
|
69
|
+
return Math.round(ms / minute) + 'm'
|
|
70
|
+
} else if (abs >= second) {
|
|
71
|
+
return Math.round(ms / second) + 's'
|
|
72
|
+
}
|
|
73
|
+
return ms + 'ms'
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export function toDigits(source: number, length = 2) {
|
|
77
|
+
return source.toString().padStart(length, '0')
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
export function template(template: string, time = new Date()) {
|
|
81
|
+
return template
|
|
82
|
+
.replace('yyyy', time.getFullYear().toString())
|
|
83
|
+
.replace('yy', time.getFullYear().toString().slice(2))
|
|
84
|
+
.replace('MM', toDigits(time.getMonth() + 1))
|
|
85
|
+
.replace('dd', toDigits(time.getDate()))
|
|
86
|
+
.replace('hh', toDigits(time.getHours()))
|
|
87
|
+
.replace('mm', toDigits(time.getMinutes()))
|
|
88
|
+
.replace('ss', toDigits(time.getSeconds()))
|
|
89
|
+
.replace('SSS', toDigits(time.getMilliseconds(), 3))
|
|
90
|
+
}
|
|
91
|
+
}
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
import { isNullable } from './misc'
|
|
2
|
+
|
|
3
|
+
type GlobalConstructorNames = keyof {
|
|
4
|
+
[K in keyof typeof globalThis as typeof globalThis[K] extends abstract new (...args: any) => any ? K : never]: K
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export function is<K extends GlobalConstructorNames>(type: K): (value: any) => value is InstanceType<typeof globalThis[K]>
|
|
8
|
+
export function is<K extends GlobalConstructorNames>(type: K, value: any): value is InstanceType<typeof globalThis[K]>
|
|
9
|
+
export function is<K extends GlobalConstructorNames>(type: K, value?: any): any {
|
|
10
|
+
if (arguments.length === 1) return (value: any) => is(type, value)
|
|
11
|
+
return type in globalThis && value instanceof (globalThis[type] as any)
|
|
12
|
+
|| Object.prototype.toString.call(value).slice(8, -1) === type
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
function isArrayBufferLike(value: any): value is ArrayBufferLike {
|
|
16
|
+
return is('ArrayBuffer', value) || is('SharedArrayBuffer', value)
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
function isArrayBufferSource(value: any): value is Binary.Source {
|
|
20
|
+
return isArrayBufferLike(value) || ArrayBuffer.isView(value)
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export namespace Binary {
|
|
24
|
+
export type Source<T extends ArrayBufferLike = ArrayBufferLike> = T | ArrayBufferView<T>
|
|
25
|
+
|
|
26
|
+
export const is = isArrayBufferLike
|
|
27
|
+
export const isSource = isArrayBufferSource
|
|
28
|
+
|
|
29
|
+
export function fromSource<T extends ArrayBufferLike>(source: Source<T>): T {
|
|
30
|
+
if (ArrayBuffer.isView(source)) {
|
|
31
|
+
// https://stackoverflow.com/questions/8609289/convert-a-binary-nodejs-buffer-to-javascript-arraybuffer#answer-31394257
|
|
32
|
+
return source.buffer.slice(source.byteOffset, source.byteOffset + source.byteLength) as T
|
|
33
|
+
} else {
|
|
34
|
+
return source
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export function toBase64(source: Source) {
|
|
39
|
+
source = fromSource(source)
|
|
40
|
+
if (typeof Buffer !== 'undefined') {
|
|
41
|
+
return Buffer.from(source).toString('base64')
|
|
42
|
+
}
|
|
43
|
+
let binary = ''
|
|
44
|
+
const bytes = new Uint8Array(source)
|
|
45
|
+
for (let i = 0; i < bytes.byteLength; i++) {
|
|
46
|
+
binary += String.fromCharCode(bytes[i])
|
|
47
|
+
}
|
|
48
|
+
return btoa(binary)
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export function fromBase64(source: string) {
|
|
52
|
+
if (typeof Buffer !== 'undefined') return fromSource(Buffer.from(source, 'base64'))
|
|
53
|
+
return Uint8Array.from(atob(source), c => c.charCodeAt(0))
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export function toHex(source: Source) {
|
|
57
|
+
source = fromSource(source)
|
|
58
|
+
if (typeof Buffer !== 'undefined') return Buffer.from(source).toString('hex')
|
|
59
|
+
return Array.from(new Uint8Array(source), byte => byte.toString(16).padStart(2, '0')).join('')
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export function fromHex(source: string) {
|
|
63
|
+
if (typeof Buffer !== 'undefined') return fromSource(Buffer.from(source, 'hex'))
|
|
64
|
+
const hex = source.length % 2 === 0 ? source : source.slice(0, source.length - 1)
|
|
65
|
+
const buffer: number[] = []
|
|
66
|
+
for (let i = 0; i < hex.length; i += 2) {
|
|
67
|
+
buffer.push(parseInt(`${hex[i]}${hex[i + 1]}`, 16))
|
|
68
|
+
}
|
|
69
|
+
return Uint8Array.from(buffer).buffer
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export const base64ToArrayBuffer = Binary.fromBase64
|
|
74
|
+
export const arrayBufferToBase64 = Binary.toBase64
|
|
75
|
+
export const hexToArrayBuffer = Binary.fromHex
|
|
76
|
+
export const arrayBufferToHex = Binary.toHex
|
|
77
|
+
|
|
78
|
+
export function clone<T>(source: T): T
|
|
79
|
+
export function clone(source: any, refs = new Map<any, any>()) {
|
|
80
|
+
if (!source || typeof source !== 'object') return source
|
|
81
|
+
if (is('Date', source)) return new Date(source.valueOf())
|
|
82
|
+
if (is('RegExp', source)) return new RegExp(source.source, source.flags)
|
|
83
|
+
if (isArrayBufferLike(source)) return source.slice(0)
|
|
84
|
+
if (ArrayBuffer.isView(source)) return source.buffer.slice(source.byteOffset, source.byteOffset + source.byteLength)
|
|
85
|
+
const cached = refs.get(source)
|
|
86
|
+
if (cached) return cached
|
|
87
|
+
if (Array.isArray(source)) {
|
|
88
|
+
const result: any[] = []
|
|
89
|
+
refs.set(source, result)
|
|
90
|
+
source.forEach((value, index) => {
|
|
91
|
+
result[index] = Reflect.apply(clone, null, [value, refs])
|
|
92
|
+
})
|
|
93
|
+
return result
|
|
94
|
+
}
|
|
95
|
+
const result = Object.create(Object.getPrototypeOf(source))
|
|
96
|
+
refs.set(source, result)
|
|
97
|
+
for (const key of Reflect.ownKeys(source)) {
|
|
98
|
+
const descriptor = { ...Reflect.getOwnPropertyDescriptor(source, key) }
|
|
99
|
+
if ('value' in descriptor) {
|
|
100
|
+
descriptor.value = Reflect.apply(clone, null, [descriptor.value, refs])
|
|
101
|
+
}
|
|
102
|
+
Reflect.defineProperty(result, key, descriptor)
|
|
103
|
+
}
|
|
104
|
+
return result
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
export function deepEqual(a: any, b: any, strict?: boolean): boolean {
|
|
108
|
+
if (a === b) return true
|
|
109
|
+
if (!strict && isNullable(a) && isNullable(b)) return true
|
|
110
|
+
if (typeof a !== typeof b) return false
|
|
111
|
+
if (typeof a !== 'object') return false
|
|
112
|
+
if (!a || !b) return false
|
|
113
|
+
|
|
114
|
+
function check<T>(test: (x: any) => x is T, then: (a: T, b: T) => boolean) {
|
|
115
|
+
return test(a) ? test(b) ? then(a, b) : false : test(b) ? false : undefined
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
return check(Array.isArray, (a, b) => a.length === b.length && a.every((item, index) => deepEqual(item, b[index])))
|
|
119
|
+
?? check(is('Date'), (a, b) => a.valueOf() === b.valueOf())
|
|
120
|
+
?? check(is('RegExp'), (a, b) => a.source === b.source && a.flags === b.flags)
|
|
121
|
+
?? check(isArrayBufferLike, (a, b) => {
|
|
122
|
+
if (a.byteLength !== b.byteLength) return false
|
|
123
|
+
const viewA = new Uint8Array(a)
|
|
124
|
+
const viewB = new Uint8Array(b)
|
|
125
|
+
for (let i = 0; i < viewA.length; i++) {
|
|
126
|
+
if (viewA[i] !== viewB[i]) return false
|
|
127
|
+
}
|
|
128
|
+
return true
|
|
129
|
+
})
|
|
130
|
+
?? Object.keys({ ...a, ...b }).every(key => deepEqual(a[key], b[key], strict))
|
|
131
|
+
}
|
|
@@ -0,0 +1,342 @@
|
|
|
1
|
+
# Schemastery
|
|
2
|
+
|
|
3
|
+
[](https://codecov.io/gh/shigma/schemastery)
|
|
4
|
+
[](https://www.npmjs.com/package/schemastery)
|
|
5
|
+
[](https://www.npmjs.com/package/schemastery)
|
|
6
|
+
[](https://github.com/shigma/schemastery/blob/master/LICENSE)
|
|
7
|
+
|
|
8
|
+
Type Driven Schema Validator.
|
|
9
|
+
|
|
10
|
+
## Features
|
|
11
|
+
|
|
12
|
+
- **Lightweight.** Much smaller than other validation libraries.
|
|
13
|
+
- **Easy to use.** You can use any schema as a function or constructor directly.
|
|
14
|
+
- **Powerful.** Schemastery supports some advanced types such as `union`, `intersect` and `transform`.
|
|
15
|
+
- **Extensible.** You can create your own schema types via `Schema.extend()`.
|
|
16
|
+
- **Serializable.** Schema objects can be serialized into JSON and then be hydrated in another environment.
|
|
17
|
+
|
|
18
|
+
## Basic Examples
|
|
19
|
+
|
|
20
|
+
### use as validator (JavaScript)
|
|
21
|
+
|
|
22
|
+
```js
|
|
23
|
+
const Schema = require('schemastery')
|
|
24
|
+
|
|
25
|
+
const validate = Schema.number().default(10)
|
|
26
|
+
|
|
27
|
+
validate(0) // 0
|
|
28
|
+
validate(null) // 10
|
|
29
|
+
validate('') // TypeError
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
### use as constructor (TypeScript)
|
|
33
|
+
|
|
34
|
+
```ts
|
|
35
|
+
import Schema from 'schemastery'
|
|
36
|
+
|
|
37
|
+
interface Config {
|
|
38
|
+
foo: Record<string, string>
|
|
39
|
+
bar: string[]
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
const Config = Schema.object({
|
|
43
|
+
foo: Schema.dict(Schema.string()).default({}),
|
|
44
|
+
bar: Schema.array(Schema.string()).default([]),
|
|
45
|
+
})
|
|
46
|
+
|
|
47
|
+
// config is an instance of Config
|
|
48
|
+
// in this case, that is { foo: {}, bar: [] }
|
|
49
|
+
const config = new Config()
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
## General Types
|
|
53
|
+
|
|
54
|
+
### Schema.any()
|
|
55
|
+
|
|
56
|
+
Assert that the value is of any type.
|
|
57
|
+
|
|
58
|
+
```js
|
|
59
|
+
const validate = Schema.any()
|
|
60
|
+
|
|
61
|
+
validate() // undefined
|
|
62
|
+
validate(0) // 0
|
|
63
|
+
validate({}) // {}
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
### Schema.never()
|
|
67
|
+
|
|
68
|
+
Assert that the value is nullable.
|
|
69
|
+
|
|
70
|
+
```js
|
|
71
|
+
const validate = Schema.never()
|
|
72
|
+
|
|
73
|
+
validate() // undefined
|
|
74
|
+
validate(0) // TypeError
|
|
75
|
+
validate({}) // TypeError
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
### Schema.const(value)
|
|
79
|
+
|
|
80
|
+
Assert that the value is equal to the given constant.
|
|
81
|
+
|
|
82
|
+
```js
|
|
83
|
+
const validate = Schema.const(10)
|
|
84
|
+
|
|
85
|
+
validate(10) // 10
|
|
86
|
+
validate(0) // TypeError
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
### Schema.number()
|
|
90
|
+
|
|
91
|
+
Assert that the value is a number.
|
|
92
|
+
|
|
93
|
+
```js
|
|
94
|
+
const validate = Schema.number()
|
|
95
|
+
|
|
96
|
+
validate() // undefined
|
|
97
|
+
validate(1) // 1
|
|
98
|
+
validate('') // TypeError
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
### Schema.string()
|
|
102
|
+
|
|
103
|
+
Assert that the value is a string.
|
|
104
|
+
|
|
105
|
+
```js
|
|
106
|
+
const validate = Schema.string()
|
|
107
|
+
|
|
108
|
+
validate() // undefined
|
|
109
|
+
validate(0) // TypeError
|
|
110
|
+
validate('foo') // 'foo'
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
### Schema.boolean()
|
|
114
|
+
|
|
115
|
+
Assert that the value is a boolean.
|
|
116
|
+
|
|
117
|
+
```js
|
|
118
|
+
const validate = Schema.boolean()
|
|
119
|
+
|
|
120
|
+
validate() // undefined
|
|
121
|
+
validate(0) // TypeError
|
|
122
|
+
validate(true) // true
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
### Schema.is(constructor)
|
|
126
|
+
|
|
127
|
+
Assert that the value is an instance of the given constructor.
|
|
128
|
+
|
|
129
|
+
```js
|
|
130
|
+
const validate = Schema.is(RegExp)
|
|
131
|
+
|
|
132
|
+
validate() // undefined
|
|
133
|
+
validate(/foo/) // /foo/
|
|
134
|
+
validate('foo') // TypeError
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
### Schema.array(inner)
|
|
138
|
+
|
|
139
|
+
Assert that the value is an array of `inner`. The default value will be `[]` if not specified.
|
|
140
|
+
|
|
141
|
+
```js
|
|
142
|
+
const validate = Schema.array(Schema.number())
|
|
143
|
+
|
|
144
|
+
validate() // []
|
|
145
|
+
validate(0) // TypeError
|
|
146
|
+
validate([0, 1]) // [0, 1]
|
|
147
|
+
validate([0, '1']) // TypeError
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
### Schema.dict(inner)
|
|
151
|
+
|
|
152
|
+
Assert that the value is a dictionary of `inner`. The default value will be `{}` if not specified.
|
|
153
|
+
|
|
154
|
+
```js
|
|
155
|
+
const validate = Schema.dict(Schema.number())
|
|
156
|
+
|
|
157
|
+
validate() // {}
|
|
158
|
+
validate(0) // TypeError
|
|
159
|
+
validate({ a: 0, b: 1 }) // { a: 0, b: 1 }
|
|
160
|
+
validate({ a: 0, b: '1' }) // TypeError
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
### Schema.tuple(list)
|
|
164
|
+
|
|
165
|
+
Assert that the value is a tuple whose each element is of corresponding subtype. The default value will be `[]` if not specified.
|
|
166
|
+
|
|
167
|
+
```js
|
|
168
|
+
const validate = Schema.tuple([
|
|
169
|
+
Schema.number(),
|
|
170
|
+
Schema.string(),
|
|
171
|
+
])
|
|
172
|
+
|
|
173
|
+
validate() // []
|
|
174
|
+
validate([0]) // { a: 0 }
|
|
175
|
+
validate([0, 1]) // TypeError
|
|
176
|
+
validate([0, '1']) // [0, '1']
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
### Schema.object(dict)
|
|
180
|
+
|
|
181
|
+
Assert that the value is an object whose each property is of corresponding subtype. The default value will be `{}` if not specified.
|
|
182
|
+
|
|
183
|
+
```js
|
|
184
|
+
const validate = Schema.object({
|
|
185
|
+
a: Schema.number(),
|
|
186
|
+
b: Schema.string(),
|
|
187
|
+
})
|
|
188
|
+
|
|
189
|
+
validate() // {}
|
|
190
|
+
validate({ a: 0 }) // { a: 0 }
|
|
191
|
+
validate({ a: 0, b: 1 }) // TypeError
|
|
192
|
+
validate({ a: 0, b: '1' }) // { a: 0, b: '1' }
|
|
193
|
+
```
|
|
194
|
+
|
|
195
|
+
### Schema.union(list)
|
|
196
|
+
|
|
197
|
+
Assert that the value is one of the specified types.
|
|
198
|
+
|
|
199
|
+
```js
|
|
200
|
+
const validate = Schema.union([
|
|
201
|
+
Schema.number(),
|
|
202
|
+
Schema.string(),
|
|
203
|
+
])
|
|
204
|
+
|
|
205
|
+
validate() // undefined
|
|
206
|
+
validate(0) // 0
|
|
207
|
+
validate('1') // '1'
|
|
208
|
+
validate(true) // TypeError
|
|
209
|
+
```
|
|
210
|
+
|
|
211
|
+
### Schema.intersect(list)
|
|
212
|
+
|
|
213
|
+
Assert that the value should match each specified type.
|
|
214
|
+
|
|
215
|
+
```js
|
|
216
|
+
const validate = Schema.intersect([
|
|
217
|
+
Schema.object({ a: Schema.string().required() }),
|
|
218
|
+
Schema.object({ b: Schema.number().default(0) }),
|
|
219
|
+
])
|
|
220
|
+
|
|
221
|
+
validate() // TypeError
|
|
222
|
+
validate({ a: '' }) // { a: '', b: 0 }
|
|
223
|
+
validate({ a: '', b: 1 }) // { a: '', b: 1 }
|
|
224
|
+
validate({ a: '', b: '2' }) // TypeError
|
|
225
|
+
```
|
|
226
|
+
|
|
227
|
+
### Schema.transform(inner, callback)
|
|
228
|
+
|
|
229
|
+
Assert that the value is of the specified subtype and then transformed by `callback`.
|
|
230
|
+
|
|
231
|
+
```js
|
|
232
|
+
const validate = Schema.transform(Schema.number().default(0), n => n + 1)
|
|
233
|
+
|
|
234
|
+
validate() // 1
|
|
235
|
+
validate('0') // TypeError
|
|
236
|
+
validate(10) // 11
|
|
237
|
+
```
|
|
238
|
+
|
|
239
|
+
## Instance Methods
|
|
240
|
+
|
|
241
|
+
Note: `default` and `required` are mutually exclusive.
|
|
242
|
+
|
|
243
|
+
### schema.required()
|
|
244
|
+
|
|
245
|
+
Assert that the value is not nullable.
|
|
246
|
+
|
|
247
|
+
### schema.default(value)
|
|
248
|
+
|
|
249
|
+
Set the fallback value when nullable.
|
|
250
|
+
|
|
251
|
+
### schema.description(text)
|
|
252
|
+
|
|
253
|
+
Set the description of the schema.
|
|
254
|
+
|
|
255
|
+
## Shorthand Syntax
|
|
256
|
+
|
|
257
|
+
Some shorthand syntax is available for inner types.
|
|
258
|
+
|
|
259
|
+
- `undefined` -> `Schema.any()`
|
|
260
|
+
- `String` -> `Schema.string()`
|
|
261
|
+
- `Number` -> `Schema.number()`
|
|
262
|
+
- `Boolean` -> `Schema.boolean()`
|
|
263
|
+
- `1` -> `Schema.const(1)` (only for primitive types)
|
|
264
|
+
- `Date` -> `Schema.is(Date)`
|
|
265
|
+
|
|
266
|
+
```js
|
|
267
|
+
Schema.array(String) // Schema.array(Schema.string())
|
|
268
|
+
Schema.dict(RegExp) // Schema.dict(Schema.is(RegExp))
|
|
269
|
+
Schema.union([1, 2]) // Schema.union([Schema.const(1), Schema.const(2)])
|
|
270
|
+
```
|
|
271
|
+
|
|
272
|
+
You can also use `Schema.from()` to get the inferred schema from a shorthand value.
|
|
273
|
+
|
|
274
|
+
```js
|
|
275
|
+
Schema.from() // Schema.any()
|
|
276
|
+
Schema.from(Date) // Schema.is(Date)
|
|
277
|
+
Schema.from('foo') // Schema.const('foo')
|
|
278
|
+
```
|
|
279
|
+
|
|
280
|
+
## Advanced Examples
|
|
281
|
+
|
|
282
|
+
Here are some examples which demonstrate how to define advanced types.
|
|
283
|
+
|
|
284
|
+
### Enumeration
|
|
285
|
+
|
|
286
|
+
```js
|
|
287
|
+
const Enum = Schema.union(['red', 'blue'])
|
|
288
|
+
|
|
289
|
+
Enum('red') // 'red'
|
|
290
|
+
Enum('blue') // 'blue'
|
|
291
|
+
Enum('green') // TypeError
|
|
292
|
+
```
|
|
293
|
+
|
|
294
|
+
### ToString
|
|
295
|
+
|
|
296
|
+
```js
|
|
297
|
+
const ToString = Schema.transform(Schema.any(), v => String(v))
|
|
298
|
+
|
|
299
|
+
ToString('') // ''
|
|
300
|
+
ToString(0) // '0'
|
|
301
|
+
ToString({}) // '{}'
|
|
302
|
+
```
|
|
303
|
+
|
|
304
|
+
### Listable
|
|
305
|
+
|
|
306
|
+
```js
|
|
307
|
+
const Listable = Schema.union([
|
|
308
|
+
Schema.array(Number),
|
|
309
|
+
Schema.transform(Number, n => [n]),
|
|
310
|
+
]).default([])
|
|
311
|
+
|
|
312
|
+
Listable() // []
|
|
313
|
+
Listable(0) // [0]
|
|
314
|
+
Listable([1, 2]) // [1, 2]
|
|
315
|
+
```
|
|
316
|
+
|
|
317
|
+
### Alias
|
|
318
|
+
|
|
319
|
+
```js
|
|
320
|
+
const Config = Schema.dict(Number, Schema.union([
|
|
321
|
+
'foo',
|
|
322
|
+
Schema.transform('bar', () => 'foo'),
|
|
323
|
+
]))
|
|
324
|
+
|
|
325
|
+
Config({ foo: 1 }) // { foo: 1 }
|
|
326
|
+
Config({ bar: 2 }) // { foo: 2 }
|
|
327
|
+
Config({ bar: '3' }) // TypeError
|
|
328
|
+
```
|
|
329
|
+
|
|
330
|
+
## Extensibility
|
|
331
|
+
|
|
332
|
+
## Serializability
|
|
333
|
+
|
|
334
|
+
```js
|
|
335
|
+
const schema1 = Schema.object({
|
|
336
|
+
foo: Schema.string(),
|
|
337
|
+
bar: Schema.number(),
|
|
338
|
+
})
|
|
339
|
+
|
|
340
|
+
// should have the same effect as schema1
|
|
341
|
+
const schema2 = new Schema(JSON.parse(JSON.stringify(schema1)))
|
|
342
|
+
```
|