From 41d5e18f6be169273e314f19c5bd96b60d9523e8 Mon Sep 17 00:00:00 2001 From: robonen Date: Sat, 7 Mar 2026 18:07:22 +0700 Subject: [PATCH] feat(monorepo): migrate vue packages and apply oxlint refactors --- .github/skills/monorepo/SKILL.md | 409 ++ .github/workflows/ci.yaml | 2 +- configs/oxlint/README.md | 13 + configs/oxlint/oxlint.config.ts | 4 +- configs/oxlint/package.json | 16 +- configs/oxlint/rules/README.md | 21 + configs/oxlint/rules/base.md | 34 + configs/oxlint/rules/imports.md | 27 + configs/oxlint/rules/node.md | 22 + configs/oxlint/rules/stylistic.md | 51 + configs/oxlint/rules/typescript.md | 33 + configs/oxlint/rules/vitest.md | 34 + configs/oxlint/rules/vue.md | 30 + configs/oxlint/src/compose.ts | 17 + configs/oxlint/src/index.ts | 3 +- configs/oxlint/src/presets/imports.ts | 2 + configs/oxlint/src/presets/index.ts | 1 + configs/oxlint/src/presets/stylistic.ts | 162 + configs/oxlint/src/types.ts | 1 + configs/oxlint/test/compose.test.ts | 25 + configs/oxlint/tsconfig.json | 8 +- configs/tsconfig/package.json | 2 +- configs/tsdown/package.json | 2 +- core/encoding/jsr.json | 6 + core/encoding/oxlint.config.ts | 14 + core/encoding/package.json | 47 + core/encoding/src/index.ts | 2 + core/encoding/src/qr/constants.ts | 67 + core/encoding/src/qr/encode.ts | 94 + core/encoding/src/qr/index.bench.ts | 96 + core/encoding/src/qr/index.test.ts | 182 + core/encoding/src/qr/index.ts | 8 + core/encoding/src/qr/qr-code.ts | 436 ++ core/encoding/src/qr/segment.ts | 82 + core/encoding/src/qr/types.ts | 17 + core/encoding/src/qr/utils.ts | 79 + core/encoding/src/reed-solomon/index.test.ts | 100 + core/encoding/src/reed-solomon/index.ts | 92 + core/encoding/tsconfig.json | 3 + core/encoding/tsdown.config.ts | 7 + core/encoding/vitest.config.ts | 7 + core/platform/oxlint.config.ts | 22 +- core/platform/package.json | 6 +- .../src/browsers/focusGuard/index.test.ts | 6 +- .../platform/src/browsers/focusGuard/index.ts | 12 +- core/platform/src/browsers/index.ts | 2 +- core/platform/src/multi/global/index.ts | 8 +- core/platform/src/multi/index.ts | 2 +- core/platform/tsdown.config.ts | 2 +- core/platform/vitest.config.ts | 1 - core/stdlib/oxlint.config.ts | 4 +- core/stdlib/package.json | 6 +- core/stdlib/src/arrays/cluster/index.test.ts | 4 +- core/stdlib/src/arrays/cluster/index.ts | 8 +- core/stdlib/src/arrays/first/index.test.ts | 2 +- core/stdlib/src/arrays/first/index.ts | 12 +- core/stdlib/src/arrays/index.ts | 2 +- core/stdlib/src/arrays/last/index.test.ts | 2 +- core/stdlib/src/arrays/last/index.ts | 8 +- core/stdlib/src/arrays/sum/index.test.ts | 8 +- core/stdlib/src/arrays/sum/index.ts | 8 +- core/stdlib/src/arrays/unique/index.test.ts | 6 +- core/stdlib/src/arrays/unique/index.ts | 8 +- core/stdlib/src/async/pool/index.ts | 2 +- core/stdlib/src/async/sleep/index.test.ts | 2 +- core/stdlib/src/async/sleep/index.ts | 12 +- core/stdlib/src/async/tryIt/index.test.ts | 10 +- core/stdlib/src/async/tryIt/index.ts | 15 +- core/stdlib/src/bits/flags/index.ts | 18 +- core/stdlib/src/bits/helpers/index.test.ts | 29 +- core/stdlib/src/bits/helpers/index.ts | 14 +- core/stdlib/src/bits/index.ts | 2 +- core/stdlib/src/bits/vector/index.test.ts | 6 +- core/stdlib/src/bits/vector/index.ts | 4 +- core/stdlib/src/collections/get/index.ts | 30 +- core/stdlib/src/collections/index.ts | 2 +- core/stdlib/src/index.ts | 2 +- .../stdlib/src/math/basic/clamp/index.test.ts | 4 +- core/stdlib/src/math/basic/clamp/index.ts | 2 +- core/stdlib/src/math/basic/lerp/index.test.ts | 4 +- .../stdlib/src/math/basic/remap/index.test.ts | 6 +- core/stdlib/src/math/basic/remap/index.ts | 6 +- .../src/math/bigint/clampBigInt/index.test.ts | 6 +- .../src/math/bigint/clampBigInt/index.ts | 4 +- .../src/math/bigint/lerpBigInt/index.test.ts | 4 +- .../src/math/bigint/lerpBigInt/index.ts | 2 +- .../src/math/bigint/maxBigInt/index.test.ts | 2 +- .../stdlib/src/math/bigint/maxBigInt/index.ts | 2 +- .../src/math/bigint/minBigInt/index.test.ts | 8 +- .../src/math/bigint/remapBigInt/index.test.ts | 6 +- .../src/math/bigint/remapBigInt/index.ts | 6 +- core/stdlib/src/objects/omit/index.test.ts | 2 +- core/stdlib/src/objects/omit/index.ts | 13 +- core/stdlib/src/objects/pick/index.test.ts | 2 +- core/stdlib/src/objects/pick/index.ts | 15 +- .../patterns/behavioral/Command/index.test.ts | 4 +- .../src/patterns/behavioral/Command/sync.ts | 2 +- .../patterns/behavioral/PubSub/index.test.ts | 2 +- .../src/patterns/behavioral/PubSub/index.ts | 20 +- .../patterns/behavioral/StateMachine/async.ts | 3 +- .../behavioral/StateMachine/index.test.ts | 22 +- .../patterns/behavioral/StateMachine/sync.ts | 3 +- core/stdlib/src/patterns/index.ts | 2 +- .../src/structs/BinaryHeap/index.test.ts | 362 +- core/stdlib/src/structs/BinaryHeap/index.ts | 202 +- .../src/structs/CircularBuffer/index.ts | 2 +- core/stdlib/src/structs/Deque/index.ts | 2 +- .../src/structs/LinkedList/index.test.ts | 652 +-- core/stdlib/src/structs/LinkedList/index.ts | 342 +- core/stdlib/src/structs/LinkedList/types.ts | 38 +- .../src/structs/PriorityQueue/index.test.ts | 328 +- .../stdlib/src/structs/PriorityQueue/index.ts | 122 +- .../stdlib/src/structs/PriorityQueue/types.ts | 18 +- core/stdlib/src/structs/Queue/index.ts | 2 +- core/stdlib/src/structs/Stack/index.test.ts | 4 +- core/stdlib/src/structs/Stack/index.ts | 144 +- core/stdlib/src/structs/index.ts | 2 +- core/stdlib/src/sync/mutex/index.test.ts | 14 +- core/stdlib/src/sync/mutex/index.ts | 10 +- core/stdlib/src/text/index.ts | 2 +- .../text/levenshtein-distance/index.test.ts | 60 +- .../src/text/levenshtein-distance/index.ts | 54 +- core/stdlib/src/text/template/index.test-d.ts | 2 +- core/stdlib/src/text/template/index.test.ts | 22 +- core/stdlib/src/text/template/index.ts | 24 +- .../src/text/trigram-distance/index.test.ts | 142 +- .../stdlib/src/text/trigram-distance/index.ts | 50 +- core/stdlib/src/types/index.ts | 2 +- core/stdlib/src/types/js/casts.test.ts | 2 +- core/stdlib/src/types/js/casts.ts | 6 +- core/stdlib/src/types/js/complex.test.ts | 40 +- core/stdlib/src/types/js/complex.ts | 36 +- core/stdlib/src/types/js/index.ts | 2 +- core/stdlib/src/types/js/primitives.test.ts | 34 +- core/stdlib/src/types/js/primitives.ts | 32 +- .../stdlib/src/types/ts/collections.test-d.ts | 2 +- core/stdlib/src/types/ts/collections.ts | 14 +- core/stdlib/src/types/ts/index.ts | 2 +- core/stdlib/src/types/ts/string.test-d.ts | 86 +- core/stdlib/src/types/ts/union.ts | 8 +- core/stdlib/tsdown.config.ts | 2 +- docs/app/assets/css/main.css | 2 +- docs/app/components/DocsBadge.vue | 3 +- docs/app/components/DocsCode.vue | 3 +- docs/app/components/DocsDemo.vue | 3 +- docs/app/components/DocsMethodsList.vue | 3 +- docs/app/components/DocsParamsTable.vue | 3 +- docs/app/components/DocsPropsTable.vue | 3 +- docs/app/components/DocsSearch.vue | 3 +- docs/app/components/DocsTag.vue | 3 +- docs/app/composables/useShiki.ts | 3 +- docs/app/layouts/default.vue | 3 +- docs/app/pages/[package]/[utility].vue | 3 +- docs/app/pages/[package]/index.vue | 3 +- docs/app/pages/index.vue | 3 +- docs/modules/extractor/extract.ts | 72 +- docs/modules/extractor/index.ts | 7 +- docs/oxlint.config.ts | 4 + docs/package.json | 17 +- infra/renovate/package.json | 4 +- package.json | 7 +- pnpm-lock.yaml | 4883 ++++++++++------- pnpm-workspace.yaml | 12 +- tools.code-workspace | 5 +- vitest.config.ts | 5 +- vue/primitives/README.md | 15 + vue/primitives/jsr.json | 7 + vue/primitives/oxlint.config.ts | 4 + vue/primitives/package.json | 54 + vue/primitives/src/env.d.ts | 1 + vue/primitives/src/index.ts | 1 + vue/primitives/src/primitive/Primitive.ts | 22 + vue/primitives/src/primitive/Slot.ts | 29 + .../src/primitive/__test__/Primitive.bench.ts | 116 + .../src/primitive/__test__/Primitive.test.ts | 482 ++ vue/primitives/src/primitive/index.ts | 2 + .../utils/__test__/getRawChildren.bench.ts | 142 + .../src/utils/__test__/getRawChildren.test.ts | 63 + vue/primitives/src/utils/getRawChildren.ts | 40 + vue/primitives/tsconfig.json | 10 + vue/primitives/tsdown.config.ts | 23 + vue/primitives/vitest.config.ts | 16 + {web/vue => vue/toolkit}/README.md | 0 {web/vue => vue/toolkit}/jsr.json | 0 vue/toolkit/oxlint.config.ts | 4 + {web/vue => vue/toolkit}/package.json | 8 +- .../toolkit}/src/composables/browser/index.ts | 0 .../browser/useEventListener/index.test.ts | 27 + .../browser/useEventListener/index.ts | 133 +- .../browser/useFocusGuard/index.test.ts | 2 +- .../browser/useFocusGuard/index.ts | 10 +- .../src/composables/browser/useFps/demo.vue | 3 +- .../composables/browser/useFps/index.test.ts | 18 +- .../src/composables/browser/useFps/index.ts | 6 +- .../browser/useIntervalFn/index.test.ts | 0 .../browser/useIntervalFn/index.ts | 0 .../browser/useRafFn/index.test.ts | 0 .../src/composables/browser/useRafFn/index.ts | 0 .../browser/useSupported/index.test.ts | 24 +- .../composables/browser/useSupported/index.ts | 24 +- .../composables/browser/useTabLeader/demo.vue | 3 +- .../browser/useTabLeader/index.test.ts | 2 +- .../composables/browser/useTabLeader/index.ts | 0 .../src/composables/component/index.ts | 3 +- .../component/unrefElement/index.test.ts | 8 +- .../component/unrefElement/index.ts | 4 +- .../component/useForwardExpose/index.test.ts | 276 + .../component/useForwardExpose/index.ts | 128 + .../useTemplateRefsList/index.test.ts | 141 + .../component/useTemplateRefsList/index.ts | 58 +- vue/toolkit/src/composables/debug/index.ts | 2 + .../debug/useRenderCount/index.test.ts | 75 + .../debug}/useRenderCount/index.ts | 20 +- .../debug}/useRenderInfo/index.test.ts | 18 +- .../composables/debug}/useRenderInfo/index.ts | 50 +- .../toolkit}/src/composables/index.ts | 1 + .../src/composables/lifecycle/index.ts | 0 .../lifecycle/tryOnBeforeMount/index.ts | 12 +- .../lifecycle/tryOnMounted/index.test.ts | 8 +- .../lifecycle/tryOnMounted/index.ts | 12 +- .../lifecycle/tryOnScopeDispose/index.test.ts | 8 +- .../lifecycle/tryOnScopeDispose/index.ts | 6 +- .../lifecycle/useMounted/index.test.ts | 27 + .../composables/lifecycle/useMounted/index.ts | 12 +- .../toolkit}/src/composables/math/index.ts | 0 .../composables/math/useClamp/index.test.ts | 2 +- .../src/composables/math/useClamp/index.ts | 8 +- .../reactivity/broadcastedRef/index.test.ts | 0 .../reactivity/broadcastedRef/index.ts | 2 +- .../src/composables/reactivity/index.ts | 0 .../reactivity/useCached/index.test.ts | 2 +- .../composables/reactivity/useCached/index.ts | 28 +- .../reactivity/useLastChanged/index.test.ts | 2 +- .../reactivity/useLastChanged/index.ts | 14 +- .../reactivity/useSyncRefs/index.test.ts | 2 +- .../reactivity/useSyncRefs/index.ts | 38 +- .../toolkit}/src/composables/state/index.ts | 0 .../state/useAppSharedState/index.test.ts | 2 +- .../state/useAppSharedState/index.ts | 12 +- .../state/useAsyncState/index.test.ts | 8 +- .../composables/state/useAsyncState/index.ts | 12 +- .../state/useContextFactory/index.test.ts | 2 +- .../state/useContextFactory/index.ts | 48 +- .../src/composables/state/useCounter/demo.vue | 4 +- .../state/useCounter/index.test.ts | 14 +- .../src/composables/state/useCounter/index.ts | 77 + .../state/useInjectionStore/index.test.ts | 8 +- .../state/useInjectionStore/index.ts | 6 +- .../composables/state/useToggle/index.test.ts | 0 .../src/composables/state/useToggle/index.ts | 46 +- .../toolkit}/src/composables/storage/index.ts | 0 .../storage/useLocalStorage/index.test.ts | 2 +- .../storage/useLocalStorage/index.ts | 45 + .../storage/useSessionStorage/index.test.ts | 2 +- .../storage/useSessionStorage/index.ts | 45 + .../storage/useStorage/index.test.ts | 177 +- .../composables/storage/useStorage/index.ts | 361 ++ .../storage/useStorageAsync/index.test.ts | 80 +- .../storage/useStorageAsync/index.ts | 266 + .../src/composables/utilities/index.ts | 0 .../useOffsetPagination/index.test.ts | 38 +- .../utilities/useOffsetPagination/index.ts | 14 +- {web/vue => vue/toolkit}/src/index.ts | 2 +- {web/vue => vue/toolkit}/src/types/flush.ts | 0 {web/vue => vue/toolkit}/src/types/index.ts | 3 +- vue/toolkit/src/types/ref.ts | 7 + .../toolkit}/src/types/resumable.ts | 8 +- {web/vue => vue/toolkit}/src/types/window.ts | 4 +- .../toolkit}/src/utils/components.ts | 12 +- {web/vue => vue/toolkit}/src/utils/error.ts | 4 +- vue/toolkit/src/utils/filters.test.ts | 171 + vue/toolkit/src/utils/filters.ts | 138 + vue/toolkit/src/utils/index.ts | 3 + {web/vue => vue/toolkit}/tsconfig.json | 0 {web/vue => vue/toolkit}/tsdown.config.ts | 2 +- {web/vue => vue/toolkit}/vitest.config.ts | 0 web/vue/oxlint.config.ts | 4 - .../component/useRenderCount/index.test.ts | 75 - .../useTemplateRefsList/index.test.ts | 141 - .../lifecycle/useMounted/index.test.ts | 27 - .../src/composables/state/useCounter/index.ts | 77 - .../storage/useLocalStorage/index.ts | 41 - .../storage/useSessionStorage/index.ts | 41 - .../composables/storage/useStorage/index.ts | 224 - .../storage/useStorageAsync/index.ts | 188 - web/vue/src/utils/index.ts | 2 - 286 files changed, 10295 insertions(+), 5028 deletions(-) create mode 100644 .github/skills/monorepo/SKILL.md create mode 100644 configs/oxlint/rules/README.md create mode 100644 configs/oxlint/rules/base.md create mode 100644 configs/oxlint/rules/imports.md create mode 100644 configs/oxlint/rules/node.md create mode 100644 configs/oxlint/rules/stylistic.md create mode 100644 configs/oxlint/rules/typescript.md create mode 100644 configs/oxlint/rules/vitest.md create mode 100644 configs/oxlint/rules/vue.md create mode 100644 configs/oxlint/src/presets/stylistic.ts create mode 100644 core/encoding/jsr.json create mode 100644 core/encoding/oxlint.config.ts create mode 100644 core/encoding/package.json create mode 100644 core/encoding/src/index.ts create mode 100644 core/encoding/src/qr/constants.ts create mode 100644 core/encoding/src/qr/encode.ts create mode 100644 core/encoding/src/qr/index.bench.ts create mode 100644 core/encoding/src/qr/index.test.ts create mode 100644 core/encoding/src/qr/index.ts create mode 100644 core/encoding/src/qr/qr-code.ts create mode 100644 core/encoding/src/qr/segment.ts create mode 100644 core/encoding/src/qr/types.ts create mode 100644 core/encoding/src/qr/utils.ts create mode 100644 core/encoding/src/reed-solomon/index.test.ts create mode 100644 core/encoding/src/reed-solomon/index.ts create mode 100644 core/encoding/tsconfig.json create mode 100644 core/encoding/tsdown.config.ts create mode 100644 core/encoding/vitest.config.ts create mode 100644 docs/oxlint.config.ts create mode 100644 vue/primitives/README.md create mode 100644 vue/primitives/jsr.json create mode 100644 vue/primitives/oxlint.config.ts create mode 100644 vue/primitives/package.json create mode 100644 vue/primitives/src/env.d.ts create mode 100644 vue/primitives/src/index.ts create mode 100644 vue/primitives/src/primitive/Primitive.ts create mode 100644 vue/primitives/src/primitive/Slot.ts create mode 100644 vue/primitives/src/primitive/__test__/Primitive.bench.ts create mode 100644 vue/primitives/src/primitive/__test__/Primitive.test.ts create mode 100644 vue/primitives/src/primitive/index.ts create mode 100644 vue/primitives/src/utils/__test__/getRawChildren.bench.ts create mode 100644 vue/primitives/src/utils/__test__/getRawChildren.test.ts create mode 100644 vue/primitives/src/utils/getRawChildren.ts create mode 100644 vue/primitives/tsconfig.json create mode 100644 vue/primitives/tsdown.config.ts create mode 100644 vue/primitives/vitest.config.ts rename {web/vue => vue/toolkit}/README.md (100%) rename {web/vue => vue/toolkit}/jsr.json (100%) create mode 100644 vue/toolkit/oxlint.config.ts rename {web/vue => vue/toolkit}/package.json (83%) rename {web/vue => vue/toolkit}/src/composables/browser/index.ts (100%) rename {web/vue => vue/toolkit}/src/composables/browser/useEventListener/index.test.ts (92%) rename {web/vue => vue/toolkit}/src/composables/browser/useEventListener/index.ts (58%) rename {web/vue => vue/toolkit}/src/composables/browser/useFocusGuard/index.test.ts (99%) rename {web/vue => vue/toolkit}/src/composables/browser/useFocusGuard/index.ts (97%) rename {web/vue => vue/toolkit}/src/composables/browser/useFps/demo.vue (96%) rename {web/vue => vue/toolkit}/src/composables/browser/useFps/index.test.ts (92%) rename {web/vue => vue/toolkit}/src/composables/browser/useFps/index.ts (100%) rename {web/vue => vue/toolkit}/src/composables/browser/useIntervalFn/index.test.ts (100%) rename {web/vue => vue/toolkit}/src/composables/browser/useIntervalFn/index.ts (100%) rename {web/vue => vue/toolkit}/src/composables/browser/useRafFn/index.test.ts (100%) rename {web/vue => vue/toolkit}/src/composables/browser/useRafFn/index.ts (100%) rename {web/vue => vue/toolkit}/src/composables/browser/useSupported/index.test.ts (58%) rename {web/vue => vue/toolkit}/src/composables/browser/useSupported/index.ts (70%) rename {web/vue => vue/toolkit}/src/composables/browser/useTabLeader/demo.vue (97%) rename {web/vue => vue/toolkit}/src/composables/browser/useTabLeader/index.test.ts (98%) rename {web/vue => vue/toolkit}/src/composables/browser/useTabLeader/index.ts (100%) rename {web/vue => vue/toolkit}/src/composables/component/index.ts (51%) rename {web/vue => vue/toolkit}/src/composables/component/unrefElement/index.test.ts (91%) rename {web/vue => vue/toolkit}/src/composables/component/unrefElement/index.ts (99%) create mode 100644 vue/toolkit/src/composables/component/useForwardExpose/index.test.ts create mode 100644 vue/toolkit/src/composables/component/useForwardExpose/index.ts create mode 100644 vue/toolkit/src/composables/component/useTemplateRefsList/index.test.ts rename {web/vue => vue/toolkit}/src/composables/component/useTemplateRefsList/index.ts (58%) create mode 100644 vue/toolkit/src/composables/debug/index.ts create mode 100644 vue/toolkit/src/composables/debug/useRenderCount/index.test.ts rename {web/vue/src/composables/component => vue/toolkit/src/composables/debug}/useRenderCount/index.ts (78%) rename {web/vue/src/composables/component => vue/toolkit/src/composables/debug}/useRenderInfo/index.test.ts (90%) rename {web/vue/src/composables/component => vue/toolkit/src/composables/debug}/useRenderInfo/index.ts (55%) rename {web/vue => vue/toolkit}/src/composables/index.ts (89%) rename {web/vue => vue/toolkit}/src/composables/lifecycle/index.ts (100%) rename {web/vue => vue/toolkit}/src/composables/lifecycle/tryOnBeforeMount/index.ts (98%) rename {web/vue => vue/toolkit}/src/composables/lifecycle/tryOnMounted/index.test.ts (94%) rename {web/vue => vue/toolkit}/src/composables/lifecycle/tryOnMounted/index.ts (97%) rename {web/vue => vue/toolkit}/src/composables/lifecycle/tryOnScopeDispose/index.test.ts (97%) rename {web/vue => vue/toolkit}/src/composables/lifecycle/tryOnScopeDispose/index.ts (98%) create mode 100644 vue/toolkit/src/composables/lifecycle/useMounted/index.test.ts rename {web/vue => vue/toolkit}/src/composables/lifecycle/useMounted/index.ts (90%) rename {web/vue => vue/toolkit}/src/composables/math/index.ts (100%) rename {web/vue => vue/toolkit}/src/composables/math/useClamp/index.test.ts (99%) rename {web/vue => vue/toolkit}/src/composables/math/useClamp/index.ts (98%) rename {web/vue => vue/toolkit}/src/composables/reactivity/broadcastedRef/index.test.ts (100%) rename {web/vue => vue/toolkit}/src/composables/reactivity/broadcastedRef/index.ts (99%) rename {web/vue => vue/toolkit}/src/composables/reactivity/index.ts (100%) rename {web/vue => vue/toolkit}/src/composables/reactivity/useCached/index.test.ts (99%) rename {web/vue => vue/toolkit}/src/composables/reactivity/useCached/index.ts (68%) rename {web/vue => vue/toolkit}/src/composables/reactivity/useLastChanged/index.test.ts (99%) rename {web/vue => vue/toolkit}/src/composables/reactivity/useLastChanged/index.ts (92%) rename {web/vue => vue/toolkit}/src/composables/reactivity/useSyncRefs/index.test.ts (99%) rename {web/vue => vue/toolkit}/src/composables/reactivity/useSyncRefs/index.ts (64%) rename {web/vue => vue/toolkit}/src/composables/state/index.ts (100%) rename {web/vue => vue/toolkit}/src/composables/state/useAppSharedState/index.test.ts (99%) rename {web/vue => vue/toolkit}/src/composables/state/useAppSharedState/index.ts (94%) rename {web/vue => vue/toolkit}/src/composables/state/useAsyncState/index.test.ts (99%) rename {web/vue => vue/toolkit}/src/composables/state/useAsyncState/index.ts (94%) rename {web/vue => vue/toolkit}/src/composables/state/useContextFactory/index.test.ts (99%) rename {web/vue => vue/toolkit}/src/composables/state/useContextFactory/index.ts (56%) rename {web/vue => vue/toolkit}/src/composables/state/useCounter/demo.vue (55%) rename {web/vue => vue/toolkit}/src/composables/state/useCounter/index.test.ts (86%) create mode 100644 vue/toolkit/src/composables/state/useCounter/index.ts rename {web/vue => vue/toolkit}/src/composables/state/useInjectionStore/index.test.ts (96%) rename {web/vue => vue/toolkit}/src/composables/state/useInjectionStore/index.ts (96%) rename {web/vue => vue/toolkit}/src/composables/state/useToggle/index.test.ts (100%) rename {web/vue => vue/toolkit}/src/composables/state/useToggle/index.ts (50%) rename {web/vue => vue/toolkit}/src/composables/storage/index.ts (100%) rename {web/vue => vue/toolkit}/src/composables/storage/useLocalStorage/index.test.ts (95%) create mode 100644 vue/toolkit/src/composables/storage/useLocalStorage/index.ts rename {web/vue => vue/toolkit}/src/composables/storage/useSessionStorage/index.test.ts (95%) create mode 100644 vue/toolkit/src/composables/storage/useSessionStorage/index.ts rename {web/vue => vue/toolkit}/src/composables/storage/useStorage/index.test.ts (63%) create mode 100644 vue/toolkit/src/composables/storage/useStorage/index.ts rename {web/vue => vue/toolkit}/src/composables/storage/useStorageAsync/index.test.ts (80%) create mode 100644 vue/toolkit/src/composables/storage/useStorageAsync/index.ts rename {web/vue => vue/toolkit}/src/composables/utilities/index.ts (100%) rename {web/vue => vue/toolkit}/src/composables/utilities/useOffsetPagination/index.test.ts (95%) rename {web/vue => vue/toolkit}/src/composables/utilities/useOffsetPagination/index.ts (96%) rename {web/vue => vue/toolkit}/src/index.ts (69%) rename {web/vue => vue/toolkit}/src/types/flush.ts (100%) rename {web/vue => vue/toolkit}/src/types/index.ts (52%) create mode 100644 vue/toolkit/src/types/ref.ts rename {web/vue => vue/toolkit}/src/types/resumable.ts (98%) rename {web/vue => vue/toolkit}/src/types/window.ts (86%) rename {web/vue => vue/toolkit}/src/utils/components.ts (90%) rename {web/vue => vue/toolkit}/src/utils/error.ts (98%) create mode 100644 vue/toolkit/src/utils/filters.test.ts create mode 100644 vue/toolkit/src/utils/filters.ts create mode 100644 vue/toolkit/src/utils/index.ts rename {web/vue => vue/toolkit}/tsconfig.json (100%) rename {web/vue => vue/toolkit}/tsdown.config.ts (98%) rename {web/vue => vue/toolkit}/vitest.config.ts (100%) delete mode 100644 web/vue/oxlint.config.ts delete mode 100644 web/vue/src/composables/component/useRenderCount/index.test.ts delete mode 100644 web/vue/src/composables/component/useTemplateRefsList/index.test.ts delete mode 100644 web/vue/src/composables/lifecycle/useMounted/index.test.ts delete mode 100644 web/vue/src/composables/state/useCounter/index.ts delete mode 100644 web/vue/src/composables/storage/useLocalStorage/index.ts delete mode 100644 web/vue/src/composables/storage/useSessionStorage/index.ts delete mode 100644 web/vue/src/composables/storage/useStorage/index.ts delete mode 100644 web/vue/src/composables/storage/useStorageAsync/index.ts delete mode 100644 web/vue/src/utils/index.ts diff --git a/.github/skills/monorepo/SKILL.md b/.github/skills/monorepo/SKILL.md new file mode 100644 index 0000000..fc10fe0 --- /dev/null +++ b/.github/skills/monorepo/SKILL.md @@ -0,0 +1,409 @@ +--- +name: monorepo +description: "Manage the @robonen/tools monorepo. Use when: installing dependencies, creating new packages, linting, building, testing, publishing, or scaffolding workspace packages. Covers pnpm catalogs, tsdown builds, oxlint presets, vitest projects, JSR/NPM publishing." +--- + +# Monorepo Management + +## Overview + +This is a pnpm workspace monorepo (`@robonen/tools`) with shared configs, strict dependency management via catalogs, and automated publishing. + +## Workspace Layout + +| Directory | Purpose | Examples | +|-----------|---------|---------| +| `configs/*` | Shared tooling configs | `oxlint`, `tsconfig`, `tsdown` | +| `core/*` | Platform-agnostic TS libraries | `stdlib`, `platform`, `encoding` | +| `vue/*` | Vue 3 packages | `primitives`, `toolkit` | +| `docs` | Nuxt 4 documentation site | — | +| `infra/*` | Infrastructure configs | `renovate` | + +## Installing Dependencies + +**Always use pnpm. Never use npm or yarn.** + +### Add a dependency to a specific package + +```bash +# Runtime dependency +pnpm -C add + +# Dev dependency +pnpm -C add -D +``` + +Examples: +```bash +pnpm -C core/stdlib add -D oxlint +pnpm -C vue/primitives add vue +``` + +### Use catalogs for shared versions + +Versions shared across multiple packages MUST use the pnpm catalog system. The catalog is defined in `pnpm-workspace.yaml`: + +```yaml +catalog: + vitest: ^4.0.18 + tsdown: ^0.21.0 + oxlint: ^1.2.0 + vue: ^3.5.28 + # ... etc +``` + +In `package.json`, reference catalog versions with the `catalog:` protocol: +```json +{ + "devDependencies": { + "vitest": "catalog:", + "oxlint": "catalog:" + } +} +``` + +**When to add to catalog:** If the dependency is used in 2+ packages, add it to `pnpm-workspace.yaml` under `catalog:` and use `catalog:` in each `package.json`. + +**When NOT to use catalog:** Package-specific dependencies used in only one package (e.g., `citty` in root). + +### Internal workspace dependencies + +Reference sibling packages with the workspace protocol: +```json +{ + "devDependencies": { + "@robonen/oxlint": "workspace:*", + "@robonen/tsconfig": "workspace:*", + "@robonen/tsdown": "workspace:*" + } +} +``` + +Nearly every package depends on these three shared config packages. + +### After installing + +Always run `pnpm install` at the root after editing `pnpm-workspace.yaml` or any `package.json` manually. + +## Creating a New Package + +> **Note:** The existing `bin/cli.ts` (`pnpm create`) is outdated — it generates Vite configs instead of tsdown and lacks oxlint/vitest setup. Follow the manual steps below instead. + +### 1. Create the directory + +Choose the correct parent based on package type: +- `core/` — Platform-agnostic TypeScript library +- `vue/` — Vue 3 library (needs jsdom, vue deps) +- `configs/` — Shared configuration package + +### 2. Create `package.json` + +```json +{ + "name": "@robonen/", + "version": "0.0.1", + "license": "Apache-2.0", + "description": "", + "packageManager": "pnpm@10.29.3", + "engines": { "node": ">=24.13.1" }, + "type": "module", + "files": ["dist"], + "exports": { + ".": { + "types": "./dist/index.d.ts", + "import": "./dist/index.js", + "require": "./dist/index.cjs" + } + }, + "scripts": { + "lint:check": "oxlint -c oxlint.config.ts", + "lint:fix": "oxlint -c oxlint.config.ts --fix", + "test": "vitest run", + "dev": "vitest dev", + "build": "tsdown" + }, + "devDependencies": { + "@robonen/oxlint": "workspace:*", + "@robonen/tsconfig": "workspace:*", + "@robonen/tsdown": "workspace:*", + "@stylistic/eslint-plugin": "catalog:", + "oxlint": "catalog:", + "tsdown": "catalog:" + } +} +``` + +For Vue packages, also add: +```json +{ + "dependencies": { + "vue": "catalog:", + "@vue/shared": "catalog:" + "@stylistic/eslint-plugin": "catalog:", + }, + "devDependencies": { + "@vue/test-utils": "catalog:" + } +} +``` + +For packages with sub-path exports (like `core/platform`): +```json +{ + "exports": { + "./browsers": { + "types": "./dist/browsers.d.ts", + "import": "./dist/browsers.js", + "require": "./dist/browsers.cjs" + } + } +} +``` + +### 3. Create `tsconfig.json` + +Node/core package: +```json +{ + "extends": "@robonen/tsconfig/tsconfig.json" +} +``` + +Vue package (needs DOM types and path aliases): +```json +{ + "extends": "@robonen/tsconfig/tsconfig.json", + "compilerOptions": { + "lib": ["DOM"], + "baseUrl": ".", + "paths": { "@/*": ["src/*"] } + } +} +``` + +### 4. Create `tsdown.config.ts` + +Standard: +```typescript +import { defineConfig } from 'tsdown'; +import { sharedConfig } from '@robonen/tsdown'; + +export default defineConfig({ + ...sharedConfig, + entry: ['src/index.ts'], +}); +``` + +Vue package (externalize vue, bundle internal deps): +```typescript +import { defineConfig } from 'tsdown'; +import { sharedConfig } from '@robonen/tsdown'; + +export default defineConfig({ + ...sharedConfig, + entry: ['src/index.ts'], + deps: { + neverBundle: ['vue'], + alwaysBundle: [/^@robonen\//, '@vue/shared'], + }, + inputOptions: { + resolve: { + alias: { '@vue/shared': '@vue/shared/dist/shared.esm-bundler.js' }, + }, + }, + define: { __DEV__: 'false' }, +}); +``` + +### 5. Create `oxlint.config.ts` + +Standard (node packages): +```typescript +import { defineConfig } from 'oxlint'; +import { compose, base, typescript, imports, stylistic } from '@robonen/oxlint'; + +export default defineConfig(compose(base, typescript, imports, stylistic)); +``` + +### 6. Create `vitest.config.ts` + +Node package: +```typescript +import { defineConfig } from 'vitest/config'; + +export default defineConfig({ + test: { + environment: 'node', + }, +}); +``` + +Vue package: +```typescript +import { defineConfig } from 'vitest/config'; +import { resolve } from 'node:path'; + +export default defineConfig({ + define: { __DEV__: 'true' }, + resolve: { + alias: { '@': resolve(__dirname, './src') }, + }, + test: { + environment: 'jsdom', + }, +}); +``` + +### 7. Create `jsr.json` (for publishable packages) + +```json +{ + "$schema": "https://jsr.io/schema/config-file.v1.json", + "name": "@robonen/", + "version": "0.0.1", + "exports": "./src/index.ts" +} +``` + +### 8. Create source files + +```bash +mkdir -p src +touch src/index.ts +``` + +### 9. Register with vitest projects + +Add the new `vitest.config.ts` path to the root `vitest.config.ts` `projects` array. + +### 10. Install dependencies + +```bash +pnpm install +``` + +### 11. Verify + +```bash +pnpm -C build +pnpm -C lint +pnpm -C test +``` + +## Linting + +Uses **oxlint** (not ESLint) with composable presets from `@robonen/oxlint`. + +### Run linting + +```bash +# Check lint errors (no auto-fix) +pnpm -C lint:check + +# Auto-fix lint errors +pnpm -C lint:fix + +# Check all packages +pnpm lint:check + +# Fix all packages +pnpm lint:fix +``` + +### Available presets + +| Preset | Purpose | +|--------|---------| +| `base` | ESLint core + Oxc + Unicorn rules | +| `typescript` | TypeScript rules (via overrides on `*.ts` files) | +| `imports` | Import ordering, cycles, duplicates | +| `stylistic` | Code style via `@stylistic/eslint-plugin` | +| `vue` | Vue 3 Composition API rules | +| `vitest` | Test file rules | +| `node` | Node.js-specific rules | + +Compose presets in `oxlint.config.ts`: +```typescript +import { defineConfig } from 'oxlint'; +import { compose, base, typescript, imports } from '@robonen/oxlint'; + +export default defineConfig(compose(base, typescript, imports)); + +**Recommended:** Include `stylistic` preset for code formatting: +```typescript +import { defineConfig } from 'oxlint'; +import { compose, base, typescript, imports, stylistic } from '@robonen/oxlint'; + +export default defineConfig(compose(base, typescript, imports, stylistic)); +``` + +When using `stylistic`, add `@stylistic/eslint-plugin` to devDependencies: +```json +{ + "devDependencies": { + "@stylistic/eslint-plugin": "catalog:" + } +} +``` +``` + +## Building + +```bash +# Build a specific package +pnpm -C build + +# Build all packages +pnpm build +``` + +All packages use `tsdown` with shared config from `@robonen/tsdown`. Output: ESM (`.js`/`.mjs`) + CJS (`.cjs`) + type declarations (`.d.ts`). Every bundle includes an Apache-2.0 license banner. + +## Testing + +```bash +# Run tests in a specific package +pnpm -C test + +# Run all tests (via vitest projects) +pnpm test + +# Interactive test UI +pnpm test:ui + +# Watch mode in a package +pnpm -C dev +``` + +Uses **vitest** with project-based configuration. Root `vitest.config.ts` lists all package vitest configs as projects. + +## Publishing + +Publishing is **automated** via GitHub Actions on push to `master`: + +1. CI builds and tests all packages +2. Publish workflow compares each package's `version` in `package.json` against npm registry +3. If version changed → `pnpm publish --access public` + +**To publish:** Bump the `version` in `package.json` (and `jsr.json` if present), then merge to `master`. + +**NPM scope:** All packages publish under `@robonen/`. + +## Documentation + +```bash +pnpm docs:dev # Start Nuxt dev server +pnpm docs:generate # Generate static site +pnpm docs:preview # Preview generated site +pnpm docs:extract # Extract API docs +``` + +## Key Conventions + +- **ESM-first:** All packages use `"type": "module"` +- **Strict TypeScript:** `strict: true`, `noUncheckedIndexedAccess: true`, `verbatimModuleSyntax: true` +- **License:** Apache-2.0 for all published packages +- **Node version:** ≥24.13.1 (set in `engines` and CI) +- **pnpm version:** Pinned in `packageManager` field +- **No barrel re-exports of entire modules** — export explicitly +- **`__DEV__` global:** `false` in builds, `true` in tests (Vue packages only) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index b2233aa..cee7b9e 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -35,7 +35,7 @@ jobs: run: pnpm build - name: Lint - run: pnpm lint + run: pnpm lint:check - name: Test run: pnpm test \ No newline at end of file diff --git a/configs/oxlint/README.md b/configs/oxlint/README.md index 96817c7..a60e84b 100644 --- a/configs/oxlint/README.md +++ b/configs/oxlint/README.md @@ -41,6 +41,19 @@ compose(base, typescript, { | `imports` | Import rules (cycles, duplicates, ordering) | | `node` | Node.js-specific rules | +## Rules Documentation + +Подробные описания правил и `good/bad` примеры вынесены в отдельную директорию: + +- `rules/README.md` +- `rules/base.md` +- `rules/typescript.md` +- `rules/vue.md` +- `rules/vitest.md` +- `rules/imports.md` +- `rules/node.md` +- `rules/stylistic.md` + ## API ### `compose(...configs: OxlintConfig[]): OxlintConfig` diff --git a/configs/oxlint/oxlint.config.ts b/configs/oxlint/oxlint.config.ts index 247c39b..8d6f15a 100644 --- a/configs/oxlint/oxlint.config.ts +++ b/configs/oxlint/oxlint.config.ts @@ -1,4 +1,4 @@ import { defineConfig } from 'oxlint'; -import { compose, base, typescript, imports } from '@robonen/oxlint'; +import { compose, base, typescript, imports, stylistic } from '@robonen/oxlint'; -export default defineConfig(compose(base, typescript, imports)); +export default defineConfig(compose(base, typescript, imports, stylistic)); diff --git a/configs/oxlint/package.json b/configs/oxlint/package.json index bd87274..258c8de 100644 --- a/configs/oxlint/package.json +++ b/configs/oxlint/package.json @@ -16,7 +16,7 @@ "url": "git+https://github.com/robonen/tools.git", "directory": "configs/oxlint" }, - "packageManager": "pnpm@10.29.3", + "packageManager": "pnpm@10.30.3", "engines": { "node": ">=24.13.1" }, @@ -27,12 +27,13 @@ "exports": { ".": { "types": "./dist/index.d.ts", - "import": "./dist/index.js", + "import": "./dist/index.mjs", "require": "./dist/index.cjs" } }, "scripts": { - "lint": "oxlint -c oxlint.config.ts", + "lint:check": "oxlint -c oxlint.config.ts", + "lint:fix": "oxlint -c oxlint.config.ts --fix", "test": "vitest run", "dev": "vitest dev", "build": "tsdown" @@ -41,11 +42,18 @@ "@robonen/oxlint": "workspace:*", "@robonen/tsconfig": "workspace:*", "@robonen/tsdown": "workspace:*", + "@stylistic/eslint-plugin": "catalog:", "oxlint": "catalog:", "tsdown": "catalog:" }, "peerDependencies": { - "oxlint": ">=1.0.0" + "oxlint": ">=1.0.0", + "@stylistic/eslint-plugin": ">=4.0.0" + }, + "peerDependenciesMeta": { + "@stylistic/eslint-plugin": { + "optional": true + } }, "publishConfig": { "access": "public" diff --git a/configs/oxlint/rules/README.md b/configs/oxlint/rules/README.md new file mode 100644 index 0000000..328ad38 --- /dev/null +++ b/configs/oxlint/rules/README.md @@ -0,0 +1,21 @@ +# Rules Reference + +Документация по preset-ам `@robonen/oxlint`: что включает каждый preset и какие правила чаще всего влияют на код. + +## Presets + +- [base](./base.md) +- [typescript](./typescript.md) +- [vue](./vue.md) +- [vitest](./vitest.md) +- [imports](./imports.md) +- [node](./node.md) +- [stylistic](./stylistic.md) + +## Как читать + +- `Purpose` — зачем preset подключать. +- `Key Rules` — ключевые правила из preset-а (не полный dump). +- `Examples` — минимальные `good/bad` примеры. + +Для точного источника правил см. файлы в `configs/oxlint/src/presets/*.ts`. \ No newline at end of file diff --git a/configs/oxlint/rules/base.md b/configs/oxlint/rules/base.md new file mode 100644 index 0000000..c31327b --- /dev/null +++ b/configs/oxlint/rules/base.md @@ -0,0 +1,34 @@ +# base preset + +## Purpose + +Базовый quality-профиль для JS/TS-проектов: корректность, анти-паттерны, безопасные дефолты. + +## Key Rules + +- `eslint/eqeqeq`: запрещает `==`, требует `===`. +- `eslint/no-unused-vars`: запрещает неиспользуемые переменные (кроме `_name`). +- `eslint/no-eval`, `eslint/no-var`, `eslint/prefer-const`. +- `unicorn/prefer-node-protocol`: требует `node:` для built-in модулей. +- `unicorn/no-thenable`: запрещает thenable-объекты. +- `oxc/*` correctness правила (`bad-comparison-sequence`, `missing-throw` и др.). + +## Examples + +```ts +// ✅ Good +import { readFile } from 'node:fs/promises'; + +const id = 42; +if (id === 42) { + throw new Error('unexpected'); +} + +// ❌ Bad +import { readFile } from 'fs/promises'; + +var id = 42; +if (id == '42') { + throw 'unexpected'; +} +``` diff --git a/configs/oxlint/rules/imports.md b/configs/oxlint/rules/imports.md new file mode 100644 index 0000000..51e7dfc --- /dev/null +++ b/configs/oxlint/rules/imports.md @@ -0,0 +1,27 @@ +# imports preset + +## Purpose + +Чистые границы модулей и предсказуемые импорты. + +## Key Rules + +- `import/no-duplicates`. +- `import/no-self-import`. +- `import/no-cycle` (warn). +- `import/no-mutable-exports`. +- `import/consistent-type-specifier-style`: `prefer-top-level`. + +## Examples + +```ts +// ✅ Good +import type { User } from './types'; +import { getUser } from './service'; + +// ❌ Bad +import { getUser } from './service'; +import { getUser as getUser2 } from './service'; + +export let state = 0; +``` diff --git a/configs/oxlint/rules/node.md b/configs/oxlint/rules/node.md new file mode 100644 index 0000000..1193619 --- /dev/null +++ b/configs/oxlint/rules/node.md @@ -0,0 +1,22 @@ +# node preset + +## Purpose + +Node.js-правила и окружение `env.node = true`. + +## Key Rules + +- `node/no-exports-assign`: запрещает перезапись `exports`. +- `node/no-new-require`: запрещает `new require(...)`. + +## Examples + +```ts +// ✅ Good +module.exports = { run }; +const mod = require('./mod'); + +// ❌ Bad +exports = { run }; +const bad = new require('./mod'); +``` diff --git a/configs/oxlint/rules/stylistic.md b/configs/oxlint/rules/stylistic.md new file mode 100644 index 0000000..683adcc --- /dev/null +++ b/configs/oxlint/rules/stylistic.md @@ -0,0 +1,51 @@ +# stylistic preset + +## Purpose + +Форматирование через `@stylistic/eslint-plugin` (отступы, пробелы, скобки, переносы, TS/JSX-стиль). + +## Defaults + +- `indent: 2` +- `quotes: single` +- `semi: always` +- `braceStyle: stroustrup` +- `commaDangle: always-multiline` +- `arrowParens: as-needed` + +## Key Rules + +- `@stylistic/indent`, `@stylistic/no-tabs`. +- `@stylistic/quotes`, `@stylistic/semi`. +- `@stylistic/object-curly-spacing`, `@stylistic/comma-spacing`. +- `@stylistic/arrow-spacing`, `@stylistic/space-before-function-paren`. +- `@stylistic/max-statements-per-line`. +- `@stylistic/no-mixed-operators`. +- `@stylistic/member-delimiter-style` (TS). + +## Examples + +```ts +// ✅ Good +type User = { + id: string; + role: 'admin' | 'user'; +}; + +const value = condition + ? 'yes' + : 'no'; + +const sum = (a: number, b: number) => a + b; + +// ❌ Bad +type User = { + id: string + role: 'admin' | 'user' +} + +const value = condition ? 'yes' : 'no'; const x = 1; +const sum=(a:number,b:number)=>{ return a+b }; +``` + +Полный список правил и их настройки см. в `src/presets/stylistic.ts`. diff --git a/configs/oxlint/rules/typescript.md b/configs/oxlint/rules/typescript.md new file mode 100644 index 0000000..5a74749 --- /dev/null +++ b/configs/oxlint/rules/typescript.md @@ -0,0 +1,33 @@ +# typescript preset + +## Purpose + +TypeScript-правила для `.ts/.tsx/.mts/.cts` через `overrides`. + +## Key Rules + +- `typescript/consistent-type-imports`: выносит типы в `import type`. +- `typescript/no-import-type-side-effects`: запрещает сайд-эффекты в type import. +- `typescript/prefer-as-const`. +- `typescript/no-namespace`, `typescript/triple-slash-reference`. +- `typescript/no-wrapper-object-types`: запрещает `String`, `Number`, `Boolean`. + +## Examples + +```ts +// ✅ Good +import type { User } from './types'; + +const status = 'ok' as const; +interface Payload { + value: string; +} + +// ❌ Bad +import { User } from './types'; + +type Boxed = String; +namespace Legacy { + export const x = 1; +} +``` diff --git a/configs/oxlint/rules/vitest.md b/configs/oxlint/rules/vitest.md new file mode 100644 index 0000000..c160a56 --- /dev/null +++ b/configs/oxlint/rules/vitest.md @@ -0,0 +1,34 @@ +# vitest preset + +## Purpose + +Правила для тестов (`*.test.*`, `*.spec.*`, `test/**`, `__tests__/**`). + +## Key Rules + +- `vitest/no-conditional-tests`. +- `vitest/no-import-node-test`. +- `vitest/prefer-to-be-truthy`, `vitest/prefer-to-be-falsy`. +- `vitest/prefer-to-have-length`. +- Relaxations: `eslint/no-unused-vars` и `typescript/no-explicit-any` выключены для тестов. + +## Examples + +```ts +// ✅ Good +import { describe, it, expect } from 'vitest'; + +describe('list', () => { + it('has items', () => { + expect([1, 2, 3]).toHaveLength(3); + expect(true).toBeTruthy(); + }); +}); + +// ❌ Bad +if (process.env.CI) { + it('conditionally runs', () => { + expect(true).toBe(true); + }); +} +``` diff --git a/configs/oxlint/rules/vue.md b/configs/oxlint/rules/vue.md new file mode 100644 index 0000000..dfa5bb5 --- /dev/null +++ b/configs/oxlint/rules/vue.md @@ -0,0 +1,30 @@ +# vue preset + +## Purpose + +Правила для Vue 3 с упором на Composition API и ` + + + +``` diff --git a/configs/oxlint/src/compose.ts b/configs/oxlint/src/compose.ts index 96993fc..b9f87ee 100644 --- a/configs/oxlint/src/compose.ts +++ b/configs/oxlint/src/compose.ts @@ -31,6 +31,7 @@ function deepMerge(target: Record, source: Record typeof e === 'string' ? e : e.specifier)); + + for (const entry of config.jsPlugins) { + const specifier = typeof entry === 'string' ? entry : entry.specifier; + if (!seen.has(specifier)) { + seen.add(specifier); + existing.push(entry); + } + } + + result.jsPlugins = existing; + } + // Categories — shallow merge if (config.categories) { result.categories = { ...result.categories, ...config.categories }; diff --git a/configs/oxlint/src/index.ts b/configs/oxlint/src/index.ts index b026d25..4bbe5bb 100644 --- a/configs/oxlint/src/index.ts +++ b/configs/oxlint/src/index.ts @@ -2,7 +2,7 @@ export { compose } from './compose'; /* Presets */ -export { base, typescript, vue, vitest, imports, node } from './presets'; +export { base, typescript, vue, vitest, imports, node, stylistic } from './presets'; /* Types */ export type { @@ -10,6 +10,7 @@ export type { OxlintOverride, OxlintEnv, OxlintGlobals, + ExternalPluginEntry, AllowWarnDeny, DummyRule, DummyRuleMap, diff --git a/configs/oxlint/src/presets/imports.ts b/configs/oxlint/src/presets/imports.ts index f3a6276..3e3a3d1 100644 --- a/configs/oxlint/src/presets/imports.ts +++ b/configs/oxlint/src/presets/imports.ts @@ -16,5 +16,7 @@ export const imports: OxlintConfig = { 'import/no-commonjs': 'warn', 'import/no-empty-named-blocks': 'warn', 'import/consistent-type-specifier-style': ['warn', 'prefer-top-level'], + + 'sort-imports': ['warn', { ignoreDeclarationSort: false, ignoreMemberSort: false, ignoreCase: true, allowSeparatedGroups: true }], }, }; diff --git a/configs/oxlint/src/presets/index.ts b/configs/oxlint/src/presets/index.ts index c17cbeb..b05f600 100644 --- a/configs/oxlint/src/presets/index.ts +++ b/configs/oxlint/src/presets/index.ts @@ -4,3 +4,4 @@ export { vue } from './vue'; export { vitest } from './vitest'; export { imports } from './imports'; export { node } from './node'; +export { stylistic } from './stylistic'; diff --git a/configs/oxlint/src/presets/stylistic.ts b/configs/oxlint/src/presets/stylistic.ts new file mode 100644 index 0000000..a69bccc --- /dev/null +++ b/configs/oxlint/src/presets/stylistic.ts @@ -0,0 +1,162 @@ +import type { OxlintConfig } from '../types'; + +/** + * Stylistic formatting rules via `@stylistic/eslint-plugin`. + * + * Uses the plugin's `customize()` defaults: + * - indent: 2 + * - quotes: single + * - semi: true + * - braceStyle: stroustrup + * - commaDangle: always-multiline + * - arrowParens: false (as-needed) + * - blockSpacing: true + * - quoteProps: consistent-as-needed + * - jsx: true + * + * Requires `@stylistic/eslint-plugin` to be installed. + * + * @see https://eslint.style/guide/config-presets + */ +export const stylistic: OxlintConfig = { + jsPlugins: ['@stylistic/eslint-plugin'], + + rules: { + /* ── spacing & layout ─────────────────────────────────── */ + '@stylistic/array-bracket-spacing': ['error', 'never'], + '@stylistic/arrow-spacing': ['error', { after: true, before: true }], + '@stylistic/block-spacing': ['error', 'always'], + '@stylistic/comma-spacing': ['error', { after: true, before: false }], + '@stylistic/computed-property-spacing': ['error', 'never', { enforceForClassMembers: true }], + '@stylistic/dot-location': ['error', 'property'], + '@stylistic/key-spacing': ['error', { afterColon: true, beforeColon: false }], + '@stylistic/keyword-spacing': ['error', { after: true, before: true }], + '@stylistic/no-mixed-spaces-and-tabs': 'error', + '@stylistic/no-multi-spaces': 'error', + '@stylistic/no-trailing-spaces': 'error', + '@stylistic/no-whitespace-before-property': 'error', + '@stylistic/rest-spread-spacing': ['error', 'never'], + '@stylistic/semi-spacing': ['error', { after: true, before: false }], + '@stylistic/space-before-blocks': ['error', 'always'], + '@stylistic/space-before-function-paren': ['error', { anonymous: 'always', asyncArrow: 'always', named: 'never' }], + '@stylistic/space-in-parens': ['error', 'never'], + '@stylistic/space-infix-ops': 'error', + '@stylistic/space-unary-ops': ['error', { nonwords: false, words: true }], + '@stylistic/template-curly-spacing': 'error', + '@stylistic/template-tag-spacing': ['error', 'never'], + + /* ── braces & blocks ──────────────────────────────────── */ + '@stylistic/brace-style': ['error', 'stroustrup', { allowSingleLine: true }], + '@stylistic/arrow-parens': ['error', 'as-needed', { requireForBlockBody: true }], + '@stylistic/no-extra-parens': ['error', 'functions'], + '@stylistic/no-floating-decimal': 'error', + '@stylistic/wrap-iife': ['error', 'any', { functionPrototypeMethods: true }], + '@stylistic/new-parens': 'error', + '@stylistic/padded-blocks': ['error', { blocks: 'never', classes: 'never', switches: 'never' }], + + /* ── punctuation ──────────────────────────────────────── */ + '@stylistic/comma-dangle': ['error', 'always-multiline'], + '@stylistic/comma-style': ['error', 'last'], + '@stylistic/semi': ['error', 'always'], + '@stylistic/quotes': ['error', 'single', { allowTemplateLiterals: 'always', avoidEscape: false }], + '@stylistic/quote-props': ['error', 'consistent-as-needed'], + + /* ── indentation ──────────────────────────────────────── */ + '@stylistic/indent': ['error', 2, { + ArrayExpression: 1, + CallExpression: { arguments: 1 }, + flatTernaryExpressions: false, + FunctionDeclaration: { body: 1, parameters: 1, returnType: 1 }, + FunctionExpression: { body: 1, parameters: 1, returnType: 1 }, + ignoreComments: false, + ignoredNodes: [ + 'TSUnionType', + 'TSIntersectionType', + ], + ImportDeclaration: 1, + MemberExpression: 1, + ObjectExpression: 1, + offsetTernaryExpressions: true, + outerIIFEBody: 1, + SwitchCase: 1, + tabLength: 2, + VariableDeclarator: 1, + }], + '@stylistic/indent-binary-ops': ['error', 2], + '@stylistic/no-tabs': 'error', + + /* ── line breaks ──────────────────────────────────────── */ + '@stylistic/eol-last': 'error', + '@stylistic/no-multiple-empty-lines': ['error', { max: 1, maxBOF: 0, maxEOF: 0 }], + '@stylistic/lines-between-class-members': ['error', 'always', { exceptAfterSingleLine: true }], + '@stylistic/max-statements-per-line': ['error', { max: 1 }], + '@stylistic/multiline-ternary': ['error', 'always-multiline'], + '@stylistic/operator-linebreak': ['error', 'before'], + '@stylistic/object-curly-spacing': ['error', 'always'], + + /* ── generators ───────────────────────────────────────── */ + '@stylistic/generator-star-spacing': ['error', { after: true, before: false }], + '@stylistic/yield-star-spacing': ['error', { after: true, before: false }], + + /* ── operators & mixed ────────────────────────────────── */ + '@stylistic/no-mixed-operators': ['error', { + allowSamePrecedence: true, + groups: [ + ['==', '!=', '===', '!==', '>', '>=', '<', '<='], + ['&&', '||'], + ['in', 'instanceof'], + ], + }], + + /* ── typescript styling ───────────────────────────────── */ + '@stylistic/member-delimiter-style': ['error', { + multiline: { delimiter: 'semi', requireLast: true }, + multilineDetection: 'brackets', + overrides: { + interface: { + multiline: { delimiter: 'semi', requireLast: true }, + }, + }, + singleline: { delimiter: 'semi' }, + }], + '@stylistic/type-annotation-spacing': ['error', {}], + '@stylistic/type-generic-spacing': 'error', + '@stylistic/type-named-tuple-spacing': 'error', + + /* ── comments ─────────────────────────────────────────── */ + '@stylistic/spaced-comment': ['error', 'always', { + block: { balanced: true, exceptions: ['*'], markers: ['!'] }, + line: { exceptions: ['/', '#'], markers: ['/'] }, + }], + + /* ── jsx ───────────────────────────────────────────────── */ + '@stylistic/jsx-closing-bracket-location': 'error', + '@stylistic/jsx-closing-tag-location': 'error', + '@stylistic/jsx-curly-brace-presence': ['error', { propElementValues: 'always' }], + '@stylistic/jsx-curly-newline': 'error', + '@stylistic/jsx-curly-spacing': ['error', 'never'], + '@stylistic/jsx-equals-spacing': 'error', + '@stylistic/jsx-first-prop-new-line': 'error', + '@stylistic/jsx-function-call-newline': ['error', 'multiline'], + '@stylistic/jsx-indent-props': ['error', 2], + '@stylistic/jsx-max-props-per-line': ['error', { maximum: 1, when: 'multiline' }], + '@stylistic/jsx-one-expression-per-line': ['error', { allow: 'single-child' }], + '@stylistic/jsx-quotes': 'error', + '@stylistic/jsx-tag-spacing': ['error', { + afterOpening: 'never', + beforeClosing: 'never', + beforeSelfClosing: 'always', + closingSlash: 'never', + }], + '@stylistic/jsx-wrap-multilines': ['error', { + arrow: 'parens-new-line', + assignment: 'parens-new-line', + condition: 'parens-new-line', + declaration: 'parens-new-line', + logical: 'parens-new-line', + prop: 'parens-new-line', + propertyValue: 'parens-new-line', + return: 'parens-new-line', + }], + }, +}; diff --git a/configs/oxlint/src/types.ts b/configs/oxlint/src/types.ts index 150b065..57ccf9f 100644 --- a/configs/oxlint/src/types.ts +++ b/configs/oxlint/src/types.ts @@ -11,6 +11,7 @@ export type { OxlintOverride, OxlintEnv, OxlintGlobals, + ExternalPluginEntry, AllowWarnDeny, DummyRule, DummyRuleMap, diff --git a/configs/oxlint/test/compose.test.ts b/configs/oxlint/test/compose.test.ts index 1ff07e1..fd12f82 100644 --- a/configs/oxlint/test/compose.test.ts +++ b/configs/oxlint/test/compose.test.ts @@ -143,4 +143,29 @@ describe('compose', () => { expect(result.env).toBeUndefined(); expect(result.settings).toBeUndefined(); }); + + it('should concatenate jsPlugins with dedup by specifier', () => { + const a: OxlintConfig = { jsPlugins: ['eslint-plugin-foo'] }; + const b: OxlintConfig = { jsPlugins: ['eslint-plugin-foo', 'eslint-plugin-bar'] }; + + const result = compose(a, b); + expect(result.jsPlugins).toEqual(['eslint-plugin-foo', 'eslint-plugin-bar']); + }); + + it('should dedup jsPlugins with mixed string and object entries', () => { + const a: OxlintConfig = { jsPlugins: ['eslint-plugin-foo'] }; + const b: OxlintConfig = { jsPlugins: [{ name: 'foo', specifier: 'eslint-plugin-foo' }] }; + + const result = compose(a, b); + expect(result.jsPlugins).toEqual(['eslint-plugin-foo']); + }); + + it('should keep jsPlugins and plugins independent', () => { + const a: OxlintConfig = { plugins: ['eslint'], jsPlugins: ['eslint-plugin-foo'] }; + const b: OxlintConfig = { plugins: ['typescript'], jsPlugins: ['eslint-plugin-bar'] }; + + const result = compose(a, b); + expect(result.plugins).toEqual(['eslint', 'typescript']); + expect(result.jsPlugins).toEqual(['eslint-plugin-foo', 'eslint-plugin-bar']); + }); }); diff --git a/configs/oxlint/tsconfig.json b/configs/oxlint/tsconfig.json index ab255ac..0eaee15 100644 --- a/configs/oxlint/tsconfig.json +++ b/configs/oxlint/tsconfig.json @@ -1,3 +1,9 @@ { - "extends": "@robonen/tsconfig/tsconfig.json" + "extends": "@robonen/tsconfig/tsconfig.json", + "compilerOptions": { + "rootDir": "src" + }, + "include": [ + "src/**/*.ts" + ] } diff --git a/configs/tsconfig/package.json b/configs/tsconfig/package.json index 8287506..eedf8d4 100644 --- a/configs/tsconfig/package.json +++ b/configs/tsconfig/package.json @@ -15,7 +15,7 @@ "url": "git+https://github.com/robonen/tools.git", "directory": "packages/tsconfig" }, - "packageManager": "pnpm@10.29.3", + "packageManager": "pnpm@10.30.3", "engines": { "node": ">=24.13.1" }, diff --git a/configs/tsdown/package.json b/configs/tsdown/package.json index 44a7213..029752f 100644 --- a/configs/tsdown/package.json +++ b/configs/tsdown/package.json @@ -15,7 +15,7 @@ "url": "git+https://github.com/robonen/tools.git", "directory": "configs/tsdown" }, - "packageManager": "pnpm@10.29.3", + "packageManager": "pnpm@10.30.3", "engines": { "node": ">=24.13.1" }, diff --git a/core/encoding/jsr.json b/core/encoding/jsr.json new file mode 100644 index 0000000..4491833 --- /dev/null +++ b/core/encoding/jsr.json @@ -0,0 +1,6 @@ +{ + "$schema": "https://jsr.io/schema/config-file.v1.json", + "name": "@robonen/encoding", + "version": "0.0.1", + "exports": "./src/index.ts" +} diff --git a/core/encoding/oxlint.config.ts b/core/encoding/oxlint.config.ts new file mode 100644 index 0000000..a45b20d --- /dev/null +++ b/core/encoding/oxlint.config.ts @@ -0,0 +1,14 @@ +import { defineConfig } from 'oxlint'; +import { compose, base, typescript, imports, stylistic } from '@robonen/oxlint'; + +export default defineConfig(compose(base, typescript, imports, stylistic, { + overrides: [ + { + files: ['src/qr/qr-code.ts'], + rules: { + '@stylistic/max-statements-per-line': 'off', + '@stylistic/no-mixed-operators': 'off', + }, + }, + ], +})); diff --git a/core/encoding/package.json b/core/encoding/package.json new file mode 100644 index 0000000..3ae2402 --- /dev/null +++ b/core/encoding/package.json @@ -0,0 +1,47 @@ +{ + "name": "@robonen/encoding", + "version": "0.0.1", + "license": "Apache-2.0", + "description": "Encoding utilities for TypeScript", + "keywords": [ + "encoding", + "tools" + ], + "author": "Robonen Andrew ", + "repository": { + "type": "git", + "url": "git+https://github.com/robonen/tools.git", + "directory": "core/encoding" + }, + "packageManager": "pnpm@10.30.3", + "engines": { + "node": ">=24.13.1" + }, + "type": "module", + "files": [ + "dist" + ], + "exports": { + ".": { + "types": "./dist/index.d.ts", + "import": "./dist/index.js", + "require": "./dist/index.cjs" + } + }, + "scripts": { + "lint:check": "oxlint -c oxlint.config.ts", + "lint:fix": "oxlint -c oxlint.config.ts --fix", + "test": "vitest run", + "dev": "vitest dev", + "bench": "vitest bench", + "build": "tsdown" + }, + "devDependencies": { + "@robonen/oxlint": "workspace:*", + "@robonen/tsconfig": "workspace:*", + "@robonen/tsdown": "workspace:*", + "@stylistic/eslint-plugin": "catalog:", + "oxlint": "catalog:", + "tsdown": "catalog:" + } +} diff --git a/core/encoding/src/index.ts b/core/encoding/src/index.ts new file mode 100644 index 0000000..0de9894 --- /dev/null +++ b/core/encoding/src/index.ts @@ -0,0 +1,2 @@ +export * from './reed-solomon'; +export * from './qr'; diff --git a/core/encoding/src/qr/constants.ts b/core/encoding/src/qr/constants.ts new file mode 100644 index 0000000..bf16150 --- /dev/null +++ b/core/encoding/src/qr/constants.ts @@ -0,0 +1,67 @@ +import type { QrCodeEcc, QrSegmentMode } from './types'; + +/* -- ECC Levels -- */ + +export const LOW: QrCodeEcc = [0, 1]; // ~7% recovery +export const MEDIUM: QrCodeEcc = [1, 0]; // ~15% recovery +export const QUARTILE: QrCodeEcc = [2, 3]; // ~25% recovery +export const HIGH: QrCodeEcc = [3, 2]; // ~30% recovery + +export const EccMap = { + L: LOW, + M: MEDIUM, + Q: QUARTILE, + H: HIGH, +} as const; + +/* -- Segment Modes -- */ + +export const MODE_NUMERIC: QrSegmentMode = [0x1, 10, 12, 14]; +export const MODE_ALPHANUMERIC: QrSegmentMode = [0x2, 9, 11, 13]; +export const MODE_BYTE: QrSegmentMode = [0x4, 8, 16, 16]; + +/* -- Version Limits -- */ + +export const MIN_VERSION = 1; +export const MAX_VERSION = 40; + +/* -- Penalty Constants -- */ + +export const PENALTY_N1 = 3; +export const PENALTY_N2 = 3; +export const PENALTY_N3 = 40; +export const PENALTY_N4 = 10; + +/* -- Character Sets & Patterns -- */ + +export const NUMERIC_REGEX = /^[0-9]*$/; +export const ALPHANUMERIC_REGEX = /^[A-Z0-9 $%*+./:_-]*$/; +export const ALPHANUMERIC_CHARSET = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ $%*+-./:' as const; + +/** Pre-computed charCode → alphanumeric index lookup (0xFF = invalid). O(1) instead of O(45) indexOf. */ +export const ALPHANUMERIC_MAP = /* @__PURE__ */ (() => { + const map = new Uint8Array(128).fill(0xFF); + for (let i = 0; i < ALPHANUMERIC_CHARSET.length; i++) + map[ALPHANUMERIC_CHARSET.charCodeAt(i)] = i; + return map; +})(); + +/* -- ECC Lookup Tables -- */ + +// prettier-ignore +export const ECC_CODEWORDS_PER_BLOCK: number[][] = [ + // 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40 + [-1, 7, 10, 15, 20, 26, 18, 20, 24, 30, 18, 20, 24, 26, 30, 22, 24, 28, 30, 28, 28, 28, 28, 30, 30, 26, 28, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30], // Low + [-1, 10, 16, 26, 18, 24, 16, 18, 22, 22, 26, 30, 22, 22, 24, 24, 28, 28, 26, 26, 26, 26, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28], // Medium + [-1, 13, 22, 18, 26, 18, 24, 18, 22, 20, 24, 28, 26, 24, 20, 30, 24, 28, 28, 26, 30, 28, 30, 30, 30, 30, 28, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30], // Quartile + [-1, 17, 28, 22, 16, 22, 28, 26, 26, 24, 28, 24, 28, 22, 24, 24, 30, 28, 28, 26, 28, 30, 24, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30], // High +]; + +// prettier-ignore +export const NUM_ERROR_CORRECTION_BLOCKS: number[][] = [ + // 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40 + [-1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 4, 4, 4, 4, 4, 6, 6, 6, 6, 7, 8, 8, 9, 9, 10, 12, 12, 12, 13, 14, 15, 16, 17, 18, 19, 19, 20, 21, 22, 24, 25], // Low + [-1, 1, 1, 1, 2, 2, 4, 4, 4, 5, 5, 5, 8, 9, 9, 10, 10, 11, 13, 14, 16, 17, 17, 18, 20, 21, 23, 25, 26, 28, 29, 31, 33, 35, 37, 38, 40, 43, 45, 47, 49], // Medium + [-1, 1, 1, 2, 2, 4, 4, 6, 6, 8, 8, 8, 10, 12, 16, 12, 17, 16, 18, 21, 20, 23, 23, 25, 27, 29, 34, 34, 35, 38, 40, 43, 45, 48, 51, 53, 56, 59, 62, 65, 68], // Quartile + [-1, 1, 1, 2, 4, 4, 4, 5, 6, 8, 8, 11, 11, 16, 16, 18, 16, 19, 21, 25, 25, 25, 34, 30, 32, 35, 37, 40, 42, 45, 48, 51, 54, 57, 60, 63, 66, 70, 74, 77, 81], // High +]; diff --git a/core/encoding/src/qr/encode.ts b/core/encoding/src/qr/encode.ts new file mode 100644 index 0000000..ed1a7e7 --- /dev/null +++ b/core/encoding/src/qr/encode.ts @@ -0,0 +1,94 @@ +import type { QrCodeEcc } from './types'; +import { HIGH, MAX_VERSION, MEDIUM, MIN_VERSION, QUARTILE } from './constants'; +import { QrCode } from './qr-code'; +import { makeBytes, makeSegments } from './segment'; +import type { QrSegment } from './segment'; +import { appendBits, assert, getNumDataCodewords, getTotalBits, numCharCountBits } from './utils'; + +/** + * Returns a QR Code representing the given Unicode text string at the given error correction level. + * As a conservative upper bound, this function is guaranteed to succeed for strings that have 738 or fewer + * Unicode code points (not UTF-16 code units) if the low error correction level is used. + * The smallest possible QR Code version is automatically chosen for the output. + */ +export function encodeText(text: string, ecl: QrCodeEcc): QrCode { + const segs = makeSegments(text); + return encodeSegments(segs, ecl); +} + +/** + * Returns a QR Code representing the given binary data at the given error correction level. + * This function always encodes using the binary segment mode, not any text mode. + * The maximum number of bytes allowed is 2953. + */ +export function encodeBinary(data: Readonly, ecl: QrCodeEcc): QrCode { + const seg = makeBytes(data); + return encodeSegments([seg], ecl); +} + +/** + * Returns a QR Code representing the given segments with the given encoding parameters. + * The smallest possible QR Code version within the given range is automatically chosen for the output. + * This is a mid-level API; the high-level API is encodeText() and encodeBinary(). + */ +export function encodeSegments( + segs: Readonly, + ecl: QrCodeEcc, + minVersion = 1, + maxVersion = 40, + mask = -1, + boostEcl = true, +): QrCode { + if (!(MIN_VERSION <= minVersion && minVersion <= maxVersion && maxVersion <= MAX_VERSION) + || mask < -1 || mask > 7) + throw new RangeError('Invalid value'); + + // Find the minimal version number to use + let version: number; + let dataUsedBits: number; + for (version = minVersion; ; version++) { + const dataCapacityBits = getNumDataCodewords(version, ecl) * 8; + const usedBits = getTotalBits(segs, version); + if (usedBits <= dataCapacityBits) { + dataUsedBits = usedBits; + break; + } + if (version >= maxVersion) + throw new RangeError('Data too long'); + } + + // Increase the error correction level while the data still fits in the current version number + for (const newEcl of [MEDIUM, QUARTILE, HIGH]) { + if (boostEcl && dataUsedBits! <= getNumDataCodewords(version, newEcl) * 8) + ecl = newEcl; + } + + // Concatenate all segments to create the data bit string + const bb: number[] = []; + for (const seg of segs) { + appendBits(seg.mode[0], 4, bb); + appendBits(seg.numChars, numCharCountBits(seg.mode, version), bb); + for (const b of seg.bitData) + bb.push(b); + } + assert(bb.length === dataUsedBits!); + + // Add terminator and pad up to a byte if applicable + const dataCapacityBits = getNumDataCodewords(version, ecl) * 8; + assert(bb.length <= dataCapacityBits); + appendBits(0, Math.min(4, dataCapacityBits - bb.length), bb); + appendBits(0, (8 - bb.length % 8) % 8, bb); + assert(bb.length % 8 === 0); + + // Pad with alternating bytes until data capacity is reached + for (let padByte = 0xEC; bb.length < dataCapacityBits; padByte ^= 0xEC ^ 0x11) + appendBits(padByte, 8, bb); + + // Pack bits into bytes in big endian + const dataCodewords = Array.from({ length: Math.ceil(bb.length / 8) }, () => 0); + for (let i = 0; i < bb.length; i++) + dataCodewords[i >>> 3]! |= bb[i]! << (7 - (i & 7)); + + // Create the QR Code object + return new QrCode(version, ecl, dataCodewords, mask); +} diff --git a/core/encoding/src/qr/index.bench.ts b/core/encoding/src/qr/index.bench.ts new file mode 100644 index 0000000..8d6f967 --- /dev/null +++ b/core/encoding/src/qr/index.bench.ts @@ -0,0 +1,96 @@ +import { bench, describe } from 'vitest'; +import { encodeBinary, encodeSegments, encodeText, makeSegments, LOW, EccMap } from '.'; + +/* -- Test data -- */ + +const SHORT_TEXT = 'Hello'; +const MEDIUM_TEXT = 'https://example.com/path?query=value&foo=bar'; +const LONG_TEXT = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit.'; +const NUMERIC_TEXT = '314159265358979323846264338327950288419716939937510'; +const ALPHANUMERIC_TEXT = 'HELLO WORLD 12345'; + +const SMALL_BINARY = Array.from({ length: 32 }, (_, i) => i); +const MEDIUM_BINARY = Array.from({ length: 256 }, (_, i) => i % 256); + +/* -- Precomputed segments for isolated encodeSegments benchmark -- */ +const precomputedSegs = makeSegments(MEDIUM_TEXT); + +/* -- encodeText benchmarks -- */ + +describe('encodeText', () => { + bench('short text (5 chars)', () => { + encodeText(SHORT_TEXT, LOW); + }); + + bench('medium text (URL ~44 chars)', () => { + encodeText(MEDIUM_TEXT, LOW); + }); + + bench('long text (~270 chars)', () => { + encodeText(LONG_TEXT, LOW); + }); + + bench('numeric text (50 digits)', () => { + encodeText(NUMERIC_TEXT, LOW); + }); + + bench('alphanumeric text (17 chars)', () => { + encodeText(ALPHANUMERIC_TEXT, LOW); + }); +}); + +/* -- ECC level impact -- */ + +describe('encodeText — ECC levels', () => { + bench('LOW (L)', () => { + encodeText(MEDIUM_TEXT, EccMap.L); + }); + + bench('MEDIUM (M)', () => { + encodeText(MEDIUM_TEXT, EccMap.M); + }); + + bench('QUARTILE (Q)', () => { + encodeText(MEDIUM_TEXT, EccMap.Q); + }); + + bench('HIGH (H)', () => { + encodeText(MEDIUM_TEXT, EccMap.H); + }); +}); + +/* -- encodeBinary benchmarks -- */ + +describe('encodeBinary', () => { + bench('small binary (32 bytes)', () => { + encodeBinary(SMALL_BINARY, LOW); + }); + + bench('medium binary (256 bytes)', () => { + encodeBinary(MEDIUM_BINARY, LOW); + }); +}); + +/* -- makeSegments benchmarks -- */ + +describe('makeSegments', () => { + bench('numeric classification', () => { + makeSegments(NUMERIC_TEXT); + }); + + bench('alphanumeric classification', () => { + makeSegments(ALPHANUMERIC_TEXT); + }); + + bench('byte mode classification', () => { + makeSegments(MEDIUM_TEXT); + }); +}); + +/* -- encodeSegments (pre-built segments) -- */ + +describe('encodeSegments', () => { + bench('from pre-built segments', () => { + encodeSegments(precomputedSegs, LOW); + }); +}); diff --git a/core/encoding/src/qr/index.test.ts b/core/encoding/src/qr/index.test.ts new file mode 100644 index 0000000..e914c70 --- /dev/null +++ b/core/encoding/src/qr/index.test.ts @@ -0,0 +1,182 @@ +import { describe, expect, it } from 'vitest'; +import { encodeText, encodeBinary, makeSegments, isNumeric, isAlphanumeric, QrCode, EccMap, LOW, MEDIUM, HIGH } from '.'; + +describe('isNumeric', () => { + it('accepts pure digit strings', () => { + expect(isNumeric('0123456789')).toBe(true); + expect(isNumeric('0')).toBe(true); + expect(isNumeric('')).toBe(true); + }); + + it('rejects non-digit characters', () => { + expect(isNumeric('12a3')).toBe(false); + expect(isNumeric('HELLO')).toBe(false); + expect(isNumeric('12 34')).toBe(false); + }); +}); + +describe('isAlphanumeric', () => { + it('accepts valid alphanumeric strings', () => { + expect(isAlphanumeric('HELLO WORLD')).toBe(true); + expect(isAlphanumeric('0123456789')).toBe(true); + expect(isAlphanumeric('ABC123')).toBe(true); + expect(isAlphanumeric('')).toBe(true); + }); + + it('rejects lowercase and special characters', () => { + expect(isAlphanumeric('hello')).toBe(false); + expect(isAlphanumeric('Hello')).toBe(false); + expect(isAlphanumeric('test@email')).toBe(false); + }); +}); + +describe('makeSegments', () => { + it('returns empty array for empty string', () => { + expect(makeSegments('')).toEqual([]); + }); + + it('selects numeric mode for digit strings', () => { + const segs = makeSegments('12345'); + expect(segs).toHaveLength(1); + expect(segs[0]!.mode[0]).toBe(0x1); // MODE_NUMERIC + }); + + it('selects alphanumeric mode for uppercase strings', () => { + const segs = makeSegments('HELLO WORLD'); + expect(segs).toHaveLength(1); + expect(segs[0]!.mode[0]).toBe(0x2); // MODE_ALPHANUMERIC + }); + + it('selects byte mode for general text', () => { + const segs = makeSegments('Hello, World!'); + expect(segs).toHaveLength(1); + expect(segs[0]!.mode[0]).toBe(0x4); // MODE_BYTE + }); +}); + +describe('encodeText', () => { + it('encodes short text at LOW ECC', () => { + const qr = encodeText('Hello', LOW); + expect(qr).toBeInstanceOf(QrCode); + expect(qr.version).toBeGreaterThanOrEqual(1); + expect(qr.size).toBe(qr.version * 4 + 17); + expect(qr.mask).toBeGreaterThanOrEqual(0); + expect(qr.mask).toBeLessThanOrEqual(7); + }); + + it('encodes text at different ECC levels', () => { + const qrL = encodeText('Test', LOW); + const qrM = encodeText('Test', MEDIUM); + const qrH = encodeText('Test', HIGH); + + // Higher ECC needs same or higher version + expect(qrH.version).toBeGreaterThanOrEqual(qrL.version); + // All produce valid sizes + for (const qr of [qrL, qrM, qrH]) { + expect(qr.size).toBe(qr.version * 4 + 17); + } + }); + + it('encodes numeric-only text', () => { + const qr = encodeText('123456789012345', LOW); + expect(qr.version).toBe(1); // Numeric mode is compact + }); + + it('encodes a URL', () => { + const qr = encodeText('https://example.com/path?query=value', LOW); + expect(qr).toBeInstanceOf(QrCode); + expect(qr.size).toBeGreaterThanOrEqual(21); + }); + + it('encodes long text', () => { + const longText = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam.'; + const qr = encodeText(longText, LOW); + expect(qr).toBeInstanceOf(QrCode); + }); + + it('throws for data too long', () => { + const tooLong = 'A'.repeat(10000); + expect(() => encodeText(tooLong, HIGH)).toThrow(RangeError); + }); +}); + +describe('encodeBinary', () => { + it('encodes binary data', () => { + const data = [0x00, 0xFF, 0x48, 0x65, 0x6C, 0x6C, 0x6F]; + const qr = encodeBinary(data, LOW); + expect(qr).toBeInstanceOf(QrCode); + }); +}); + +describe('QrCode', () => { + it('modules grid has correct dimensions', () => { + const qr = encodeText('Test', LOW); + // Flat Uint8Array grid, verify via getModule + expect(qr.size).toBeGreaterThanOrEqual(21); + for (let y = 0; y < qr.size; y++) { + for (let x = 0; x < qr.size; x++) { + const mod = qr.getModule(x, y); + expect(typeof mod).toBe('boolean'); + } + } + }); + + it('types grid has correct dimensions', () => { + const qr = encodeText('Test', LOW); + // Flat Int8Array grid, verify via getType + for (let y = 0; y < qr.size; y++) { + for (let x = 0; x < qr.size; x++) { + const t = qr.getType(x, y); + expect(typeof t).toBe('number'); + } + } + }); + + it('getModule returns false for out of bounds', () => { + const qr = encodeText('Test', LOW); + expect(qr.getModule(-1, 0)).toBe(false); + expect(qr.getModule(0, -1)).toBe(false); + expect(qr.getModule(qr.size, 0)).toBe(false); + expect(qr.getModule(0, qr.size)).toBe(false); + }); + + it('produces deterministic output', () => { + const qr1 = encodeText('Hello', LOW); + const qr2 = encodeText('Hello', LOW); + expect(qr1.version).toBe(qr2.version); + expect(qr1.mask).toBe(qr2.mask); + for (let y = 0; y < qr1.size; y++) { + for (let x = 0; x < qr1.size; x++) { + expect(qr1.getModule(x, y)).toBe(qr2.getModule(x, y)); + } + } + }); + + it('different inputs produce different outputs', () => { + const qr1 = encodeText('Hello', LOW); + const qr2 = encodeText('World', LOW); + // They might have the same version/size but different modules + let hasDiff = false; + for (let y = 0; y < qr1.size && !hasDiff; y++) { + for (let x = 0; x < qr1.size && !hasDiff; x++) { + if (qr1.getModule(x, y) !== qr2.getModule(x, y)) + hasDiff = true; + } + } + expect(hasDiff).toBe(true); + }); +}); + +describe('EccMap', () => { + it('has all four levels', () => { + expect(EccMap.L).toBeDefined(); + expect(EccMap.M).toBeDefined(); + expect(EccMap.Q).toBeDefined(); + expect(EccMap.H).toBeDefined(); + }); + + it('works with encodeText', () => { + const qr = encodeText('Test', EccMap.L); + expect(qr).toBeInstanceOf(QrCode); + }); +}); diff --git a/core/encoding/src/qr/index.ts b/core/encoding/src/qr/index.ts new file mode 100644 index 0000000..36a713c --- /dev/null +++ b/core/encoding/src/qr/index.ts @@ -0,0 +1,8 @@ +export { QrCodeDataType } from './types'; +export type { QrCodeEcc, QrSegmentMode } from './types'; + +export { EccMap, LOW, MEDIUM, QUARTILE, HIGH } from './constants'; + +export { QrCode } from './qr-code'; +export { QrSegment, makeBytes, makeSegments, isNumeric, isAlphanumeric } from './segment'; +export { encodeText, encodeBinary, encodeSegments } from './encode'; diff --git a/core/encoding/src/qr/qr-code.ts b/core/encoding/src/qr/qr-code.ts new file mode 100644 index 0000000..7b40f49 --- /dev/null +++ b/core/encoding/src/qr/qr-code.ts @@ -0,0 +1,436 @@ +/* + * QR Code generator — core QrCode class + * + * Based on Project Nayuki's QR Code generator library (MIT License) + * https://www.nayuki.io/page/qr-code-generator-library + */ + +import type { QrCodeEcc } from './types'; +import { QrCodeDataType } from './types'; +import { ECC_CODEWORDS_PER_BLOCK, MAX_VERSION, MIN_VERSION, NUM_ERROR_CORRECTION_BLOCKS } from './constants'; +import { assert, getBit, getNumDataCodewords, getNumRawDataModules } from './utils'; +import { computeDivisor, computeRemainder } from '../reed-solomon'; + +const PENALTY_N1 = 3; +const PENALTY_N2 = 3; +const PENALTY_N3 = 40; +const PENALTY_N4 = 10; + +/** + * A QR Code symbol, which is a type of two-dimension barcode. + * Invented by Denso Wave and described in the ISO/IEC 18004 standard. + * Instances of this class represent an immutable square grid of dark and light cells. + */ +export class QrCode { + /** The width and height of this QR Code, measured in modules, between 21 and 177 (inclusive). */ + public readonly size: number; + + /** The index of the mask pattern used in this QR Code, which is between 0 and 7 (inclusive). */ + public readonly mask: number; + + /** The modules of this QR Code (0 = light, 1 = dark). Flat row-major Uint8Array. */ + private readonly modules: Uint8Array; + + /** Data type of each module. Flat row-major Int8Array. */ + private readonly types: Int8Array; + + /** + * Creates a new QR Code with the given version number, error correction level, data codeword bytes, and mask number. + * This is a low-level API that most users should not use directly. + */ + public constructor( + /** The version number of this QR Code, which is between 1 and 40 (inclusive). */ + public readonly version: number, + /** The error correction level used in this QR Code. */ + public readonly ecc: QrCodeEcc, + dataCodewords: Readonly, + msk: number, + ) { + if (version < MIN_VERSION || version > MAX_VERSION) + throw new RangeError('Version value out of range'); + if (msk < -1 || msk > 7) + throw new RangeError('Mask value out of range'); + this.size = version * 4 + 17; + + const totalModules = this.size * this.size; + this.modules = new Uint8Array(totalModules); + this.types = new Int8Array(totalModules); // 0 = QrCodeDataType.Data + + // Compute ECC, draw modules + this.drawFunctionPatterns(); + const allCodewords = this.addEccAndInterleave(dataCodewords); + this.drawCodewords(allCodewords); + + // Do masking + if (msk === -1) { + let minPenalty = 1_000_000_000; + for (let i = 0; i < 8; i++) { + this.applyMask(i); + this.drawFormatBits(i); + const penalty = this.getPenaltyScore(); + if (penalty < minPenalty) { + msk = i; + minPenalty = penalty; + } + this.applyMask(i); // Undoes the mask due to XOR + } + } + + assert(msk >= 0 && msk <= 7); + this.mask = msk; + this.applyMask(msk); + this.drawFormatBits(msk); + } + + /** + * Returns the color of the module (pixel) at the given coordinates. + * false for light, true for dark. Out of bounds returns false (light). + */ + public getModule(x: number, y: number): boolean { + return x >= 0 && x < this.size && y >= 0 && y < this.size + && this.modules[y * this.size + x] === 1; + } + + /** Returns the data type of the module at the given coordinates. */ + public getType(x: number, y: number): QrCodeDataType { + return this.types[y * this.size + x] as QrCodeDataType; + } + + /* -- Private helper methods for constructor: Drawing function modules -- */ + + private drawFunctionPatterns(): void { + const size = this.size; + // Draw horizontal and vertical timing patterns + for (let i = 0; i < size; i++) { + const dark = i % 2 === 0 ? 1 : 0; + this.setFunctionModule(6, i, dark, QrCodeDataType.Timing); + this.setFunctionModule(i, 6, dark, QrCodeDataType.Timing); + } + + // Draw 3 finder patterns (all corners except bottom right) + this.drawFinderPattern(3, 3); + this.drawFinderPattern(size - 4, 3); + this.drawFinderPattern(3, size - 4); + + // Draw numerous alignment patterns + const alignPatPos = this.getAlignmentPatternPositions(); + const numAlign = alignPatPos.length; + for (let i = 0; i < numAlign; i++) { + for (let j = 0; j < numAlign; j++) { + if (!(i === 0 && j === 0 || i === 0 && j === numAlign - 1 || i === numAlign - 1 && j === 0)) + this.drawAlignmentPattern(alignPatPos[i]!, alignPatPos[j]!); + } + } + + // Draw configuration data + this.drawFormatBits(0); // Dummy mask value; overwritten later in the constructor + this.drawVersion(); + } + + private drawFormatBits(mask: number): void { + const data = this.ecc[1] << 3 | mask; + let rem = data; + for (let i = 0; i < 10; i++) + rem = (rem << 1) ^ ((rem >>> 9) * 0x537); + const bits = (data << 10 | rem) ^ 0x5412; + assert(bits >>> 15 === 0); + + const size = this.size; + // Draw first copy + for (let i = 0; i <= 5; i++) + this.setFunctionModule(8, i, getBit(bits, i) ? 1 : 0); + this.setFunctionModule(8, 7, getBit(bits, 6) ? 1 : 0); + this.setFunctionModule(8, 8, getBit(bits, 7) ? 1 : 0); + this.setFunctionModule(7, 8, getBit(bits, 8) ? 1 : 0); + for (let i = 9; i < 15; i++) + this.setFunctionModule(14 - i, 8, getBit(bits, i) ? 1 : 0); + + // Draw second copy + for (let i = 0; i < 8; i++) + this.setFunctionModule(size - 1 - i, 8, getBit(bits, i) ? 1 : 0); + for (let i = 8; i < 15; i++) + this.setFunctionModule(8, size - 15 + i, getBit(bits, i) ? 1 : 0); + this.setFunctionModule(8, size - 8, 1); + } + + private drawVersion(): void { + if (this.version < 7) + return; + + let rem = this.version; + for (let i = 0; i < 12; i++) + rem = (rem << 1) ^ ((rem >>> 11) * 0x1F25); + const bits = this.version << 12 | rem; + assert(bits >>> 18 === 0); + + const size = this.size; + for (let i = 0; i < 18; i++) { + const color = getBit(bits, i) ? 1 : 0; + const a = size - 11 + i % 3; + const b = (i / 3) | 0; + this.setFunctionModule(a, b, color); + this.setFunctionModule(b, a, color); + } + } + + private drawFinderPattern(x: number, y: number): void { + const size = this.size; + for (let dy = -4; dy <= 4; dy++) { + for (let dx = -4; dx <= 4; dx++) { + const dist = Math.max(Math.abs(dx), Math.abs(dy)); + const xx = x + dx; + const yy = y + dy; + if (xx >= 0 && xx < size && yy >= 0 && yy < size) + this.setFunctionModule(xx, yy, dist !== 2 && dist !== 4 ? 1 : 0, QrCodeDataType.Position); + } + } + } + + private drawAlignmentPattern(x: number, y: number): void { + for (let dy = -2; dy <= 2; dy++) { + for (let dx = -2; dx <= 2; dx++) { + this.setFunctionModule( + x + dx, + y + dy, + Math.max(Math.abs(dx), Math.abs(dy)) !== 1 ? 1 : 0, + QrCodeDataType.Alignment, + ); + } + } + } + + private setFunctionModule(x: number, y: number, isDark: number, type: QrCodeDataType = QrCodeDataType.Function): void { + const idx = y * this.size + x; + this.modules[idx] = isDark; + this.types[idx] = type; + } + + /* -- Private helper methods for constructor: Codewords and masking -- */ + + private addEccAndInterleave(data: Readonly): number[] { + const ver = this.version; + const ecl = this.ecc; + if (data.length !== getNumDataCodewords(ver, ecl)) + throw new RangeError('Invalid argument'); + + const numBlocks = NUM_ERROR_CORRECTION_BLOCKS[ecl[0]]![ver]!; + const blockEccLen = ECC_CODEWORDS_PER_BLOCK[ecl[0]]![ver]!; + const rawCodewords = (getNumRawDataModules(ver) / 8) | 0; + const numShortBlocks = numBlocks - rawCodewords % numBlocks; + const shortBlockLen = (rawCodewords / numBlocks) | 0; + + // Split data into blocks and append ECC to each block + const blocks: number[][] = []; + const rsDiv = computeDivisor(blockEccLen); + for (let i = 0, k = 0; i < numBlocks; i++) { + const dat: number[] = data.slice(k, k + shortBlockLen - blockEccLen + (i < numShortBlocks ? 0 : 1)) as number[]; + k += dat.length; + const ecc = computeRemainder(dat, rsDiv); + if (i < numShortBlocks) + dat.push(0); + blocks.push([...dat, ...ecc]); + } + + // Interleave (not concatenate) the bytes from every block into a single sequence + const result: number[] = []; + const blockLen = blocks[0]!.length; + for (let i = 0; i < blockLen; i++) { + for (let j = 0; j < blocks.length; j++) { + if (i !== shortBlockLen - blockEccLen || j >= numShortBlocks) + result.push(blocks[j]![i]!); + } + } + assert(result.length === rawCodewords); + return result; + } + + private drawCodewords(data: Readonly): void { + if (data.length !== ((getNumRawDataModules(this.version) / 8) | 0)) + throw new RangeError('Invalid argument'); + + const size = this.size; + const modules = this.modules; + const types = this.types; + let i = 0; + for (let right = size - 1; right >= 1; right -= 2) { + if (right === 6) + right = 5; + for (let vert = 0; vert < size; vert++) { + for (let j = 0; j < 2; j++) { + const x = right - j; + const upward = ((right + 1) & 2) === 0; + const y = upward ? size - 1 - vert : vert; + const idx = y * size + x; + if (types[idx] === QrCodeDataType.Data && i < data.length * 8) { + modules[idx] = (data[i >>> 3]! >>> (7 - (i & 7))) & 1; + i++; + } + } + } + } + assert(i === data.length * 8); + } + + private applyMask(mask: number): void { + if (mask < 0 || mask > 7) + throw new RangeError('Mask value out of range'); + const size = this.size; + const modules = this.modules; + const types = this.types; + for (let y = 0; y < size; y++) { + const yOffset = y * size; + for (let x = 0; x < size; x++) { + const idx = yOffset + x; + if (types[idx] !== QrCodeDataType.Data) + continue; + let invert: boolean; + switch (mask) { + case 0: invert = (x + y) % 2 === 0; break; + case 1: invert = y % 2 === 0; break; + case 2: invert = x % 3 === 0; break; + case 3: invert = (x + y) % 3 === 0; break; + case 4: invert = (((x / 3) | 0) + ((y / 2) | 0)) % 2 === 0; break; + case 5: invert = x * y % 2 + x * y % 3 === 0; break; + case 6: invert = (x * y % 2 + x * y % 3) % 2 === 0; break; + case 7: invert = ((x + y) % 2 + x * y % 3) % 2 === 0; break; + default: throw new Error('Unreachable'); + } + if (invert) + modules[idx]! ^= 1; + } + } + } + + private getPenaltyScore(): number { + const size = this.size; + const modules = this.modules; + let result = 0; + + // Adjacent modules in row having same color, and finder-like patterns + for (let y = 0; y < size; y++) { + const yOffset = y * size; + let runColor = 0; + let runX = 0; + let h0 = 0, h1 = 0, h2 = 0, h3 = 0, h4 = 0, h5 = 0, h6 = 0; + for (let x = 0; x < size; x++) { + const mod = modules[yOffset + x]!; + if (mod === runColor) { + runX++; + if (runX === 5) + result += PENALTY_N1; + else if (runX > 5) + result++; + } + else { + // finderPenaltyAddHistory inlined + if (h0 === 0) runX += size; + h6 = h5; h5 = h4; h4 = h3; h3 = h2; h2 = h1; h1 = runX; h0 = runX; + // finderPenaltyCountPatterns inlined (only when runColor is light = 0) + if (runColor === 0) { + const core = h1 > 0 && h2 === h1 && h3 === h1 * 3 && h4 === h1 && h5 === h1; + if (core && h0 >= h1 * 4 && h6 >= h1) result += PENALTY_N3; + if (core && h6 >= h1 * 4 && h0 >= h1) result += PENALTY_N3; + } + runColor = mod; + runX = 1; + } + } + // finderPenaltyTerminateAndCount inlined + { + let currentRunLength = runX; + if (runColor === 1) { + if (h0 === 0) currentRunLength += size; + h6 = h5; h5 = h4; h4 = h3; h3 = h2; h2 = h1; h1 = currentRunLength; h0 = currentRunLength; + currentRunLength = 0; + } + currentRunLength += size; + if (h0 === 0) currentRunLength += size; + h6 = h5; h5 = h4; h4 = h3; h3 = h2; h2 = h1; h1 = currentRunLength; h0 = currentRunLength; + const core = h1 > 0 && h2 === h1 && h3 === h1 * 3 && h4 === h1 && h5 === h1; + if (core && h0 >= h1 * 4 && h6 >= h1) result += PENALTY_N3; + if (core && h6 >= h1 * 4 && h0 >= h1) result += PENALTY_N3; + } + } + + // Adjacent modules in column having same color, and finder-like patterns + for (let x = 0; x < size; x++) { + let runColor = 0; + let runY = 0; + let h0 = 0, h1 = 0, h2 = 0, h3 = 0, h4 = 0, h5 = 0, h6 = 0; + for (let y = 0; y < size; y++) { + const mod = modules[y * size + x]!; + if (mod === runColor) { + runY++; + if (runY === 5) + result += PENALTY_N1; + else if (runY > 5) + result++; + } + else { + if (h0 === 0) runY += size; + h6 = h5; h5 = h4; h4 = h3; h3 = h2; h2 = h1; h1 = runY; h0 = runY; + if (runColor === 0) { + const core = h1 > 0 && h2 === h1 && h3 === h1 * 3 && h4 === h1 && h5 === h1; + if (core && h0 >= h1 * 4 && h6 >= h1) result += PENALTY_N3; + if (core && h6 >= h1 * 4 && h0 >= h1) result += PENALTY_N3; + } + runColor = mod; + runY = 1; + } + } + { + let currentRunLength = runY; + if (runColor === 1) { + if (h0 === 0) currentRunLength += size; + h6 = h5; h5 = h4; h4 = h3; h3 = h2; h2 = h1; h1 = currentRunLength; h0 = currentRunLength; + currentRunLength = 0; + } + currentRunLength += size; + if (h0 === 0) currentRunLength += size; + h6 = h5; h5 = h4; h4 = h3; h3 = h2; h2 = h1; h1 = currentRunLength; h0 = currentRunLength; + const core = h1 > 0 && h2 === h1 && h3 === h1 * 3 && h4 === h1 && h5 === h1; + if (core && h0 >= h1 * 4 && h6 >= h1) result += PENALTY_N3; + if (core && h6 >= h1 * 4 && h0 >= h1) result += PENALTY_N3; + } + } + + // 2*2 blocks of modules having same color + for (let y = 0; y < size - 1; y++) { + const yOffset = y * size; + const nextYOffset = yOffset + size; + for (let x = 0; x < size - 1; x++) { + const color = modules[yOffset + x]!; + if (color === modules[yOffset + x + 1] + && color === modules[nextYOffset + x] + && color === modules[nextYOffset + x + 1]) + result += PENALTY_N2; + } + } + + // Balance of dark and light modules + let dark = 0; + const total = size * size; + for (let i = 0; i < total; i++) + dark += modules[i]!; + const k = Math.ceil(Math.abs(dark * 20 - total * 10) / total) - 1; + assert(k >= 0 && k <= 9); + result += k * PENALTY_N4; + assert(result >= 0 && result <= 2568888); + return result; + } + + private getAlignmentPatternPositions(): number[] { + if (this.version === 1) + return []; + + const numAlign = ((this.version / 7) | 0) + + 2; + const step = (this.version === 32) + ? 26 + : Math.ceil((this.version * 4 + 4) / (numAlign * 2 - 2)) * 2; + const result = [6]; + for (let pos = this.size - 7; result.length < numAlign; pos -= step) + result.splice(1, 0, pos); + return result; + } +} diff --git a/core/encoding/src/qr/segment.ts b/core/encoding/src/qr/segment.ts new file mode 100644 index 0000000..8030479 --- /dev/null +++ b/core/encoding/src/qr/segment.ts @@ -0,0 +1,82 @@ +import type { QrSegmentMode } from './types'; +import { ALPHANUMERIC_MAP, ALPHANUMERIC_REGEX, MODE_ALPHANUMERIC, MODE_BYTE, MODE_NUMERIC, NUMERIC_REGEX } from './constants'; +import { appendBits, toUtf8ByteArray } from './utils'; + +/** + * A segment of character/binary/control data in a QR Code symbol. + * Instances of this class are immutable. + */ +export class QrSegment { + public constructor( + /** The mode indicator of this segment. */ + public readonly mode: QrSegmentMode, + /** The length of this segment's unencoded data. */ + public readonly numChars: number, + /** The data bits of this segment. */ + public readonly bitData: readonly number[], + ) { + if (numChars < 0) + throw new RangeError('Invalid argument'); + } +} + +/** Returns a segment representing the given binary data encoded in byte mode. */ +export function makeBytes(data: ArrayLike): QrSegment { + const bb: number[] = []; + for (let i = 0, len = data.length; i < len; i++) + appendBits(data[i]!, 8, bb); + return new QrSegment(MODE_BYTE, data.length, bb); +} + +/** Returns a segment representing the given string of decimal digits encoded in numeric mode. */ +export function makeNumeric(digits: string): QrSegment { + if (!isNumeric(digits)) + throw new RangeError('String contains non-numeric characters'); + const bb: number[] = []; + for (let i = 0; i < digits.length;) { + const n = Math.min(digits.length - i, 3); + appendBits(Number.parseInt(digits.slice(i, i + n), 10), n * 3 + 1, bb); + i += n; + } + return new QrSegment(MODE_NUMERIC, digits.length, bb); +} + +/** Returns a segment representing the given text string encoded in alphanumeric mode. */ +export function makeAlphanumeric(text: string): QrSegment { + if (!isAlphanumeric(text)) + throw new RangeError('String contains unencodable characters in alphanumeric mode'); + const bb: number[] = []; + let i: number; + for (i = 0; i + 2 <= text.length; i += 2) { + let temp = ALPHANUMERIC_MAP[text.charCodeAt(i)]! * 45; + temp += ALPHANUMERIC_MAP[text.charCodeAt(i + 1)]!; + appendBits(temp, 11, bb); + } + if (i < text.length) + appendBits(ALPHANUMERIC_MAP[text.charCodeAt(i)]!, 6, bb); + return new QrSegment(MODE_ALPHANUMERIC, text.length, bb); +} + +/** + * Returns a new mutable list of zero or more segments to represent the given Unicode text string. + * The result may use various segment modes and switch modes to optimize the length of the bit stream. + */ +export function makeSegments(text: string): QrSegment[] { + if (text === '') + return []; + if (isNumeric(text)) + return [makeNumeric(text)]; + if (isAlphanumeric(text)) + return [makeAlphanumeric(text)]; + return [makeBytes(toUtf8ByteArray(text))]; +} + +/** Tests whether the given string can be encoded as a segment in numeric mode. */ +export function isNumeric(text: string): boolean { + return NUMERIC_REGEX.test(text); +} + +/** Tests whether the given string can be encoded as a segment in alphanumeric mode. */ +export function isAlphanumeric(text: string): boolean { + return ALPHANUMERIC_REGEX.test(text); +} diff --git a/core/encoding/src/qr/types.ts b/core/encoding/src/qr/types.ts new file mode 100644 index 0000000..cf60ba6 --- /dev/null +++ b/core/encoding/src/qr/types.ts @@ -0,0 +1,17 @@ +export type QrCodeEcc = readonly [ordinal: number, formatBits: number]; + +export type QrSegmentMode = [ + modeBits: number, + numBitsCharCount1: number, + numBitsCharCount2: number, + numBitsCharCount3: number, +]; + +export enum QrCodeDataType { + Border = -1, + Data = 0, + Function = 1, + Position = 2, + Timing = 3, + Alignment = 4, +} diff --git a/core/encoding/src/qr/utils.ts b/core/encoding/src/qr/utils.ts new file mode 100644 index 0000000..30e8791 --- /dev/null +++ b/core/encoding/src/qr/utils.ts @@ -0,0 +1,79 @@ +import type { QrCodeEcc, QrSegmentMode } from './types'; +import { ECC_CODEWORDS_PER_BLOCK, MAX_VERSION, MIN_VERSION, NUM_ERROR_CORRECTION_BLOCKS } from './constants'; +import type { QrSegment } from './segment'; + +const utf8Encoder = new TextEncoder(); + +/** Appends the given number of low-order bits of the given value to the buffer. */ +export function appendBits(val: number, len: number, bb: number[]): void { + if (len < 0 || len > 31 || val >>> len !== 0) + throw new RangeError('Value out of range'); + for (let i = len - 1; i >= 0; i--) + bb.push((val >>> i) & 1); +} + +/** Returns true iff the i'th bit of x is set to 1. */ +export function getBit(x: number, i: number): boolean { + return ((x >>> i) & 1) !== 0; +} + +/** Throws an exception if the given condition is false. */ +export function assert(cond: boolean): asserts cond { + if (!cond) + throw new Error('Assertion error'); +} + +/** Returns a Uint8Array representing the given string encoded in UTF-8. */ +export function toUtf8ByteArray(str: string): Uint8Array { + return utf8Encoder.encode(str); +} + +/** Returns the bit width of the character count field for a segment in this mode at the given version number. */ +export function numCharCountBits(mode: QrSegmentMode, ver: number): number { + return mode[((ver + 7) / 17 | 0) + 1]!; +} + +/** + * Returns the number of data bits that can be stored in a QR Code of the given version number, + * after all function modules are excluded. This includes remainder bits, so it might not be a multiple of 8. + * The result is in the range [208, 29648]. + */ +export function getNumRawDataModules(ver: number): number { + if (ver < MIN_VERSION || ver > MAX_VERSION) + throw new RangeError('Version number out of range'); + + let result = (16 * ver + 128) * ver + 64; + if (ver >= 2) { + const numAlign = (ver / 7 | 0) + 2; + result -= (25 * numAlign - 10) * numAlign - 55; + if (ver >= 7) + result -= 36; + } + assert(result >= 208 && result <= 29648); + return result; +} + +/** + * Returns the number of 8-bit data (i.e. not error correction) codewords contained in any + * QR Code of the given version number and error correction level, with remainder bits discarded. + */ +export function getNumDataCodewords(ver: number, ecl: QrCodeEcc): number { + return (getNumRawDataModules(ver) / 8 | 0) + - ECC_CODEWORDS_PER_BLOCK[ecl[0]]![ver]! + * NUM_ERROR_CORRECTION_BLOCKS[ecl[0]]![ver]!; +} + +/** + * Calculates and returns the number of bits needed to encode the given segments at the given version. + * The result is infinity if a segment has too many characters to fit its length field. + */ +export function getTotalBits(segs: Readonly, version: number): number { + let result = 0; + for (const seg of segs) { + const ccbits = numCharCountBits(seg.mode, version); + if (seg.numChars >= (1 << ccbits)) + return Number.POSITIVE_INFINITY; + result += 4 + ccbits + seg.bitData.length; + } + return result; +} diff --git a/core/encoding/src/reed-solomon/index.test.ts b/core/encoding/src/reed-solomon/index.test.ts new file mode 100644 index 0000000..321cac4 --- /dev/null +++ b/core/encoding/src/reed-solomon/index.test.ts @@ -0,0 +1,100 @@ +import { describe, expect, it } from 'vitest'; +import { computeDivisor, computeRemainder, multiply } from '.'; + +describe('multiply', () => { + it('multiplies zero by anything to get zero', () => { + expect(multiply(0, 0)).toBe(0); + expect(multiply(0, 1)).toBe(0); + expect(multiply(0, 255)).toBe(0); + expect(multiply(1, 0)).toBe(0); + }); + + it('multiplies by one (identity)', () => { + expect(multiply(1, 1)).toBe(1); + expect(multiply(1, 42)).toBe(42); + expect(multiply(42, 1)).toBe(42); + expect(multiply(1, 255)).toBe(255); + }); + + it('is commutative', () => { + expect(multiply(5, 7)).toBe(multiply(7, 5)); + expect(multiply(0x53, 0xCA)).toBe(multiply(0xCA, 0x53)); + expect(multiply(100, 200)).toBe(multiply(200, 100)); + }); + + it('produces known GF(2^8) products', () => { + expect(multiply(2, 2)).toBe(4); + expect(multiply(2, 0x80)).toBe(0x1D); + }); + + it('throws on out of range inputs', () => { + expect(() => multiply(256, 0)).toThrow(RangeError); + expect(() => multiply(0, 256)).toThrow(RangeError); + expect(() => multiply(1000, 1000)).toThrow(RangeError); + }); +}); + +describe('computeDivisor', () => { + it('computes a degree-1 divisor', () => { + expect(computeDivisor(1)).toEqual(Uint8Array.from([1])); + }); + + it('computes a degree-2 divisor', () => { + const result = computeDivisor(2); + expect(result).toHaveLength(2); + expect(result).toEqual(Uint8Array.from([3, 2])); + }); + + it('has correct length for arbitrary degrees', () => { + expect(computeDivisor(7)).toHaveLength(7); + expect(computeDivisor(10)).toHaveLength(10); + expect(computeDivisor(30)).toHaveLength(30); + }); + + it('returns Uint8Array', () => { + expect(computeDivisor(5)).toBeInstanceOf(Uint8Array); + }); + + it('throws on degree out of range', () => { + expect(() => computeDivisor(0)).toThrow(RangeError); + expect(() => computeDivisor(256)).toThrow(RangeError); + expect(() => computeDivisor(-1)).toThrow(RangeError); + }); +}); + +describe('computeRemainder', () => { + it('returns zero remainder for empty data', () => { + const divisor = computeDivisor(4); + const result = computeRemainder([], divisor); + expect(result).toEqual(new Uint8Array(4)); + }); + + it('produces non-zero remainder for non-empty data', () => { + const divisor = computeDivisor(7); + const data = [0x40, 0xD2, 0x75, 0x47, 0x76, 0x17, 0x32, 0x06, 0x27, 0x26, 0x96, 0xC6, 0xC6, 0x96, 0x70, 0xEC]; + const result = computeRemainder(data, divisor); + expect(result).toHaveLength(7); + expect(result).toBeInstanceOf(Uint8Array); + for (const b of result) { + expect(b).toBeGreaterThanOrEqual(0); + expect(b).toBeLessThanOrEqual(255); + } + }); + + it('accepts Uint8Array as data input', () => { + const divisor = computeDivisor(7); + const data = Uint8Array.from([0x40, 0xD2, 0x75, 0x47]); + const result = computeRemainder(data, divisor); + expect(result).toHaveLength(7); + expect(result).toBeInstanceOf(Uint8Array); + }); + + it('remainder length matches divisor length', () => { + for (const degree of [1, 5, 10, 20]) { + const divisor = computeDivisor(degree); + const data = [1, 2, 3, 4, 5]; + const result = computeRemainder(data, divisor); + expect(result).toHaveLength(degree); + } + }); +}); diff --git a/core/encoding/src/reed-solomon/index.ts b/core/encoding/src/reed-solomon/index.ts new file mode 100644 index 0000000..bbff108 --- /dev/null +++ b/core/encoding/src/reed-solomon/index.ts @@ -0,0 +1,92 @@ +/* + * Reed-Solomon error correction over GF(2^8/0x11D) + * + * Based on Project Nayuki's QR Code generator library (MIT License) + * https://www.nayuki.io/page/qr-code-generator-library + */ + +/* -- GF(2^8) exp/log lookup tables (generator α=0x02, primitive polynomial 0x11D) -- */ + +const GF_EXP = new Uint8Array(256); +const GF_LOG = new Uint8Array(256); + +{ + let x = 1; + for (let i = 0; i < 255; i++) { + GF_EXP[i] = x; + GF_LOG[x] = i; + x = (x << 1) ^ ((x >>> 7) * 0x11D); + } + GF_EXP[255] = GF_EXP[0]!; +} + +/** + * Returns the product of the two given field elements modulo GF(2^8/0x11D). + * The arguments and result are unsigned 8-bit integers. + */ +export function multiply(x: number, y: number): number { + if (x >>> 8 !== 0 || y >>> 8 !== 0) + throw new RangeError('Byte out of range'); + + if (x === 0 || y === 0) + return 0; + + return GF_EXP[(GF_LOG[x]! + GF_LOG[y]!) % 255]!; +} + +/** + * Returns a Reed-Solomon ECC generator polynomial for the given degree. + * + * Polynomial coefficients are stored from highest to lowest power, excluding the leading term which is always 1. + * For example the polynomial x^3 + 255x^2 + 8x + 93 is stored as the uint8 array [255, 8, 93]. + */ +export function computeDivisor(degree: number): Uint8Array { + if (degree < 1 || degree > 255) + throw new RangeError('Degree out of range'); + + const result = new Uint8Array(degree); + result[degree - 1] = 1; + + // Compute the product polynomial (x - r^0) * (x - r^1) * ... * (x - r^{degree-1}), + // dropping the leading term which is always 1x^degree. + // r = 0x02, a generator element of GF(2^8/0x11D). + let root = 0; // GF_LOG[1] = 0, i.e. α^0 = 1 + for (let i = 0; i < degree; i++) { + // Multiply the current product by (x - r^i) + for (let j = 0; j < degree; j++) { + // result[j] = multiply(result[j], α^root) — inlined for performance + if (result[j] !== 0) + result[j] = GF_EXP[(GF_LOG[result[j]!]! + root) % 255]!; + if (j + 1 < degree) + result[j]! ^= result[j + 1]!; + } + root = (root + 1) % 255; // root tracks log(α^i) = i mod 255 + } + + return result; +} + +/** + * Returns the Reed-Solomon error correction codeword for the given data and divisor polynomials. + */ +export function computeRemainder(data: ArrayLike, divisor: Uint8Array): Uint8Array { + const len = divisor.length; + const result = new Uint8Array(len); + + for (let d = 0, dLen = data.length; d < dLen; d++) { + const factor = data[d]! ^ result[0]!; + // Shift left by 1 position (native memcpy) + result.copyWithin(0, 1); + result[len - 1] = 0; + // XOR with divisor scaled by factor — inlined GF multiply for performance + if (factor !== 0) { + const logFactor = GF_LOG[factor]!; + for (let i = 0; i < len; i++) { + if (divisor[i] !== 0) + result[i]! ^= GF_EXP[(GF_LOG[divisor[i]!]! + logFactor) % 255]!; + } + } + } + + return result; +} diff --git a/core/encoding/tsconfig.json b/core/encoding/tsconfig.json new file mode 100644 index 0000000..ab255ac --- /dev/null +++ b/core/encoding/tsconfig.json @@ -0,0 +1,3 @@ +{ + "extends": "@robonen/tsconfig/tsconfig.json" +} diff --git a/core/encoding/tsdown.config.ts b/core/encoding/tsdown.config.ts new file mode 100644 index 0000000..ae9657f --- /dev/null +++ b/core/encoding/tsdown.config.ts @@ -0,0 +1,7 @@ +import { defineConfig } from 'tsdown'; +import { sharedConfig } from '@robonen/tsdown'; + +export default defineConfig({ + ...sharedConfig, + entry: ['src/index.ts'], +}); diff --git a/core/encoding/vitest.config.ts b/core/encoding/vitest.config.ts new file mode 100644 index 0000000..4ac6027 --- /dev/null +++ b/core/encoding/vitest.config.ts @@ -0,0 +1,7 @@ +import { defineConfig } from 'vitest/config'; + +export default defineConfig({ + test: { + environment: 'node', + }, +}); diff --git a/core/platform/oxlint.config.ts b/core/platform/oxlint.config.ts index 2a8522e..b295e66 100644 --- a/core/platform/oxlint.config.ts +++ b/core/platform/oxlint.config.ts @@ -1,15 +1,15 @@ import { defineConfig } from 'oxlint'; -import { compose, base, typescript, imports } from '@robonen/oxlint'; +import { compose, base, typescript, imports, stylistic } from '@robonen/oxlint'; export default defineConfig( - compose(base, typescript, imports, { - overrides: [ - { - files: ['src/multi/global/index.ts'], - rules: { - 'unicorn/prefer-global-this': 'off', - }, - }, - ], - }), + compose(base, typescript, imports, stylistic, { + overrides: [ + { + files: ['src/multi/global/index.ts'], + rules: { + 'unicorn/prefer-global-this': 'off', + }, + }, + ], + }), ); diff --git a/core/platform/package.json b/core/platform/package.json index d12a24f..7876556 100644 --- a/core/platform/package.json +++ b/core/platform/package.json @@ -18,7 +18,7 @@ "url": "git+https://github.com/robonen/tools.git", "directory": "packages/platform" }, - "packageManager": "pnpm@10.29.3", + "packageManager": "pnpm@10.30.3", "engines": { "node": ">=24.13.1" }, @@ -39,7 +39,8 @@ } }, "scripts": { - "lint": "oxlint -c oxlint.config.ts", + "lint:check": "oxlint -c oxlint.config.ts", + "lint:fix": "oxlint -c oxlint.config.ts --fix", "test": "vitest run", "dev": "vitest dev", "build": "tsdown" @@ -48,6 +49,7 @@ "@robonen/oxlint": "workspace:*", "@robonen/tsconfig": "workspace:*", "@robonen/tsdown": "workspace:*", + "@stylistic/eslint-plugin": "catalog:", "oxlint": "catalog:", "tsdown": "catalog:" } diff --git a/core/platform/src/browsers/focusGuard/index.test.ts b/core/platform/src/browsers/focusGuard/index.test.ts index 22e29a9..6fc80bf 100644 --- a/core/platform/src/browsers/focusGuard/index.test.ts +++ b/core/platform/src/browsers/focusGuard/index.test.ts @@ -8,7 +8,7 @@ describe('focusGuard', () => { it('initialize with the correct default namespace', () => { const guard = focusGuard(); - + expect(guard.selector).toBe('data-focus-guard'); }); @@ -31,7 +31,7 @@ describe('focusGuard', () => { guard.removeGuard(); const guards = document.querySelectorAll(`[${guard.selector}]`); - + expect(guards.length).toBe(0); }); @@ -66,4 +66,4 @@ describe('focusGuard', () => { expect(element.getAttribute('tabindex')).toBe('0'); expect(element.getAttribute('style')).toBe('outline: none; opacity: 0; pointer-events: none; position: fixed;'); }); -}); \ No newline at end of file +}); diff --git a/core/platform/src/browsers/focusGuard/index.ts b/core/platform/src/browsers/focusGuard/index.ts index 4ad1cf8..675075d 100644 --- a/core/platform/src/browsers/focusGuard/index.ts +++ b/core/platform/src/browsers/focusGuard/index.ts @@ -2,20 +2,20 @@ * @name focusGuard * @category Browsers * @description Adds a pair of focus guards at the boundaries of the DOM tree to ensure consistent focus behavior - * + * * @param {string} namespace - The namespace to use for the guard attributes * @returns {Object} - An object containing the selector, createGuard, and removeGuard functions - * + * * @example * const guard = focusGuard(); * guard.createGuard(); * guard.removeGuard(); - * + * * @example * const guard = focusGuard('focus-guard'); * guard.createGuard(); * guard.removeGuard(); - * + * * @since 0.0.3 */ export function focusGuard(namespace = 'focus-guard') { @@ -29,7 +29,7 @@ export function focusGuard(namespace = 'focus-guard') { }; const removeGuard = () => { - document.querySelectorAll(`[${guardAttr}]`).forEach((element) => element.remove()); + document.querySelectorAll(`[${guardAttr}]`).forEach(element => element.remove()); }; return { @@ -47,4 +47,4 @@ export function createGuardAttrs(namespace = 'focus-guard') { element.setAttribute('style', 'outline: none; opacity: 0; pointer-events: none; position: fixed;'); return element; -} \ No newline at end of file +} diff --git a/core/platform/src/browsers/index.ts b/core/platform/src/browsers/index.ts index 0569ecb..568f902 100644 --- a/core/platform/src/browsers/index.ts +++ b/core/platform/src/browsers/index.ts @@ -1 +1 @@ -export * from './focusGuard'; \ No newline at end of file +export * from './focusGuard'; diff --git a/core/platform/src/multi/global/index.ts b/core/platform/src/multi/global/index.ts index e337e88..a867321 100644 --- a/core/platform/src/multi/global/index.ts +++ b/core/platform/src/multi/global/index.ts @@ -7,8 +7,8 @@ * * @since 0.0.1 */ -export const _global = - typeof globalThis !== 'undefined' +export const _global + = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window @@ -22,7 +22,7 @@ export const _global = * @name isClient * @category Multi * @description Check if the current environment is the client - * + * * @since 0.0.1 */ -export const isClient = typeof window !== 'undefined' && typeof document !== 'undefined'; \ No newline at end of file +export const isClient = typeof window !== 'undefined' && typeof document !== 'undefined'; diff --git a/core/platform/src/multi/index.ts b/core/platform/src/multi/index.ts index 8774985..dd6469d 100644 --- a/core/platform/src/multi/index.ts +++ b/core/platform/src/multi/index.ts @@ -1,2 +1,2 @@ export * from './global'; -// export * from './debounce'; \ No newline at end of file +// export * from './debounce'; diff --git a/core/platform/tsdown.config.ts b/core/platform/tsdown.config.ts index 1b37543..02149ad 100644 --- a/core/platform/tsdown.config.ts +++ b/core/platform/tsdown.config.ts @@ -7,4 +7,4 @@ export default defineConfig({ browsers: 'src/browsers/index.ts', multi: 'src/multi/index.ts', }, -}); \ No newline at end of file +}); diff --git a/core/platform/vitest.config.ts b/core/platform/vitest.config.ts index 69e22cf..647a9e5 100644 --- a/core/platform/vitest.config.ts +++ b/core/platform/vitest.config.ts @@ -5,4 +5,3 @@ export default defineConfig({ environment: 'jsdom', }, }); - diff --git a/core/stdlib/oxlint.config.ts b/core/stdlib/oxlint.config.ts index 247c39b..8d6f15a 100644 --- a/core/stdlib/oxlint.config.ts +++ b/core/stdlib/oxlint.config.ts @@ -1,4 +1,4 @@ import { defineConfig } from 'oxlint'; -import { compose, base, typescript, imports } from '@robonen/oxlint'; +import { compose, base, typescript, imports, stylistic } from '@robonen/oxlint'; -export default defineConfig(compose(base, typescript, imports)); +export default defineConfig(compose(base, typescript, imports, stylistic)); diff --git a/core/stdlib/package.json b/core/stdlib/package.json index 077068c..e451469 100644 --- a/core/stdlib/package.json +++ b/core/stdlib/package.json @@ -18,7 +18,7 @@ "url": "git+https://github.com/robonen/tools.git", "directory": "packages/stdlib" }, - "packageManager": "pnpm@10.29.3", + "packageManager": "pnpm@10.30.3", "engines": { "node": ">=24.13.1" }, @@ -34,7 +34,8 @@ } }, "scripts": { - "lint": "oxlint -c oxlint.config.ts", + "lint:check": "oxlint -c oxlint.config.ts", + "lint:fix": "oxlint -c oxlint.config.ts --fix", "test": "vitest run", "dev": "vitest dev", "build": "tsdown" @@ -43,6 +44,7 @@ "@robonen/oxlint": "workspace:*", "@robonen/tsconfig": "workspace:*", "@robonen/tsdown": "workspace:*", + "@stylistic/eslint-plugin": "catalog:", "oxlint": "catalog:", "tsdown": "catalog:" } diff --git a/core/stdlib/src/arrays/cluster/index.test.ts b/core/stdlib/src/arrays/cluster/index.test.ts index c0a8648..2232109 100644 --- a/core/stdlib/src/arrays/cluster/index.test.ts +++ b/core/stdlib/src/arrays/cluster/index.test.ts @@ -34,7 +34,7 @@ describe('cluster', () => { it('return an empty array if the input array is empty', () => { const result = cluster([], 3); - + expect(result).toEqual([]); }); -}); \ No newline at end of file +}); diff --git a/core/stdlib/src/arrays/cluster/index.ts b/core/stdlib/src/arrays/cluster/index.ts index 8f045f6..35f377f 100644 --- a/core/stdlib/src/arrays/cluster/index.ts +++ b/core/stdlib/src/arrays/cluster/index.ts @@ -2,17 +2,17 @@ * @name cluster * @category Arrays * @description Cluster an array into subarrays of a specific size - * + * * @param {Value[]} arr The array to cluster * @param {number} size The size of each cluster * @returns {Value[][]} The clustered array - * + * * @example * cluster([1, 2, 3, 4, 5, 6, 7, 8], 3) // => [[1, 2, 3], [4, 5, 6], [7, 8]] - * + * * @example * cluster([1, 2, 3, 4], -1) // => [] - * + * * @since 0.0.3 */ export function cluster(arr: Value[], size: number): Value[][] { diff --git a/core/stdlib/src/arrays/first/index.test.ts b/core/stdlib/src/arrays/first/index.test.ts index 74a0062..c6afd5f 100644 --- a/core/stdlib/src/arrays/first/index.test.ts +++ b/core/stdlib/src/arrays/first/index.test.ts @@ -20,4 +20,4 @@ describe('first', () => { expect(first([1, 2, 3], 42)).toBe(1); expect(first(['a', 'b', 'c'], 'default')).toBe('a'); }); -}); \ No newline at end of file +}); diff --git a/core/stdlib/src/arrays/first/index.ts b/core/stdlib/src/arrays/first/index.ts index 720bb8a..00e16cb 100644 --- a/core/stdlib/src/arrays/first/index.ts +++ b/core/stdlib/src/arrays/first/index.ts @@ -2,19 +2,19 @@ * @name first * @category Arrays * @description Returns the first element of an array - * + * * @param {Value[]} arr The array to get the first element from * @param {Value} [defaultValue] The default value to return if the array is empty * @returns {Value | undefined} The first element of the array, or the default value if the array is empty - * + * * @example * first([1, 2, 3]); // => 1 - * + * * @example * first([]); // => undefined - * + * * @since 0.0.3 */ export function first(arr: Value[], defaultValue?: Value) { - return arr[0] ?? defaultValue; -} \ No newline at end of file + return arr[0] ?? defaultValue; +} diff --git a/core/stdlib/src/arrays/index.ts b/core/stdlib/src/arrays/index.ts index cf02dc8..8a1ce5f 100644 --- a/core/stdlib/src/arrays/index.ts +++ b/core/stdlib/src/arrays/index.ts @@ -2,4 +2,4 @@ export * from './cluster'; export * from './first'; export * from './last'; export * from './sum'; -export * from './unique'; \ No newline at end of file +export * from './unique'; diff --git a/core/stdlib/src/arrays/last/index.test.ts b/core/stdlib/src/arrays/last/index.test.ts index 9f9ce60..30bb6ba 100644 --- a/core/stdlib/src/arrays/last/index.test.ts +++ b/core/stdlib/src/arrays/last/index.test.ts @@ -20,4 +20,4 @@ describe('last', () => { expect(last([1, 2, 3], 42)).toBe(3); expect(last(['a', 'b', 'c'], 'default')).toBe('c'); }); -}); \ No newline at end of file +}); diff --git a/core/stdlib/src/arrays/last/index.ts b/core/stdlib/src/arrays/last/index.ts index 4661aaf..5b92ff1 100644 --- a/core/stdlib/src/arrays/last/index.ts +++ b/core/stdlib/src/arrays/last/index.ts @@ -2,17 +2,17 @@ * @name last * @section Arrays * @description Gets the last element of an array - * + * * @param {Value[]} arr The array to get the last element of * @param {Value} [defaultValue] The default value to return if the array is empty * @returns {Value | undefined} The last element of the array, or the default value if the array is empty - * + * * @example * last([1, 2, 3, 4, 5]); // => 5 - * + * * @example * last([], 3); // => 3 - * + * * @since 0.0.3 */ export function last(arr: Value[], defaultValue?: Value) { diff --git a/core/stdlib/src/arrays/sum/index.test.ts b/core/stdlib/src/arrays/sum/index.test.ts index 6785353..b7570d9 100644 --- a/core/stdlib/src/arrays/sum/index.test.ts +++ b/core/stdlib/src/arrays/sum/index.test.ts @@ -15,7 +15,7 @@ describe('sum', () => { }); it('return the sum of all elements using a getValue function', () => { - const result = sum([{ value: 1 }, { value: 2 }, { value: 3 }], (item) => item.value); + const result = sum([{ value: 1 }, { value: 2 }, { value: 3 }], item => item.value); expect(result).toBe(6); }); @@ -39,8 +39,8 @@ describe('sum', () => { }); it('handle arrays with a getValue function returning floating point numbers', () => { - const result = sum([{ value: 1.5 }, { value: 2.5 }, { value: 3.5 }], (item) => item.value); - + const result = sum([{ value: 1.5 }, { value: 2.5 }, { value: 3.5 }], item => item.value); + expect(result).toBe(7.5); }); -}); \ No newline at end of file +}); diff --git a/core/stdlib/src/arrays/sum/index.ts b/core/stdlib/src/arrays/sum/index.ts index 4a82850..427e979 100644 --- a/core/stdlib/src/arrays/sum/index.ts +++ b/core/stdlib/src/arrays/sum/index.ts @@ -2,16 +2,16 @@ * @name sum * @category Arrays * @description Returns the sum of all the elements in an array - * + * * @param {Value[]} array - The array to sum * @param {(item: Value) => number} [getValue] - A function that returns the value to sum from each element in the array * @returns {number} The sum of all the elements in the array - * + * * @example * sum([1, 2, 3, 4, 5]) // => 15 - * + * * sum([{ value: 1 }, { value: 2 }, { value: 3 }], (item) => item.value) // => 6 - * + * * @since 0.0.3 */ export function sum(array: Value[]): number; diff --git a/core/stdlib/src/arrays/unique/index.test.ts b/core/stdlib/src/arrays/unique/index.test.ts index bb4e30a..2ad9108 100644 --- a/core/stdlib/src/arrays/unique/index.test.ts +++ b/core/stdlib/src/arrays/unique/index.test.ts @@ -11,7 +11,7 @@ describe('unique', () => { it('return an array with unique objects based on id', () => { const result = unique( [{ id: 1 }, { id: 2 }, { id: 1 }], - (item) => item.id, + item => item.id, ); expect(result).toEqual([{ id: 1 }, { id: 2 }]); @@ -33,7 +33,7 @@ describe('unique', () => { const sym1 = Symbol('a'); const sym2 = Symbol('b'); const result = unique([sym1, sym2, sym1]); - + expect(result).toEqual([sym1, sym2]); }); @@ -42,4 +42,4 @@ describe('unique', () => { expect(result).toEqual([]); }); -}); \ No newline at end of file +}); diff --git a/core/stdlib/src/arrays/unique/index.ts b/core/stdlib/src/arrays/unique/index.ts index 6175c1d..9c54132 100644 --- a/core/stdlib/src/arrays/unique/index.ts +++ b/core/stdlib/src/arrays/unique/index.ts @@ -5,17 +5,17 @@ export type Extractor = (value: Value) => Key; * @name unique * @category Arrays * @description Returns a new array with unique values from the original array - * + * * @param {Value[]} array - The array to filter * @param {Function} [extractor] - The function to extract the value to compare * @returns {Value[]} - The new array with unique values - * + * * @example * unique([1, 2, 3, 3, 4, 5, 5, 6]) //=> [1, 2, 3, 4, 5, 6] - * + * * @example * unique([{ id: 1 }, { id: 2 }, { id: 1 }], (a, b) => a.id === b.id) //=> [{ id: 1 }, { id: 2 }] - * + * * @since 0.0.3 */ export function unique( diff --git a/core/stdlib/src/async/pool/index.ts b/core/stdlib/src/async/pool/index.ts index 0f6010f..96524bd 100644 --- a/core/stdlib/src/async/pool/index.ts +++ b/core/stdlib/src/async/pool/index.ts @@ -1,3 +1,3 @@ export interface AsyncPoolOptions { concurrency?: number; -} \ No newline at end of file +} diff --git a/core/stdlib/src/async/sleep/index.test.ts b/core/stdlib/src/async/sleep/index.test.ts index c018887..c10b91a 100644 --- a/core/stdlib/src/async/sleep/index.test.ts +++ b/core/stdlib/src/async/sleep/index.test.ts @@ -16,4 +16,4 @@ describe('sleep', () => { expect(end - start).toBeGreaterThan(delay - 5); }); -}); \ No newline at end of file +}); diff --git a/core/stdlib/src/async/sleep/index.ts b/core/stdlib/src/async/sleep/index.ts index db3f5fc..74019c7 100644 --- a/core/stdlib/src/async/sleep/index.ts +++ b/core/stdlib/src/async/sleep/index.ts @@ -2,20 +2,20 @@ * @name sleep * @category Async * @description Delays the execution of the current function by the specified amount of time - * + * * @param {number} ms - The amount of time to delay the execution of the current function * @returns {Promise} - A promise that resolves after the specified amount of time - * + * * @example * await sleep(1000); - * + * * @example * sleep(1000).then(() => { * console.log('Hello, World!'); * }); - * + * * @since 0.0.3 */ export function sleep(ms: number): Promise { - return new Promise((resolve) => setTimeout(resolve, ms)); -} \ No newline at end of file + return new Promise(resolve => setTimeout(resolve, ms)); +} diff --git a/core/stdlib/src/async/tryIt/index.test.ts b/core/stdlib/src/async/tryIt/index.test.ts index c84da67..8adadc1 100644 --- a/core/stdlib/src/async/tryIt/index.test.ts +++ b/core/stdlib/src/async/tryIt/index.test.ts @@ -13,7 +13,9 @@ describe('tryIt', () => { }); it('handle synchronous functions with errors', () => { - const syncFn = (): void => { throw new Error('Test error') }; + const syncFn = (): void => { + throw new Error('Test error'); + }; const wrappedSyncFn = tryIt(syncFn); const [error, result] = wrappedSyncFn(); @@ -34,7 +36,9 @@ describe('tryIt', () => { }); it('handle asynchronous functions with errors', async () => { - const asyncFn = async () => { throw new Error('Test error') }; + const asyncFn = async () => { + throw new Error('Test error'); + }; const wrappedAsyncFn = tryIt(asyncFn); const [error, result] = await wrappedAsyncFn(); @@ -64,4 +68,4 @@ describe('tryIt', () => { expect(error?.message).toBe('Test error'); expect(result).toBeUndefined(); }); -}); \ No newline at end of file +}); diff --git a/core/stdlib/src/async/tryIt/index.ts b/core/stdlib/src/async/tryIt/index.ts index 9b4d9ba..c9deb8c 100644 --- a/core/stdlib/src/async/tryIt/index.ts +++ b/core/stdlib/src/async/tryIt/index.ts @@ -8,17 +8,17 @@ export type TryItReturn = Return extends Promise * @name tryIt * @category Async * @description Wraps promise-based code in a try/catch block without forking the control flow - * + * * @param {Function} fn - The function to try * @returns {Function} - The function that will return a tuple with the error and the result - * + * * @example * const wrappedFetch = tryIt(fetch); * const [error, result] = await wrappedFetch('https://jsonplaceholder.typicode.com/todos/1'); - * + * * @example * const [error, result] = await tryIt(fetch)('https://jsonplaceholder.typicode.com/todos/1'); - * + * * @since 0.0.3 */ export function tryIt( @@ -30,11 +30,12 @@ export function tryIt( if (isPromise(result)) return result - .then((value) => [undefined, value]) - .catch((error) => [error, undefined]) as TryItReturn; + .then(value => [undefined, value]) + .catch(error => [error, undefined]) as TryItReturn; return [undefined, result] as TryItReturn; - } catch (error) { + } + catch (error) { return [error, undefined] as TryItReturn; } }; diff --git a/core/stdlib/src/bits/flags/index.ts b/core/stdlib/src/bits/flags/index.ts index 1be8ed3..b098e19 100644 --- a/core/stdlib/src/bits/flags/index.ts +++ b/core/stdlib/src/bits/flags/index.ts @@ -2,21 +2,21 @@ * @name flagsGenerator * @category Bits * @description Create a function that generates unique flags - * + * * @returns {Function} A function that generates unique flags * @throws {RangeError} If more than 31 flags are created * * @since 0.0.2 */ export function flagsGenerator() { - let lastFlag = 0; + let lastFlag = 0; - return () => { - // 31 flags is the maximum number of flags that can be created - // (without zero) because of the 32-bit integer limit in bitwise operations - if (lastFlag & 0x40000000) - throw new RangeError('Cannot create more than 31 flags'); + return () => { + // 31 flags is the maximum number of flags that can be created + // (without zero) because of the 32-bit integer limit in bitwise operations + if (lastFlag & 0x40000000) + throw new RangeError('Cannot create more than 31 flags'); - return (lastFlag = lastFlag === 0 ? 1 : lastFlag << 1); - }; + return (lastFlag = lastFlag === 0 ? 1 : lastFlag << 1); + }; } diff --git a/core/stdlib/src/bits/helpers/index.test.ts b/core/stdlib/src/bits/helpers/index.test.ts index 502e322..fdea3b6 100644 --- a/core/stdlib/src/bits/helpers/index.test.ts +++ b/core/stdlib/src/bits/helpers/index.test.ts @@ -1,7 +1,6 @@ import { describe, it, expect } from 'vitest'; import { and, or, not, has, is, unset, toggle } from '.'; - describe('flagsAnd', () => { it('no effect on zero flags', () => { const result = and(); @@ -14,7 +13,7 @@ describe('flagsAnd', () => { expect(result).toBe(0b1010); }); - + it('perform bitwise AND operation on flags', () => { const result = and(0b1111, 0b1010, 0b1100); @@ -30,15 +29,15 @@ describe('flagsOr', () => { }); it('source flag is returned if no flags are provided', () => { - const result = or(0b1010); - - expect(result).toBe(0b1010); + const result = or(0b1010); + + expect(result).toBe(0b1010); }); it('perform bitwise OR operation on flags', () => { - const result = or(0b1111, 0b1010, 0b1100); - - expect(result).toBe(0b1111); + const result = or(0b1111, 0b1010, 0b1100); + + expect(result).toBe(0b1111); }); }); @@ -58,9 +57,9 @@ describe('flagsHas', () => { }); it('check if a flag has a specific bit unset', () => { - const result = has(0b1010, 0b0100); - - expect(result).toBe(false); + const result = has(0b1010, 0b0100); + + expect(result).toBe(false); }); }); @@ -72,9 +71,9 @@ describe('flagsIs', () => { }); it('check if a flag is unset', () => { - const result = is(0); - - expect(result).toBe(false); + const result = is(0); + + expect(result).toBe(false); }); }); @@ -92,4 +91,4 @@ describe('flagsToggle', () => { expect(result).toBe(0b0010); }); -}); \ No newline at end of file +}); diff --git a/core/stdlib/src/bits/helpers/index.ts b/core/stdlib/src/bits/helpers/index.ts index ff4b692..4a5e840 100644 --- a/core/stdlib/src/bits/helpers/index.ts +++ b/core/stdlib/src/bits/helpers/index.ts @@ -2,7 +2,7 @@ * @name and * @category Bits * @description Function to combine multiple flags using the AND operator - * + * * @param {number[]} flags - The flags to combine * @returns {number} The combined flags * @@ -16,7 +16,7 @@ export function and(...flags: number[]) { * @name or * @category Bits * @description Function to combine multiple flags using the OR operator -* +* * @param {number[]} flags - The flags to combine * @returns {number} The combined flags * @@ -30,7 +30,7 @@ export function or(...flags: number[]) { * @name not * @category Bits * @description Function to combine multiple flags using the XOR operator -* +* * @param {number} flag - The flag to apply the NOT operator to * @returns {number} The result of the NOT operator * @@ -44,7 +44,7 @@ export function not(flag: number) { * @name has * @category Bits * @description Function to make sure a flag has a specific bit set -* +* * @param {number} flag - The flag to check * @param {number} other - Flag to check * @returns {boolean} Whether the flag has the bit set @@ -59,7 +59,7 @@ export function has(flag: number, other: number) { * @name is * @category Bits * @description Function to check if a flag is set -* +* * @param {number} flag - The flag to check * @returns {boolean} Whether the flag is set * @@ -73,7 +73,7 @@ export function is(flag: number) { * @name unset * @category Bits * @description Function to unset a flag -* +* * @param {number} flag - Source flag * @param {number} other - Flag to unset * @returns {number} The new flag @@ -88,7 +88,7 @@ export function unset(flag: number, other: number) { * @name toggle * @category Bits * @description Function to toggle (xor) a flag -* +* * @param {number} flag - Source flag * @param {number} other - Flag to toggle * @returns {number} The new flag diff --git a/core/stdlib/src/bits/index.ts b/core/stdlib/src/bits/index.ts index a21ed83..4fa0d37 100644 --- a/core/stdlib/src/bits/index.ts +++ b/core/stdlib/src/bits/index.ts @@ -1 +1 @@ -export * from './flags'; \ No newline at end of file +export * from './flags'; diff --git a/core/stdlib/src/bits/vector/index.test.ts b/core/stdlib/src/bits/vector/index.test.ts index 2aca9b3..e0fccc1 100644 --- a/core/stdlib/src/bits/vector/index.test.ts +++ b/core/stdlib/src/bits/vector/index.test.ts @@ -13,7 +13,7 @@ describe('BitVector', () => { it('set and get bits correctly', () => { const bitVector = new BitVector(16); bitVector.setBit(5); - + expect(bitVector.getBit(5)).toBe(true); expect(bitVector.getBit(4)).toBe(false); }); @@ -40,7 +40,7 @@ describe('BitVector', () => { const indices = [99, 88, 66, 65, 64, 63, 15, 14, 1, 0]; const result = []; indices.forEach(index => bitVector.setBit(index)); - + for (let i = bitVector.previousBit(100); i !== -1; i = bitVector.previousBit(i)) { result.push(i); } @@ -60,4 +60,4 @@ describe('BitVector', () => { expect(() => bitVector.previousBit(24)).toThrow(new RangeError('Unreachable value')); }); -}); \ No newline at end of file +}); diff --git a/core/stdlib/src/bits/vector/index.ts b/core/stdlib/src/bits/vector/index.ts index dbccbff..2ee8275 100644 --- a/core/stdlib/src/bits/vector/index.ts +++ b/core/stdlib/src/bits/vector/index.ts @@ -9,7 +9,7 @@ export interface BitVectorLike { * @name BitVector * @category Bits * @description A bit vector is a vector of bits that can be used to store a collection of bits - * + * * @since 0.0.3 */ export class BitVector extends Uint8Array implements BitVectorLike { @@ -58,4 +58,4 @@ export class BitVector extends Uint8Array implements BitVectorLike { throw new RangeError('Unreachable value'); } -} \ No newline at end of file +} diff --git a/core/stdlib/src/collections/get/index.ts b/core/stdlib/src/collections/get/index.ts index e5bc56c..0037948 100644 --- a/core/stdlib/src/collections/get/index.ts +++ b/core/stdlib/src/collections/get/index.ts @@ -1,14 +1,14 @@ import type { Collection, Path } from '../../types'; -export type ExtractFromObject, K> = - K extends keyof O +export type ExtractFromObject, K> + = K extends keyof O ? O[K] : K extends keyof NonNullable ? NonNullable[K] : never; -export type ExtractFromArray = - any[] extends A +export type ExtractFromArray + = any[] extends A ? A extends ReadonlyArray ? T | undefined : undefined @@ -16,19 +16,19 @@ export type ExtractFromArray = ? A[K] : undefined; -export type ExtractFromCollection = - K extends [] - ? O - : K extends [infer Key, ...infer Rest] - ? O extends Record - ? ExtractFromCollection, Rest> - : O extends readonly any[] - ? ExtractFromCollection, Rest> - : never - : never; +export type ExtractFromCollection + = K extends [] + ? O + : K extends [infer Key, ...infer Rest] + ? O extends Record + ? ExtractFromCollection, Rest> + : O extends readonly any[] + ? ExtractFromCollection, Rest> + : never + : never; type Get = ExtractFromCollection>; export function get(obj: O, path: K) { return path.split('.').reduce((acc, key) => (acc as any)?.[key], obj) as Get | undefined; -} \ No newline at end of file +} diff --git a/core/stdlib/src/collections/index.ts b/core/stdlib/src/collections/index.ts index cab6a69..a34bde4 100644 --- a/core/stdlib/src/collections/index.ts +++ b/core/stdlib/src/collections/index.ts @@ -1 +1 @@ -export * from './get'; \ No newline at end of file +export * from './get'; diff --git a/core/stdlib/src/index.ts b/core/stdlib/src/index.ts index 62ebea0..84a8548 100644 --- a/core/stdlib/src/index.ts +++ b/core/stdlib/src/index.ts @@ -8,4 +8,4 @@ export * from './structs'; export * from './sync'; export * from './text'; export * from './types'; -export * from './utils' +export * from './utils'; diff --git a/core/stdlib/src/math/basic/clamp/index.test.ts b/core/stdlib/src/math/basic/clamp/index.test.ts index 342485b..594a48b 100644 --- a/core/stdlib/src/math/basic/clamp/index.test.ts +++ b/core/stdlib/src/math/basic/clamp/index.test.ts @@ -1,4 +1,4 @@ -import { describe,it, expect } from 'vitest'; +import { describe, it, expect } from 'vitest'; import { clamp } from '.'; describe('clamp', () => { @@ -78,4 +78,4 @@ describe('clamp', () => { // min and max are -Infinity expect(clamp(50, -Infinity, -Infinity)).toBe(-Infinity); }); -}); \ No newline at end of file +}); diff --git a/core/stdlib/src/math/basic/clamp/index.ts b/core/stdlib/src/math/basic/clamp/index.ts index 898adc6..cd93525 100644 --- a/core/stdlib/src/math/basic/clamp/index.ts +++ b/core/stdlib/src/math/basic/clamp/index.ts @@ -2,7 +2,7 @@ * @name clamp * @category Math * @description Clamps a number between a minimum and maximum value - * + * * @param {number} value The number to clamp * @param {number} min Minimum value * @param {number} max Maximum value diff --git a/core/stdlib/src/math/basic/lerp/index.test.ts b/core/stdlib/src/math/basic/lerp/index.test.ts index c0455cb..dd55732 100644 --- a/core/stdlib/src/math/basic/lerp/index.test.ts +++ b/core/stdlib/src/math/basic/lerp/index.test.ts @@ -1,5 +1,5 @@ -import {describe, it, expect} from 'vitest'; -import {inverseLerp, lerp} from '.'; +import { describe, it, expect } from 'vitest'; +import { inverseLerp, lerp } from '.'; describe('lerp', () => { it('interpolates between two values', () => { diff --git a/core/stdlib/src/math/basic/remap/index.test.ts b/core/stdlib/src/math/basic/remap/index.test.ts index 778b249..022bae2 100644 --- a/core/stdlib/src/math/basic/remap/index.test.ts +++ b/core/stdlib/src/math/basic/remap/index.test.ts @@ -1,5 +1,5 @@ -import {describe, expect, it} from 'vitest'; -import {remap} from '.'; +import { describe, expect, it } from 'vitest'; +import { remap } from '.'; describe('remap', () => { it('map values from one range to another', () => { @@ -43,4 +43,4 @@ describe('remap', () => { // input range is zero (should return output min) expect(remap(5, 0, 0, 0, 100)).toBe(0); }); -}); \ No newline at end of file +}); diff --git a/core/stdlib/src/math/basic/remap/index.ts b/core/stdlib/src/math/basic/remap/index.ts index ddf5a5e..fe2bf61 100644 --- a/core/stdlib/src/math/basic/remap/index.ts +++ b/core/stdlib/src/math/basic/remap/index.ts @@ -1,11 +1,11 @@ import { clamp } from '../clamp'; -import {inverseLerp, lerp} from '../lerp'; +import { inverseLerp, lerp } from '../lerp'; /** * @name remap * @category Math * @description Map a value from one range to another - * + * * @param {number} value The value to map * @param {number} in_min The minimum value of the input range * @param {number} in_max The maximum value of the input range @@ -22,4 +22,4 @@ export function remap(value: number, in_min: number, in_max: number, out_min: nu const clampedValue = clamp(value, in_min, in_max); return lerp(out_min, out_max, inverseLerp(in_min, in_max, clampedValue)); -} \ No newline at end of file +} diff --git a/core/stdlib/src/math/bigint/clampBigInt/index.test.ts b/core/stdlib/src/math/bigint/clampBigInt/index.test.ts index 0c9fabc..b80db2b 100644 --- a/core/stdlib/src/math/bigint/clampBigInt/index.test.ts +++ b/core/stdlib/src/math/bigint/clampBigInt/index.test.ts @@ -1,5 +1,5 @@ -import {describe, it, expect} from 'vitest'; -import {clampBigInt} from '.'; +import { describe, it, expect } from 'vitest'; +import { clampBigInt } from '.'; describe('clampBigInt', () => { it('clamp a value within the given range', () => { @@ -32,4 +32,4 @@ describe('clampBigInt', () => { // negative range and value expect(clampBigInt(-10n, -100n, -5n)).toBe(-10n); }); -}); \ No newline at end of file +}); diff --git a/core/stdlib/src/math/bigint/clampBigInt/index.ts b/core/stdlib/src/math/bigint/clampBigInt/index.ts index a9a3200..d71747c 100644 --- a/core/stdlib/src/math/bigint/clampBigInt/index.ts +++ b/core/stdlib/src/math/bigint/clampBigInt/index.ts @@ -1,5 +1,5 @@ -import {minBigInt} from '../minBigInt'; -import {maxBigInt} from '../maxBigInt'; +import { minBigInt } from '../minBigInt'; +import { maxBigInt } from '../maxBigInt'; /** * @name clampBigInt diff --git a/core/stdlib/src/math/bigint/lerpBigInt/index.test.ts b/core/stdlib/src/math/bigint/lerpBigInt/index.test.ts index 95e6f5d..b955b4b 100644 --- a/core/stdlib/src/math/bigint/lerpBigInt/index.test.ts +++ b/core/stdlib/src/math/bigint/lerpBigInt/index.test.ts @@ -1,5 +1,5 @@ -import {describe, it, expect} from 'vitest'; -import {inverseLerpBigInt, lerpBigInt} from '.'; +import { describe, it, expect } from 'vitest'; +import { inverseLerpBigInt, lerpBigInt } from '.'; const MAX_SAFE_INTEGER = BigInt(Number.MAX_SAFE_INTEGER); diff --git a/core/stdlib/src/math/bigint/lerpBigInt/index.ts b/core/stdlib/src/math/bigint/lerpBigInt/index.ts index 3ba5f48..cb6483d 100644 --- a/core/stdlib/src/math/bigint/lerpBigInt/index.ts +++ b/core/stdlib/src/math/bigint/lerpBigInt/index.ts @@ -35,4 +35,4 @@ export function lerpBigInt(start: bigint, end: bigint, t: number) { */ export function inverseLerpBigInt(start: bigint, end: bigint, value: bigint) { return start === end ? 0 : Number((value - start) * SCALE_N / (end - start)) / SCALE; -} \ No newline at end of file +} diff --git a/core/stdlib/src/math/bigint/maxBigInt/index.test.ts b/core/stdlib/src/math/bigint/maxBigInt/index.test.ts index a88ef2b..165d43b 100644 --- a/core/stdlib/src/math/bigint/maxBigInt/index.test.ts +++ b/core/stdlib/src/math/bigint/maxBigInt/index.test.ts @@ -36,4 +36,4 @@ describe('maxBigInt', () => { const result = maxBigInt(...values); expect(result).toBe(999n); }); -}); \ No newline at end of file +}); diff --git a/core/stdlib/src/math/bigint/maxBigInt/index.ts b/core/stdlib/src/math/bigint/maxBigInt/index.ts index fe386c9..215f588 100644 --- a/core/stdlib/src/math/bigint/maxBigInt/index.ts +++ b/core/stdlib/src/math/bigint/maxBigInt/index.ts @@ -14,4 +14,4 @@ export function maxBigInt(...values: bigint[]) { throw new TypeError('maxBigInt requires at least one argument'); return values.reduce((acc, val) => val > acc ? val : acc); -} \ No newline at end of file +} diff --git a/core/stdlib/src/math/bigint/minBigInt/index.test.ts b/core/stdlib/src/math/bigint/minBigInt/index.test.ts index ca601ed..38f8f4b 100644 --- a/core/stdlib/src/math/bigint/minBigInt/index.test.ts +++ b/core/stdlib/src/math/bigint/minBigInt/index.test.ts @@ -1,5 +1,5 @@ -import {describe, it, expect} from 'vitest'; -import {minBigInt} from '.'; +import { describe, it, expect } from 'vitest'; +import { minBigInt } from '.'; describe('minBigInt', () => { it('returns Infinity when no values are provided', () => { @@ -32,8 +32,8 @@ describe('minBigInt', () => { }); it('handles a large number of bigints', () => { - const values = Array.from({length: 1000}, (_, i) => BigInt(i)); + const values = Array.from({ length: 1000 }, (_, i) => BigInt(i)); const result = minBigInt(...values); expect(result).toBe(0n); }); -}); \ No newline at end of file +}); diff --git a/core/stdlib/src/math/bigint/remapBigInt/index.test.ts b/core/stdlib/src/math/bigint/remapBigInt/index.test.ts index a934aaa..84dc691 100644 --- a/core/stdlib/src/math/bigint/remapBigInt/index.test.ts +++ b/core/stdlib/src/math/bigint/remapBigInt/index.test.ts @@ -1,5 +1,5 @@ -import {describe, expect, it} from 'vitest'; -import {remapBigInt} from '.'; +import { describe, expect, it } from 'vitest'; +import { remapBigInt } from '.'; describe('remapBigInt', () => { it('map values from one range to another', () => { @@ -29,4 +29,4 @@ describe('remapBigInt', () => { // input range is zero (should return output min) expect(remapBigInt(5n, 0n, 0n, 0n, 100n)).toBe(0n); }); -}); \ No newline at end of file +}); diff --git a/core/stdlib/src/math/bigint/remapBigInt/index.ts b/core/stdlib/src/math/bigint/remapBigInt/index.ts index 2de7a9d..c37ce52 100644 --- a/core/stdlib/src/math/bigint/remapBigInt/index.ts +++ b/core/stdlib/src/math/bigint/remapBigInt/index.ts @@ -1,11 +1,11 @@ import { clampBigInt } from '../clampBigInt'; -import {inverseLerpBigInt, lerpBigInt} from '../lerpBigInt'; +import { inverseLerpBigInt, lerpBigInt } from '../lerpBigInt'; /** * @name remapBigInt * @category Math * @description Map a bigint value from one range to another - * + * * @param {bigint} value The value to map * @param {bigint} in_min The minimum value of the input range * @param {bigint} in_max The maximum value of the input range @@ -22,4 +22,4 @@ export function remapBigInt(value: bigint, in_min: bigint, in_max: bigint, out_m const clampedValue = clampBigInt(value, in_min, in_max); return lerpBigInt(out_min, out_max, inverseLerpBigInt(in_min, in_max, clampedValue)); -} \ No newline at end of file +} diff --git a/core/stdlib/src/objects/omit/index.test.ts b/core/stdlib/src/objects/omit/index.test.ts index c086a89..2d96a4b 100644 --- a/core/stdlib/src/objects/omit/index.test.ts +++ b/core/stdlib/src/objects/omit/index.test.ts @@ -47,4 +47,4 @@ describe('omit', () => { expect(emptyTarget).toEqual({}); expect(emptyKeys).toEqual({ a: 1 }); }); -}); \ No newline at end of file +}); diff --git a/core/stdlib/src/objects/omit/index.ts b/core/stdlib/src/objects/omit/index.ts index 9f99b51..5c825c8 100644 --- a/core/stdlib/src/objects/omit/index.ts +++ b/core/stdlib/src/objects/omit/index.ts @@ -5,22 +5,22 @@ import type { Arrayable } from '../../types'; * @name omit * @category Objects * @description Returns a new object with the specified keys omitted - * + * * @param {object} target - The object to omit keys from * @param {Arrayable} keys - The keys to omit * @returns {Omit} The new object with the specified keys omitted - * + * * @example * omit({ a: 1, b: 2, c: 3 }, 'a') // => { b: 2, c: 3 } - * + * * @example * omit({ a: 1, b: 2, c: 3 }, ['a', 'b']) // => { c: 3 } - * + * * @since 0.0.3 */ export function omit( target: Target, - keys: Arrayable + keys: Arrayable, ): Omit { const result = { ...target }; @@ -31,7 +31,8 @@ export function omit( for (const key of keys) { delete result[key]; } - } else { + } + else { delete result[keys]; } diff --git a/core/stdlib/src/objects/pick/index.test.ts b/core/stdlib/src/objects/pick/index.test.ts index 48c088f..9475327 100644 --- a/core/stdlib/src/objects/pick/index.test.ts +++ b/core/stdlib/src/objects/pick/index.test.ts @@ -33,4 +33,4 @@ describe('pick', () => { expect(emptyTarget).toEqual({}); expect(emptyKeys).toEqual({}); }); -}); \ No newline at end of file +}); diff --git a/core/stdlib/src/objects/pick/index.ts b/core/stdlib/src/objects/pick/index.ts index 2589d9c..d7ff404 100644 --- a/core/stdlib/src/objects/pick/index.ts +++ b/core/stdlib/src/objects/pick/index.ts @@ -5,22 +5,22 @@ import type { Arrayable } from '../../types'; * @name pick * @category Objects * @description Returns a partial copy of an object containing only the keys specified - * + * * @param {object} target - The object to pick keys from * @param {Arrayable} keys - The keys to pick * @returns {Pick} The new object with the specified keys picked - * + * * @example * pick({ a: 1, b: 2, c: 3 }, 'a') // => { a: 1 } - * + * * @example * pick({ a: 1, b: 2, c: 3 }, ['a', 'b']) // => { a: 1, b: 2 } - * + * * @since 0.0.3 */ export function pick( target: Target, - keys: Arrayable + keys: Arrayable, ): Pick { const result = {} as Pick; @@ -31,9 +31,10 @@ export function pick( for (const key of keys) { result[key] = target[key]; } - } else { + } + else { result[keys] = target[keys]; } return result; -} \ No newline at end of file +} diff --git a/core/stdlib/src/patterns/behavioral/Command/index.test.ts b/core/stdlib/src/patterns/behavioral/Command/index.test.ts index c6691b5..7e29ce9 100644 --- a/core/stdlib/src/patterns/behavioral/Command/index.test.ts +++ b/core/stdlib/src/patterns/behavioral/Command/index.test.ts @@ -205,11 +205,11 @@ describe('asyncCommandHistory', () => { function addItemAsync(item: string): AsyncCommand { return { execute: async () => { - await new Promise((r) => setTimeout(r, 5)); + await new Promise(r => setTimeout(r, 5)); items.push(item); }, undo: async () => { - await new Promise((r) => setTimeout(r, 5)); + await new Promise(r => setTimeout(r, 5)); items.pop(); }, }; diff --git a/core/stdlib/src/patterns/behavioral/Command/sync.ts b/core/stdlib/src/patterns/behavioral/Command/sync.ts index 0e1b137..c057d89 100644 --- a/core/stdlib/src/patterns/behavioral/Command/sync.ts +++ b/core/stdlib/src/patterns/behavioral/Command/sync.ts @@ -40,7 +40,7 @@ export class CommandHistory extends BaseCommandHistory { batch(commands: Command[]): void { const macro: Command = { - execute: () => commands.forEach((c) => c.execute()), + execute: () => commands.forEach(c => c.execute()), undo: () => { for (let i = commands.length - 1; i >= 0; i--) commands[i]!.undo(); diff --git a/core/stdlib/src/patterns/behavioral/PubSub/index.test.ts b/core/stdlib/src/patterns/behavioral/PubSub/index.test.ts index c329d19..c2406e8 100644 --- a/core/stdlib/src/patterns/behavioral/PubSub/index.test.ts +++ b/core/stdlib/src/patterns/behavioral/PubSub/index.test.ts @@ -115,4 +115,4 @@ describe('pubsub', () => { expect(listener).toHaveBeenCalledTimes(1); }); -}); \ No newline at end of file +}); diff --git a/core/stdlib/src/patterns/behavioral/PubSub/index.ts b/core/stdlib/src/patterns/behavioral/PubSub/index.ts index fc47f04..82b22fb 100644 --- a/core/stdlib/src/patterns/behavioral/PubSub/index.ts +++ b/core/stdlib/src/patterns/behavioral/PubSub/index.ts @@ -4,13 +4,13 @@ * @description Simple PubSub implementation * * @since 0.0.2 - * + * * @template Events - Event map where keys are event names and values are listener signatures */ export class PubSub any>> { /** * Events map - * + * * @private * @type {Map>} */ @@ -25,7 +25,7 @@ export class PubSub any>> { /** * Subscribe to an event - * + * * @template {keyof Events} K * @param {K} event Name of the event * @param {Events[K]} listener Listener function @@ -44,7 +44,7 @@ export class PubSub any>> { /** * Unsubscribe from an event - * + * * @template {keyof Events} K * @param {K} event Name of the event * @param {Events[K]} listener Listener function @@ -61,7 +61,7 @@ export class PubSub any>> { /** * Subscribe to an event only once - * + * * @template {keyof Events} K * @param {K} event Name of the event * @param {Events[K]} listener Listener function @@ -69,8 +69,8 @@ export class PubSub any>> { */ public once(event: K, listener: Events[K]) { const onceListener = (...args: Parameters) => { - this.off(event, onceListener as Events[K]); - listener(...args); + this.off(event, onceListener as Events[K]); + listener(...args); }; this.on(event, onceListener as Events[K]); @@ -80,7 +80,7 @@ export class PubSub any>> { /** * Emit an event - * + * * @template {keyof Events} K * @param {K} event Name of the event * @param {...Parameters} args Arguments for the listener @@ -92,14 +92,14 @@ export class PubSub any>> { if (!listeners) return false; - listeners.forEach((listener) => listener(...args)); + listeners.forEach(listener => listener(...args)); return true; } /** * Clear all listeners for an event - * + * * @template {keyof Events} K * @param {K} event Name of the event * @returns {this} diff --git a/core/stdlib/src/patterns/behavioral/StateMachine/async.ts b/core/stdlib/src/patterns/behavioral/StateMachine/async.ts index 28f2777..4844698 100644 --- a/core/stdlib/src/patterns/behavioral/StateMachine/async.ts +++ b/core/stdlib/src/patterns/behavioral/StateMachine/async.ts @@ -39,7 +39,8 @@ export class AsyncStateMachine< if (isString(transition)) { target = transition; - } else { + } + else { if (transition.guard && !(await transition.guard(this.context))) return this.currentState; diff --git a/core/stdlib/src/patterns/behavioral/StateMachine/index.test.ts b/core/stdlib/src/patterns/behavioral/StateMachine/index.test.ts index 070cde1..f97f773 100644 --- a/core/stdlib/src/patterns/behavioral/StateMachine/index.test.ts +++ b/core/stdlib/src/patterns/behavioral/StateMachine/index.test.ts @@ -169,7 +169,7 @@ describe('stateMachine', () => { on: { FAIL: { target: 'idle', - guard: (ctx) => ctx.retries < 3, + guard: ctx => ctx.retries < 3, }, SUCCESS: 'done', }, @@ -255,7 +255,7 @@ describe('stateMachine', () => { on: { UNLOCK: { target: 'unlocked', - guard: (ctx) => ctx.unlocked, + guard: ctx => ctx.unlocked, }, }, exit: exitHook, @@ -374,7 +374,7 @@ describe('stateMachine', () => { on: { NEXT: { target: 'c', - guard: (ctx) => ctx.step === 1, + guard: ctx => ctx.step === 1, action: (ctx) => { ctx.step = 2; }, }, }, @@ -434,7 +434,7 @@ describe('asyncStateMachine', () => { on: { GO: { target: 'active', - guard: async (ctx) => ctx.allowed, + guard: async ctx => ctx.allowed, }, }, }, @@ -456,7 +456,7 @@ describe('asyncStateMachine', () => { on: { GO: { target: 'active', - guard: async (ctx) => ctx.allowed, + guard: async ctx => ctx.allowed, }, }, }, @@ -483,7 +483,7 @@ describe('asyncStateMachine', () => { FETCH: { target: 'done', action: async (ctx) => { - await new Promise((r) => setTimeout(r, 10)); + await new Promise(r => setTimeout(r, 10)); ctx.data = 'fetched'; order.push('action'); }, @@ -513,13 +513,13 @@ describe('asyncStateMachine', () => { a: { on: { GO: 'b' }, exit: async () => { - await new Promise((r) => setTimeout(r, 10)); + await new Promise(r => setTimeout(r, 10)); order.push('exit-a'); }, }, b: { entry: async () => { - await new Promise((r) => setTimeout(r, 10)); + await new Promise(r => setTimeout(r, 10)); order.push('entry-b'); }, }, @@ -544,7 +544,7 @@ describe('asyncStateMachine', () => { on: { UNLOCK: { target: 'unlocked', - guard: async (ctx) => ctx.unlocked, + guard: async ctx => ctx.unlocked, }, }, exit: exitHook, @@ -573,7 +573,7 @@ describe('asyncStateMachine', () => { on: { GO: { target: 'active', - guard: async (ctx) => ctx.ready, + guard: async ctx => ctx.ready, }, }, }, @@ -667,7 +667,7 @@ describe('asyncStateMachine', () => { on: { GO: { target: 'active', - guard: (ctx) => ctx.count === 0, + guard: ctx => ctx.count === 0, action: (ctx) => { ctx.count++; }, }, }, diff --git a/core/stdlib/src/patterns/behavioral/StateMachine/sync.ts b/core/stdlib/src/patterns/behavioral/StateMachine/sync.ts index cf10d18..9dd24d6 100644 --- a/core/stdlib/src/patterns/behavioral/StateMachine/sync.ts +++ b/core/stdlib/src/patterns/behavioral/StateMachine/sync.ts @@ -39,7 +39,8 @@ export class StateMachine< if (isString(transition)) { target = transition; - } else { + } + else { if (transition.guard && !transition.guard(this.context)) return this.currentState; diff --git a/core/stdlib/src/patterns/index.ts b/core/stdlib/src/patterns/index.ts index 6c0db50..1c69a96 100644 --- a/core/stdlib/src/patterns/index.ts +++ b/core/stdlib/src/patterns/index.ts @@ -1,3 +1,3 @@ export * from './behavioral/Command'; export * from './behavioral/PubSub'; -export * from './behavioral/StateMachine'; \ No newline at end of file +export * from './behavioral/StateMachine'; diff --git a/core/stdlib/src/structs/BinaryHeap/index.test.ts b/core/stdlib/src/structs/BinaryHeap/index.test.ts index 02c44ef..3a3e7c9 100644 --- a/core/stdlib/src/structs/BinaryHeap/index.test.ts +++ b/core/stdlib/src/structs/BinaryHeap/index.test.ts @@ -3,227 +3,227 @@ import { describe, expect, it } from 'vitest'; import { BinaryHeap } from '.'; describe('BinaryHeap', () => { - describe('constructor', () => { - it('should create an empty heap', () => { - const heap = new BinaryHeap(); + describe('constructor', () => { + it('should create an empty heap', () => { + const heap = new BinaryHeap(); - expect(heap.length).toBe(0); - expect(heap.isEmpty).toBe(true); - }); - - it('should create a heap from single value', () => { - const heap = new BinaryHeap(42); - - expect(heap.length).toBe(1); - expect(heap.peek()).toBe(42); - }); - - it('should create a heap from array (heapify)', () => { - const heap = new BinaryHeap([5, 3, 8, 1, 4]); - - expect(heap.length).toBe(5); - expect(heap.peek()).toBe(1); - }); - - it('should accept a custom comparator for max-heap', () => { - const heap = new BinaryHeap([5, 3, 8, 1, 4], { - comparator: (a, b) => b - a, - }); - - expect(heap.peek()).toBe(8); - }); + expect(heap.length).toBe(0); + expect(heap.isEmpty).toBe(true); }); - describe('push', () => { - it('should insert elements maintaining heap property', () => { - const heap = new BinaryHeap(); + it('should create a heap from single value', () => { + const heap = new BinaryHeap(42); - heap.push(5); - heap.push(3); - heap.push(8); - heap.push(1); - - expect(heap.peek()).toBe(1); - expect(heap.length).toBe(4); - }); - - it('should handle duplicate values', () => { - const heap = new BinaryHeap(); - - heap.push(3); - heap.push(3); - heap.push(3); - - expect(heap.length).toBe(3); - expect(heap.peek()).toBe(3); - }); + expect(heap.length).toBe(1); + expect(heap.peek()).toBe(42); }); - describe('pop', () => { - it('should return undefined for empty heap', () => { - const heap = new BinaryHeap(); + it('should create a heap from array (heapify)', () => { + const heap = new BinaryHeap([5, 3, 8, 1, 4]); - expect(heap.pop()).toBeUndefined(); - }); - - it('should extract elements in min-heap order', () => { - const heap = new BinaryHeap([5, 3, 8, 1, 4, 2, 7, 6]); - const sorted: number[] = []; - - while (!heap.isEmpty) { - sorted.push(heap.pop()!); - } - - expect(sorted).toEqual([1, 2, 3, 4, 5, 6, 7, 8]); - }); - - it('should extract elements in max-heap order with custom comparator', () => { - const heap = new BinaryHeap([5, 3, 8, 1, 4], { - comparator: (a, b) => b - a, - }); - const sorted: number[] = []; - - while (!heap.isEmpty) { - sorted.push(heap.pop()!); - } - - expect(sorted).toEqual([8, 5, 4, 3, 1]); - }); - - it('should handle single element', () => { - const heap = new BinaryHeap(42); - - expect(heap.pop()).toBe(42); - expect(heap.isEmpty).toBe(true); - }); + expect(heap.length).toBe(5); + expect(heap.peek()).toBe(1); }); - describe('peek', () => { - it('should return undefined for empty heap', () => { - const heap = new BinaryHeap(); + it('should accept a custom comparator for max-heap', () => { + const heap = new BinaryHeap([5, 3, 8, 1, 4], { + comparator: (a, b) => b - a, + }); - expect(heap.peek()).toBeUndefined(); - }); + expect(heap.peek()).toBe(8); + }); + }); - it('should return root without removing it', () => { - const heap = new BinaryHeap([5, 3, 1]); + describe('push', () => { + it('should insert elements maintaining heap property', () => { + const heap = new BinaryHeap(); - expect(heap.peek()).toBe(1); - expect(heap.length).toBe(3); - }); + heap.push(5); + heap.push(3); + heap.push(8); + heap.push(1); + + expect(heap.peek()).toBe(1); + expect(heap.length).toBe(4); }); - describe('clear', () => { - it('should remove all elements', () => { - const heap = new BinaryHeap([1, 2, 3]); + it('should handle duplicate values', () => { + const heap = new BinaryHeap(); - const result = heap.clear(); + heap.push(3); + heap.push(3); + heap.push(3); - expect(heap.length).toBe(0); - expect(heap.isEmpty).toBe(true); - expect(result).toBe(heap); - }); + expect(heap.length).toBe(3); + expect(heap.peek()).toBe(3); + }); + }); + + describe('pop', () => { + it('should return undefined for empty heap', () => { + const heap = new BinaryHeap(); + + expect(heap.pop()).toBeUndefined(); }); - describe('toArray', () => { - it('should return empty array for empty heap', () => { - const heap = new BinaryHeap(); + it('should extract elements in min-heap order', () => { + const heap = new BinaryHeap([5, 3, 8, 1, 4, 2, 7, 6]); + const sorted: number[] = []; - expect(heap.toArray()).toEqual([]); - }); + while (!heap.isEmpty) { + sorted.push(heap.pop()!); + } - it('should return a shallow copy', () => { - const heap = new BinaryHeap([3, 1, 2]); - const arr = heap.toArray(); - - arr.push(99); - - expect(heap.length).toBe(3); - }); + expect(sorted).toEqual([1, 2, 3, 4, 5, 6, 7, 8]); }); - describe('toString', () => { - it('should return formatted string', () => { - const heap = new BinaryHeap([1, 2, 3]); + it('should extract elements in max-heap order with custom comparator', () => { + const heap = new BinaryHeap([5, 3, 8, 1, 4], { + comparator: (a, b) => b - a, + }); + const sorted: number[] = []; - expect(heap.toString()).toBe('BinaryHeap(3)'); - }); + while (!heap.isEmpty) { + sorted.push(heap.pop()!); + } + + expect(sorted).toEqual([8, 5, 4, 3, 1]); }); - describe('iterator', () => { - it('should iterate over heap elements', () => { - const heap = new BinaryHeap([5, 3, 8, 1]); - const elements = [...heap]; + it('should handle single element', () => { + const heap = new BinaryHeap(42); - expect(elements.length).toBe(4); - expect(elements[0]).toBe(1); - }); + expect(heap.pop()).toBe(42); + expect(heap.isEmpty).toBe(true); + }); + }); + + describe('peek', () => { + it('should return undefined for empty heap', () => { + const heap = new BinaryHeap(); + + expect(heap.peek()).toBeUndefined(); }); - describe('custom comparator', () => { - it('should work with string length comparator', () => { - const heap = new BinaryHeap(['banana', 'apple', 'kiwi', 'fig'], { - comparator: (a, b) => a.length - b.length, - }); + it('should return root without removing it', () => { + const heap = new BinaryHeap([5, 3, 1]); - expect(heap.pop()).toBe('fig'); - expect(heap.pop()).toBe('kiwi'); - }); + expect(heap.peek()).toBe(1); + expect(heap.length).toBe(3); + }); + }); - it('should work with object comparator', () => { - interface Task { - priority: number; - name: string; - } + describe('clear', () => { + it('should remove all elements', () => { + const heap = new BinaryHeap([1, 2, 3]); - const heap = new BinaryHeap( - [ - { priority: 3, name: 'low' }, - { priority: 1, name: 'high' }, - { priority: 2, name: 'medium' }, - ], - { comparator: (a, b) => a.priority - b.priority }, - ); + const result = heap.clear(); - expect(heap.pop()?.name).toBe('high'); - expect(heap.pop()?.name).toBe('medium'); - expect(heap.pop()?.name).toBe('low'); - }); + expect(heap.length).toBe(0); + expect(heap.isEmpty).toBe(true); + expect(result).toBe(heap); + }); + }); + + describe('toArray', () => { + it('should return empty array for empty heap', () => { + const heap = new BinaryHeap(); + + expect(heap.toArray()).toEqual([]); }); - describe('heapify', () => { - it('should correctly heapify large arrays', () => { - const values = Array.from({ length: 1000 }, () => Math.random() * 1000 | 0); - const heap = new BinaryHeap(values); - const sorted: number[] = []; + it('should return a shallow copy', () => { + const heap = new BinaryHeap([3, 1, 2]); + const arr = heap.toArray(); - while (!heap.isEmpty) { - sorted.push(heap.pop()!); - } + arr.push(99); - const expected = [...values].sort((a, b) => a - b); + expect(heap.length).toBe(3); + }); + }); - expect(sorted).toEqual(expected); - }); + describe('toString', () => { + it('should return formatted string', () => { + const heap = new BinaryHeap([1, 2, 3]); + + expect(heap.toString()).toBe('BinaryHeap(3)'); + }); + }); + + describe('iterator', () => { + it('should iterate over heap elements', () => { + const heap = new BinaryHeap([5, 3, 8, 1]); + const elements = [...heap]; + + expect(elements.length).toBe(4); + expect(elements[0]).toBe(1); + }); + }); + + describe('custom comparator', () => { + it('should work with string length comparator', () => { + const heap = new BinaryHeap(['banana', 'apple', 'kiwi', 'fig'], { + comparator: (a, b) => a.length - b.length, + }); + + expect(heap.pop()).toBe('fig'); + expect(heap.pop()).toBe('kiwi'); }); - describe('interleaved operations', () => { - it('should maintain heap property with mixed push and pop', () => { - const heap = new BinaryHeap(); + it('should work with object comparator', () => { + interface Task { + priority: number; + name: string; + } - heap.push(10); - heap.push(5); - expect(heap.pop()).toBe(5); + const heap = new BinaryHeap( + [ + { priority: 3, name: 'low' }, + { priority: 1, name: 'high' }, + { priority: 2, name: 'medium' }, + ], + { comparator: (a, b) => a.priority - b.priority }, + ); - heap.push(3); - heap.push(7); - expect(heap.pop()).toBe(3); - - heap.push(1); - expect(heap.pop()).toBe(1); - expect(heap.pop()).toBe(7); - expect(heap.pop()).toBe(10); - expect(heap.pop()).toBeUndefined(); - }); + expect(heap.pop()?.name).toBe('high'); + expect(heap.pop()?.name).toBe('medium'); + expect(heap.pop()?.name).toBe('low'); }); + }); + + describe('heapify', () => { + it('should correctly heapify large arrays', () => { + const values = Array.from({ length: 1000 }, () => Math.random() * 1000 | 0); + const heap = new BinaryHeap(values); + const sorted: number[] = []; + + while (!heap.isEmpty) { + sorted.push(heap.pop()!); + } + + const expected = [...values].sort((a, b) => a - b); + + expect(sorted).toEqual(expected); + }); + }); + + describe('interleaved operations', () => { + it('should maintain heap property with mixed push and pop', () => { + const heap = new BinaryHeap(); + + heap.push(10); + heap.push(5); + expect(heap.pop()).toBe(5); + + heap.push(3); + heap.push(7); + expect(heap.pop()).toBe(3); + + heap.push(1); + expect(heap.pop()).toBe(1); + expect(heap.pop()).toBe(7); + expect(heap.pop()).toBe(10); + expect(heap.pop()).toBeUndefined(); + }); + }); }); diff --git a/core/stdlib/src/structs/BinaryHeap/index.ts b/core/stdlib/src/structs/BinaryHeap/index.ts index e5b04cf..71a193f 100644 --- a/core/stdlib/src/structs/BinaryHeap/index.ts +++ b/core/stdlib/src/structs/BinaryHeap/index.ts @@ -5,7 +5,7 @@ import type { BinaryHeapLike, Comparator } from './types'; export type { BinaryHeapLike, Comparator } from './types'; export interface BinaryHeapOptions { - comparator?: Comparator; + comparator?: Comparator; } /** @@ -27,194 +27,194 @@ const defaultComparator: Comparator = (a: number, b: number) => a - b; * @template T The type of elements stored in the heap */ export class BinaryHeap implements BinaryHeapLike { - /** + /** * The comparator function used to order elements * * @private * @type {Comparator} */ - private readonly comparator: Comparator; + private readonly comparator: Comparator; - /** + /** * Internal flat array backing the heap * * @private * @type {T[]} */ - private readonly heap: T[] = []; + private readonly heap: T[] = []; - /** + /** * Creates an instance of BinaryHeap * * @param {(T[] | T)} [initialValues] The initial values to heapify * @param {BinaryHeapOptions} [options] Heap configuration */ - constructor(initialValues?: T[] | T, options?: BinaryHeapOptions) { - this.comparator = options?.comparator ?? defaultComparator; + constructor(initialValues?: T[] | T, options?: BinaryHeapOptions) { + this.comparator = options?.comparator ?? defaultComparator; - if (initialValues !== null && initialValues !== undefined) { - const items = isArray(initialValues) ? initialValues : [initialValues]; - this.heap.push(...items); - this.heapify(); - } + if (initialValues !== null && initialValues !== undefined) { + const items = isArray(initialValues) ? initialValues : [initialValues]; + this.heap.push(...items); + this.heapify(); } + } - /** + /** * Gets the number of elements in the heap * @returns {number} The number of elements in the heap */ - public get length(): number { - return this.heap.length; - } + public get length(): number { + return this.heap.length; + } - /** + /** * Checks if the heap is empty * @returns {boolean} `true` if the heap is empty, `false` otherwise */ - public get isEmpty(): boolean { - return this.heap.length === 0; - } + public get isEmpty(): boolean { + return this.heap.length === 0; + } - /** + /** * Pushes an element into the heap * @param {T} element The element to insert */ - public push(element: T): void { - this.heap.push(element); - this.siftUp(this.heap.length - 1); - } + public push(element: T): void { + this.heap.push(element); + this.siftUp(this.heap.length - 1); + } - /** + /** * Removes and returns the root element (min or max depending on comparator) * @returns {T | undefined} The root element, or `undefined` if the heap is empty */ - public pop(): T | undefined { - if (this.heap.length === 0) return undefined; + public pop(): T | undefined { + if (this.heap.length === 0) return undefined; - const root = first(this.heap)!; - const last = this.heap.pop()!; + const root = first(this.heap)!; + const last = this.heap.pop()!; - if (this.heap.length > 0) { - this.heap[0] = last; - this.siftDown(0); - } - - return root; + if (this.heap.length > 0) { + this.heap[0] = last; + this.siftDown(0); } - /** + return root; + } + + /** * Returns the root element without removing it * @returns {T | undefined} The root element, or `undefined` if the heap is empty */ - public peek(): T | undefined { - return first(this.heap); - } + public peek(): T | undefined { + return first(this.heap); + } - /** + /** * Removes all elements from the heap * @returns {this} The heap instance for chaining */ - public clear(): this { - this.heap.length = 0; - return this; - } + public clear(): this { + this.heap.length = 0; + return this; + } - /** + /** * Returns a shallow copy of the heap elements as an array (heap order, not sorted) * @returns {T[]} Array of elements in heap order */ - public toArray(): T[] { - return this.heap.slice(); - } + public toArray(): T[] { + return this.heap.slice(); + } - /** + /** * Returns a string representation of the heap * @returns {string} String representation */ - public toString(): string { - return `BinaryHeap(${this.heap.length})`; - } + public toString(): string { + return `BinaryHeap(${this.heap.length})`; + } - /** + /** * Iterator over heap elements in heap order */ - public *[Symbol.iterator](): Iterator { - yield* this.heap; - } + public* [Symbol.iterator](): Iterator { + yield* this.heap; + } - /** + /** * Async iterator over heap elements in heap order */ - public async *[Symbol.asyncIterator](): AsyncIterator { - for (const element of this.heap) - yield element; - } + public async* [Symbol.asyncIterator](): AsyncIterator { + for (const element of this.heap) + yield element; + } - /** + /** * Restores heap property by sifting an element up * * @private * @param {number} index The index of the element to sift up */ - private siftUp(index: number): void { - const heap = this.heap; - const cmp = this.comparator; + private siftUp(index: number): void { + const heap = this.heap; + const cmp = this.comparator; - while (index > 0) { - const parent = (index - 1) >> 1; + while (index > 0) { + const parent = (index - 1) >> 1; - if (cmp(heap[index]!, heap[parent]!) >= 0) break; + if (cmp(heap[index]!, heap[parent]!) >= 0) break; - const temp = heap[index]!; - heap[index] = heap[parent]!; - heap[parent] = temp; + const temp = heap[index]!; + heap[index] = heap[parent]!; + heap[parent] = temp; - index = parent; - } + index = parent; } + } - /** + /** * Restores heap property by sifting an element down * * @private * @param {number} index The index of the element to sift down */ - private siftDown(index: number): void { - const heap = this.heap; - const cmp = this.comparator; - const length = heap.length; + private siftDown(index: number): void { + const heap = this.heap; + const cmp = this.comparator; + const length = heap.length; - while (true) { - let smallest = index; - const left = 2 * index + 1; - const right = 2 * index + 2; + while (true) { + let smallest = index; + const left = 2 * index + 1; + const right = 2 * index + 2; - if (left < length && cmp(heap[left]!, heap[smallest]!) < 0) { - smallest = left; - } + if (left < length && cmp(heap[left]!, heap[smallest]!) < 0) { + smallest = left; + } - if (right < length && cmp(heap[right]!, heap[smallest]!) < 0) { - smallest = right; - } + if (right < length && cmp(heap[right]!, heap[smallest]!) < 0) { + smallest = right; + } - if (smallest === index) break; + if (smallest === index) break; - const temp = heap[index]!; - heap[index] = heap[smallest]!; - heap[smallest] = temp; + const temp = heap[index]!; + heap[index] = heap[smallest]!; + heap[smallest] = temp; - index = smallest; - } + index = smallest; } + } - /** + /** * Builds heap from unordered array in O(n) using Floyd's algorithm * * @private */ - private heapify(): void { - for (let i = (this.heap.length >> 1) - 1; i >= 0; i--) { - this.siftDown(i); - } + private heapify(): void { + for (let i = (this.heap.length >> 1) - 1; i >= 0; i--) { + this.siftDown(i); } + } } diff --git a/core/stdlib/src/structs/CircularBuffer/index.ts b/core/stdlib/src/structs/CircularBuffer/index.ts index 9f59972..ced95bd 100644 --- a/core/stdlib/src/structs/CircularBuffer/index.ts +++ b/core/stdlib/src/structs/CircularBuffer/index.ts @@ -234,7 +234,7 @@ export class CircularBuffer implements CircularBufferLike { * * @returns {AsyncIterableIterator} */ - async *[Symbol.asyncIterator]() { + async* [Symbol.asyncIterator]() { for (const element of this) yield element; } diff --git a/core/stdlib/src/structs/Deque/index.ts b/core/stdlib/src/structs/Deque/index.ts index 05847bb..a649631 100644 --- a/core/stdlib/src/structs/Deque/index.ts +++ b/core/stdlib/src/structs/Deque/index.ts @@ -173,7 +173,7 @@ export class Deque implements DequeLike { * * @returns {AsyncIterableIterator} */ - async *[Symbol.asyncIterator]() { + async* [Symbol.asyncIterator]() { for (const element of this.buffer) yield element; } diff --git a/core/stdlib/src/structs/LinkedList/index.test.ts b/core/stdlib/src/structs/LinkedList/index.test.ts index a8ef9ed..31867ec 100644 --- a/core/stdlib/src/structs/LinkedList/index.test.ts +++ b/core/stdlib/src/structs/LinkedList/index.test.ts @@ -3,404 +3,404 @@ import { describe, expect, it } from 'vitest'; import { LinkedList } from '.'; describe('LinkedList', () => { - describe('constructor', () => { - it('should create an empty list', () => { - const list = new LinkedList(); + describe('constructor', () => { + it('should create an empty list', () => { + const list = new LinkedList(); - expect(list.length).toBe(0); - expect(list.isEmpty).toBe(true); - expect(list.head).toBeUndefined(); - expect(list.tail).toBeUndefined(); - }); - - it('should create a list from single value', () => { - const list = new LinkedList(42); - - expect(list.length).toBe(1); - expect(list.peekFront()).toBe(42); - expect(list.peekBack()).toBe(42); - }); - - it('should create a list from array', () => { - const list = new LinkedList([1, 2, 3]); - - expect(list.length).toBe(3); - expect(list.peekFront()).toBe(1); - expect(list.peekBack()).toBe(3); - }); + expect(list.length).toBe(0); + expect(list.isEmpty).toBe(true); + expect(list.head).toBeUndefined(); + expect(list.tail).toBeUndefined(); }); - describe('pushBack', () => { - it('should append to empty list', () => { - const list = new LinkedList(); + it('should create a list from single value', () => { + const list = new LinkedList(42); - const node = list.pushBack(1); - - expect(list.length).toBe(1); - expect(node.value).toBe(1); - expect(list.head).toBe(node); - expect(list.tail).toBe(node); - }); - - it('should append to non-empty list', () => { - const list = new LinkedList([1, 2]); - - list.pushBack(3); - - expect(list.length).toBe(3); - expect(list.peekBack()).toBe(3); - expect(list.peekFront()).toBe(1); - }); - - it('should return the created node', () => { - const list = new LinkedList(); - - const node = list.pushBack(5); - - expect(node.value).toBe(5); - expect(node.prev).toBeUndefined(); - expect(node.next).toBeUndefined(); - }); + expect(list.length).toBe(1); + expect(list.peekFront()).toBe(42); + expect(list.peekBack()).toBe(42); }); - describe('pushFront', () => { - it('should prepend to empty list', () => { - const list = new LinkedList(); + it('should create a list from array', () => { + const list = new LinkedList([1, 2, 3]); - const node = list.pushFront(1); + expect(list.length).toBe(3); + expect(list.peekFront()).toBe(1); + expect(list.peekBack()).toBe(3); + }); + }); - expect(list.length).toBe(1); - expect(list.head).toBe(node); - expect(list.tail).toBe(node); - }); + describe('pushBack', () => { + it('should append to empty list', () => { + const list = new LinkedList(); - it('should prepend to non-empty list', () => { - const list = new LinkedList([2, 3]); + const node = list.pushBack(1); - list.pushFront(1); - - expect(list.length).toBe(3); - expect(list.peekFront()).toBe(1); - expect(list.peekBack()).toBe(3); - }); + expect(list.length).toBe(1); + expect(node.value).toBe(1); + expect(list.head).toBe(node); + expect(list.tail).toBe(node); }); - describe('popBack', () => { - it('should return undefined for empty list', () => { - const list = new LinkedList(); + it('should append to non-empty list', () => { + const list = new LinkedList([1, 2]); - expect(list.popBack()).toBeUndefined(); - }); + list.pushBack(3); - it('should remove and return last value', () => { - const list = new LinkedList([1, 2, 3]); - - expect(list.popBack()).toBe(3); - expect(list.length).toBe(2); - expect(list.peekBack()).toBe(2); - }); - - it('should handle single element', () => { - const list = new LinkedList(1); - - expect(list.popBack()).toBe(1); - expect(list.isEmpty).toBe(true); - expect(list.head).toBeUndefined(); - expect(list.tail).toBeUndefined(); - }); + expect(list.length).toBe(3); + expect(list.peekBack()).toBe(3); + expect(list.peekFront()).toBe(1); }); - describe('popFront', () => { - it('should return undefined for empty list', () => { - const list = new LinkedList(); + it('should return the created node', () => { + const list = new LinkedList(); - expect(list.popFront()).toBeUndefined(); - }); + const node = list.pushBack(5); - it('should remove and return first value', () => { - const list = new LinkedList([1, 2, 3]); + expect(node.value).toBe(5); + expect(node.prev).toBeUndefined(); + expect(node.next).toBeUndefined(); + }); + }); - expect(list.popFront()).toBe(1); - expect(list.length).toBe(2); - expect(list.peekFront()).toBe(2); - }); + describe('pushFront', () => { + it('should prepend to empty list', () => { + const list = new LinkedList(); - it('should handle single element', () => { - const list = new LinkedList(1); + const node = list.pushFront(1); - expect(list.popFront()).toBe(1); - expect(list.isEmpty).toBe(true); - expect(list.head).toBeUndefined(); - expect(list.tail).toBeUndefined(); - }); + expect(list.length).toBe(1); + expect(list.head).toBe(node); + expect(list.tail).toBe(node); }); - describe('peekBack', () => { - it('should return undefined for empty list', () => { - const list = new LinkedList(); + it('should prepend to non-empty list', () => { + const list = new LinkedList([2, 3]); - expect(list.peekBack()).toBeUndefined(); - }); + list.pushFront(1); - it('should return last value without removing', () => { - const list = new LinkedList([1, 2, 3]); + expect(list.length).toBe(3); + expect(list.peekFront()).toBe(1); + expect(list.peekBack()).toBe(3); + }); + }); - expect(list.peekBack()).toBe(3); - expect(list.length).toBe(3); - }); + describe('popBack', () => { + it('should return undefined for empty list', () => { + const list = new LinkedList(); + + expect(list.popBack()).toBeUndefined(); }); - describe('peekFront', () => { - it('should return undefined for empty list', () => { - const list = new LinkedList(); + it('should remove and return last value', () => { + const list = new LinkedList([1, 2, 3]); - expect(list.peekFront()).toBeUndefined(); - }); - - it('should return first value without removing', () => { - const list = new LinkedList([1, 2, 3]); - - expect(list.peekFront()).toBe(1); - expect(list.length).toBe(3); - }); + expect(list.popBack()).toBe(3); + expect(list.length).toBe(2); + expect(list.peekBack()).toBe(2); }); - describe('insertBefore', () => { - it('should insert before head', () => { - const list = new LinkedList(); - const node = list.pushBack(2); + it('should handle single element', () => { + const list = new LinkedList(1); - list.insertBefore(node, 1); + expect(list.popBack()).toBe(1); + expect(list.isEmpty).toBe(true); + expect(list.head).toBeUndefined(); + expect(list.tail).toBeUndefined(); + }); + }); - expect(list.peekFront()).toBe(1); - expect(list.peekBack()).toBe(2); - expect(list.length).toBe(2); - }); + describe('popFront', () => { + it('should return undefined for empty list', () => { + const list = new LinkedList(); - it('should insert before middle node', () => { - const list = new LinkedList([1, 3]); - const tail = list.tail!; - - list.insertBefore(tail, 2); - - expect(list.toArray()).toEqual([1, 2, 3]); - }); - - it('should return the created node', () => { - const list = new LinkedList(); - const existing = list.pushBack(2); - - const newNode = list.insertBefore(existing, 1); - - expect(newNode.value).toBe(1); - expect(newNode.next).toBe(existing); - }); + expect(list.popFront()).toBeUndefined(); }); - describe('insertAfter', () => { - it('should insert after tail', () => { - const list = new LinkedList(); - const node = list.pushBack(1); + it('should remove and return first value', () => { + const list = new LinkedList([1, 2, 3]); - list.insertAfter(node, 2); - - expect(list.peekFront()).toBe(1); - expect(list.peekBack()).toBe(2); - expect(list.length).toBe(2); - }); - - it('should insert after middle node', () => { - const list = new LinkedList([1, 3]); - const head = list.head!; - - list.insertAfter(head, 2); - - expect(list.toArray()).toEqual([1, 2, 3]); - }); - - it('should return the created node', () => { - const list = new LinkedList(); - const existing = list.pushBack(1); - - const newNode = list.insertAfter(existing, 2); - - expect(newNode.value).toBe(2); - expect(newNode.prev).toBe(existing); - }); + expect(list.popFront()).toBe(1); + expect(list.length).toBe(2); + expect(list.peekFront()).toBe(2); }); - describe('remove', () => { - it('should remove head node', () => { - const list = new LinkedList([1, 2, 3]); - const head = list.head!; + it('should handle single element', () => { + const list = new LinkedList(1); - const value = list.remove(head); + expect(list.popFront()).toBe(1); + expect(list.isEmpty).toBe(true); + expect(list.head).toBeUndefined(); + expect(list.tail).toBeUndefined(); + }); + }); - expect(value).toBe(1); - expect(list.length).toBe(2); - expect(list.peekFront()).toBe(2); - }); + describe('peekBack', () => { + it('should return undefined for empty list', () => { + const list = new LinkedList(); - it('should remove tail node', () => { - const list = new LinkedList([1, 2, 3]); - const tail = list.tail!; - - const value = list.remove(tail); - - expect(value).toBe(3); - expect(list.length).toBe(2); - expect(list.peekBack()).toBe(2); - }); - - it('should remove middle node', () => { - const list = new LinkedList([1, 2, 3]); - const middle = list.head!.next!; - - const value = list.remove(middle); - - expect(value).toBe(2); - expect(list.toArray()).toEqual([1, 3]); - }); - - it('should remove single element', () => { - const list = new LinkedList(); - const node = list.pushBack(1); - - list.remove(node); - - expect(list.isEmpty).toBe(true); - expect(list.head).toBeUndefined(); - expect(list.tail).toBeUndefined(); - }); - - it('should detach the removed node', () => { - const list = new LinkedList([1, 2, 3]); - const middle = list.head!.next!; - - list.remove(middle); - - expect(middle.prev).toBeUndefined(); - expect(middle.next).toBeUndefined(); - }); + expect(list.peekBack()).toBeUndefined(); }); - describe('clear', () => { - it('should remove all elements', () => { - const list = new LinkedList([1, 2, 3]); + it('should return last value without removing', () => { + const list = new LinkedList([1, 2, 3]); - const result = list.clear(); + expect(list.peekBack()).toBe(3); + expect(list.length).toBe(3); + }); + }); - expect(list.length).toBe(0); - expect(list.isEmpty).toBe(true); - expect(list.head).toBeUndefined(); - expect(list.tail).toBeUndefined(); - expect(result).toBe(list); - }); + describe('peekFront', () => { + it('should return undefined for empty list', () => { + const list = new LinkedList(); + + expect(list.peekFront()).toBeUndefined(); }); - describe('toArray', () => { - it('should return empty array for empty list', () => { - const list = new LinkedList(); + it('should return first value without removing', () => { + const list = new LinkedList([1, 2, 3]); - expect(list.toArray()).toEqual([]); - }); + expect(list.peekFront()).toBe(1); + expect(list.length).toBe(3); + }); + }); - it('should return values from head to tail', () => { - const list = new LinkedList([1, 2, 3]); + describe('insertBefore', () => { + it('should insert before head', () => { + const list = new LinkedList(); + const node = list.pushBack(2); - expect(list.toArray()).toEqual([1, 2, 3]); - }); + list.insertBefore(node, 1); + + expect(list.peekFront()).toBe(1); + expect(list.peekBack()).toBe(2); + expect(list.length).toBe(2); }); - describe('toString', () => { - it('should return comma-separated values', () => { - const list = new LinkedList([1, 2, 3]); + it('should insert before middle node', () => { + const list = new LinkedList([1, 3]); + const tail = list.tail!; - expect(list.toString()).toBe('1,2,3'); - }); + list.insertBefore(tail, 2); + + expect(list.toArray()).toEqual([1, 2, 3]); }); - describe('iterator', () => { - it('should iterate from head to tail', () => { - const list = new LinkedList([1, 2, 3]); + it('should return the created node', () => { + const list = new LinkedList(); + const existing = list.pushBack(2); - expect([...list]).toEqual([1, 2, 3]); - }); + const newNode = list.insertBefore(existing, 1); - it('should yield nothing for empty list', () => { - const list = new LinkedList(); + expect(newNode.value).toBe(1); + expect(newNode.next).toBe(existing); + }); + }); - expect([...list]).toEqual([]); - }); + describe('insertAfter', () => { + it('should insert after tail', () => { + const list = new LinkedList(); + const node = list.pushBack(1); + + list.insertAfter(node, 2); + + expect(list.peekFront()).toBe(1); + expect(list.peekBack()).toBe(2); + expect(list.length).toBe(2); }); - describe('async iterator', () => { - it('should async iterate from head to tail', async () => { - const list = new LinkedList([1, 2, 3]); - const result: number[] = []; + it('should insert after middle node', () => { + const list = new LinkedList([1, 3]); + const head = list.head!; - for await (const value of list) - result.push(value); + list.insertAfter(head, 2); - expect(result).toEqual([1, 2, 3]); - }); + expect(list.toArray()).toEqual([1, 2, 3]); }); - describe('node linking', () => { - it('should maintain correct prev/next references', () => { - const list = new LinkedList(); - const a = list.pushBack(1); - const b = list.pushBack(2); - const c = list.pushBack(3); + it('should return the created node', () => { + const list = new LinkedList(); + const existing = list.pushBack(1); - expect(a.next).toBe(b); - expect(b.prev).toBe(a); - expect(b.next).toBe(c); - expect(c.prev).toBe(b); - expect(a.prev).toBeUndefined(); - expect(c.next).toBeUndefined(); - }); + const newNode = list.insertAfter(existing, 2); - it('should update links after removal', () => { - const list = new LinkedList(); - const a = list.pushBack(1); - const b = list.pushBack(2); - const c = list.pushBack(3); + expect(newNode.value).toBe(2); + expect(newNode.prev).toBe(existing); + }); + }); - list.remove(b); + describe('remove', () => { + it('should remove head node', () => { + const list = new LinkedList([1, 2, 3]); + const head = list.head!; - expect(a.next).toBe(c); - expect(c.prev).toBe(a); - }); + const value = list.remove(head); + + expect(value).toBe(1); + expect(list.length).toBe(2); + expect(list.peekFront()).toBe(2); }); - describe('interleaved operations', () => { - it('should handle mixed push/pop from both ends', () => { - const list = new LinkedList(); + it('should remove tail node', () => { + const list = new LinkedList([1, 2, 3]); + const tail = list.tail!; - list.pushBack(1); - list.pushBack(2); - list.pushFront(0); + const value = list.remove(tail); - expect(list.popFront()).toBe(0); - expect(list.popBack()).toBe(2); - expect(list.popFront()).toBe(1); - expect(list.isEmpty).toBe(true); - }); - - it('should handle insert and remove by node reference', () => { - const list = new LinkedList(); - const a = list.pushBack(1); - const c = list.pushBack(3); - const b = list.insertAfter(a, 2); - const d = list.insertBefore(c, 2.5); - - expect(list.toArray()).toEqual([1, 2, 2.5, 3]); - - list.remove(b); - list.remove(d); - - expect(list.toArray()).toEqual([1, 3]); - }); + expect(value).toBe(3); + expect(list.length).toBe(2); + expect(list.peekBack()).toBe(2); }); + + it('should remove middle node', () => { + const list = new LinkedList([1, 2, 3]); + const middle = list.head!.next!; + + const value = list.remove(middle); + + expect(value).toBe(2); + expect(list.toArray()).toEqual([1, 3]); + }); + + it('should remove single element', () => { + const list = new LinkedList(); + const node = list.pushBack(1); + + list.remove(node); + + expect(list.isEmpty).toBe(true); + expect(list.head).toBeUndefined(); + expect(list.tail).toBeUndefined(); + }); + + it('should detach the removed node', () => { + const list = new LinkedList([1, 2, 3]); + const middle = list.head!.next!; + + list.remove(middle); + + expect(middle.prev).toBeUndefined(); + expect(middle.next).toBeUndefined(); + }); + }); + + describe('clear', () => { + it('should remove all elements', () => { + const list = new LinkedList([1, 2, 3]); + + const result = list.clear(); + + expect(list.length).toBe(0); + expect(list.isEmpty).toBe(true); + expect(list.head).toBeUndefined(); + expect(list.tail).toBeUndefined(); + expect(result).toBe(list); + }); + }); + + describe('toArray', () => { + it('should return empty array for empty list', () => { + const list = new LinkedList(); + + expect(list.toArray()).toEqual([]); + }); + + it('should return values from head to tail', () => { + const list = new LinkedList([1, 2, 3]); + + expect(list.toArray()).toEqual([1, 2, 3]); + }); + }); + + describe('toString', () => { + it('should return comma-separated values', () => { + const list = new LinkedList([1, 2, 3]); + + expect(list.toString()).toBe('1,2,3'); + }); + }); + + describe('iterator', () => { + it('should iterate from head to tail', () => { + const list = new LinkedList([1, 2, 3]); + + expect([...list]).toEqual([1, 2, 3]); + }); + + it('should yield nothing for empty list', () => { + const list = new LinkedList(); + + expect([...list]).toEqual([]); + }); + }); + + describe('async iterator', () => { + it('should async iterate from head to tail', async () => { + const list = new LinkedList([1, 2, 3]); + const result: number[] = []; + + for await (const value of list) + result.push(value); + + expect(result).toEqual([1, 2, 3]); + }); + }); + + describe('node linking', () => { + it('should maintain correct prev/next references', () => { + const list = new LinkedList(); + const a = list.pushBack(1); + const b = list.pushBack(2); + const c = list.pushBack(3); + + expect(a.next).toBe(b); + expect(b.prev).toBe(a); + expect(b.next).toBe(c); + expect(c.prev).toBe(b); + expect(a.prev).toBeUndefined(); + expect(c.next).toBeUndefined(); + }); + + it('should update links after removal', () => { + const list = new LinkedList(); + const a = list.pushBack(1); + const b = list.pushBack(2); + const c = list.pushBack(3); + + list.remove(b); + + expect(a.next).toBe(c); + expect(c.prev).toBe(a); + }); + }); + + describe('interleaved operations', () => { + it('should handle mixed push/pop from both ends', () => { + const list = new LinkedList(); + + list.pushBack(1); + list.pushBack(2); + list.pushFront(0); + + expect(list.popFront()).toBe(0); + expect(list.popBack()).toBe(2); + expect(list.popFront()).toBe(1); + expect(list.isEmpty).toBe(true); + }); + + it('should handle insert and remove by node reference', () => { + const list = new LinkedList(); + const a = list.pushBack(1); + const c = list.pushBack(3); + const b = list.insertAfter(a, 2); + const d = list.insertBefore(c, 2.5); + + expect(list.toArray()).toEqual([1, 2, 2.5, 3]); + + list.remove(b); + list.remove(d); + + expect(list.toArray()).toEqual([1, 3]); + }); + }); }); diff --git a/core/stdlib/src/structs/LinkedList/index.ts b/core/stdlib/src/structs/LinkedList/index.ts index 38d60fa..c9666aa 100644 --- a/core/stdlib/src/structs/LinkedList/index.ts +++ b/core/stdlib/src/structs/LinkedList/index.ts @@ -11,7 +11,7 @@ export type { LinkedListLike, LinkedListNode } from './types'; * @returns {LinkedListNode} The created node */ function createNode(value: T): LinkedListNode { - return { value, prev: undefined, next: undefined }; + return { value, prev: undefined, next: undefined }; } /** @@ -24,301 +24,307 @@ function createNode(value: T): LinkedListNode { * @template T The type of elements stored in the list */ export class LinkedList implements LinkedListLike { - /** + /** * The number of elements in the list * * @private * @type {number} */ - private count = 0; + private count = 0; - /** + /** * The first node in the list * * @private * @type {LinkedListNode | undefined} */ - private first: LinkedListNode | undefined; + private first: LinkedListNode | undefined; - /** + /** * The last node in the list * * @private * @type {LinkedListNode | undefined} */ - private last: LinkedListNode | undefined; + private last: LinkedListNode | undefined; - /** + /** * Creates an instance of LinkedList * * @param {(T[] | T)} [initialValues] The initial values to add to the list */ - constructor(initialValues?: T[] | T) { - if (initialValues !== null && initialValues !== undefined) { - const items = isArray(initialValues) ? initialValues : [initialValues]; + constructor(initialValues?: T[] | T) { + if (initialValues !== null && initialValues !== undefined) { + const items = isArray(initialValues) ? initialValues : [initialValues]; - for (const item of items) - this.pushBack(item); - } + for (const item of items) + this.pushBack(item); } + } - /** + /** * Gets the number of elements in the list * @returns {number} The number of elements in the list */ - public get length(): number { - return this.count; - } + public get length(): number { + return this.count; + } - /** + /** * Checks if the list is empty * @returns {boolean} `true` if the list is empty, `false` otherwise */ - public get isEmpty(): boolean { - return this.count === 0; - } + public get isEmpty(): boolean { + return this.count === 0; + } - /** + /** * Gets the first node * @returns {LinkedListNode | undefined} The first node, or `undefined` if the list is empty */ - public get head(): LinkedListNode | undefined { - return this.first; - } + public get head(): LinkedListNode | undefined { + return this.first; + } - /** + /** * Gets the last node * @returns {LinkedListNode | undefined} The last node, or `undefined` if the list is empty */ - public get tail(): LinkedListNode | undefined { - return this.last; - } + public get tail(): LinkedListNode | undefined { + return this.last; + } - /** + /** * Appends a value to the end of the list * @param {T} value The value to append * @returns {LinkedListNode} The created node */ - public pushBack(value: T): LinkedListNode { - const node = createNode(value); + public pushBack(value: T): LinkedListNode { + const node = createNode(value); - if (this.last) { - node.prev = this.last; - this.last.next = node; - this.last = node; - } else { - this.first = node; - this.last = node; - } - - this.count++; - - return node; + if (this.last) { + node.prev = this.last; + this.last.next = node; + this.last = node; + } + else { + this.first = node; + this.last = node; } - /** + this.count++; + + return node; + } + + /** * Prepends a value to the beginning of the list * @param {T} value The value to prepend * @returns {LinkedListNode} The created node */ - public pushFront(value: T): LinkedListNode { - const node = createNode(value); + public pushFront(value: T): LinkedListNode { + const node = createNode(value); - if (this.first) { - node.next = this.first; - this.first.prev = node; - this.first = node; - } else { - this.first = node; - this.last = node; - } - - this.count++; - - return node; + if (this.first) { + node.next = this.first; + this.first.prev = node; + this.first = node; + } + else { + this.first = node; + this.last = node; } - /** + this.count++; + + return node; + } + + /** * Removes and returns the last value * @returns {T | undefined} The last value, or `undefined` if the list is empty */ - public popBack(): T | undefined { - if (!this.last) return undefined; + public popBack(): T | undefined { + if (!this.last) return undefined; - const node = this.last; + const node = this.last; - this.detach(node); + this.detach(node); - return node.value; - } + return node.value; + } - /** + /** * Removes and returns the first value * @returns {T | undefined} The first value, or `undefined` if the list is empty */ - public popFront(): T | undefined { - if (!this.first) return undefined; + public popFront(): T | undefined { + if (!this.first) return undefined; - const node = this.first; + const node = this.first; - this.detach(node); + this.detach(node); - return node.value; - } + return node.value; + } - /** + /** * Returns the last value without removing it * @returns {T | undefined} The last value, or `undefined` if the list is empty */ - public peekBack(): T | undefined { - return this.last?.value; - } + public peekBack(): T | undefined { + return this.last?.value; + } - /** + /** * Returns the first value without removing it * @returns {T | undefined} The first value, or `undefined` if the list is empty */ - public peekFront(): T | undefined { - return this.first?.value; - } + public peekFront(): T | undefined { + return this.first?.value; + } - /** + /** * Inserts a value before the given node * @param {LinkedListNode} node The reference node * @param {T} value The value to insert * @returns {LinkedListNode} The created node */ - public insertBefore(node: LinkedListNode, value: T): LinkedListNode { - const newNode = createNode(value); + public insertBefore(node: LinkedListNode, value: T): LinkedListNode { + const newNode = createNode(value); - newNode.next = node; - newNode.prev = node.prev; + newNode.next = node; + newNode.prev = node.prev; - if (node.prev) { - node.prev.next = newNode; - } else { - this.first = newNode; - } - - node.prev = newNode; - this.count++; - - return newNode; + if (node.prev) { + node.prev.next = newNode; + } + else { + this.first = newNode; } - /** + node.prev = newNode; + this.count++; + + return newNode; + } + + /** * Inserts a value after the given node * @param {LinkedListNode} node The reference node * @param {T} value The value to insert * @returns {LinkedListNode} The created node */ - public insertAfter(node: LinkedListNode, value: T): LinkedListNode { - const newNode = createNode(value); + public insertAfter(node: LinkedListNode, value: T): LinkedListNode { + const newNode = createNode(value); - newNode.prev = node; - newNode.next = node.next; + newNode.prev = node; + newNode.next = node.next; - if (node.next) { - node.next.prev = newNode; - } else { - this.last = newNode; - } - - node.next = newNode; - this.count++; - - return newNode; + if (node.next) { + node.next.prev = newNode; + } + else { + this.last = newNode; } - /** + node.next = newNode; + this.count++; + + return newNode; + } + + /** * Removes a node from the list by reference in O(1) * @param {LinkedListNode} node The node to remove * @returns {T} The value of the removed node */ - public remove(node: LinkedListNode): T { - this.detach(node); + public remove(node: LinkedListNode): T { + this.detach(node); - return node.value; - } + return node.value; + } - /** + /** * Removes all elements from the list * @returns {this} The list instance for chaining */ - public clear(): this { - this.first = undefined; - this.last = undefined; - this.count = 0; + public clear(): this { + this.first = undefined; + this.last = undefined; + this.count = 0; - return this; - } + return this; + } - /** + /** * Returns a shallow copy of the list values as an array * @returns {T[]} Array of values from head to tail */ - public toArray(): T[] { - const result = Array.from({ length: this.count }); - let current = this.first; - let i = 0; + public toArray(): T[] { + const result = Array.from({ length: this.count }); + let current = this.first; + let i = 0; - while (current) { - result[i++] = current.value; - current = current.next; - } - - return result; + while (current) { + result[i++] = current.value; + current = current.next; } - /** + return result; + } + + /** * Returns a string representation of the list * @returns {string} String representation */ - public toString(): string { - return this.toArray().toString(); - } + public toString(): string { + return this.toArray().toString(); + } - /** + /** * Iterator over list values from head to tail */ - public *[Symbol.iterator](): Iterator { - let current = this.first; + public* [Symbol.iterator](): Iterator { + let current = this.first; - while (current) { - yield current.value; - current = current.next; - } + while (current) { + yield current.value; + current = current.next; } + } - /** + /** * Async iterator over list values from head to tail */ - public async *[Symbol.asyncIterator](): AsyncIterator { - for (const value of this) - yield value; - } + public async* [Symbol.asyncIterator](): AsyncIterator { + for (const value of this) + yield value; + } - /** + /** * Detaches a node from the list, updating head/tail and count * * @private * @param {LinkedListNode} node The node to detach */ - private detach(node: LinkedListNode): void { - if (node.prev) { - node.prev.next = node.next; - } else { - this.first = node.next; - } - - if (node.next) { - node.next.prev = node.prev; - } else { - this.last = node.prev; - } - - node.prev = undefined; - node.next = undefined; - this.count--; + private detach(node: LinkedListNode): void { + if (node.prev) { + node.prev.next = node.next; } + else { + this.first = node.next; + } + + if (node.next) { + node.next.prev = node.prev; + } + else { + this.last = node.prev; + } + + node.prev = undefined; + node.next = undefined; + this.count--; + } } diff --git a/core/stdlib/src/structs/LinkedList/types.ts b/core/stdlib/src/structs/LinkedList/types.ts index 79e3b1b..095aa21 100644 --- a/core/stdlib/src/structs/LinkedList/types.ts +++ b/core/stdlib/src/structs/LinkedList/types.ts @@ -1,28 +1,28 @@ export interface LinkedListNode { - value: T; - prev: LinkedListNode | undefined; - next: LinkedListNode | undefined; + value: T; + prev: LinkedListNode | undefined; + next: LinkedListNode | undefined; } export interface LinkedListLike extends Iterable, AsyncIterable { - readonly length: number; - readonly isEmpty: boolean; + readonly length: number; + readonly isEmpty: boolean; - readonly head: LinkedListNode | undefined; - readonly tail: LinkedListNode | undefined; + readonly head: LinkedListNode | undefined; + readonly tail: LinkedListNode | undefined; - pushBack(value: T): LinkedListNode; - pushFront(value: T): LinkedListNode; - popBack(): T | undefined; - popFront(): T | undefined; - peekBack(): T | undefined; - peekFront(): T | undefined; + pushBack(value: T): LinkedListNode; + pushFront(value: T): LinkedListNode; + popBack(): T | undefined; + popFront(): T | undefined; + peekBack(): T | undefined; + peekFront(): T | undefined; - insertBefore(node: LinkedListNode, value: T): LinkedListNode; - insertAfter(node: LinkedListNode, value: T): LinkedListNode; - remove(node: LinkedListNode): T; + insertBefore(node: LinkedListNode, value: T): LinkedListNode; + insertAfter(node: LinkedListNode, value: T): LinkedListNode; + remove(node: LinkedListNode): T; - clear(): this; - toArray(): T[]; - toString(): string; + clear(): this; + toArray(): T[]; + toString(): string; } diff --git a/core/stdlib/src/structs/PriorityQueue/index.test.ts b/core/stdlib/src/structs/PriorityQueue/index.test.ts index c842799..6b2de46 100644 --- a/core/stdlib/src/structs/PriorityQueue/index.test.ts +++ b/core/stdlib/src/structs/PriorityQueue/index.test.ts @@ -3,211 +3,211 @@ import { describe, expect, it } from 'vitest'; import { PriorityQueue } from '.'; describe('PriorityQueue', () => { - describe('constructor', () => { - it('should create an empty queue', () => { - const pq = new PriorityQueue(); + describe('constructor', () => { + it('should create an empty queue', () => { + const pq = new PriorityQueue(); - expect(pq.length).toBe(0); - expect(pq.isEmpty).toBe(true); - expect(pq.isFull).toBe(false); - }); - - it('should create a queue from single value', () => { - const pq = new PriorityQueue(42); - - expect(pq.length).toBe(1); - expect(pq.peek()).toBe(42); - }); - - it('should create a queue from array', () => { - const pq = new PriorityQueue([5, 3, 8, 1, 4]); - - expect(pq.length).toBe(5); - expect(pq.peek()).toBe(1); - }); - - it('should throw if initial values exceed maxSize', () => { - expect(() => new PriorityQueue([1, 2, 3], { maxSize: 2 })) - .toThrow('Initial values exceed maxSize'); - }); + expect(pq.length).toBe(0); + expect(pq.isEmpty).toBe(true); + expect(pq.isFull).toBe(false); }); - describe('enqueue', () => { - it('should enqueue elements by priority', () => { - const pq = new PriorityQueue(); + it('should create a queue from single value', () => { + const pq = new PriorityQueue(42); - pq.enqueue(5); - pq.enqueue(1); - pq.enqueue(3); - - expect(pq.peek()).toBe(1); - expect(pq.length).toBe(3); - }); - - it('should throw when queue is full', () => { - const pq = new PriorityQueue(undefined, { maxSize: 2 }); - - pq.enqueue(1); - pq.enqueue(2); - - expect(() => pq.enqueue(3)).toThrow('PriorityQueue is full'); - }); + expect(pq.length).toBe(1); + expect(pq.peek()).toBe(42); }); - describe('dequeue', () => { - it('should return undefined for empty queue', () => { - const pq = new PriorityQueue(); + it('should create a queue from array', () => { + const pq = new PriorityQueue([5, 3, 8, 1, 4]); - expect(pq.dequeue()).toBeUndefined(); - }); - - it('should dequeue elements in priority order (min-heap)', () => { - const pq = new PriorityQueue([5, 3, 8, 1, 4]); - const result: number[] = []; - - while (!pq.isEmpty) { - result.push(pq.dequeue()!); - } - - expect(result).toEqual([1, 3, 4, 5, 8]); - }); - - it('should dequeue elements in priority order (max-heap)', () => { - const pq = new PriorityQueue([5, 3, 8, 1, 4], { - comparator: (a, b) => b - a, - }); - const result: number[] = []; - - while (!pq.isEmpty) { - result.push(pq.dequeue()!); - } - - expect(result).toEqual([8, 5, 4, 3, 1]); - }); + expect(pq.length).toBe(5); + expect(pq.peek()).toBe(1); }); - describe('peek', () => { - it('should return undefined for empty queue', () => { - const pq = new PriorityQueue(); + it('should throw if initial values exceed maxSize', () => { + expect(() => new PriorityQueue([1, 2, 3], { maxSize: 2 })) + .toThrow('Initial values exceed maxSize'); + }); + }); - expect(pq.peek()).toBeUndefined(); - }); + describe('enqueue', () => { + it('should enqueue elements by priority', () => { + const pq = new PriorityQueue(); - it('should return highest-priority element without removing', () => { - const pq = new PriorityQueue([5, 1, 3]); + pq.enqueue(5); + pq.enqueue(1); + pq.enqueue(3); - expect(pq.peek()).toBe(1); - expect(pq.length).toBe(3); - }); + expect(pq.peek()).toBe(1); + expect(pq.length).toBe(3); }); - describe('isFull', () => { - it('should be false when no maxSize', () => { - const pq = new PriorityQueue([1, 2, 3]); + it('should throw when queue is full', () => { + const pq = new PriorityQueue(undefined, { maxSize: 2 }); - expect(pq.isFull).toBe(false); - }); + pq.enqueue(1); + pq.enqueue(2); - it('should be true when at maxSize', () => { - const pq = new PriorityQueue([1, 2], { maxSize: 2 }); + expect(() => pq.enqueue(3)).toThrow('PriorityQueue is full'); + }); + }); - expect(pq.isFull).toBe(true); - }); + describe('dequeue', () => { + it('should return undefined for empty queue', () => { + const pq = new PriorityQueue(); - it('should become false after dequeue', () => { - const pq = new PriorityQueue([1, 2], { maxSize: 2 }); - - pq.dequeue(); - - expect(pq.isFull).toBe(false); - }); + expect(pq.dequeue()).toBeUndefined(); }); - describe('clear', () => { - it('should remove all elements', () => { - const pq = new PriorityQueue([1, 2, 3]); + it('should dequeue elements in priority order (min-heap)', () => { + const pq = new PriorityQueue([5, 3, 8, 1, 4]); + const result: number[] = []; - const result = pq.clear(); + while (!pq.isEmpty) { + result.push(pq.dequeue()!); + } - expect(pq.length).toBe(0); - expect(pq.isEmpty).toBe(true); - expect(result).toBe(pq); - }); + expect(result).toEqual([1, 3, 4, 5, 8]); }); - describe('toArray', () => { - it('should return empty array for empty queue', () => { - const pq = new PriorityQueue(); + it('should dequeue elements in priority order (max-heap)', () => { + const pq = new PriorityQueue([5, 3, 8, 1, 4], { + comparator: (a, b) => b - a, + }); + const result: number[] = []; - expect(pq.toArray()).toEqual([]); - }); + while (!pq.isEmpty) { + result.push(pq.dequeue()!); + } - it('should return a shallow copy', () => { - const pq = new PriorityQueue([3, 1, 2]); - const arr = pq.toArray(); + expect(result).toEqual([8, 5, 4, 3, 1]); + }); + }); - arr.push(99); + describe('peek', () => { + it('should return undefined for empty queue', () => { + const pq = new PriorityQueue(); - expect(pq.length).toBe(3); - }); + expect(pq.peek()).toBeUndefined(); }); - describe('toString', () => { - it('should return formatted string', () => { - const pq = new PriorityQueue([1, 2, 3]); + it('should return highest-priority element without removing', () => { + const pq = new PriorityQueue([5, 1, 3]); - expect(pq.toString()).toBe('PriorityQueue(3)'); - }); + expect(pq.peek()).toBe(1); + expect(pq.length).toBe(3); + }); + }); + + describe('isFull', () => { + it('should be false when no maxSize', () => { + const pq = new PriorityQueue([1, 2, 3]); + + expect(pq.isFull).toBe(false); }); - describe('iterator', () => { - it('should iterate over elements', () => { - const pq = new PriorityQueue([5, 3, 1]); - const elements = [...pq]; + it('should be true when at maxSize', () => { + const pq = new PriorityQueue([1, 2], { maxSize: 2 }); - expect(elements.length).toBe(3); - }); + expect(pq.isFull).toBe(true); }); - describe('custom comparator', () => { - it('should work with object priority', () => { - interface Job { - priority: number; - name: string; - } + it('should become false after dequeue', () => { + const pq = new PriorityQueue([1, 2], { maxSize: 2 }); - const pq = new PriorityQueue( - [ - { priority: 3, name: 'low' }, - { priority: 1, name: 'critical' }, - { priority: 2, name: 'normal' }, - ], - { comparator: (a, b) => a.priority - b.priority }, - ); + pq.dequeue(); - expect(pq.dequeue()?.name).toBe('critical'); - expect(pq.dequeue()?.name).toBe('normal'); - expect(pq.dequeue()?.name).toBe('low'); - }); + expect(pq.isFull).toBe(false); + }); + }); + + describe('clear', () => { + it('should remove all elements', () => { + const pq = new PriorityQueue([1, 2, 3]); + + const result = pq.clear(); + + expect(pq.length).toBe(0); + expect(pq.isEmpty).toBe(true); + expect(result).toBe(pq); + }); + }); + + describe('toArray', () => { + it('should return empty array for empty queue', () => { + const pq = new PriorityQueue(); + + expect(pq.toArray()).toEqual([]); }); - describe('interleaved operations', () => { - it('should maintain priority with mixed enqueue and dequeue', () => { - const pq = new PriorityQueue(); + it('should return a shallow copy', () => { + const pq = new PriorityQueue([3, 1, 2]); + const arr = pq.toArray(); - pq.enqueue(10); - pq.enqueue(5); - expect(pq.dequeue()).toBe(5); + arr.push(99); - pq.enqueue(3); - pq.enqueue(7); - expect(pq.dequeue()).toBe(3); - - pq.enqueue(1); - expect(pq.dequeue()).toBe(1); - expect(pq.dequeue()).toBe(7); - expect(pq.dequeue()).toBe(10); - expect(pq.dequeue()).toBeUndefined(); - }); + expect(pq.length).toBe(3); }); + }); + + describe('toString', () => { + it('should return formatted string', () => { + const pq = new PriorityQueue([1, 2, 3]); + + expect(pq.toString()).toBe('PriorityQueue(3)'); + }); + }); + + describe('iterator', () => { + it('should iterate over elements', () => { + const pq = new PriorityQueue([5, 3, 1]); + const elements = [...pq]; + + expect(elements.length).toBe(3); + }); + }); + + describe('custom comparator', () => { + it('should work with object priority', () => { + interface Job { + priority: number; + name: string; + } + + const pq = new PriorityQueue( + [ + { priority: 3, name: 'low' }, + { priority: 1, name: 'critical' }, + { priority: 2, name: 'normal' }, + ], + { comparator: (a, b) => a.priority - b.priority }, + ); + + expect(pq.dequeue()?.name).toBe('critical'); + expect(pq.dequeue()?.name).toBe('normal'); + expect(pq.dequeue()?.name).toBe('low'); + }); + }); + + describe('interleaved operations', () => { + it('should maintain priority with mixed enqueue and dequeue', () => { + const pq = new PriorityQueue(); + + pq.enqueue(10); + pq.enqueue(5); + expect(pq.dequeue()).toBe(5); + + pq.enqueue(3); + pq.enqueue(7); + expect(pq.dequeue()).toBe(3); + + pq.enqueue(1); + expect(pq.dequeue()).toBe(1); + expect(pq.dequeue()).toBe(7); + expect(pq.dequeue()).toBe(10); + expect(pq.dequeue()).toBeUndefined(); + }); + }); }); diff --git a/core/stdlib/src/structs/PriorityQueue/index.ts b/core/stdlib/src/structs/PriorityQueue/index.ts index e1cee9f..242f1c4 100644 --- a/core/stdlib/src/structs/PriorityQueue/index.ts +++ b/core/stdlib/src/structs/PriorityQueue/index.ts @@ -5,8 +5,8 @@ export type { PriorityQueueLike } from './types'; export type { Comparator } from './types'; export interface PriorityQueueOptions { - comparator?: Comparator; - maxSize?: number; + comparator?: Comparator; + maxSize?: number; } /** @@ -19,126 +19,126 @@ export interface PriorityQueueOptions { * @template T The type of elements stored in the queue */ export class PriorityQueue implements PriorityQueueLike { - /** + /** * The maximum number of elements the queue can hold * * @private * @type {number} */ - private readonly maxSize: number; + private readonly maxSize: number; - /** + /** * Internal binary heap backing the queue * * @private * @type {BinaryHeap} */ - private readonly heap: BinaryHeap; + private readonly heap: BinaryHeap; - /** + /** * Creates an instance of PriorityQueue * * @param {(T[] | T)} [initialValues] The initial values to add to the queue * @param {PriorityQueueOptions} [options] Queue configuration */ - constructor(initialValues?: T[] | T, options?: PriorityQueueOptions) { - this.maxSize = options?.maxSize ?? Infinity; - this.heap = new BinaryHeap(initialValues, { comparator: options?.comparator }); + constructor(initialValues?: T[] | T, options?: PriorityQueueOptions) { + this.maxSize = options?.maxSize ?? Infinity; + this.heap = new BinaryHeap(initialValues, { comparator: options?.comparator }); - if (this.heap.length > this.maxSize) { - throw new RangeError('Initial values exceed maxSize'); - } + if (this.heap.length > this.maxSize) { + throw new RangeError('Initial values exceed maxSize'); } + } - /** + /** * Gets the number of elements in the queue * @returns {number} The number of elements in the queue */ - public get length(): number { - return this.heap.length; - } + public get length(): number { + return this.heap.length; + } - /** + /** * Checks if the queue is empty * @returns {boolean} `true` if the queue is empty, `false` otherwise */ - public get isEmpty(): boolean { - return this.heap.isEmpty; - } + public get isEmpty(): boolean { + return this.heap.isEmpty; + } - /** + /** * Checks if the queue is full * @returns {boolean} `true` if the queue has reached maxSize, `false` otherwise */ - public get isFull(): boolean { - return this.heap.length >= this.maxSize; - } + public get isFull(): boolean { + return this.heap.length >= this.maxSize; + } - /** + /** * Enqueues an element by priority * @param {T} element The element to enqueue * @throws {RangeError} If the queue is full */ - public enqueue(element: T): void { - if (this.isFull) - throw new RangeError('PriorityQueue is full'); + public enqueue(element: T): void { + if (this.isFull) + throw new RangeError('PriorityQueue is full'); - this.heap.push(element); - } + this.heap.push(element); + } - /** + /** * Dequeues the highest-priority element * @returns {T | undefined} The highest-priority element, or `undefined` if empty */ - public dequeue(): T | undefined { - return this.heap.pop(); - } + public dequeue(): T | undefined { + return this.heap.pop(); + } - /** + /** * Returns the highest-priority element without removing it * @returns {T | undefined} The highest-priority element, or `undefined` if empty */ - public peek(): T | undefined { - return this.heap.peek(); - } + public peek(): T | undefined { + return this.heap.peek(); + } - /** + /** * Removes all elements from the queue * @returns {this} The queue instance for chaining */ - public clear(): this { - this.heap.clear(); - return this; - } + public clear(): this { + this.heap.clear(); + return this; + } - /** + /** * Returns a shallow copy of elements in heap order * @returns {T[]} Array of elements */ - public toArray(): T[] { - return this.heap.toArray(); - } + public toArray(): T[] { + return this.heap.toArray(); + } - /** + /** * Returns a string representation of the queue * @returns {string} String representation */ - public toString(): string { - return `PriorityQueue(${this.heap.length})`; - } + public toString(): string { + return `PriorityQueue(${this.heap.length})`; + } - /** + /** * Iterator over queue elements in heap order */ - public *[Symbol.iterator](): Iterator { - yield* this.heap; - } + public* [Symbol.iterator](): Iterator { + yield* this.heap; + } - /** + /** * Async iterator over queue elements in heap order */ - public async *[Symbol.asyncIterator](): AsyncIterator { - for (const element of this.heap) - yield element; - } + public async* [Symbol.asyncIterator](): AsyncIterator { + for (const element of this.heap) + yield element; + } } diff --git a/core/stdlib/src/structs/PriorityQueue/types.ts b/core/stdlib/src/structs/PriorityQueue/types.ts index 46144b7..d282dfc 100644 --- a/core/stdlib/src/structs/PriorityQueue/types.ts +++ b/core/stdlib/src/structs/PriorityQueue/types.ts @@ -1,16 +1,16 @@ import type { Comparator } from '../BinaryHeap'; export interface PriorityQueueLike extends Iterable, AsyncIterable { - readonly length: number; - readonly isEmpty: boolean; - readonly isFull: boolean; + readonly length: number; + readonly isEmpty: boolean; + readonly isFull: boolean; - enqueue(element: T): void; - dequeue(): T | undefined; - peek(): T | undefined; - clear(): this; - toArray(): T[]; - toString(): string; + enqueue(element: T): void; + dequeue(): T | undefined; + peek(): T | undefined; + clear(): this; + toArray(): T[]; + toString(): string; } export type { Comparator }; diff --git a/core/stdlib/src/structs/Queue/index.ts b/core/stdlib/src/structs/Queue/index.ts index 50f6cf7..0455665 100644 --- a/core/stdlib/src/structs/Queue/index.ts +++ b/core/stdlib/src/structs/Queue/index.ts @@ -133,7 +133,7 @@ export class Queue implements QueueLike { * * @returns {AsyncIterableIterator} */ - async *[Symbol.asyncIterator]() { + async* [Symbol.asyncIterator]() { for (const element of this.deque) yield element; } diff --git a/core/stdlib/src/structs/Stack/index.test.ts b/core/stdlib/src/structs/Stack/index.test.ts index 7d3920a..895a9b4 100644 --- a/core/stdlib/src/structs/Stack/index.test.ts +++ b/core/stdlib/src/structs/Stack/index.test.ts @@ -112,8 +112,8 @@ describe('stack', () => { for await (const element of stack) { elements.push(element); } - + expect(elements).toEqual([3, 2, 1]); }); }); -}); \ No newline at end of file +}); diff --git a/core/stdlib/src/structs/Stack/index.ts b/core/stdlib/src/structs/Stack/index.ts index 05fee93..6ef3e8c 100644 --- a/core/stdlib/src/structs/Stack/index.ts +++ b/core/stdlib/src/structs/Stack/index.ts @@ -5,7 +5,7 @@ import type { StackLike } from './types'; export type { StackLike } from './types'; export interface StackOptions { - maxSize?: number; + maxSize?: number; } /** @@ -18,138 +18,138 @@ export interface StackOptions { * @template T The type of elements stored in the stack */ export class Stack implements StackLike { - /** + /** * The maximum number of elements that the stack can hold - * + * * @private * @type {number} */ - private readonly maxSize: number; + private readonly maxSize: number; - /** + /** * The stack data structure - * + * * @private * @type {T[]} */ - private readonly stack: T[]; + private readonly stack: T[]; - /** + /** * Creates an instance of Stack - * + * * @param {(T[] | T)} [initialValues] The initial values to add to the stack * @param {StackOptions} [options] The options for the stack * @memberof Stack */ - constructor(initialValues?: T[] | T, options?: StackOptions) { - this.maxSize = options?.maxSize ?? Infinity; - this.stack = isArray(initialValues) ? initialValues : initialValues ? [initialValues] : []; - } - - /** + constructor(initialValues?: T[] | T, options?: StackOptions) { + this.maxSize = options?.maxSize ?? Infinity; + this.stack = isArray(initialValues) ? initialValues : initialValues ? [initialValues] : []; + } + + /** * Gets the number of elements in the stack * @returns {number} The number of elements in the stack */ - public get length() { - return this.stack.length; - } + public get length() { + return this.stack.length; + } - /** + /** * Checks if the stack is empty * @returns {boolean} `true` if the stack is empty, `false` otherwise */ - public get isEmpty() { - return this.stack.length === 0; - } + public get isEmpty() { + return this.stack.length === 0; + } - /** + /** * Checks if the stack is full * @returns {boolean} `true` if the stack is full, `false` otherwise */ - public get isFull() { - return this.stack.length === this.maxSize; - } + public get isFull() { + return this.stack.length === this.maxSize; + } - /** + /** * Pushes an element onto the stack * @param {T} element The element to push onto the stack * @returns {this} * @throws {RangeError} If the stack is full */ - public push(element: T) { - if (this.isFull) - throw new RangeError('Stack is full'); - - this.stack.push(element); + public push(element: T) { + if (this.isFull) + throw new RangeError('Stack is full'); - return this; - } + this.stack.push(element); - /** + return this; + } + + /** * Pops an element from the stack * @returns {T | undefined} The element popped from the stack */ - public pop() { - return this.stack.pop(); - } + public pop() { + return this.stack.pop(); + } - /** + /** * Peeks at the top element of the stack * @returns {T | undefined} The top element of the stack */ - public peek() { - if (this.isEmpty) - return undefined; - - return last(this.stack); - } + public peek() { + if (this.isEmpty) + return undefined; - /** + return last(this.stack); + } + + /** * Clears the stack - * + * * @returns {this} */ - public clear() { - this.stack.length = 0; + public clear() { + this.stack.length = 0; - return this; - } + return this; + } - /** + /** * Converts the stack to an array - * + * * @returns {T[]} */ - public toArray() { - return this.stack.toReversed(); - } + public toArray() { + return this.stack.toReversed(); + } - /** + /** * Returns a string representation of the stack - * + * * @returns {string} */ - public toString() { - return this.toArray().toString(); - } + public toString() { + return this.toArray().toString(); + } - /** + /** * Returns an iterator for the stack - * + * * @returns {IterableIterator} */ - public [Symbol.iterator]() { - return this.toArray()[Symbol.iterator](); - } + public [Symbol.iterator]() { + return this.toArray()[Symbol.iterator](); + } - /** + /** * Returns an async iterator for the stack - * + * * @returns {AsyncIterableIterator} */ - public async *[Symbol.asyncIterator]() { - for (const element of this.toArray()) { - yield element; - } + public async* [Symbol.asyncIterator]() { + for (const element of this.toArray()) { + yield element; } + } } diff --git a/core/stdlib/src/structs/index.ts b/core/stdlib/src/structs/index.ts index 1f304c9..7110086 100644 --- a/core/stdlib/src/structs/index.ts +++ b/core/stdlib/src/structs/index.ts @@ -4,4 +4,4 @@ export * from './Deque'; export * from './LinkedList'; export * from './PriorityQueue'; export * from './Queue'; -export * from './Stack'; \ No newline at end of file +export * from './Stack'; diff --git a/core/stdlib/src/sync/mutex/index.test.ts b/core/stdlib/src/sync/mutex/index.test.ts index 3b97fc0..09b2ed3 100644 --- a/core/stdlib/src/sync/mutex/index.test.ts +++ b/core/stdlib/src/sync/mutex/index.test.ts @@ -14,21 +14,21 @@ describe('SyncMutex', () => { it('lock the mutex', () => { mutex.lock(); - + expect(mutex.isLocked).toBe(true); }); it('remain locked when locked multiple times', () => { mutex.lock(); mutex.lock(); - + expect(mutex.isLocked).toBe(true); }); it('unlock a locked mutex', () => { mutex.lock(); mutex.unlock(); - + expect(mutex.isLocked).toBe(false); }); @@ -50,7 +50,7 @@ describe('SyncMutex', () => { it('execute a callback when unlocked', async () => { const callback = vi.fn(() => 'done'); const result = await mutex.execute(callback); - + expect(result).toBe('done'); expect(callback).toHaveBeenCalled(); }); @@ -58,7 +58,7 @@ describe('SyncMutex', () => { it('execute a promise callback when unlocked', async () => { const callback = vi.fn(() => Promise.resolve('done')); const result = await mutex.execute(callback); - + expect(result).toBe('done'); expect(callback).toHaveBeenCalled(); }); @@ -71,7 +71,7 @@ describe('SyncMutex', () => { mutex.execute(callback), mutex.execute(callback), ]); - + expect(result).toEqual(['done', undefined, undefined]); expect(callback).toHaveBeenCalledTimes(1); }); @@ -88,7 +88,7 @@ describe('SyncMutex', () => { it('unlocks after executing a callback', async () => { const callback = vi.fn(() => 'done'); await mutex.execute(callback); - + expect(mutex.isLocked).toBe(false); }); }); diff --git a/core/stdlib/src/sync/mutex/index.ts b/core/stdlib/src/sync/mutex/index.ts index b2ddaa2..1485125 100644 --- a/core/stdlib/src/sync/mutex/index.ts +++ b/core/stdlib/src/sync/mutex/index.ts @@ -2,19 +2,19 @@ * @name SyncMutex * @category Utils * @description A simple synchronous mutex to provide more readable locking and unlocking of code blocks - * + * * @example * const mutex = new SyncMutex(); - * + * * mutex.lock(); * * mutex.unlock(); - * + * * const result = await mutex.execute(() => { * // do something - * return Promise.resolve('done'); + * return Promise.resolve('done'); * }); - * + * * @since 0.0.5 */ export class SyncMutex { diff --git a/core/stdlib/src/text/index.ts b/core/stdlib/src/text/index.ts index 1a0231f..824ca09 100644 --- a/core/stdlib/src/text/index.ts +++ b/core/stdlib/src/text/index.ts @@ -1,4 +1,4 @@ export * from './levenshtein-distance'; export * from './trigram-distance'; // TODO: Template is not implemented yet -// export * from './template'; \ No newline at end of file +// export * from './template'; diff --git a/core/stdlib/src/text/levenshtein-distance/index.test.ts b/core/stdlib/src/text/levenshtein-distance/index.test.ts index a3c0f79..94ac0d5 100644 --- a/core/stdlib/src/text/levenshtein-distance/index.test.ts +++ b/core/stdlib/src/text/levenshtein-distance/index.test.ts @@ -1,32 +1,32 @@ import { describe, expect, it } from 'vitest'; -import {levenshteinDistance} from '.'; - -describe('levenshteinDistance', () => { - it('calculate edit distance between two strings', () => { - // just one substitution I at the beginning - expect(levenshteinDistance('islander', 'slander')).toBe(1); - - // substitution M->K, T->M and add an A to the end - expect(levenshteinDistance('mart', 'karma')).toBe(3); - - // substitution K->S, E->I and insert G at the end - expect(levenshteinDistance('kitten', 'sitting')).toBe(3); - - // should add 4 letters FOOT at the beginning - expect(levenshteinDistance('ball', 'football')).toBe(4); - - // should delete 4 letters FOOT at the beginning - expect(levenshteinDistance('football', 'foot')).toBe(4); - - // needs to substitute the first 5 chars INTEN->EXECU - expect(levenshteinDistance('intention', 'execution')).toBe(5); - }); +import { levenshteinDistance } from '.'; - it('handle empty strings', () => { - expect(levenshteinDistance('', '')).toBe(0); - expect(levenshteinDistance('a', '')).toBe(1); - expect(levenshteinDistance('', 'a')).toBe(1); - expect(levenshteinDistance('abc', '')).toBe(3); - expect(levenshteinDistance('', 'abc')).toBe(3); - }); - }); \ No newline at end of file +describe('levenshteinDistance', () => { + it('calculate edit distance between two strings', () => { + // just one substitution I at the beginning + expect(levenshteinDistance('islander', 'slander')).toBe(1); + + // substitution M->K, T->M and add an A to the end + expect(levenshteinDistance('mart', 'karma')).toBe(3); + + // substitution K->S, E->I and insert G at the end + expect(levenshteinDistance('kitten', 'sitting')).toBe(3); + + // should add 4 letters FOOT at the beginning + expect(levenshteinDistance('ball', 'football')).toBe(4); + + // should delete 4 letters FOOT at the beginning + expect(levenshteinDistance('football', 'foot')).toBe(4); + + // needs to substitute the first 5 chars INTEN->EXECU + expect(levenshteinDistance('intention', 'execution')).toBe(5); + }); + + it('handle empty strings', () => { + expect(levenshteinDistance('', '')).toBe(0); + expect(levenshteinDistance('a', '')).toBe(1); + expect(levenshteinDistance('', 'a')).toBe(1); + expect(levenshteinDistance('abc', '')).toBe(3); + expect(levenshteinDistance('', 'abc')).toBe(3); + }); +}); diff --git a/core/stdlib/src/text/levenshtein-distance/index.ts b/core/stdlib/src/text/levenshtein-distance/index.ts index 8ddd0ec..6aff978 100644 --- a/core/stdlib/src/text/levenshtein-distance/index.ts +++ b/core/stdlib/src/text/levenshtein-distance/index.ts @@ -2,7 +2,7 @@ * @name levenshteinDistance * @category Text * @description Calculate the Levenshtein distance between two strings - * + * * @param {string} left First string * @param {string} right Second string * @returns {number} The Levenshtein distance between the two strings @@ -10,37 +10,37 @@ * @since 0.0.1 */ export function levenshteinDistance(left: string, right: string): number { - if (left === right) return 0; + if (left === right) return 0; - if (left.length === 0) return right.length; - if (right.length === 0) return left.length; + if (left.length === 0) return right.length; + if (right.length === 0) return left.length; - // Create empty edit distance matrix for all possible modifications of - // substrings of left to substrings of right - const distanceMatrix = Array(right.length + 1).fill(null).map(() => Array(left.length + 1).fill(null)); + // Create empty edit distance matrix for all possible modifications of + // substrings of left to substrings of right + const distanceMatrix = Array(right.length + 1).fill(null).map(() => Array(left.length + 1).fill(null)); - // Fill the first row of the matrix - // If this is the first row, we're transforming from an empty string to left - // In this case, the number of operations equals the length of left substring - for (let i = 0; i <= left.length; i++) - distanceMatrix[0]![i]! = i; + // Fill the first row of the matrix + // If this is the first row, we're transforming from an empty string to left + // In this case, the number of operations equals the length of left substring + for (let i = 0; i <= left.length; i++) + distanceMatrix[0]![i]! = i; - // Fill the first column of the matrix - // If this is the first column, we're transforming empty string to right - // In this case, the number of operations equals the length of right substring - for (let j = 0; j <= right.length; j++) - distanceMatrix[j]![0]! = j; + // Fill the first column of the matrix + // If this is the first column, we're transforming empty string to right + // In this case, the number of operations equals the length of right substring + for (let j = 0; j <= right.length; j++) + distanceMatrix[j]![0]! = j; - for (let j = 1; j <= right.length; j++) { - for (let i = 1; i <= left.length; i++) { - const indicator = left[i - 1] === right[j - 1] ? 0 : 1; - distanceMatrix[j]![i]! = Math.min( - distanceMatrix[j]![i - 1]! + 1, // deletion - distanceMatrix[j - 1]![i]! + 1, // insertion - distanceMatrix[j - 1]![i - 1]! + indicator // substitution - ); - } + for (let j = 1; j <= right.length; j++) { + for (let i = 1; i <= left.length; i++) { + const indicator = left[i - 1] === right[j - 1] ? 0 : 1; + distanceMatrix[j]![i]! = Math.min( + distanceMatrix[j]![i - 1]! + 1, // deletion + distanceMatrix[j - 1]![i]! + 1, // insertion + distanceMatrix[j - 1]![i - 1]! + indicator, // substitution + ); } + } - return distanceMatrix[right.length]![left.length]!; + return distanceMatrix[right.length]![left.length]!; } diff --git a/core/stdlib/src/text/template/index.test-d.ts b/core/stdlib/src/text/template/index.test-d.ts index f64bb32..81ad8a9 100644 --- a/core/stdlib/src/text/template/index.test-d.ts +++ b/core/stdlib/src/text/template/index.test-d.ts @@ -102,4 +102,4 @@ describe.skip('template', () => { expectTypeOf().toEqualTypeOf(); }); }); -}); \ No newline at end of file +}); diff --git a/core/stdlib/src/text/template/index.test.ts b/core/stdlib/src/text/template/index.test.ts index eabb1f2..5bb8a53 100644 --- a/core/stdlib/src/text/template/index.test.ts +++ b/core/stdlib/src/text/template/index.test.ts @@ -33,16 +33,16 @@ describe.skip('templateObject', () => { }); it('replace template placeholders with nested values from args', () => { - const result = templateObject('Hello {{user.name}, your address {user.addresses.0.street}', { - user: { - name: 'John Doe', - addresses: [ - { street: '123 Main St', city: 'Springfield'}, - { street: '456 Elm St', city: 'Shelbyville'} - ] - } - }); + const result = templateObject('Hello {{user.name}, your address {user.addresses.0.street}', { + user: { + name: 'John Doe', + addresses: [ + { street: '123 Main St', city: 'Springfield' }, + { street: '456 Elm St', city: 'Shelbyville' }, + ], + }, + }); - expect(result).toBe('Hello {John Doe, your address 123 Main St'); + expect(result).toBe('Hello {John Doe, your address 123 Main St'); }); -}); \ No newline at end of file +}); diff --git a/core/stdlib/src/text/template/index.ts b/core/stdlib/src/text/template/index.ts index a23f447..06f6a55 100644 --- a/core/stdlib/src/text/template/index.ts +++ b/core/stdlib/src/text/template/index.ts @@ -19,14 +19,14 @@ const TEMPLATE_PLACEHOLDER = /\{\s*([^{}]+?)\s*\}/gm; /** * Removes the placeholder syntax from a template string. - * + * * @example * type Base = ClearPlaceholder<'{user.name}'>; // 'user.name' * type Unbalanced = ClearPlaceholder<'{user.name'>; // 'user.name' * type Spaces = ClearPlaceholder<'{ user.name }'>; // 'user.name' */ -export type ClearPlaceholder = - In extends `${string}{${infer Template}` +export type ClearPlaceholder + = In extends `${string}{${infer Template}` ? ClearPlaceholder