├── .gitattributes ├── .prettierignore ├── .gitignore ├── eslint.config.js ├── pnpm-workspace.yaml ├── src ├── index.ts ├── merge-weak-sets.ts ├── merge-weak-maps.ts ├── merge-weak-sets.test.ts └── merge-weak-maps.test.ts ├── vitest.setup.ts ├── vitest.config.ts ├── types └── jest-extended.d.ts ├── notice-apache ├── tsdown.config.ts ├── tsconfig.json ├── license-mit ├── .github └── workflows │ └── ci.yml ├── package.json ├── readme.md └── license-apache /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto eol=lf 2 | -------------------------------------------------------------------------------- /.prettierignore: -------------------------------------------------------------------------------- 1 | pnpm-lock.yaml 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /node_modules/ 2 | /coverage/ 3 | /dist/ 4 | -------------------------------------------------------------------------------- /eslint.config.js: -------------------------------------------------------------------------------- 1 | export { default } from '@tomer/eslint-config' 2 | -------------------------------------------------------------------------------- /pnpm-workspace.yaml: -------------------------------------------------------------------------------- 1 | onlyBuiltDependencies: 2 | - esbuild 3 | - unrs-resolver 4 | 5 | shellEmulator: true 6 | -------------------------------------------------------------------------------- /src/index.ts: -------------------------------------------------------------------------------- 1 | export { mergeWeakSets } from './merge-weak-sets.ts' 2 | export { mergeWeakMaps } from './merge-weak-maps.ts' 3 | -------------------------------------------------------------------------------- /vitest.setup.ts: -------------------------------------------------------------------------------- 1 | import { expect } from 'vitest' 2 | import * as matchers from 'jest-extended' 3 | 4 | expect.extend(matchers) 5 | -------------------------------------------------------------------------------- /vitest.config.ts: -------------------------------------------------------------------------------- 1 | import { defineConfig } from 'vitest/config' 2 | 3 | export default defineConfig({ 4 | test: { 5 | environment: `jsdom`, 6 | setupFiles: [`vitest.setup.ts`], 7 | coverage: { 8 | include: [`src`], 9 | }, 10 | testTimeout: 10_000, 11 | }, 12 | }) 13 | -------------------------------------------------------------------------------- /types/jest-extended.d.ts: -------------------------------------------------------------------------------- 1 | /* eslint-disable typescript/no-empty-object-type */ 2 | /* eslint-disable typescript/consistent-type-definitions */ 3 | import type CustomMatchers from 'jest-extended' 4 | import 'vitest' 5 | 6 | declare module 'vitest' { 7 | interface Assertion extends CustomMatchers {} 8 | interface AsymmetricMatchersContaining extends CustomMatchers {} 9 | interface ExpectStatic extends CustomMatchers {} 10 | } 11 | -------------------------------------------------------------------------------- /notice-apache: -------------------------------------------------------------------------------- 1 | Copyright 2021-2024 Google LLC 2 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. 14 | -------------------------------------------------------------------------------- /tsdown.config.ts: -------------------------------------------------------------------------------- 1 | import { defineConfig } from 'tsdown/config' 2 | import treeShakeable from 'rollup-plugin-tree-shakeable' 3 | import terser from '@rollup/plugin-terser' 4 | 5 | export default defineConfig([ 6 | { 7 | entry: `src/index.ts`, 8 | platform: `neutral`, 9 | sourcemap: `inline`, 10 | dts: false, 11 | publint: true, 12 | plugins: [ 13 | terser({ 14 | mangle: { 15 | properties: { 16 | regex: `^_[^_]+`, 17 | }, 18 | }, 19 | }), 20 | treeShakeable(), 21 | ], 22 | }, 23 | { 24 | entry: `src/index.ts`, 25 | dts: { emitDtsOnly: true }, 26 | }, 27 | ]) 28 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | // Base options 4 | "esModuleInterop": true, 5 | "skipLibCheck": true, 6 | "target": "esnext", 7 | "allowJs": true, 8 | "resolveJsonModule": true, 9 | "moduleDetection": "force", 10 | "isolatedModules": true, 11 | "verbatimModuleSyntax": true, 12 | "incremental": true, 13 | "tsBuildInfoFile": "node_modules/.cache/tsconfig.tsbuildinfo", 14 | // Strictness 15 | "strict": true, 16 | "noUncheckedIndexedAccess": true, 17 | "noImplicitOverride": true, 18 | "noImplicitReturns": true, 19 | // Using a bundler 20 | "allowImportingTsExtensions": true, 21 | "module": "preserve", 22 | "moduleResolution": "bundler", 23 | "noEmit": true, 24 | // Runtime 25 | "lib": ["esnext"] 26 | }, 27 | "include": ["**/*"] 28 | } 29 | -------------------------------------------------------------------------------- /src/merge-weak-sets.ts: -------------------------------------------------------------------------------- 1 | class MergedWeakSet extends WeakSet { 2 | readonly #deletedValues: WeakSet 3 | readonly #weakSets: WeakSet[] 4 | 5 | public constructor(weakSets: WeakSet[]) { 6 | super() 7 | this.#deletedValues = new WeakSet() 8 | this.#weakSets = weakSets 9 | } 10 | 11 | public override add(value: Value): this { 12 | this.#deletedValues.delete(value) 13 | return super.add(value) 14 | } 15 | 16 | public override delete(value: Value): boolean { 17 | const isDeleting = this.has(value) 18 | 19 | super.delete(value) 20 | this.#deletedValues.add(value) 21 | 22 | return isDeleting 23 | } 24 | 25 | public override has(value: Value): boolean { 26 | return ( 27 | !this.#deletedValues.has(value) && 28 | (super.has(value) || this.#weakSets.some(weakSet => weakSet.has(value))) 29 | ) 30 | } 31 | } 32 | 33 | export const mergeWeakSets = ( 34 | ...weakSets: WeakSet[] 35 | ): WeakSet => new MergedWeakSet(weakSets) 36 | -------------------------------------------------------------------------------- /license-mit: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2025 Tomer Aberbach 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and 6 | associated documentation files (the "Software"), to deal in the Software without restriction, including 7 | without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the 9 | following conditions: 10 | 11 | The above copyright notice and this permission notice shall be included in all copies or substantial 12 | portions of the Software. 13 | 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT 15 | LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO 16 | EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 17 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 18 | USE OR OTHER DEALINGS IN THE SOFTWARE. -------------------------------------------------------------------------------- /.github/workflows/ci.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | 3 | on: 4 | push: 5 | branches: [main] 6 | pull_request: 7 | branches: [main] 8 | 9 | concurrency: 10 | group: ${{ github.workflow }}-${{ github.ref }} 11 | cancel-in-progress: true 12 | 13 | jobs: 14 | test: 15 | name: Node.js v${{ matrix.nodejs }} (${{ matrix.os }}) 16 | timeout-minutes: 5 17 | runs-on: ${{ matrix.os }} 18 | strategy: 19 | matrix: 20 | nodejs: [22, 24] 21 | os: [ubuntu-latest, macOS-latest] 22 | fail-fast: false 23 | steps: 24 | - uses: actions/checkout@v4 25 | 26 | - name: Install Node.js 27 | uses: actions/setup-node@v4 28 | with: 29 | node-version: ${{ matrix.nodejs }} 30 | 31 | - name: Install pnpm 32 | run: npm install -g pnpm 33 | 34 | - name: Install dependencies 35 | run: pnpm install 36 | 37 | - name: Build 38 | run: pnpm build 39 | 40 | - name: Lint 41 | run: pnpm lint --no-fix 42 | 43 | - name: Format 44 | run: pnpm format --no-write --check 45 | 46 | - name: Typecheck 47 | run: pnpm typecheck 48 | 49 | - name: Test 50 | run: pnpm coverage 51 | -------------------------------------------------------------------------------- /src/merge-weak-maps.ts: -------------------------------------------------------------------------------- 1 | class MergedWeakMap extends WeakMap { 2 | readonly #deletedKeys: WeakSet 3 | readonly #weakMaps: WeakMap[] 4 | 5 | public constructor(weakMaps: WeakMap[]) { 6 | super() 7 | this.#deletedKeys = new WeakSet() 8 | this.#weakMaps = weakMaps.reverse() 9 | } 10 | 11 | public override delete(key: Key): boolean { 12 | const isDeleting = this.has(key) 13 | 14 | super.delete(key) 15 | this.#deletedKeys.add(key) 16 | 17 | return isDeleting 18 | } 19 | 20 | public override get(key: Key): Value | undefined { 21 | if (this.#deletedKeys.has(key)) { 22 | return undefined 23 | } 24 | 25 | if (super.has(key)) { 26 | return super.get(key) 27 | } 28 | 29 | const index = this.#weakMaps.findIndex(weakMap => weakMap.has(key)) 30 | return index === -1 ? undefined : this.#weakMaps[index]!.get(key) 31 | } 32 | 33 | public override has(key: Key): boolean { 34 | return ( 35 | !this.#deletedKeys.has(key) && 36 | (super.has(key) || this.#weakMaps.some(weakMap => weakMap.has(key))) 37 | ) 38 | } 39 | 40 | public override set(key: Key, value: Value): this { 41 | this.#deletedKeys.delete(key) 42 | return super.set(key, value) 43 | } 44 | } 45 | 46 | export const mergeWeakMaps = ( 47 | ...weakMaps: WeakMap[] 48 | ): WeakMap => new MergedWeakMap(weakMaps) 49 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "weak-merge", 3 | "version": "2.0.0", 4 | "author": { 5 | "name": "Tomer Aberbach", 6 | "email": "tomer@aberba.ch", 7 | "url": "https://tomeraberba.ch" 8 | }, 9 | "description": "A module for merging WeakSets and WeakMaps.", 10 | "keywords": [ 11 | "weakset", 12 | "weak-set", 13 | "weakmap", 14 | "weak-map", 15 | "merge" 16 | ], 17 | "homepage": "https://github.com/TomerAberbach/weak-merge", 18 | "repository": "TomerAberbach/weak-merge", 19 | "bugs": { 20 | "url": "https://github.com/TomerAberbach/weak-merge/issues" 21 | }, 22 | "funding": { 23 | "url": "https://github.com/sponsors/TomerAberbach" 24 | }, 25 | "license": "Apache-2.0 AND MIT", 26 | "files": [ 27 | "dist", 28 | "license-apache", 29 | "license-mit", 30 | "notice-apache" 31 | ], 32 | "type": "module", 33 | "sideEffects": false, 34 | "engines": { 35 | "node": ">= 22" 36 | }, 37 | "exports": { 38 | ".": { 39 | "types": "./dist/index.d.ts", 40 | "default": "./dist/index.js" 41 | }, 42 | "./package.json": "./package.json" 43 | }, 44 | "scripts": { 45 | "format": "prettier --cache --write .", 46 | "lint": "eslint --cache --cache-location node_modules/.cache/eslint/ --fix .", 47 | "typecheck": "tsc --noEmit", 48 | "test": "vitest", 49 | "coverage": "vitest --coverage", 50 | "bench": "vitest bench", 51 | "build": "tsdown", 52 | "prepublishOnly": "pnpm build" 53 | }, 54 | "prettier": "@tomer/prettier-config", 55 | "devDependencies": { 56 | "@fast-check/vitest": "^0.2.2", 57 | "@rollup/plugin-terser": "^0.4.4", 58 | "@tomer/eslint-config": "^4.1.1", 59 | "@tomer/prettier-config": "^4.0.0", 60 | "@vitest/coverage-v8": "^3.2.4", 61 | "eslint": "^9.34.0", 62 | "is-weakmap": "^2.0.2", 63 | "is-weakset": "^2.0.4", 64 | "jest-extended": "^6.0.0", 65 | "jsdom": "^26.1.0", 66 | "prettier": "^3.6.2", 67 | "publint": "^0.3.12", 68 | "rollup-plugin-tree-shakeable": "^2.0.0", 69 | "tsdown": "^0.14.2", 70 | "typescript": "^5.9.2", 71 | "vitest": "^3.2.4" 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /src/merge-weak-sets.test.ts: -------------------------------------------------------------------------------- 1 | import { fc, test } from '@fast-check/vitest' 2 | import isWeakSet from 'is-weakset' 3 | import { expect } from 'vitest' 4 | import { mergeWeakSets } from './index.ts' 5 | 6 | const arraysArb = fc.array(fc.array(fc.object())) 7 | 8 | test.prop([arraysArb])(`mergeWeakSets returns a WeakSet`, arrays => { 9 | const weakSet = mergeWeakSets(...arrays.map(array => new WeakSet(array))) 10 | 11 | expect(isWeakSet(weakSet)).toBeTrue() 12 | }) 13 | 14 | test.prop([arraysArb, fc.array(fc.object(), { minLength: 1 })])( 15 | `mergeWeakSets returns a WeakSet with an add method that returns the same WeakSet`, 16 | (arrays, array) => { 17 | const weakSet = mergeWeakSets(...arrays.map(array => new WeakSet(array))) 18 | 19 | for (const value of array) { 20 | expect(weakSet.add(value)).toBe(weakSet) 21 | } 22 | }, 23 | ) 24 | 25 | test.prop( 26 | [ 27 | arraysArb 28 | .filter(arrays => arrays.some(array => array.length > 0)) 29 | .chain(arrays => { 30 | const valueArb = fc.oneof( 31 | fc.object(), 32 | fc.constantFrom(...arrays.flat()), 33 | ) 34 | 35 | return fc.tuple( 36 | fc.constant(arrays), 37 | fc.commands( 38 | [ 39 | valueArb.map(value => ({ 40 | check: () => true, 41 | run: (model: WeakSet, real: WeakSet) => { 42 | model.add(value) 43 | real.add(value) 44 | }, 45 | toString: () => `add(${fc.stringify(value)})`, 46 | })), 47 | valueArb.map(value => ({ 48 | check: () => true, 49 | run: (model: WeakSet, real: WeakSet) => 50 | expect(real.delete(value)).toBe(model.delete(value)), 51 | toString: () => `delete(${fc.stringify(value)})`, 52 | })), 53 | valueArb.map(value => ({ 54 | check: () => true, 55 | run: (model: WeakSet, real: WeakSet) => 56 | expect(real.has(value)).toBe(model.has(value)), 57 | toString: () => `has(${fc.stringify(value)})`, 58 | })), 59 | ], 60 | { maxCommands: 1000 }, 61 | ), 62 | ) 63 | }), 64 | ], 65 | { numRuns: 500 }, 66 | )( 67 | `mergeWeakSets returns a WeakSet that behaves like a non-merged WeakSet containing the same values as the merged WeakSets`, 68 | ([arrays, commands]) => { 69 | fc.modelRun( 70 | () => ({ 71 | model: new WeakSet(arrays.flat()), 72 | real: mergeWeakSets(...arrays.map(array => new WeakSet(array))), 73 | }), 74 | commands, 75 | ) 76 | }, 77 | ) 78 | 79 | test(`mergeWeakSets concrete example`, () => { 80 | const [a, b, c, d] = [[], {}, new Set(), new Map()] 81 | 82 | const weakSet1 = new WeakSet([a, b]) 83 | const weakSet2 = new WeakSet([c]) 84 | 85 | const mergedWeakSet = mergeWeakSets(weakSet1, weakSet2) 86 | 87 | expect(mergedWeakSet.has(a)).toBeTrue() 88 | expect(mergedWeakSet.has(b)).toBeTrue() 89 | expect(mergedWeakSet.has(c)).toBeTrue() 90 | 91 | mergedWeakSet.delete(a) 92 | 93 | expect(weakSet1.has(a)).toBeTrue() 94 | expect(mergedWeakSet.has(a)).toBeFalse() 95 | 96 | mergedWeakSet.add(d) 97 | 98 | expect(weakSet1.has(d)).toBeFalse() 99 | expect(weakSet2.has(d)).toBeFalse() 100 | expect(mergedWeakSet.has(d)).toBeTrue() 101 | }) 102 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 |

