@fable-org/fable-library-ts 1.3.0 → 1.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
7
7
 
8
8
  ## Unreleased
9
9
 
10
+ ## 1.4.0 - 2024-03-18
11
+
12
+ ### Added
13
+
14
+ * [JS/TS] Add `ConditionalWeakTable` (by @chkn)
15
+
10
16
  ## 1.3.0 - 2024-03-18
11
17
 
12
18
  * [JS/TS] `Boolean.tryParse` should not crash on `null` string (@goswinr)
@@ -0,0 +1,26 @@
1
+
2
+ export class ConditionalWeakTable<TKey extends object, TValue> {
3
+ private weakMap: WeakMap<TKey, TValue> = new WeakMap<TKey, TValue>();
4
+
5
+ public delete(key: TKey) {
6
+ return this.weakMap.delete(key);
7
+ }
8
+
9
+ public get(key: TKey) {
10
+ return this.weakMap.get(key);
11
+ }
12
+
13
+ public has(key: TKey) {
14
+ return this.weakMap.has(key);
15
+ }
16
+
17
+ public set(key: TKey, value: TValue) {
18
+ return this.weakMap.set(key, value);
19
+ }
20
+
21
+ public clear() {
22
+ this.weakMap = new WeakMap<TKey, TValue>();
23
+ }
24
+ }
25
+
26
+ export default ConditionalWeakTable;
package/MapUtil.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { equals, IMap, ISet } from "./Util.js";
1
+ import { equals, IMap, IMapOrWeakMap, ISet } from "./Util.js";
2
2
  import { FSharpRef, Union } from "./Types.js";
3
3
 
4
4
  const CaseRules = {
@@ -88,7 +88,7 @@ export function containsValue<K, V>(v: V, map: IMap<K, V>) {
88
88
  return false;
89
89
  }
90
90
 
91
- export function tryGetValue<K, V>(map: IMap<K, V>, key: K, defaultValue: FSharpRef<V>): boolean {
91
+ export function tryGetValue<K, V>(map: IMapOrWeakMap<K, V>, key: K, defaultValue: FSharpRef<V>): boolean {
92
92
  if (map.has(key)) {
93
93
  defaultValue.contents = map.get(key) as V;
94
94
  return true;
@@ -104,7 +104,15 @@ export function addToSet<T>(v: T, set: ISet<T>) {
104
104
  return true;
105
105
  }
106
106
 
107
- export function addToDict<K, V>(dict: IMap<K, V>, k: K, v: V) {
107
+ export function tryAddToDict<K, V>(dict: IMapOrWeakMap<K, V>, k: K, v: V) {
108
+ if (dict.has(k)) {
109
+ return false;
110
+ }
111
+ dict.set(k, v);
112
+ return true;
113
+ }
114
+
115
+ export function addToDict<K, V>(dict: IMapOrWeakMap<K, V>, k: K, v: V) {
108
116
  if (dict.has(k)) {
109
117
  throw new Error("An item with the same key has already been added. Key: " + k);
110
118
  }
@@ -118,3 +126,12 @@ export function getItemFromDict<K, V>(map: IMap<K, V>, key: K) {
118
126
  throw new Error(`The given key '${key}' was not present in the dictionary.`);
119
127
  }
120
128
  }
129
+
130
+ export function getItemFromDictOrCreate<K, V>(map: IMapOrWeakMap<K, V>, key: K, createValue: (key: K) => V) {
131
+ if (map.has(key)) {
132
+ return map.get(key) as V;
133
+ }
134
+ const value = createValue(key);
135
+ map.set(key, value);
136
+ return value;
137
+ }
package/Util.ts CHANGED
@@ -183,13 +183,18 @@ export interface ISet<T> {
183
183
  values(): Iterable<T>;
184
184
  }
185
185
 
186
- export interface IMap<K, V> {
187
- clear(): void;
186
+ export interface IMapOrWeakMap<K, V> {
188
187
  delete(key: K): boolean;
189
- forEach(callbackfn: (value: V, key: K, map: IMap<K, V>) => void, thisArg?: any): void;
190
188
  get(key: K): V | undefined;
191
189
  has(key: K): boolean;
190
+ set(key: K, value: V): IMapOrWeakMap<K, V>;
191
+ }
192
+
193
+ export interface IMap<K, V> extends IMapOrWeakMap<K, V> {
194
+ clear(): void;
192
195
  set(key: K, value: V): IMap<K, V>;
196
+
197
+ forEach(callbackfn: (value: V, key: K, map: IMap<K, V>) => void, thisArg?: any): void;
193
198
  readonly size: number;
194
199
 
195
200
  [Symbol.iterator](): Iterator<[K, V]>;
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "private": false,
4
4
  "type": "module",
5
5
  "name": "@fable-org/fable-library-ts",
6
- "version": "1.3.0",
6
+ "version": "1.4.0",
7
7
  "description": "Core library used by F# projects compiled with fable.io",
8
8
  "author": "Fable Contributors",
9
9
  "license": "MIT",