@fable-org/fable-library-ts 1.6.0 → 1.7.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.7.0 - 2024-11-19
11
+
12
+ ### Fixed
13
+
14
+ * [JS/TS] Added missing IReadOnlyCollection helpers (#3953)
15
+
10
16
  ## 1.6.0 - 2024-10-02
11
17
 
12
18
  ### Removed
@@ -0,0 +1,161 @@
1
+ import { equals, isArrayLike } from "./Util.js";
2
+
3
+ export function count<T>(col: Iterable<T>): number {
4
+ if (typeof (col as any)["System.Collections.Generic.ICollection`1.get_Count"] === "function") {
5
+ return (col as any)["System.Collections.Generic.ICollection`1.get_Count"](); // collection
6
+ } else {
7
+ if (typeof (col as any)["System.Collections.Generic.IReadOnlyCollection`1.get_Count"] === "function") {
8
+ return (col as any)["System.Collections.Generic.IReadOnlyCollection`1.get_Count"](); // collection
9
+ } else {
10
+ if (isArrayLike(col)) {
11
+ return col.length; // array, resize array
12
+ } else {
13
+ if (typeof (col as any).size === "number") {
14
+ return (col as any).size; // map, set
15
+ } else {
16
+ let count = 0;
17
+ for (const _ of col) {
18
+ count++;
19
+ }
20
+ return count; // other collections
21
+ }
22
+ }
23
+ }
24
+ }
25
+ }
26
+
27
+ export function isReadOnly<T>(col: Iterable<T>): boolean {
28
+ if (typeof (col as any)["System.Collections.Generic.ICollection`1.get_IsReadOnly"] === "function") {
29
+ return (col as any)["System.Collections.Generic.ICollection`1.get_IsReadOnly"](); // collection
30
+ } else {
31
+ if (isArrayLike(col)) {
32
+ return ArrayBuffer.isView(col); // true for typed arrays, false for other arrays
33
+ } else {
34
+ if (typeof (col as any).size === "number") {
35
+ return false; // map, set
36
+ } else {
37
+ return true; // other collections
38
+ }
39
+ }
40
+
41
+ }
42
+ }
43
+
44
+ export function copyTo<T>(col: Iterable<T>, array: T[], arrayIndex: number) {
45
+ if (typeof (col as any)["System.Collections.Generic.ICollection`1.CopyToZ3B4C077E"] === "function") {
46
+ (col as any)["System.Collections.Generic.ICollection`1.CopyToZ3B4C077E"](array, arrayIndex); // collection
47
+ } else {
48
+ let i = arrayIndex;
49
+ for (const v of col) {
50
+ array[i] = v;
51
+ i++;
52
+ }
53
+ }
54
+ }
55
+
56
+ export function contains<T>(col: Iterable<T>, item: T): boolean {
57
+ if (typeof (col as any)["System.Collections.Generic.ICollection`1.Contains2B595"] === "function") {
58
+ return (col as any)["System.Collections.Generic.ICollection`1.Contains2B595"](item); // collection
59
+ } else {
60
+ if (isArrayLike(col)) {
61
+ let i = col.findIndex(x => equals(x, item)); // array, resize array
62
+ return i >= 0;
63
+ } else {
64
+ if (typeof (col as any).has === "function") {
65
+ if (typeof (col as any).set === "function" && isArrayLike(item)) {
66
+ return (col as any).has(item[0]) && equals((col as any).get(item[0]), item[1]); // map
67
+ } else {
68
+ return (col as any).has(item); // set
69
+ }
70
+ } else {
71
+ return false; // other collections
72
+ }
73
+ }
74
+ }
75
+ }
76
+
77
+ export function add<T>(col: Iterable<T>, item: T): void {
78
+ if (typeof (col as any)["System.Collections.Generic.ICollection`1.Add2B595"] === "function") {
79
+ return (col as any)["System.Collections.Generic.ICollection`1.Add2B595"](item); // collection
80
+ } else {
81
+ if (isArrayLike(col)) {
82
+ if (ArrayBuffer.isView(col)) {
83
+ // TODO: throw for typed arrays?
84
+ } else {
85
+ col.push(item); // array, resize array
86
+ }
87
+ } else {
88
+ if (typeof (col as any).add === "function") {
89
+ return (col as any).add(item); // set
90
+ } else {
91
+ if (typeof (col as any).has === "function"
92
+ && typeof (col as any).set === "function"
93
+ && isArrayLike(item)) {
94
+ if ((col as any).has(item[0]) === false) {
95
+ (col as any).set(item[0], item[1]); // map
96
+ } else {
97
+ throw new Error("An item with the same key has already been added. Key: " + item[0]);
98
+ }
99
+ } else {
100
+ // TODO: throw for other collections?
101
+ }
102
+ }
103
+ }
104
+ }
105
+ }
106
+
107
+ export function remove<T>(col: Iterable<T>, item: T): boolean {
108
+ if (typeof (col as any)["System.Collections.Generic.ICollection`1.Remove2B595"] === "function") {
109
+ return (col as any)["System.Collections.Generic.ICollection`1.Remove2B595"](item); // collection
110
+ } else {
111
+ if (isArrayLike(col)) {
112
+ if (ArrayBuffer.isView(col)) {
113
+ // TODO: throw for typed arrays
114
+ return false;
115
+ } else {
116
+ let i = col.findIndex(x => equals(x, item));
117
+ if (i >= 0) {
118
+ col.splice(i, 1); // array, resize array
119
+ return true;
120
+ } else {
121
+ return false;
122
+ }
123
+ }
124
+ } else {
125
+ if (typeof (col as any).delete === "function") {
126
+ if (typeof (col as any).set === "function" && isArrayLike(item)) {
127
+ if ((col as any).has(item[0]) && equals((col as any).get(item[0]), item[1])) {
128
+ return (col as any).delete(item[0]); // map
129
+ } else {
130
+ return false;
131
+ }
132
+ } else {
133
+ return (col as any).delete(item); // set
134
+ }
135
+ } else {
136
+ // TODO: throw for other collections?
137
+ return false; // other collections
138
+ }
139
+ }
140
+ }
141
+ }
142
+
143
+ export function clear<T>(col: Iterable<T>): void {
144
+ if (typeof (col as any)["System.Collections.Generic.ICollection`1.Clear"] === "function") {
145
+ return (col as any)["System.Collections.Generic.ICollection`1.Clear"](); // collection
146
+ } else {
147
+ if (isArrayLike(col)) {
148
+ if (ArrayBuffer.isView(col)) {
149
+ // TODO: throw for typed arrays?
150
+ } else {
151
+ col.splice(0); // array, resize array
152
+ }
153
+ } else {
154
+ if (typeof (col as any).clear === "function") {
155
+ (col as any).clear(); // map, set
156
+ } else {
157
+ // TODO: throw for other collections?
158
+ }
159
+ }
160
+ }
161
+ }
package/String.ts CHANGED
@@ -63,7 +63,7 @@ export function compareTo(x: string, y: string) {
63
63
  return cmp(x, y, StringComparison.CurrentCulture);
64
64
  }
65
65
 
66
- export function startsWith(str: string, pattern: string, ic: number) {
66
+ export function startsWith(str: string, pattern: string, ic: boolean | StringComparison) {
67
67
  if (ic === StringComparison.Ordinal) { // to avoid substring allocation
68
68
  return str.startsWith(pattern);
69
69
  }
@@ -73,7 +73,7 @@ export function startsWith(str: string, pattern: string, ic: number) {
73
73
  return false;
74
74
  }
75
75
 
76
- export function endsWith(str: string, pattern: string, ic: number) {
76
+ export function endsWith(str: string, pattern: string, ic: boolean | StringComparison) {
77
77
  if (ic === StringComparison.Ordinal) { // to avoid substring allocation
78
78
  return str.endsWith(pattern);
79
79
  }
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.6.0",
6
+ "version": "1.7.0",
7
7
  "description": "Core library used by F# projects compiled with fable.io",
8
8
  "author": "Fable Contributors",
9
9
  "license": "MIT",