semantic-typescript 0.2.6 → 0.2.7

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/readme.md CHANGED
@@ -52,9 +52,9 @@ npm install semantic-typescript
52
52
 
53
53
  ```typescript
54
54
  // Type usage examples
55
- const predicate: Predicate<number> = (n) => n > 0;
56
- const mapper: Functional<string, number> = (str) => str.length;
57
- const comparator: Comparator<number> = (a, b) => a - b;
55
+ let predicate: Predicate<number> = (n: number): boolean => n > 0;
56
+ let mapper: Functional<string, number> = (text: string): number => text.length;
57
+ let comparator: Comparator<number> = (a: number, b: number): number => a - b;
58
58
  ```
59
59
 
60
60
  ## Type Guards
@@ -82,17 +82,26 @@ const comparator: Comparator<number> = (a, b) => a - b;
82
82
  | `isStatistics(t: unknown): t is Statistics<unknown, number \| bigint>` | Check if it is a Statistics instance | O(1) | O(1) |
83
83
  | `isNumericStatistics(t: unknown): t is NumericStatistics<unknown>` | Check if it is a NumericStatistics instance | O(1) | O(1) |
84
84
  | `isBigIntStatistics(t: unknown): t is BigIntStatistics<unknown>` | Check if it is a BigIntStatistics instance | O(1) | O(1) |
85
+ | `isPromise(t: unknown): t is Promise<unknown>` | Check if it is a Promise object | O(1) | O(1) |
86
+ | `isAsync(t: unknown): t is AsyncFunction` | Check if it is an AsyncFunction | O(1) | O(1) |
85
87
 
86
88
  ```typescript
87
89
  // Type guard usage examples
88
- const value: unknown = "hello";
90
+ let value: unknown = "hello";
89
91
 
90
92
  if (isString(value)) {
91
93
  console.log(value.length); // Type-safe, value inferred as string
92
94
  }
93
95
 
94
96
  if (isOptional(someValue)) {
95
- someValue.ifPresent(val => console.log(val));
97
+ someValue.ifPresent((value): void => console.log(val));
98
+ }
99
+
100
+ if(isIterable(value)){
101
+ // Type-safe, now it's an iterable object.
102
+ for(let item of value){
103
+ console.log(item);
104
+ }
96
105
  }
97
106
  ```
98
107
 