2 | weak-merge 3 |

4 | 5 | 22 | 23 |
24 | A module for merging WeakSets and WeakMaps. 25 |
26 | 27 | ## Install 28 | 29 | ```sh 30 | $ npm i weak-merge 31 | ``` 32 | 33 | ## Usage 34 | 35 | ```js 36 | import { mergeWeakMaps, mergeWeakSets } from 'weak-merge' 37 | 38 | const [a, b, c, d] = [{}, {}, {}, {}] 39 | 40 | const weakSet1 = new WeakSet([a, b]) 41 | const weakSet2 = new WeakSet([c]) 42 | 43 | const mergedWeakSet = mergeWeakSets(weakSet1, weakSet2) 44 | 45 | console.log([a, b, c].map(key => mergedWeakSet.has(key))) 46 | //=> [ true, true, true ] 47 | 48 | mergedWeakSet.delete(a) 49 | console.log(mergedWeakSet.has(a)) 50 | //=> false 51 | 52 | console.log(weakSet1.has(a)) 53 | //=> true 54 | 55 | mergedWeakSet.add(d) 56 | console.log(mergedWeakSet.has(d)) 57 | //=> true 58 | 59 | console.log(weakSet1.has(d)) 60 | //=> false 61 | 62 | const weakMap1 = new WeakMap([ 63 | [a, 1], 64 | [b, 2], 65 | ]) 66 | const weakMap2 = new WeakMap([[c, 3]]) 67 | 68 | const mergedWeakMap = mergeWeakMaps(weakMap1, weakMap2) 69 | 70 | console.log([a, b, c].map(key => mergedWeakMap.get(key))) 71 | //=> [ 1, 2, 3 ] 72 | 73 | mergedWeakMap.delete(a) 74 | console.log(mergedWeakMap.has(a)) 75 | //=> false 76 | 77 | console.log(weakMap1.has(a)) 78 | //=> true 79 | 80 | mergedWeakMap.set(a, 5) 81 | console.log(mergedWeakMap.get(a)) 82 | //=> 5 83 | 84 | console.log(weakMap1.get(a)) 85 | //=> 1 86 | ``` 87 | 88 | See the 89 | [TypeScript types](https://github.com/TomerAberbach/weak-merge/blob/main/src/index.d.ts) 90 | for more documentation. 91 | 92 | ## Why? 93 | 94 | Merging `WeakSet` or `WeakMap` instances is not trivial because they 95 | [are not enumerable](https://javascript.info/weakmap-weakset). 96 | 97 | ## Performance 98 | 99 | `WeakSet` instances returned from `mergeWeakSets` and `WeakMap` instances 100 | returned from `mergeWeakMaps` are not as performant as native `WeakSet` and 101 | `WeakMap` instances (due to the lack of a native way to merge or copy `WeakSet` 102 | and `WeakMap` instances): 103 | 104 | ### `WeakSet` Time Complexity 105 | 106 | | Operation | Native `WeakSet` | Merge of `n` native `WeakSet` instances | 107 | | --------- | ---------------- | --------------------------------------- | 108 | | `add` | `O(1)` | `O(1)` | 109 | | `delete` | `O(1)` | `O(1)` | 110 | | `has` | `O(1)` | `O(n)` | 111 | 112 | ### `WeakMap` Time Complexity 113 | 114 | | Operation | Native `WeakMap` | Merge of `n` native `WeakMap` instances | 115 | | --------- | ---------------- | --------------------------------------- | 116 | | `delete` | `O(1)` | `O(1)` | 117 | | `get` | `O(1)` | `O(n)` | 118 | | `has` | `O(1)` | `O(n)` | 119 | | `set` | `O(1)` | `O(1)` | 120 | 121 | ## Contributing 122 | 123 | Stars are always welcome! 124 | 125 | For bugs and feature requests, 126 | [please create an issue](https://github.com/TomerAberbach/weak-merge/issues/new). 127 | 128 | ## License 129 | 130 | [MIT](https://github.com/TomerAberbach/weak-merge/blob/main/license-mit) © 131 | [Tomer Aberbach](https://github.com/TomerAberbach) \ 132 | [Apache 2.0](https://github.com/TomerAberbach/weak-merge/blob/main/license-apache) © 133 | [Google](https://github.com/TomerAberbach/weak-merge/blob/main/notice-apache) 134 | -------------------------------------------------------------------------------- /src/merge-weak-maps.test.ts: -------------------------------------------------------------------------------- 1 | import { fc, test } from '@fast-check/vitest' 2 | import isWeakMap from 'is-weakmap' 3 | import { expect } from 'vitest' 4 | import { mergeWeakMaps } from './index.ts' 5 | 6 | const entryArb = fc.tuple(fc.object(), fc.anything()) 7 | const arraysOfEntriesArb = fc.array(fc.array(entryArb)) 8 | 9 | test.prop([arraysOfEntriesArb])(`mergeWeakMaps returns a WeakMap`, arrays => { 10 | const weakMap = mergeWeakMaps(...arrays.map(entries => new WeakMap(entries))) 11 | 12 | expect(isWeakMap(weakMap)).toBeTrue() 13 | }) 14 | 15 | test.prop([arraysOfEntriesArb, fc.array(entryArb, { minLength: 1 })])( 16 | `mergeWeakMaps returns a WeakMap with a set method that returns the same WeakMap`, 17 | (arrays, entries) => { 18 | const weakMap = mergeWeakMaps( 19 | ...arrays.map(entries => new WeakMap(entries)), 20 | ) 21 | 22 | for (const [key, value] of entries) { 23 | expect(weakMap.set(key, value)).toBe(weakMap) 24 | } 25 | }, 26 | ) 27 | 28 | test.prop( 29 | [ 30 | arraysOfEntriesArb 31 | .filter(arrays => arrays.some(entries => entries.length > 0)) 32 | .chain(arrays => { 33 | const keyArb = fc.oneof( 34 | fc.object(), 35 | fc.constantFrom( 36 | ...arrays.flatMap(entries => entries.map(([key]) => key)), 37 | ), 38 | ) 39 | 40 | return fc.tuple( 41 | fc.constant(arrays), 42 | fc.commands( 43 | [ 44 | keyArb.map(key => ({ 45 | check: () => true, 46 | run: ( 47 | model: WeakMap, 48 | real: WeakMap, 49 | ) => expect(real.delete(key)).toBe(model.delete(key)), 50 | toString: () => `delete(${fc.stringify(key)})`, 51 | })), 52 | keyArb.map(key => ({ 53 | check: () => true, 54 | run: ( 55 | model: WeakMap, 56 | real: WeakMap, 57 | ) => expect(real.get(key)).toBe(model.get(key)), 58 | toString: () => `get(${fc.stringify(key)})`, 59 | })), 60 | keyArb.map(key => ({ 61 | check: () => true, 62 | run: ( 63 | model: WeakMap, 64 | real: WeakMap, 65 | ) => expect(real.has(key)).toBe(model.has(key)), 66 | toString: () => `has(${fc.stringify(key)})`, 67 | })), 68 | fc 69 | .oneof(entryArb, fc.tuple(keyArb, fc.anything())) 70 | .map(([key, value]) => ({ 71 | check: () => true, 72 | run: ( 73 | model: WeakMap, 74 | real: WeakMap, 75 | ) => { 76 | model.set(key, value) 77 | real.set(key, value) 78 | }, 79 | toString: () => 80 | `set(${fc.stringify(key)}, ${fc.stringify(value)})`, 81 | })), 82 | ], 83 | { maxCommands: 1000 }, 84 | ), 85 | ) 86 | }), 87 | ], 88 | { numRuns: 500 }, 89 | )( 90 | `mergeWeakMaps returns a WeakMap that behaves like a non-merged WeakMap containing the same values as the merged WeakMaps`, 91 | ([arrays, commands]) => { 92 | fc.modelRun( 93 | () => ({ 94 | model: new WeakMap(arrays.flat()), 95 | real: mergeWeakMaps(...arrays.map(entries => new WeakMap(entries))), 96 | }), 97 | commands, 98 | ) 99 | }, 100 | ) 101 | 102 | test(`mergeWeakMaps concrete example`, () => { 103 | const [a, b, c, d] = [[], {}, new Set(), new Map()] 104 | 105 | const weakMap1 = new WeakMap([ 106 | [a, 1], 107 | [b, 2], 108 | ]) 109 | const weakMap2 = new WeakMap([[c, 3]]) 110 | 111 | const mergedWeakMap = mergeWeakMaps(weakMap1, weakMap2) 112 | 113 | expect(mergedWeakMap.has(a)).toBeTrue() 114 | expect(mergedWeakMap.has(b)).toBeTrue() 115 | expect(mergedWeakMap.has(c)).toBeTrue() 116 | 117 | mergedWeakMap.delete(a) 118 | 119 | expect(weakMap1.has(a)).toBeTrue() 120 | expect(mergedWeakMap.has(a)).toBeFalse() 121 | 122 | mergedWeakMap.set(d, 5) 123 | 124 | expect(weakMap1.has(d)).toBeFalse() 125 | expect(weakMap2.has(d)).toBeFalse() 126 | expect(mergedWeakMap.get(d)).toBe(5) 127 | }) 128 | -------------------------------------------------------------------------------- /license-apache: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. 10 | 11 | "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. 12 | 13 | "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. 14 | 15 | "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. 16 | 17 | "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. 18 | 19 | "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. 20 | 21 | "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). 22 | 23 | "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. 24 | 25 | "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." 26 | 27 | "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 28 | 29 | 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 30 | 31 | 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 32 | 33 | 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: 34 | 35 | (a) You must give any other recipients of the Work or Derivative Works a copy of this License; and 36 | 37 | (b) You must cause any modified files to carry prominent notices stating that You changed the files; and 38 | 39 | (c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and 40 | 41 | (d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. 42 | 43 | You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 44 | 45 | 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 46 | 47 | 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 48 | 49 | 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 50 | 51 | 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 52 | 53 | 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. 54 | 55 | END OF TERMS AND CONDITIONS 56 | 57 | APPENDIX: How to apply the Apache License to your work. 58 | 59 | To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "[]" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives. 60 | 61 | Copyright [yyyy] [name of copyright owner] 62 | 63 | Licensed under the Apache License, Version 2.0 (the "License"); 64 | you may not use this file except in compliance with the License. 65 | You may obtain a copy of the License at 66 | 67 | http://www.apache.org/licenses/LICENSE-2.0 68 | 69 | Unless required by applicable law or agreed to in writing, software 70 | distributed under the License is distributed on an "AS IS" BASIS, 71 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 72 | See the License for the specific language governing permissions and 73 | limitations under the License. --------------------------------------------------------------------------------