linqx 0.2.0 → 0.2.1

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.
Files changed (2) hide show
  1. package/README.md +109 -91
  2. package/package.json +1 -1
package/README.md CHANGED
@@ -1,151 +1,169 @@
1
1
  # linqx
2
2
 
3
- Fork of the original [linq](https://github.com/mihaifm/linq) for JavaScript, updated with modern improvements and API changes.
3
+ Modern fork of the original [linq](https://github.com/mihaifm/linq) for JavaScript.
4
4
 
5
- It contains all the original .NET methods plus a few additions.
5
+ linqx keeps the familiar LINQ-style API while adding modern exports, new utility methods, improved TypeScript support, and active maintenance.
6
6
 
7
- Written in pure JavaScript with no dependencies.
7
+ Written in pure JavaScript with no runtime dependencies.
8
8
 
9
- ## Examples
9
+ ---
10
10
 
11
- ```js
12
- // C# LINQ - delegate
13
- Enumerable.Range(1, 10)
14
- .Where(delegate(int i) { return i % 3 == 0; })
15
- .Select(delegate(int i) { return i * 10; });
11
+ # Key Differences from linq
16
12
 
17
- // linq.js - anonymous function
18
- Enumerable.range(1, 10)
19
- .where(function(i) { return i % 3 == 0; })
20
- .select(function(i) { return i * 10; });
21
- ```
22
-
23
- ```js
24
- // C# LINQ - lambda
25
- Enumerable.Range(1, 10).Where((i) => i % 3 == 0).Select((i) => i * 10);
13
+ ## 1. Named Export Added
26
14
 
27
- // linq.js - arrow function
28
- Enumerable.range(1, 10).where((i) => i % 3 == 0).select((i) => i * 10);
29
- ```
15
+ The original package only exposed a default export.
30
16
 
31
- ```js
32
- // C# LINQ - anonymous type
33
- array.Select((val, i) => new { Value: val, Index: i }());
17
+ linqx also provides:
34
18
 
35
- // linq.js - object literal
36
- Enumerable.from(array).select((val, i) => ({ value: val, index: i }));
19
+ ```ts
20
+ export { Enumerable };
37
21
  ```
38
22
 
39
- See [sample/tutorial.js](https://github.com/mihaifm/linq/blob/master/sample/tutorial.js) and the [test](https://github.com/mihaifm/linq/tree/master/test) folder for more examples.
40
-
41
- # Usage
42
-
43
- ## Node.js (ES modules)
23
+ This makes extensions, wrappers, and custom integrations cleaner and more flexible for `typescript`.
44
24
 
45
- Install the latest version of the library with npm:
25
+ Example:
46
26
 
47
- npm install linq
48
-
49
- Load it in your code with the `import` syntax:
27
+ ```ts
28
+ // enumerable.extensions.ts
29
+ import { Enumerable } from "linqx";
50
30
 
51
- ```js
52
- import Enumerable from 'linq'
31
+ declare module "linqx" {
32
+ namespace Enumerable {
33
+ export function crossJoin<T>(this: Enumerable.IEnumerable<T>): Enumerable.IEnumerable<{ left: T; right: T; }>;
34
+ }
35
+ }
53
36
 
54
- let result = Enumerable.range(1, 10).where(i => i % 3 == 0).select(i => i * 10)
55
- console.log(result.toArray()) // [ 30, 60, 90 ]
37
+ Enumerable.prototype.crossJoin = function <T>(this: Enumerable.IEnumerable<T>): Enumerable.IEnumerable<{ left: T; right: T; }> {
38
+ return Enumerable.from(crossJoinIterator(this));
39
+
40
+ function* crossJoinIterator(enumerable: Enumerable.IEnumerable<T>): Iterable<{ left: T; right: T; }> {
41
+ for (const item of enumerable) {
42
+ for (const otherItem of enumerable) {
43
+ yield { left: item, right: otherItem };
44
+ }
45
+ }
46
+ }
47
+ };
56
48
  ```
57
49
 
58
- Because the library is an ES module, this code will only work if your project is also configured as an ES module. Add the following line in your `package.json` to make it an ES module:
50
+ Useful when augmenting prototypes or writing reusable helpers.
59
51
 
60
- ```json
61
- "type": "module"
62
- ```
52
+ ---
63
53
 
64
- If you're not planning to use ES modules, check the CommonJS section below.
54
+ ## 2. Additional APIs
65
55
 
66
- ## Node.js (CommonJS modules)
56
+ linqx adds practical methods not available in the original package.
67
57
 
68
- Install version 3 of this library:
69
58
 
70
- npm install linq@3
59
+ ```ts
60
+ whereIf(flag, predicate)
61
+ page(pageNumber, pageSize)
62
+ page({ pageNumber, pageSize })
63
+ joinWith(separator)
64
+ toMap(keySelector, valueSelector)
65
+ chunk(size)
66
+ index()
67
+ position()
68
+ withNeighbors()
69
+ ```
71
70
 
72
- Load it with the `require` syntax:
71
+ ---
73
72
 
74
- ```js
75
- const Enumerable = require('linq')
73
+ # Installation
76
74
 
77
- let count = Enumerable.range(1, 10).count(i => i < 5)
78
- console.log(count) // 4
75
+ ```bash
76
+ npm install linqx
79
77
  ```
80
78
 
81
- The [cjs](https://github.com/mihaifm/linq/tree/cjs) branch contains the source code for the CommonJS version of the library.
79
+ ---
82
80
 
83
- ## TypeScript
81
+ # Usage
84
82
 
85
- Install the latest version of the library with npm.
83
+ ## ES Modules
86
84
 
87
- Configure your compiler options in `tsconfig.json`
85
+ ```ts
86
+ import Enumerable from "linqx";
88
87
 
89
- ```json
90
- "compilerOptions": {
91
- "target": "ES2020",
92
- "moduleResolution": "node"
93
- }
88
+ const result = Enumerable
89
+ .range(1, 10)
90
+ .where(x => x % 3 === 0)
91
+ .select(x => x * 10)
92
+ .toArray();
94
93
  ```
95
94
 
96
- The library comes with a `d.ts` file containing type definitions for all the objects and methods, feel free to use them in your code:
95
+ ---
96
+
97
+ ## Named Export
97
98
 
98
99
  ```ts
99
- import Enumerable from 'linq';
100
+ import { Enumerable } from "linqx";
100
101
 
101
- type tnum = Enumerable.IEnumerable<number>;
102
- let x: tnum = Enumerable.from([1, 2, 3]);
102
+ const values = Enumerable.from([1, 2, 3]);
103
103
  ```
104
104
 
105
- ## Deno
105
+ ---
106
106
 
107
- Import the library from deno.land. Use the `@deno-types` annotation to load type definitions:
107
+ ## TypeScript
108
108
 
109
109
  ```ts
110
- // @deno-types="https://deno.land/x/linq@4.0.0/linq.d.ts"
111
- import Enumerable from 'https://deno.land/x/linq@4.0.0/linq.js'
110
+ import Enumerable from "linqx";
112
111
 
113
- let radius = Enumerable.toInfinity(1).where(r => r * r * Math.PI > 10000).first()
112
+ const items: Enumerable.IEnumerable<number> = Enumerable.from([1, 2, 3]);
114
113
  ```
114
+ ---
115
115
 
116
- You can also install locally with npm. Use the full file path when importing the library:
116
+ ## New API Examples
117
117
 
118
118
  ```ts
119
- // @deno-types="./node_modules/linq/linq.d.ts"
120
- import Enumerable from './node_modules/linq/linq.js'
121
- ```
119
+ // whereIf
120
+ Enumerable
121
+ .from([1, 2, 3, 4])
122
+ .whereIf(true, x => x > 2)
123
+ .toArray();
124
+
125
+ // chunk
126
+ Enumerable
127
+ .from([1, 2, 3, 4, 5])
128
+ .chunk(2)
129
+ .toArray();
130
+ // result: [[1, 2], [3, 4], [5]]
131
+
132
+ // withNeighbors
133
+ for (const { prev, item, next } of Enumerable.from([10, 20, 30]).withNeighbors()) {
134
+ }
122
135
 
123
- ## Browser
136
+ // position
137
+ for (const { index, item, isFirst, isLast } of Enumerable.from(["a", "b", "c"]).position()) {
138
+ }
139
+ ```
140
+ ---
124
141
 
125
- The minified version of the library is available in the [release](https://github.com/mihaifm/linq/releases/latest) archive.
142
+ # Migration from linq
126
143
 
127
- Load it via `<script type="module">`:
144
+ Most projects can switch directly:
128
145
 
129
- ```html
130
- <script type="module" src="./linq.min.js"></script>
131
- <script type="module">
132
- import Enumerable from './linq.min.js'
133
- Enumerable.from([1, 2, 3]).forEach(x => console.log(x))
134
- </script>
146
+ ```diff
147
+ -import Enumerable from "linq";
148
+ +import Enumerable from "linqx";
135
149
  ```
136
150
 
137
- You can also load the library via a CDN:
151
+ ---
152
+
153
+ # Repository
138
154
 
139
- | CDN | URL |
140
- | ---------: | :----------------------------------------- |
141
- | unpkg | <https://unpkg.com/linq/> |
142
- | jsDelivr | <https://jsdelivr.com/package/npm/linq> |
143
- | packd | <https://bundle.run/linq@latest?name=linq> |
155
+ https://github.com/huoshan12345/linqx
156
+
157
+ ---
144
158
 
145
159
  # Credits
146
160
 
147
- [Yoshifumi Kawai](https://github.com/neuecc) developed the [original version](https://github.com/neuecc/linq.js/) of this library.
161
+ Based on the original work by Yoshifumi Kawai and later linq maintainers.
162
+
163
+ Independent community fork.
164
+
165
+ ---
148
166
 
149
167
  # License
150
168
 
151
- [MIT License](https://github.com/mihaifm/linq/blob/master/LICENSE)
169
+ MIT
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "author": "huoshan12345",
4
4
  "description": "Modern fork of linq with more APIs",
5
5
  "type": "module",
6
- "version": "0.2.0",
6
+ "version": "0.2.1",
7
7
  "license": "MIT",
8
8
  "homepage": "https://github.com/huoshan12345/linqx",
9
9
  "repository": {