@@ -105,11 +114,10 @@ if (isOptional(someValue)) {
105
114
 
106
115
  ```typescript
107
116
  // Utility function usage examples
108
- const numbers = [3, 1, 4, 1, 5];
117
+ let numbers: Array<number> = [3, 1, 4, 1, 5];
109
118
  numbers.sort(useCompare); // [1, 1, 3, 4, 5]
110
119
 
111
- const randomNum = useRandom(42); // Seed-based random number
112
- const randomBigInt = useRandom(1000n); // BigInt random number
120
+ let randomNum = useRandom(42); // Seed-based random number
113
121
  ```
114
122
 
115
123
  ## Factory Methods
@@ -125,13 +133,13 @@ const randomBigInt = useRandom(1000n); // BigInt random number
125
133
 
126
134
  ```typescript
127
135
  // Optional usage examples
128
- const emptyOpt = Optional.empty<number>();
129
- const presentOpt = Optional.of(42);
130
- const nullableOpt = Optional.ofNullable<string>(null);
131
- const nonNullOpt = Optional.ofNonNull("hello");
136
+ let empty: Optional<number> = Optional.empty();
137
+ let present: Optional<number> = Optional.of(42);
138
+ let nullable: Optional<string> = Optional.ofNullable<string>(null);
139
+ let nonNull: Optional<string> = Optional.ofNonNull("hello");
132
140
 
133
- presentOpt.ifPresent(val => console.log(val)); // Outputs 42
134
- console.log(emptyOpt.get(100)); // Outputs 100
141
+ presentO.ifPresent((value: number): void => console.log(value)); // Outputs 42
142
+ console.log(empty.get(100)); // Outputs 100
135
143
  ```
136
144
 
137
145
  ### Collector Factory Methods
@@ -143,30 +151,31 @@ console.log(emptyOpt.get(100)); // Outputs 100
143
151
 
144
152
  ```typescript
145
153
  // Collector conversion examples
146
- const numbers = from([3, 1, 4, 1, 5, 9, 2, 6, 5]);
154
+ let numbers: Semantic<number> = from([3, 1, 4, 1, 5, 9, 2, 6, 5]);
147
155
 
148
156
  // Performance first: use unordered collector
149
- const unordered = numbers
150
- .filter(n => n > 3)
157
+ let unordered: UnorderedCollectable<number> = from([3, 1, 4, 1, 5, 9, 2, 6, 5])
158
+ .filter((n: number): boolean => n > 3)
151
159
  .toUnoredered();
152
160
 
153
161
  // Sorting needed: use ordered collector
154
- const ordered = numbers.sorted();
162
+ let ordered: OrderedCollectable<number> = from([3, 1, 4, 1, 5, 9, 2, 6, 5])
163
+ .sorted();
155
164
 
156
165
  // Counts the number of elements
157
- let count = Collector.full(
158
- () => 0, // Initial value
159
- (accumulator, element) => accumulator + element, // Accumulate
160
- (accumulator) => accumulator // Finish
166
+ let count: Collector<number, number, number> = Collector.full(
167
+ (): number => 0, // Initial value
168
+ (accumulator: number, element: number): number => accumulator + element, // Accumulate
169
+ (accumulator: number): number => accumulator // Finish
161
170
  );
162
171
  count.collect(from([1,2,3,4,5])); // Counts from a stream
163
172
  count.collect([1,2,3,4,5]); // Counts from an iterable object
164
173
 
165
- let find = Collector.shortable(
166
- () => Optional.empty(), // Initial value
167
- (element, index, accumulator) => accumulator.isPresent(), // Interrupt
168
- (accumulator, element, index) => Optional.of(element), // Accumulate
169
- (accumulator) => accumulator // Finish
174
+ let find: Optional<number> = Collector.shortable(
175
+ (): Optional<number> => Optional.empty(), // Initial value
176
+ (element: number, index: bigint, accumulator: Optional<number>): Optional<number> => accumulator.isPresent(), // Interrupt
177
+ (accumulator: Optional<number>, element: number, index: bigint): Optional<number> => Optional.of(element), // Accumulate
178
+ (accumulator: Optional<number>): Optional<number> => accumulator // Finish
170
179
  );
171
180
  find.collect(from([1,2,3,4,5])); // Finds the first element
172
181
  find.collect([1,2,3,4,5]); // Finds the first element
@@ -176,6 +185,7 @@ find.collect([1,2,3,4,5]); // Finds the first element
176
185
 
177
186
  | Method | Description | Time Complexity | Space Complexity |
178
187
  |------|------|------------|------------|
188
+ | `animationFrame(period: number, delay: number = 0)` | Create a timed animation frame stream | O(1)* | O(1) |
179
189
  | `blob(blob, chunkSize)` | Create a stream from a Blob | O(n) | O(chunkSize) |
180
190
  | `empty<E>()` | Create an empty stream | O(1) | O(1) |
181
191
  | `fill<E>(element, count)` | Create a filled stream | O(n) | O(1) |
@@ -188,37 +198,45 @@ find.collect([1,2,3,4,5]); // Finds the first element
188
198
  ```typescript
189
199
  // Semantic factory method usage examples
190
200
 
201
+ // Create a stream from a timed animation frame
202
+ animationFrame(1000)
203
+ .toUnordered()
204
+ .forEach(frame => console.log(frame));
205
+
191
206
  // Create a stream from a Blob (chunked reading)
192
207
  blob(someBlob, 1024n)
193
- .toUnordered()
194
- .write(WritableStream)
195
- .then(callback) // Write stream successful
196
- .catch(callback); // Write stream failed
208
+ .toUnordered()
209
+ .write(WritableStream)
210
+ .then(callback) // Write stream successful
211
+ .catch(callback); // Write stream failed
197
212
 
198
213
  // Create an empty stream, won't execute until concatenated with other streams
199
214
  empty<string>()
200
- .toUnordered()
201
- .join(); //[]
215
+ .toUnordered()
216
+ .join(); //[]
202
217
 
203
218
  // Create a filled stream
204
- const filledStream = fill("hello", 3); // "hello", "hello", "hello"
219
+ let filledStream = fill("hello", 3); // "hello", "hello", "hello"
205
220
 
206
221
  // Create a timed stream with initial 2-second delay and 5-second execution period, implemented based on timer mechanism; may experience time drift due to system scheduling precision limitations.
207
- const intervalStream = interval(5000, 2000);
222
+ let intervalStream = interval(5000, 2000);
208
223
 
209
224
  // Create a stream from an iterable object
210
- const numberStream = from([1, 2, 3, 4, 5]);
211
- const stringStream = from(new Set(["Alex", "Bob"]));
225
+ let numberStream = from([1, 2, 3, 4, 5]);
226
+ let stringStream = from(new Set(["Alex", "Bob"]));
227
+
228
+ // Create a stream from a resolved Promise
229
+ let promisedStream: Semantic<Array<number>> = Promise.resolve([1, 2, 3, 4, 5]);
212
230
 
213
231
  // Create a range stream
214
- const rangeStream = range(1, 10, 2); // 1, 3, 5, 7, 9
232
+ let rangeStream = range(1, 10, 2); // 1, 3, 5, 7, 9
215
233
 
216
234
  // WebSocket event stream
217
- const ws = new WebSocket("ws://localhost:8080");
235
+ let ws = new WebSocket("ws://localhost:8080");
218
236
  websocket(ws)
219
- .filter((event)=> event.type === "message"); // Only listen to message events
237
+ .filter((event): boolean => event.type === "message"); // Only listen to message events
220
238
  .toUnordered() // Generally not ordered for events
221
- .forEach((event)=> receive(event)); // Receive messages
239
+ .forEach((event): void => receive(event)); // Receive messages
222
240
  ```
223
241
 
224
242
  ## Semantic Class Methods
@@ -249,9 +267,9 @@ websocket(ws)
249
267
 
250
268
  ```typescript
251
269
  // Semantic operation examples
252
- const result = from([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
253
- .filter(n => n % 2 === 0) // Filter even numbers
254
- .map(n => n * 2) // Multiply by 2
270
+ let result = from([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
271
+ .filter((n: number): boolean => n % 2 === 0) // Filter even numbers
272
+ .map((n: number): number => n * 2) // Multiply by 2
255
273
  .skip(1) // Skip the first
256
274
  .limit(3) // Limit to 3 elements
257
275
  .toUnordered() // Convert to unordered collector
@@ -259,11 +277,11 @@ const result = from([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
259
277
  // Result: [8, 12, 20]
260
278
 
261
279
  // Complex operation example
262
- const complexResult = range(1, 100, 1)
263
- .flatMap(n => from([n, n * 2])) // Map each element to two
280
+ let complexResult = range(1, 100, 1)
281
+ .flatMap((n: number): Semantics<number> => from([n, n * 2])) // Map each element to two
264
282
  .distinct() // Remove duplicates
265
283
  .shuffle() // Shuffle order
266
- .takeWhile(n => n < 50) // Take elements less than 50
284
+ .takeWhile((n: number): boolean => n < 50) // Take elements less than 50
267
285
  .toOrdered() // Convert to ordered collector
268
286
  .toArray(); // Convert to array
269
287
  ```
@@ -289,18 +307,18 @@ from([6,4,3,5,2]) // Creates a stream
289
307
 
290
308
  // Convert to a descending sorted array
291
309
  from([6,4,3,5,2]) // Creates a stream
292
- .soted((a, b) => b - a) // Sorts the stream in descending order
310
+ .soted((a: number, b: number): number => b - a) // Sorts the stream in descending order
293
311
  .toArray(); // [6, 5, 4, 3, 2]
294
312
 
295
313
  // Redirect to a reversed array
296
314
  from([6,4,3,5,2])
297
- .redirect((element, index) => -index) // Redirects to reversed order
315
+ .redirect((element, index): bigint => -index) // Redirects to reversed order
298
316
  .toOrderd() // Keeps the redirected order
299
317
  .toArray(); // [2, 5, 3, 4, 6]
300
318
 
301
319
  // Ignore redirections to reverse array
302
320
  from([6,4,3,5,2])
303
- .redirect((element, index) => -index) // Redirects to reversed order
321
+ .redirect((element: number, index: bigint) => -index) // Redirects to reversed order
304
322
  .toUnorderd() // Drops the redirected order. This operation will ignore `redirect`, `reverse`, `shuffle` and `translate` operations
305
323
  .toArray(); // [2, 5, 3, 4, 6]
306
324
 
@@ -357,27 +375,27 @@ let customizedCollector = from([1, 2, 3, 4, 5]).toCollectable((generator: Genera
357
375
 
358
376
  ```typescript
359
377
  // Collectable operation examples
360
- const data = from([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
361
- .filter(n => n % 2 === 0)
378
+ let data = from([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
379
+ .filter((n: number): boolean => n % 2 === 0)
362
380
  .toOrdered();
363
381
 
364
382
  // Match checks
365
- console.log(data.anyMatch(n => n > 5)); // true
366
- console.log(data.allMatch(n => n < 20)); // true
383
+ console.log(data.anyMatch((n: number): boolean => n > 5)); // true
384
+ console.log(data.allMatch((n: number): boolean => n < 20)); // true
367
385
 
368
386
  // Find operations
369
- data.findFirst().ifPresent(n => console.log(n)); // 2
370
- data.findAny().ifPresent(n => console.log(n)); // Any element
387
+ data.findFirst().ifPresent((n: number): void => console.log(n)); // 2
388
+ data.findAny().ifPresent((n: number): void => console.log(n)); // Any element
371
389
 
372
390
  // Grouping operations
373
- const grouped = data.groupBy(
374
- n => n > 5 ? "large" : "small",
375
- n => n * 2
391
+ let grouped = data.groupBy(
392
+ (n: number): string => n > 5 ? "large" : "small",
393
+ (n: number): number => n * 2
376
394
  );
377
395
  // {small: [4, 8], large: [12, 16, 20]}
378
396
 
379
397
  // Reduction operations
380
- const sum = data.reduce(0, (acc, n) => acc + n); // 30
398
+ let sum = data.reduce(0, (accumulator: number, n: number): number => accumulator + n); // 30
381
399
 
382
400
  // Output operations
383
401
  data.join(", "); // "[2, 4, 6, 8, 10]"
@@ -404,7 +422,7 @@ data.join(", "); // "[2, 4, 6, 8, 10]"
404
422
 
405
423
  ```typescript
406
424
  // Statistical analysis examples
407
- const numbers = from([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
425
+ let numbers = from([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
408
426
  .toNumericStatistics();
409
427
 
410
428
  console.log("Mean:", numbers.mean()); // 5.5
@@ -413,7 +431,7 @@ console.log("Standard deviation:", numbers.standardDeviation()); // ~2.87
413
431
  console.log("Sum:", numbers.summate()); // 55
414
432
 
415
433
  // Statistical analysis using mappers
416
- const objects = from([
434
+ let objects = from([
417
435
  { value: 10 },
418
436
  { value: 20 },
419
437
  { value: 30 }
@@ -427,7 +445,7 @@ console.log("Mapped mean:", objects.mean(obj => obj.value)); // 20
427
445
  ### Choose Unordered Collector (Performance First)
428
446
  ```typescript
429
447
  // When order guarantee is not needed, use unordered collector for best performance
430
- const highPerformance = data
448
+ let highPerformance = data
431
449
  .filter(predicate)
432
450
  .map(mapper)
433
451
  .toUnoredered(); // Best performance
@@ -436,13 +454,13 @@ const highPerformance = data
436
454
  ### Choose Ordered Collector (Order Required)
437
455
  ```typescript
438
456
  // When element order needs to be maintained, use ordered collector
439
- const ordered = data.sorted(comparator);
457
+ let ordered = data.sorted(comparator);
440
458
  ```
441
459
 
442
460
  ### Choose Window Collector (Window Operations)
443
461
  ```typescript
444
462
  // When window operations are needed
445
- const windowed = data
463
+ let window: WindowCollectable<number> = data
446
464
  .toWindow()
447
465
  .slide(5n, 2n); // Sliding window
448
466
  ```
@@ -450,10 +468,10 @@ const windowed = data
450
468
  ### Choose Statistical Analysis (Numerical Calculations)
451
469
  ```typescript
452
470
  // When statistical analysis is needed
453
- const stats = data
471
+ let statistics: NumericStatistics<number> = data
454
472
  .toNumericStatistics(); // Numerical statistics
455
473
 
456
- const bigIntStats = data
474
+ let bigIntStatistics: BigintStatistics<bigint> = data
457
475
  .toBigintStatistics(); // Big integer statistics
458
476
  ```
459
477