@fable-org/fable-library-js 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 +6 -0
- package/ConditionalWeakTable.d.ts +9 -0
- package/ConditionalWeakTable.js +21 -0
- package/MapUtil.d.ts +5 -3
- package/MapUtil.js +15 -0
- package/Util.d.ts +6 -3
- package/package.json +1 -1
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,9 @@
|
|
|
1
|
+
export declare class ConditionalWeakTable<TKey extends object, TValue> {
|
|
2
|
+
private weakMap;
|
|
3
|
+
delete(key: TKey): boolean;
|
|
4
|
+
get(key: TKey): TValue | undefined;
|
|
5
|
+
has(key: TKey): boolean;
|
|
6
|
+
set(key: TKey, value: TValue): WeakMap<TKey, TValue>;
|
|
7
|
+
clear(): void;
|
|
8
|
+
}
|
|
9
|
+
export default ConditionalWeakTable;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
export class ConditionalWeakTable {
|
|
2
|
+
constructor() {
|
|
3
|
+
this.weakMap = new WeakMap();
|
|
4
|
+
}
|
|
5
|
+
delete(key) {
|
|
6
|
+
return this.weakMap.delete(key);
|
|
7
|
+
}
|
|
8
|
+
get(key) {
|
|
9
|
+
return this.weakMap.get(key);
|
|
10
|
+
}
|
|
11
|
+
has(key) {
|
|
12
|
+
return this.weakMap.has(key);
|
|
13
|
+
}
|
|
14
|
+
set(key, value) {
|
|
15
|
+
return this.weakMap.set(key, value);
|
|
16
|
+
}
|
|
17
|
+
clear() {
|
|
18
|
+
this.weakMap = new WeakMap();
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
export default ConditionalWeakTable;
|
package/MapUtil.d.ts
CHANGED
|
@@ -1,10 +1,12 @@
|
|
|
1
|
-
import { IMap, ISet } from "./Util.js";
|
|
1
|
+
import { IMap, IMapOrWeakMap, ISet } from "./Util.js";
|
|
2
2
|
import { FSharpRef } from "./Types.js";
|
|
3
3
|
export declare function keyValueList(fields: Iterable<any>, caseRule?: number): {
|
|
4
4
|
[k: string]: any;
|
|
5
5
|
};
|
|
6
6
|
export declare function containsValue<K, V>(v: V, map: IMap<K, V>): boolean;
|
|
7
|
-
export declare function tryGetValue<K, V>(map:
|
|
7
|
+
export declare function tryGetValue<K, V>(map: IMapOrWeakMap<K, V>, key: K, defaultValue: FSharpRef<V>): boolean;
|
|
8
8
|
export declare function addToSet<T>(v: T, set: ISet<T>): boolean;
|
|
9
|
-
export declare function
|
|
9
|
+
export declare function tryAddToDict<K, V>(dict: IMapOrWeakMap<K, V>, k: K, v: V): boolean;
|
|
10
|
+
export declare function addToDict<K, V>(dict: IMapOrWeakMap<K, V>, k: K, v: V): void;
|
|
10
11
|
export declare function getItemFromDict<K, V>(map: IMap<K, V>, key: K): V;
|
|
12
|
+
export declare function getItemFromDictOrCreate<K, V>(map: IMapOrWeakMap<K, V>, key: K, createValue: (key: K) => V): V;
|
package/MapUtil.js
CHANGED
|
@@ -96,6 +96,13 @@ export function addToSet(v, set) {
|
|
|
96
96
|
set.add(v);
|
|
97
97
|
return true;
|
|
98
98
|
}
|
|
99
|
+
export function tryAddToDict(dict, k, v) {
|
|
100
|
+
if (dict.has(k)) {
|
|
101
|
+
return false;
|
|
102
|
+
}
|
|
103
|
+
dict.set(k, v);
|
|
104
|
+
return true;
|
|
105
|
+
}
|
|
99
106
|
export function addToDict(dict, k, v) {
|
|
100
107
|
if (dict.has(k)) {
|
|
101
108
|
throw new Error("An item with the same key has already been added. Key: " + k);
|
|
@@ -110,3 +117,11 @@ export function getItemFromDict(map, key) {
|
|
|
110
117
|
throw new Error(`The given key '${key}' was not present in the dictionary.`);
|
|
111
118
|
}
|
|
112
119
|
}
|
|
120
|
+
export function getItemFromDictOrCreate(map, key, createValue) {
|
|
121
|
+
if (map.has(key)) {
|
|
122
|
+
return map.get(key);
|
|
123
|
+
}
|
|
124
|
+
const value = createValue(key);
|
|
125
|
+
map.set(key, value);
|
|
126
|
+
return value;
|
|
127
|
+
}
|
package/Util.d.ts
CHANGED
|
@@ -93,13 +93,16 @@ export interface ISet<T> {
|
|
|
93
93
|
keys(): Iterable<T>;
|
|
94
94
|
values(): Iterable<T>;
|
|
95
95
|
}
|
|
96
|
-
export interface
|
|
97
|
-
clear(): void;
|
|
96
|
+
export interface IMapOrWeakMap<K, V> {
|
|
98
97
|
delete(key: K): boolean;
|
|
99
|
-
forEach(callbackfn: (value: V, key: K, map: IMap<K, V>) => void, thisArg?: any): void;
|
|
100
98
|
get(key: K): V | undefined;
|
|
101
99
|
has(key: K): boolean;
|
|
100
|
+
set(key: K, value: V): IMapOrWeakMap<K, V>;
|
|
101
|
+
}
|
|
102
|
+
export interface IMap<K, V> extends IMapOrWeakMap<K, V> {
|
|
103
|
+
clear(): void;
|
|
102
104
|
set(key: K, value: V): IMap<K, V>;
|
|
105
|
+
forEach(callbackfn: (value: V, key: K, map: IMap<K, V>) => void, thisArg?: any): void;
|
|
103
106
|
readonly size: number;
|
|
104
107
|
[Symbol.iterator](): Iterator<[K, V]>;
|
|
105
108
|
entries(): Iterable<[K, V]>;
|
package/package.json
CHANGED