linqx 0.1.8

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.
@@ -0,0 +1,36 @@
1
+ declare module './linq.js' {
2
+ type IEnumerable<T> = Enumerable.IEnumerable<T>;
3
+ namespace Enumerable {
4
+ interface IPageInfo {
5
+ pageNumber: number;
6
+ pageSize: number;
7
+ }
8
+ interface IndexedItem<T> {
9
+ index: number;
10
+ item: T;
11
+ }
12
+ interface PositionedItem<T> extends IndexedItem<T> {
13
+ isFirst: boolean;
14
+ isLast: boolean;
15
+ }
16
+ interface ItemWithNeighbors<T> {
17
+ prev: T | null;
18
+ item: T;
19
+ next: T | null;
20
+ }
21
+ const prototype: any;
22
+ interface IEnumerable<T> {
23
+ whereIf(flag: boolean | string | undefined | null, filter: (t: T) => boolean): IEnumerable<T>;
24
+ page(pageNumber: number, pageSize: number): IEnumerable<T>;
25
+ page(info: IPageInfo): IEnumerable<T>;
26
+ joinWith(separator: string): string;
27
+ joinWith<T>(this: IEnumerable<T>, separator: (t: T) => T): IEnumerable<T>;
28
+ toMap<K, V>(keySelector: (element: T) => K, valueSelector: (element: T) => V): Map<K, V>;
29
+ chunk(size: number): IEnumerable<T[]>;
30
+ index(): IEnumerable<IndexedItem<T>>;
31
+ position(): IEnumerable<PositionedItem<T>>;
32
+ withNeighbors(): IEnumerable<ItemWithNeighbors<T>>;
33
+ }
34
+ }
35
+ }
36
+ export {};
package/dist/linqx.js ADDED
@@ -0,0 +1,153 @@
1
+ import { Enumerable } from "./linq.js";
2
+ Enumerable.prototype.whereIf = function (flag, filter) {
3
+ return flag ? this.where(filter) : this;
4
+ };
5
+ function page(...args) {
6
+ let number = 0;
7
+ let size = 0;
8
+ if (args.length === 1) {
9
+ const info = args[0];
10
+ number = info.pageNumber;
11
+ size = info.pageSize;
12
+ }
13
+ else {
14
+ number = args[0];
15
+ size = args[1];
16
+ }
17
+ return this.skip((number - 1) * size).take(size);
18
+ }
19
+ Enumerable.prototype.page = page;
20
+ Enumerable.prototype.map = function (selector) {
21
+ return this.select(selector).toArray();
22
+ };
23
+ function joinWith(separator) {
24
+ if (typeof separator === 'string') {
25
+ return this.toArray().join(separator);
26
+ }
27
+ return Enumerable.from(joinWithIterator(this, separator));
28
+ function* joinWithIterator(enumerable, sep) {
29
+ for (const { index, item, isFirst, isLast } of enumerable.position()) {
30
+ yield item;
31
+ if (!isLast) {
32
+ yield sep(item);
33
+ }
34
+ }
35
+ }
36
+ }
37
+ Enumerable.prototype.joinWith = joinWith;
38
+ Enumerable.prototype.toMap = function (keySelector, valueSelector) {
39
+ const map = new Map();
40
+ for (const m of this) {
41
+ const k = keySelector(m);
42
+ const v = valueSelector(m);
43
+ map.set(k, v);
44
+ }
45
+ return map;
46
+ };
47
+ function* chunkIterator(enumerable, size) {
48
+ const e = enumerable.getEnumerator();
49
+ // Before allocating anything, make sure there's at least one element.
50
+ if (e.moveNext()) {
51
+ // Now that we know we have at least one item, allocate an initial storage array. This is not
52
+ // the array we'll yield. It starts out small in order to avoid significantly overallocating
53
+ // when the source has many fewer elements than the chunk size.
54
+ let arraySize = Math.min(size, 4);
55
+ let i;
56
+ do {
57
+ const array = new Array(arraySize);
58
+ // Store the first item.
59
+ array[0] = e.current();
60
+ i = 1;
61
+ if (size != array.length) {
62
+ // This is the first chunk. As we fill the array, grow it as needed.
63
+ for (; i < size && e.moveNext(); i++) {
64
+ if (i >= array.length) {
65
+ arraySize = Math.min(size, 2 * array.length);
66
+ resize(array, arraySize);
67
+ }
68
+ array[i] = e.current();
69
+ }
70
+ }
71
+ else {
72
+ // For all but the first chunk, the array will already be correctly sized.
73
+ // We can just store into it until either it's full or MoveNext returns false.
74
+ const local = array; // avoid bounds checks by using cached local (`array` is lifted to iterator object as a field)
75
+ for (; i < local.length && e.moveNext(); i++) {
76
+ local[i] = e.current();
77
+ }
78
+ }
79
+ if (i != array.length) {
80
+ resize(array, i);
81
+ }
82
+ yield array;
83
+ } while (i >= size && e.moveNext());
84
+ }
85
+ }
86
+ Enumerable.prototype.chunk = function (size) {
87
+ if (size < 1)
88
+ throw new Error('size cannot be less than 1');
89
+ const e = chunkIterator(this, size);
90
+ return Enumerable.from(e);
91
+ };
92
+ function* positionIterator(enumerable) {
93
+ const e = enumerable.getEnumerator();
94
+ if (!e.moveNext()) {
95
+ return;
96
+ }
97
+ let i = 0;
98
+ let current = e.current();
99
+ while (e.moveNext()) {
100
+ yield {
101
+ index: i,
102
+ item: current,
103
+ isFirst: i === 0,
104
+ isLast: false
105
+ };
106
+ current = e.current();
107
+ ++i;
108
+ }
109
+ yield {
110
+ index: i,
111
+ item: current,
112
+ isFirst: i === 0,
113
+ isLast: true
114
+ };
115
+ }
116
+ Enumerable.prototype.position = function () {
117
+ const e = positionIterator(this);
118
+ return Enumerable.from(e);
119
+ };
120
+ Enumerable.prototype.index = function () {
121
+ const e = positionIterator(this);
122
+ return this.select((m, i) => ({ index: i, item: m }));
123
+ };
124
+ function* withNeighborsIterator(enumerable) {
125
+ const e = enumerable.getEnumerator();
126
+ if (!e.moveNext()) {
127
+ return;
128
+ }
129
+ let previous = null;
130
+ let current = e.current();
131
+ while (e.moveNext()) {
132
+ const next = e.current();
133
+ yield { prev: previous, item: current, next: next };
134
+ previous = current;
135
+ current = next;
136
+ }
137
+ yield { prev: previous, item: current, next: null };
138
+ }
139
+ Enumerable.prototype.withNeighbors = function () {
140
+ const e = withNeighborsIterator(this);
141
+ return Enumerable.from(e);
142
+ };
143
+ function resize(array, newSize) {
144
+ const oldSize = array.length;
145
+ if (newSize > oldSize) {
146
+ array.length = newSize;
147
+ array.fill(undefined, oldSize, newSize);
148
+ }
149
+ else if (newSize < oldSize) {
150
+ array.length = newSize;
151
+ }
152
+ }
153
+ ;
package/package.json ADDED
@@ -0,0 +1,48 @@
1
+ {
2
+ "name": "linqx",
3
+ "author": "huoshan12345",
4
+ "description": "Modern fork of linq with more APIs",
5
+ "type": "module",
6
+ "version": "0.1.8",
7
+ "license": "MIT",
8
+ "homepage": "https://github.com/huoshan12345/linqx",
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "https://github.com/huoshan12345/linqx.git"
12
+ },
13
+ "preferGlobal": false,
14
+ "keywords": [
15
+ "linq",
16
+ "linqx",
17
+ "linqjs",
18
+ "linqts"
19
+ ],
20
+ "engines": {
21
+ "node": "*"
22
+ },
23
+ "main": "dist/index.js",
24
+ "exports": "./dist/index.js",
25
+ "types": "dist/index.d.ts",
26
+ "files": [
27
+ "dist",
28
+ "README.md",
29
+ "LICENSE"
30
+ ],
31
+ "devDependencies": {
32
+ "@types/node": "^25.6.0",
33
+ "npm-run-all2": "^8.0.4",
34
+ "shx": "^0.4.0",
35
+ "typescript": "^6.0.3",
36
+ "vitest": "^4.1.4"
37
+ },
38
+ "scripts": {
39
+ "preinstall": "npx only-allow pnpm",
40
+ "clean": "shx rm -rf dist/** && shx rm -f *.tgz",
41
+ "copy": "shx cp linq.js dist && shx cp linq.d.ts dist",
42
+ "build": "run-s clean && tsc && run-s copy",
43
+ "type-check": "tsc --noEmit",
44
+ "test:ts": "run-s type-check && vitest run",
45
+ "test:js": "node test/testrunner.js",
46
+ "test": "run-p test:ts test:js"
47
+ }
48
+ }