@helpers4/observable 2.0.0-alpha.11 → 2.0.0-alpha.14
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/lib/index.d.ts +1 -1
- package/lib/index.js +35 -3
- package/lib/index.js.map +1 -1
- package/llms.txt +122 -0
- package/meta/api.json +3 -3
- package/package.json +3 -1
package/lib/index.d.ts
CHANGED
|
@@ -34,7 +34,7 @@ declare function combine<T, U, R>(source1: Observable<T>, source2: Observable<U>
|
|
|
34
34
|
*/
|
|
35
35
|
|
|
36
36
|
declare function combineLatest<A extends readonly unknown[]>(sources: readonly [...ObservableInputTuple<A>]): Observable<A>;
|
|
37
|
-
declare function combineLatest<T extends Record<string, ObservableInput<
|
|
37
|
+
declare function combineLatest<T extends Record<string, ObservableInput<unknown>>>(sourcesObject: T): Observable<{
|
|
38
38
|
[K in keyof T]: ObservedValueOf<T[K]>;
|
|
39
39
|
}>;
|
|
40
40
|
|
package/lib/index.js
CHANGED
|
@@ -26,6 +26,35 @@ function combine(source1, source2, map$1, options) {
|
|
|
26
26
|
else return combineLatest$1([source1, source2]).pipe(map(map$1));
|
|
27
27
|
}
|
|
28
28
|
//#endregion
|
|
29
|
+
//#region helpers/type/isDefined.ts
|
|
30
|
+
/**
|
|
31
|
+
* Checks if a value is defined (not undefined nor null).
|
|
32
|
+
* This is the inverse of {@link isNullish}.
|
|
33
|
+
*
|
|
34
|
+
* Use as a type-safe filter callback to remove `null`/`undefined` from arrays.
|
|
35
|
+
*
|
|
36
|
+
* @param value - The value to check
|
|
37
|
+
* @returns True if value is not undefined nor null
|
|
38
|
+
* @example
|
|
39
|
+
* isDefined(42) // => true
|
|
40
|
+
* isDefined('') // => true (empty string is defined)
|
|
41
|
+
* isDefined(null) // => false
|
|
42
|
+
* isDefined(undefined) // => false
|
|
43
|
+
*
|
|
44
|
+
* @example
|
|
45
|
+
* // Type-safe alternative to filter out null/undefined
|
|
46
|
+
* const items: (string | null | undefined)[] = ['a', null, 'b', undefined];
|
|
47
|
+
* const result = items.filter(isDefined);
|
|
48
|
+
* // => ['a', 'b'] with type string[]
|
|
49
|
+
*
|
|
50
|
+
* @see {@link isNullish} for the inverse check
|
|
51
|
+
* @see {@link isTruthy} to also filter out `0`, `""`, `false`, and `NaN`
|
|
52
|
+
* @since 2.0.0
|
|
53
|
+
*/
|
|
54
|
+
function isDefined(value) {
|
|
55
|
+
return value !== void 0 && value !== null;
|
|
56
|
+
}
|
|
57
|
+
//#endregion
|
|
29
58
|
//#region helpers/observable/combineLatest.ts
|
|
30
59
|
/**
|
|
31
60
|
* This file is part of helpers4.
|
|
@@ -59,9 +88,12 @@ function combine(source1, source2, map$1, options) {
|
|
|
59
88
|
*/
|
|
60
89
|
function combineLatest(input) {
|
|
61
90
|
if (Array.isArray(input)) {
|
|
62
|
-
|
|
63
|
-
return
|
|
64
|
-
} else
|
|
91
|
+
const filtered = input.filter(isDefined);
|
|
92
|
+
return filtered.length ? combineLatest$1(filtered) : of([]);
|
|
93
|
+
} else {
|
|
94
|
+
const filtered = Object.fromEntries(Object.entries(input).filter(([_, v]) => isDefined(v)));
|
|
95
|
+
return Object.keys(filtered).length ? combineLatest$1(filtered) : of({});
|
|
96
|
+
}
|
|
65
97
|
}
|
|
66
98
|
//#endregion
|
|
67
99
|
export { combine, combineLatest };
|
package/lib/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","names":[],"sources":["../../../helpers/observable/combine.ts","../../../helpers/observable/combineLatest.ts"],"sourcesContent":["/**\n * This file is part of helpers4.\n * Copyright (C) 2025 baxyz\n * SPDX-License-Identifier: LGPL-3.0-or-later\n */\n\n/*\n * This program is under the terms of the GNU Lesser General Public License version 3\n * The full license information can be found in LICENSE in the root directory of this project.\n */\nimport {\n Observable,\n OperatorFunction,\n combineLatest as combineLatestOperator,\n map as mapOperator,\n} from \"rxjs\";\n\n\ntype combineOptions<T, U> = {\n readonly preTreatment?: OperatorFunction<readonly [T, U], readonly [T, U]>;\n};\n\n/**\n * Combine two observables with a map function and an optional pre-treatment.\n *\n * Note: you can use the pre-treatment to add a filter, a distinctUntilChanged,\n * any other operator that can be used in a pipe, or even an `UntilDestroy`\n * operator.\n *\n * @param source1 first source of data\n * @param source2 second source of data\n * @param map way to combine data\n * @param options options for the combineLatest operator\n * @returns an observable that emits the result of the map function\n * @see combineLatestOperator\n * @see mapOperator\n * @since 1.0.0\n */\nexport function combine<T, U, R>(\n source1: Observable<T>,\n source2: Observable<U>,\n map: (c: readonly [T, U]) => R,\n options?: combineOptions<T, U>\n): Observable<R> {\n if (options?.preTreatment) {\n return combineLatestOperator([source1, source2]).pipe(\n options.preTreatment,\n mapOperator(map)\n );\n } else {\n return combineLatestOperator([source1, source2]).pipe(mapOperator(map));\n }\n}\n","/**\n * This file is part of helpers4.\n * Copyright (C) 2025 baxyz\n * SPDX-License-Identifier: LGPL-3.0-or-later\n */\n\n/*\n * This program is under the terms of the GNU Lesser General Public License version 3\n * The full license information can be found in LICENSE in the root directory of this project.\n */\nimport {\n Observable,\n ObservableInput,\n ObservableInputTuple,\n ObservedValueOf,\n combineLatest as combineLatestOperator,\n of,\n} from \"rxjs\";\n\n// combineLatest([a, b, c])\nexport function combineLatest<A extends readonly unknown[]>(\n sources: readonly [...ObservableInputTuple<A>]\n): Observable<A>;\n\n// combineLatest({a, b, c})\nexport function combineLatest<T extends Record<string, ObservableInput<
|
|
1
|
+
{"version":3,"file":"index.js","names":[],"sources":["../../../helpers/observable/combine.ts","../../../helpers/type/isDefined.ts","../../../helpers/observable/combineLatest.ts"],"sourcesContent":["/**\n * This file is part of helpers4.\n * Copyright (C) 2025 baxyz\n * SPDX-License-Identifier: LGPL-3.0-or-later\n */\n\n/*\n * This program is under the terms of the GNU Lesser General Public License version 3\n * The full license information can be found in LICENSE in the root directory of this project.\n */\nimport {\n Observable,\n OperatorFunction,\n combineLatest as combineLatestOperator,\n map as mapOperator,\n} from \"rxjs\";\n\n\ntype combineOptions<T, U> = {\n readonly preTreatment?: OperatorFunction<readonly [T, U], readonly [T, U]>;\n};\n\n/**\n * Combine two observables with a map function and an optional pre-treatment.\n *\n * Note: you can use the pre-treatment to add a filter, a distinctUntilChanged,\n * any other operator that can be used in a pipe, or even an `UntilDestroy`\n * operator.\n *\n * @param source1 first source of data\n * @param source2 second source of data\n * @param map way to combine data\n * @param options options for the combineLatest operator\n * @returns an observable that emits the result of the map function\n * @see combineLatestOperator\n * @see mapOperator\n * @since 1.0.0\n */\nexport function combine<T, U, R>(\n source1: Observable<T>,\n source2: Observable<U>,\n map: (c: readonly [T, U]) => R,\n options?: combineOptions<T, U>\n): Observable<R> {\n if (options?.preTreatment) {\n return combineLatestOperator([source1, source2]).pipe(\n options.preTreatment,\n mapOperator(map)\n );\n } else {\n return combineLatestOperator([source1, source2]).pipe(mapOperator(map));\n }\n}\n","/**\n * This file is part of helpers4.\n * Copyright (C) 2025 baxyz\n * SPDX-License-Identifier: LGPL-3.0-or-later\n */\n\nimport type { Maybe } from './Maybe';\n\n/**\n * Checks if a value is defined (not undefined nor null).\n * This is the inverse of {@link isNullish}.\n *\n * Use as a type-safe filter callback to remove `null`/`undefined` from arrays.\n *\n * @param value - The value to check\n * @returns True if value is not undefined nor null\n * @example\n * isDefined(42) // => true\n * isDefined('') // => true (empty string is defined)\n * isDefined(null) // => false\n * isDefined(undefined) // => false\n *\n * @example\n * // Type-safe alternative to filter out null/undefined\n * const items: (string | null | undefined)[] = ['a', null, 'b', undefined];\n * const result = items.filter(isDefined);\n * // => ['a', 'b'] with type string[]\n *\n * @see {@link isNullish} for the inverse check\n * @see {@link isTruthy} to also filter out `0`, `\"\"`, `false`, and `NaN`\n * @since 2.0.0\n */\nexport function isDefined<T>(value: Maybe<T>): value is T {\n return value !== undefined && value !== null;\n}\n","/**\n * This file is part of helpers4.\n * Copyright (C) 2025 baxyz\n * SPDX-License-Identifier: LGPL-3.0-or-later\n */\n\n/*\n * This program is under the terms of the GNU Lesser General Public License version 3\n * The full license information can be found in LICENSE in the root directory of this project.\n */\nimport {\n Observable,\n ObservableInput,\n ObservableInputTuple,\n ObservedValueOf,\n combineLatest as combineLatestOperator,\n of,\n} from \"rxjs\";\nimport { isDefined } from '../type/isDefined';\n\n// combineLatest([a, b, c])\nexport function combineLatest<A extends readonly unknown[]>(\n sources: readonly [...ObservableInputTuple<A>]\n): Observable<A>;\n\n// combineLatest({a, b, c})\nexport function combineLatest<T extends Record<string, ObservableInput<unknown>>>(\n sourcesObject: T\n): Observable<{ [K in keyof T]: ObservedValueOf<T[K]> }>;\n\n/**\n * Combines multiple Observables to create an Observable whose values are\n * calculated from the latest values of each of its input Observables.\n *\n * This method relies on {@link combineLatestOperator} of rxjs.\n *\n * The main difference with {@link combineLatestOperator} is in case of empty parameters.\n * If the parameter is empty (empty array or empty object), the result will be\n * also empty.\n *\n * ATTENTION: this version doesn't support `scheduler` nor `mapper` as last\n * argument like in {@link combineLatestOperator}.\n *\n * @see {@link combineLatestOperator}\n * @see {@link combineLatestAll}\n * @see {@link merge}\n * @see {@link withLatestFrom}\n *\n * @param {ObservableInput} [observables] An array of input Observables to combine with each other.\n * An array of Observables must be given as the first argument.\n * @returns {Observable} An Observable of projected values from the most recent\n * values from each input Observable, or an array of the most recent values from\n * each input Observable.\n * @since 1.0.0\n */\nexport function combineLatest(input: readonly ObservableInput<unknown>[] | Record<string, ObservableInput<unknown>>): Observable<unknown> {\n if (Array.isArray(input)) {\n const filtered = input.filter(isDefined);\n return filtered.length ? combineLatestOperator(filtered) : of([]);\n } else {\n const filtered = Object.fromEntries(\n Object.entries(input).filter(([_, v]) => isDefined(v)),\n );\n return Object.keys(filtered).length\n ? combineLatestOperator(filtered)\n : of({});\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;AAsCA,SAAgB,QACd,SACA,SACA,OACA,SACe;AACf,KAAI,SAAS,aACX,QAAO,gBAAsB,CAAC,SAAS,QAAQ,CAAC,CAAC,KAC/C,QAAQ,cACR,IAAY,MAAI,CACjB;KAED,QAAO,gBAAsB,CAAC,SAAS,QAAQ,CAAC,CAAC,KAAK,IAAY,MAAI,CAAC;;;;;;;;;;;;;;;;;;;;;;;;;;;;AClB3E,SAAgB,UAAa,OAA6B;AACxD,QAAO,UAAU,KAAA,KAAa,UAAU;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;ACsB1C,SAAgB,cAAc,OAA4G;AACxI,KAAI,MAAM,QAAQ,MAAM,EAAE;EACxB,MAAM,WAAW,MAAM,OAAO,UAAU;AACxC,SAAO,SAAS,SAAS,gBAAsB,SAAS,GAAG,GAAG,EAAE,CAAC;QAC5D;EACL,MAAM,WAAW,OAAO,YACtB,OAAO,QAAQ,MAAM,CAAC,QAAQ,CAAC,GAAG,OAAO,UAAU,EAAE,CAAC,CACvD;AACD,SAAO,OAAO,KAAK,SAAS,CAAC,SACzB,gBAAsB,SAAS,GAC/B,GAAG,EAAE,CAAC"}
|
package/llms.txt
ADDED
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
# @helpers4/observable
|
|
2
|
+
|
|
3
|
+
> Tree-shakable TypeScript utility functions for the `observable` domain.
|
|
4
|
+
> Package: `@helpers4/observable` — Version: 2.0.0-alpha.14
|
|
5
|
+
> License: LGPL-3.0-or-later
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
```sh
|
|
10
|
+
npm install @helpers4/observable
|
|
11
|
+
# or
|
|
12
|
+
pnpm add @helpers4/observable
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## Usage
|
|
16
|
+
|
|
17
|
+
```typescript
|
|
18
|
+
import { combine, combineLatest } from '@helpers4/observable';
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
## Functions
|
|
22
|
+
|
|
23
|
+
| Function | Description |
|
|
24
|
+
|---|---|
|
|
25
|
+
| `combine` | Combine two observables with a map function and an optional pre-treatment. Note: you can use the pr |
|
|
26
|
+
| `combineLatest` | Combines multiple Observables to create an Observable whose values are calculated from the latest va |
|
|
27
|
+
|
|
28
|
+
---
|
|
29
|
+
|
|
30
|
+
## API Reference
|
|
31
|
+
|
|
32
|
+
### `combine`
|
|
33
|
+
|
|
34
|
+
Combine two observables with a map function and an optional pre-treatment.
|
|
35
|
+
|
|
36
|
+
Note: you can use the pre-treatment to add a filter, a distinctUntilChanged,
|
|
37
|
+
any other operator that can be used in a pipe, or even an `UntilDestroy`
|
|
38
|
+
operator.
|
|
39
|
+
|
|
40
|
+
```typescript
|
|
41
|
+
import { combine } from '@helpers4/observable';
|
|
42
|
+
|
|
43
|
+
combine<T, U, R>(source1: Observable<T>, source2: Observable<U>, map: function, options?: combineOptions<T, U>): Observable<R>
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
**Parameters:**
|
|
47
|
+
|
|
48
|
+
- `source1: Observable<T>` — first source of data
|
|
49
|
+
- `source2: Observable<U>` — second source of data
|
|
50
|
+
- `map: function` — way to combine data
|
|
51
|
+
- `options?: combineOptions<T, U>` — options for the combineLatest operator
|
|
52
|
+
|
|
53
|
+
**Returns:** `Observable<R>` — an observable that emits the result of the map function
|
|
54
|
+
|
|
55
|
+
**Examples:**
|
|
56
|
+
|
|
57
|
+
*Combine two observables with a map*
|
|
58
|
+
|
|
59
|
+
Combines the latest values of two observables using a mapping function.
|
|
60
|
+
|
|
61
|
+
```typescript
|
|
62
|
+
combine(of(1), of(2), ([a, b]) => a + b)
|
|
63
|
+
// emits 3
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
---
|
|
67
|
+
|
|
68
|
+
### `combineLatest`
|
|
69
|
+
|
|
70
|
+
Combines multiple Observables to create an Observable whose values are
|
|
71
|
+
calculated from the latest values of each of its input Observables.
|
|
72
|
+
|
|
73
|
+
This method relies on combineLatestOperator of rxjs.
|
|
74
|
+
|
|
75
|
+
The main difference with combineLatestOperator is in case of empty parameters.
|
|
76
|
+
If the parameter is empty (empty array or empty object), the result will be
|
|
77
|
+
also empty.
|
|
78
|
+
|
|
79
|
+
ATTENTION: this version doesn't support `scheduler` nor `mapper` as last
|
|
80
|
+
argument like in combineLatestOperator.
|
|
81
|
+
|
|
82
|
+
```typescript
|
|
83
|
+
import { combineLatest } from '@helpers4/observable';
|
|
84
|
+
|
|
85
|
+
combineLatest<A extends readonly unknown[]>(sources: readonly [ObservableInputTuple<A>]): Observable<A>
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
**Parameters:**
|
|
89
|
+
|
|
90
|
+
- `sources: readonly [ObservableInputTuple<A>]`
|
|
91
|
+
|
|
92
|
+
```typescript
|
|
93
|
+
import { combineLatest } from '@helpers4/observable';
|
|
94
|
+
|
|
95
|
+
combineLatest<T extends Record<string, ObservableInput<unknown>>>(sourcesObject: T): Observable<mapped>
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
**Parameters:**
|
|
99
|
+
|
|
100
|
+
- `sourcesObject: T`
|
|
101
|
+
|
|
102
|
+
**Examples:**
|
|
103
|
+
|
|
104
|
+
*Combine array of observables*
|
|
105
|
+
|
|
106
|
+
Combines an array of observables into one that emits arrays of their latest values.
|
|
107
|
+
|
|
108
|
+
```typescript
|
|
109
|
+
combineLatest([of(1), of(2), of(3)])
|
|
110
|
+
// emits [1, 2, 3]
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
*Handle empty array*
|
|
114
|
+
|
|
115
|
+
Returns an observable that emits an empty array when given no sources.
|
|
116
|
+
|
|
117
|
+
```typescript
|
|
118
|
+
combineLatest([])
|
|
119
|
+
// emits []
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
---
|
package/meta/api.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"category": "observable",
|
|
3
|
-
"version": "2.0.0-alpha.
|
|
3
|
+
"version": "2.0.0-alpha.14",
|
|
4
4
|
"functions": [
|
|
5
5
|
{
|
|
6
6
|
"name": "combine",
|
|
@@ -88,7 +88,7 @@
|
|
|
88
88
|
]
|
|
89
89
|
},
|
|
90
90
|
{
|
|
91
|
-
"signature": "combineLatest<T extends Record<string, ObservableInput<
|
|
91
|
+
"signature": "combineLatest<T extends Record<string, ObservableInput<unknown>>>(sourcesObject: T): Observable<mapped>",
|
|
92
92
|
"description": "",
|
|
93
93
|
"params": [
|
|
94
94
|
{
|
|
@@ -104,7 +104,7 @@
|
|
|
104
104
|
"typeParameters": [
|
|
105
105
|
{
|
|
106
106
|
"name": "T",
|
|
107
|
-
"constraint": "Record<string, ObservableInput<
|
|
107
|
+
"constraint": "Record<string, ObservableInput<unknown>>"
|
|
108
108
|
}
|
|
109
109
|
]
|
|
110
110
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@helpers4/observable",
|
|
3
|
-
"version": "2.0.0-alpha.
|
|
3
|
+
"version": "2.0.0-alpha.14",
|
|
4
4
|
"description": "A set of helpers in TS/JS, compatible with tree-shaking, for observable.",
|
|
5
5
|
"author": "baxyz <baxy@etik.com>",
|
|
6
6
|
"license": "LGPL-3.0",
|
|
@@ -21,6 +21,7 @@
|
|
|
21
21
|
"./meta/api.json": "./meta/api.json",
|
|
22
22
|
"./meta/examples.json": "./meta/examples.json",
|
|
23
23
|
"./meta/licenses.json": "./meta/licenses.json",
|
|
24
|
+
"./llms.txt": "./llms.txt",
|
|
24
25
|
"./package.json": "./package.json"
|
|
25
26
|
},
|
|
26
27
|
"keywords": [
|
|
@@ -34,6 +35,7 @@
|
|
|
34
35
|
"lib/index.d.ts",
|
|
35
36
|
"lib/index.js.map",
|
|
36
37
|
"meta/",
|
|
38
|
+
"llms.txt",
|
|
37
39
|
"LICENSE.md",
|
|
38
40
|
"package.json",
|
|
39
41
|
"README.md"
|