iterable-linq-utility 0.3.0 → 0.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/README.md CHANGED
@@ -1,6 +1,9 @@
1
- [![](https://data.jsdelivr.com/v1/package/npm/iterable-linq-utility/badge)](https://www.jsdelivr.com/package/npm/iterable-linq-utility)
2
- [![](https://img.shields.io/npm/v/iterable-linq-utility.svg)](https://npmjs.org/package/iterable-linq-utility)
3
- [![](https://img.shields.io/npm/dm/iterable-linq-utility.svg)](https://npmjs.org/package/iterable-linq-utility)
1
+ | | |
2
+ | --- | --- |
3
+ | **Package** | [![npm version](https://img.shields.io/npm/v/iterable-linq-utility.svg)](https://npmjs.org/package/iterable-linq-utility) [![npm downloads](https://img.shields.io/npm/dm/iterable-linq-utility.svg)](https://npmjs.org/package/iterable-linq-utility) [![jsDelivr hits](https://data.jsdelivr.com/v1/package/npm/iterable-linq-utility/badge)](https://www.jsdelivr.com/package/npm/iterable-linq-utility) |
4
+ | **Quality** | [![CI status](https://github.com/Amebus/iterable-linq-utility/actions/workflows/build-test.yml/badge.svg?branch=main&event=push)](https://github.com/Amebus/iterable-linq-utility/actions/workflows/build-test.yml?query=branch%3Amain+event%3Apush) [![Coverage](https://codecov.io/gh/Amebus/iterable-linq-utility/branch/main/graph/badge.svg)](https://codecov.io/gh/Amebus/iterable-linq-utility) |
5
+ | **Activity** | [![Commits since the latest release](https://img.shields.io/github/commits-since/Amebus/iterable-linq-utility/latest)](https://amebus.github.io/iterable-linq-utility/next/) [![Latest release date](https://img.shields.io/github/release-date/Amebus/iterable-linq-utility)](https://github.com/Amebus/iterable-linq-utility/releases/latest) [![Open issues](https://img.shields.io/github/issues/Amebus/iterable-linq-utility)](https://github.com/Amebus/iterable-linq-utility/issues) [![Open pull requests](https://img.shields.io/github/issues-pr/Amebus/iterable-linq-utility)](https://github.com/Amebus/iterable-linq-utility/pulls) |
6
+ | **Community** | [![GitHub stars](https://img.shields.io/github/stars/Amebus/iterable-linq-utility)](https://github.com/Amebus/iterable-linq-utility/stargazers) [![Forks](https://img.shields.io/github/forks/Amebus/iterable-linq-utility)](https://github.com/Amebus/iterable-linq-utility/forks) |
4
7
 
5
8
  A [.NET Linq to Objects](https://learn.microsoft.com/it-it/dotnet/csharp/programming-guide/concepts/linq/linq-to-objects) porting with javacript naming conventions (e.g.: `Select` as been ranamed to `map`) and some new features (e.g.: [memoize](https://amebus.github.io/iterable-linq-utility/api-reference/transformations.md#memoize) and [materialize](https://amebus.github.io/iterable-linq-utility/api-reference/actions.md#materialize)).
6
9
 
@@ -85,11 +88,11 @@ A difference smaller than the `rme` of the two rows is noise.
85
88
  Create `test/bench/functions/<name>.bench.ts` or `test/bench/chains/<scenario>.bench.ts`:
86
89
 
87
90
  ```ts
88
- import * as IterableLinq from 'iterable-linq-utility';
89
91
  import { test } from 'vitest';
90
-
91
92
  import * as Helpers from '../helpers';
92
93
 
94
+ import * as IterableLinq from 'iterable-linq-utility';
95
+
93
96
  // Read the exports once: an imported binding goes through a module runner getter on every read.
94
97
  const { from } = IterableLinq;
95
98
  const { cases, numbers, sum } = Helpers;
package/dist/index.d.cts CHANGED
@@ -98,6 +98,24 @@ declare function empty_2<T>(): Iterable<T>;
98
98
  */
99
99
  export declare function extend<K extends Extract<keyof IIterableLinq<unknown>, string>>(name: K, implementation: ChainMethod): void;
100
100
 
101
+ /**
102
+ * Lazily keeps the values accepted by a type guard and narrows their type.
103
+ * If `predicate` throws, the source is closed and the error propagates.
104
+ * @operation `Transformation`
105
+ * @param iterable - the source `Iterable`
106
+ * @param predicate - a type guard called with each value and its index; return `true` to keep the value
107
+ * @returns a lazy, re-runnable `Iterable` of the narrowed values
108
+ * @throws Error if `iterable` is missing or does not implement `[Symbol.iterator]`, or if `predicate` is not a function
109
+ * @example
110
+ * ```ts
111
+ * const values: (number | string)[] = [1, 'two', 3];
112
+ * const strings = Functions.filter(values, (v): v is string => typeof v === 'string');
113
+ * Array.from(strings); // string[], ['two']
114
+ * ```
115
+ * @since 0.4.0
116
+ */
117
+ declare function filter<T, S extends T>(iterable: Iterable<T>, predicate: (value: T, index: number) => value is S): Iterable<S>;
118
+
101
119
  /**
102
120
  * Lazily keeps only the values that satisfy `predicate`.
103
121
  * If `predicate` throws, the source is closed and the error propagates.
@@ -296,6 +314,21 @@ export declare interface IIterableLinqBase<T> {
296
314
  * @since 0.0.1
297
315
  */
298
316
  collectToArray(): T[];
317
+ /**
318
+ * Keeps the values accepted by a type guard and narrows their type.
319
+ * If `predicate` throws, the source is closed and the error propagates.
320
+ * @operation `Transformation`
321
+ * @param predicate - a type guard called with each value and its index; return `true` to keep the value
322
+ * @returns a new chain with the narrowed values
323
+ * @throws Error if `predicate` is not a function
324
+ * @example
325
+ * ```ts
326
+ * const values: (number | string)[] = [1, 'two', 3];
327
+ * IterableLinq.from(values).filter((v): v is string => typeof v === 'string').collectToArray(); // string[], ['two']
328
+ * ```
329
+ * @since 0.4.0
330
+ */
331
+ filter<S extends T>(predicate: (value: T, index: number) => value is S): IIterableLinq<S>;
299
332
  /**
300
333
  * Keeps only the values that satisfy `predicate`.
301
334
  * If `predicate` throws, the source is closed and the error propagates.
@@ -555,11 +588,12 @@ export declare interface IIterableLinqBase<T> {
555
588
  * @example
556
589
  * ```ts
557
590
  * let evens: IIterableLinq<number> | undefined;
558
- * IterableLinq.fromRange(10)
591
+ * const evensByTen = IterableLinq.fromRange(10)
559
592
  * .filter(v => v % 2 === 0)
560
593
  * .tapChainCreation(chain => { evens = chain; return unit(); })
561
594
  * .map(v => v * 10);
562
595
  * evens?.collectToArray(); // [0, 2, 4, 6, 8]
596
+ * evensByTen.collectToArray(); // [0, 20, 40, 60, 80]
563
597
  * ```
564
598
  * @since 0.0.10
565
599
  */
package/dist/index.d.ts CHANGED
@@ -98,6 +98,24 @@ declare function empty_2<T>(): Iterable<T>;
98
98
  */
99
99
  export declare function extend<K extends Extract<keyof IIterableLinq<unknown>, string>>(name: K, implementation: ChainMethod): void;
100
100
 
101
+ /**
102
+ * Lazily keeps the values accepted by a type guard and narrows their type.
103
+ * If `predicate` throws, the source is closed and the error propagates.
104
+ * @operation `Transformation`
105
+ * @param iterable - the source `Iterable`
106
+ * @param predicate - a type guard called with each value and its index; return `true` to keep the value
107
+ * @returns a lazy, re-runnable `Iterable` of the narrowed values
108
+ * @throws Error if `iterable` is missing or does not implement `[Symbol.iterator]`, or if `predicate` is not a function
109
+ * @example
110
+ * ```ts
111
+ * const values: (number | string)[] = [1, 'two', 3];
112
+ * const strings = Functions.filter(values, (v): v is string => typeof v === 'string');
113
+ * Array.from(strings); // string[], ['two']
114
+ * ```
115
+ * @since 0.4.0
116
+ */
117
+ declare function filter<T, S extends T>(iterable: Iterable<T>, predicate: (value: T, index: number) => value is S): Iterable<S>;
118
+
101
119
  /**
102
120
  * Lazily keeps only the values that satisfy `predicate`.
103
121
  * If `predicate` throws, the source is closed and the error propagates.
@@ -296,6 +314,21 @@ export declare interface IIterableLinqBase<T> {
296
314
  * @since 0.0.1
297
315
  */
298
316
  collectToArray(): T[];
317
+ /**
318
+ * Keeps the values accepted by a type guard and narrows their type.
319
+ * If `predicate` throws, the source is closed and the error propagates.
320
+ * @operation `Transformation`
321
+ * @param predicate - a type guard called with each value and its index; return `true` to keep the value
322
+ * @returns a new chain with the narrowed values
323
+ * @throws Error if `predicate` is not a function
324
+ * @example
325
+ * ```ts
326
+ * const values: (number | string)[] = [1, 'two', 3];
327
+ * IterableLinq.from(values).filter((v): v is string => typeof v === 'string').collectToArray(); // string[], ['two']
328
+ * ```
329
+ * @since 0.4.0
330
+ */
331
+ filter<S extends T>(predicate: (value: T, index: number) => value is S): IIterableLinq<S>;
299
332
  /**
300
333
  * Keeps only the values that satisfy `predicate`.
301
334
  * If `predicate` throws, the source is closed and the error propagates.
@@ -555,11 +588,12 @@ export declare interface IIterableLinqBase<T> {
555
588
  * @example
556
589
  * ```ts
557
590
  * let evens: IIterableLinq<number> | undefined;
558
- * IterableLinq.fromRange(10)
591
+ * const evensByTen = IterableLinq.fromRange(10)
559
592
  * .filter(v => v % 2 === 0)
560
593
  * .tapChainCreation(chain => { evens = chain; return unit(); })
561
594
  * .map(v => v * 10);
562
595
  * evens?.collectToArray(); // [0, 2, 4, 6, 8]
596
+ * evensByTen.collectToArray(); // [0, 20, 40, 60, 80]
563
597
  * ```
564
598
  * @since 0.0.10
565
599
  */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "iterable-linq-utility",
3
- "version": "0.3.0",
3
+ "version": "0.4.0",
4
4
  "description": "An Iterable mutation utility inspired by .net Linq and Javascript Array interface",
5
5
  "type": "module",
6
6
  "main": "./dist/iterable-linq-utility.umd.cjs",
@@ -25,6 +25,7 @@
25
25
  "build": "tsc -p tsconfig.build.json && vite build && cp dist/index.d.ts dist/index.d.cts",
26
26
  "check:package": "attw --pack .",
27
27
  "check:changeset": "changeset status --since=origin/main",
28
+ "check:commits": "node scripts/check-commits.ts",
28
29
  "check:since": "node scripts/check-since.ts",
29
30
  "check:instructions": "node scripts/check-instructions.ts",
30
31
  "check:structure": "node scripts/check-structure.ts",
@@ -65,6 +66,7 @@
65
66
  "@vitest/coverage-istanbul": "^5.0.2",
66
67
  "@vitest/ui": "^5.0.2",
67
68
  "eslint": "^10.11.0",
69
+ "eslint-plugin-perfectionist": "^5.12.1",
68
70
  "eslint-plugin-tsdoc": "^0.5.3",
69
71
  "globals": "^17.12.0",
70
72
  "typescript": "^6.0.3",