inqjs 1.0.0 → 1.0.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.
- package/README.md +4 -0
- package/dist/{src/Query.js → Query.js} +35 -0
- package/package.json +3 -3
- package/dist/src/async/AsyncQuery.js +0 -107
- package/dist/src/async/operators/allAsync.js +0 -11
- package/dist/src/async/operators/anyAsync.js +0 -17
- package/dist/src/async/operators/countAsync.js +0 -12
- package/dist/src/async/operators/distinctAsync.js +0 -13
- package/dist/src/async/operators/firstAsync.js +0 -20
- package/dist/src/async/operators/maxAsync.js +0 -16
- package/dist/src/async/operators/minAsync.js +0 -16
- package/dist/src/async/operators/orderByAsync.js +0 -18
- package/dist/src/async/operators/selectAsync.js +0 -9
- package/dist/src/async/operators/skipAsync.js +0 -13
- package/dist/src/async/operators/sumAsync.js +0 -14
- package/dist/src/async/operators/takeAsync.js +0 -12
- package/dist/src/async/operators/toArrayAsync.js +0 -10
- package/dist/src/async/operators/whereAsync.js +0 -11
- package/dist/src/json/fromJson.js +0 -48
- package/dist/src/operators/all.js +0 -11
- package/dist/src/operators/any.js +0 -16
- package/dist/src/operators/count.js +0 -12
- package/dist/src/operators/distinct.js +0 -13
- package/dist/src/operators/first.js +0 -20
- package/dist/src/operators/max.js +0 -16
- package/dist/src/operators/min.js +0 -16
- package/dist/src/operators/orderBy.js +0 -14
- package/dist/src/operators/select.js +0 -9
- package/dist/src/operators/skip.js +0 -13
- package/dist/src/operators/sum.js +0 -14
- package/dist/src/operators/take.js +0 -12
- package/dist/src/operators/toArray.js +0 -6
- package/dist/src/operators/where.js +0 -11
- package/dist/src/utils/comparers.js +0 -20
- package/dist/src/utils/guards.js +0 -34
package/README.md
CHANGED
|
@@ -1,5 +1,9 @@
|
|
|
1
1
|
# inqjs (Integrated Query for JavaScript)
|
|
2
2
|
|
|
3
|
+
[](https://www.npmjs.com/package/inqjs)
|
|
4
|
+
[](https://www.npmjs.com/package/inqjs)
|
|
5
|
+
[](https://github.com/ptp28/inqjs/blob/main/LICENSE)
|
|
6
|
+
|
|
3
7
|
A lightweight, dependency-free LINQ engine for TypeScript and JavaScript with comprehensive query capabilities.
|
|
4
8
|
|
|
5
9
|
> **Note**: This project was developed with heavy assistance from AI (Google Gemini), demonstrating modern AI-powered software development workflows.
|
|
@@ -18,6 +18,12 @@ const first_1 = require("./operators/first");
|
|
|
18
18
|
const toArray_1 = require("./operators/toArray");
|
|
19
19
|
const take_1 = require("./operators/take");
|
|
20
20
|
const count_1 = require("./operators/count");
|
|
21
|
+
const append_1 = require("./operators/append");
|
|
22
|
+
const prepend_1 = require("./operators/prepend");
|
|
23
|
+
const concat_1 = require("./operators/concat");
|
|
24
|
+
const union_1 = require("./operators/union");
|
|
25
|
+
const intersect_1 = require("./operators/intersect");
|
|
26
|
+
const except_1 = require("./operators/except");
|
|
21
27
|
const AsyncQuery_1 = require("./async/AsyncQuery");
|
|
22
28
|
class Query {
|
|
23
29
|
constructor(source) {
|
|
@@ -100,6 +106,34 @@ class Query {
|
|
|
100
106
|
(0, guards_1.assertFunction)(predicate, 'predicate');
|
|
101
107
|
return (0, count_1.count)(this._source, predicate);
|
|
102
108
|
}
|
|
109
|
+
append(element) {
|
|
110
|
+
return new Query((0, append_1.append)(this._source, element));
|
|
111
|
+
}
|
|
112
|
+
prepend(element) {
|
|
113
|
+
return new Query((0, prepend_1.prepend)(this._source, element));
|
|
114
|
+
}
|
|
115
|
+
concat(other) {
|
|
116
|
+
(0, guards_1.assertIterable)(other, 'other');
|
|
117
|
+
return new Query((0, concat_1.concat)(this._source, other));
|
|
118
|
+
}
|
|
119
|
+
union(other, keySelector = comparers_1.identity) {
|
|
120
|
+
(0, guards_1.assertIterable)(other, 'other');
|
|
121
|
+
if (keySelector !== undefined)
|
|
122
|
+
(0, guards_1.assertFunction)(keySelector, 'keySelector');
|
|
123
|
+
return new Query((0, union_1.union)(this._source, other, keySelector));
|
|
124
|
+
}
|
|
125
|
+
intersect(other, keySelector = comparers_1.identity) {
|
|
126
|
+
(0, guards_1.assertIterable)(other, 'other');
|
|
127
|
+
if (keySelector !== undefined)
|
|
128
|
+
(0, guards_1.assertFunction)(keySelector, 'keySelector');
|
|
129
|
+
return new Query((0, intersect_1.intersect)(this._source, other, keySelector));
|
|
130
|
+
}
|
|
131
|
+
except(other, keySelector = comparers_1.identity) {
|
|
132
|
+
(0, guards_1.assertIterable)(other, 'other');
|
|
133
|
+
if (keySelector !== undefined)
|
|
134
|
+
(0, guards_1.assertFunction)(keySelector, 'keySelector');
|
|
135
|
+
return new Query((0, except_1.except)(this._source, other, keySelector));
|
|
136
|
+
}
|
|
103
137
|
toAsync() {
|
|
104
138
|
return AsyncQuery_1.AsyncQuery.from(this._source);
|
|
105
139
|
}
|
|
@@ -113,3 +147,4 @@ var fromJson_1 = require("./json/fromJson");
|
|
|
113
147
|
Object.defineProperty(exports, "fromJson", { enumerable: true, get: function () { return fromJson_1.fromJson; } });
|
|
114
148
|
Object.defineProperty(exports, "fromJsonArray", { enumerable: true, get: function () { return fromJson_1.fromJsonArray; } });
|
|
115
149
|
Object.defineProperty(exports, "fromJsonObject", { enumerable: true, get: function () { return fromJson_1.fromJsonObject; } });
|
|
150
|
+
//# sourceMappingURL=Query.js.map
|
package/package.json
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "inqjs",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.1",
|
|
4
4
|
"description": "A lightweight, dependency-free LINQ engine for TypeScript and JavaScript with comprehensive query capabilities",
|
|
5
|
-
"main": "dist/
|
|
6
|
-
"types": "dist/
|
|
5
|
+
"main": "dist/Query.js",
|
|
6
|
+
"types": "dist/Query.d.ts",
|
|
7
7
|
"files": [
|
|
8
8
|
"dist/src/**/*",
|
|
9
9
|
"README.md",
|
|
@@ -1,107 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.AsyncQuery = void 0;
|
|
4
|
-
exports.fromAsync = fromAsync;
|
|
5
|
-
const guards_1 = require("../utils/guards");
|
|
6
|
-
const comparers_1 = require("../utils/comparers");
|
|
7
|
-
const whereAsync_1 = require("./operators/whereAsync");
|
|
8
|
-
const selectAsync_1 = require("./operators/selectAsync");
|
|
9
|
-
const orderByAsync_1 = require("./operators/orderByAsync");
|
|
10
|
-
const skipAsync_1 = require("./operators/skipAsync");
|
|
11
|
-
const distinctAsync_1 = require("./operators/distinctAsync");
|
|
12
|
-
const anyAsync_1 = require("./operators/anyAsync");
|
|
13
|
-
const allAsync_1 = require("./operators/allAsync");
|
|
14
|
-
const sumAsync_1 = require("./operators/sumAsync");
|
|
15
|
-
const minAsync_1 = require("./operators/minAsync");
|
|
16
|
-
const maxAsync_1 = require("./operators/maxAsync");
|
|
17
|
-
const firstAsync_1 = require("./operators/firstAsync");
|
|
18
|
-
const toArrayAsync_1 = require("./operators/toArrayAsync");
|
|
19
|
-
const takeAsync_1 = require("./operators/takeAsync");
|
|
20
|
-
const countAsync_1 = require("./operators/countAsync");
|
|
21
|
-
class AsyncQuery {
|
|
22
|
-
constructor(source) {
|
|
23
|
-
this._source = source;
|
|
24
|
-
}
|
|
25
|
-
[Symbol.asyncIterator]() {
|
|
26
|
-
return this._source[Symbol.asyncIterator]();
|
|
27
|
-
}
|
|
28
|
-
static from(source) {
|
|
29
|
-
async function* wrap(src) {
|
|
30
|
-
for await (const item of src) {
|
|
31
|
-
yield item;
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
return new AsyncQuery(wrap(source));
|
|
35
|
-
}
|
|
36
|
-
where(predicate) {
|
|
37
|
-
(0, guards_1.assertFunction)(predicate, 'predicate');
|
|
38
|
-
return new AsyncQuery((0, whereAsync_1.whereAsync)(this._source, predicate));
|
|
39
|
-
}
|
|
40
|
-
select(selector) {
|
|
41
|
-
(0, guards_1.assertFunction)(selector, 'selector');
|
|
42
|
-
return new AsyncQuery((0, selectAsync_1.selectAsync)(this._source, selector));
|
|
43
|
-
}
|
|
44
|
-
orderBy(keySelector = comparers_1.identity, comparer = comparers_1.defaultComparer) {
|
|
45
|
-
if (keySelector !== undefined)
|
|
46
|
-
(0, guards_1.assertFunction)(keySelector, 'keySelector');
|
|
47
|
-
if (comparer !== undefined)
|
|
48
|
-
(0, guards_1.assertFunction)(comparer, 'comparer');
|
|
49
|
-
return new AsyncQuery((0, orderByAsync_1.orderByAsync)(this._source, keySelector, comparer));
|
|
50
|
-
}
|
|
51
|
-
skip(count) {
|
|
52
|
-
(0, guards_1.assertInteger)(count, 'count');
|
|
53
|
-
(0, guards_1.assertNonNegative)(count, 'count');
|
|
54
|
-
return new AsyncQuery((0, skipAsync_1.skipAsync)(this._source, count));
|
|
55
|
-
}
|
|
56
|
-
distinct(keySelector = comparers_1.identity) {
|
|
57
|
-
if (keySelector !== undefined)
|
|
58
|
-
(0, guards_1.assertFunction)(keySelector, 'keySelector');
|
|
59
|
-
return new AsyncQuery((0, distinctAsync_1.distinctAsync)(this._source, keySelector));
|
|
60
|
-
}
|
|
61
|
-
any(predicate) {
|
|
62
|
-
if (predicate !== undefined)
|
|
63
|
-
(0, guards_1.assertFunction)(predicate, 'predicate');
|
|
64
|
-
return (0, anyAsync_1.anyAsync)(this._source, predicate);
|
|
65
|
-
}
|
|
66
|
-
all(predicate) {
|
|
67
|
-
(0, guards_1.assertFunction)(predicate, 'predicate');
|
|
68
|
-
return (0, allAsync_1.allAsync)(this._source, predicate);
|
|
69
|
-
}
|
|
70
|
-
sum(selector = comparers_1.identity) {
|
|
71
|
-
if (selector !== undefined)
|
|
72
|
-
(0, guards_1.assertFunction)(selector, 'selector');
|
|
73
|
-
return (0, sumAsync_1.sumAsync)(this._source, selector);
|
|
74
|
-
}
|
|
75
|
-
min(selector = comparers_1.identity) {
|
|
76
|
-
if (selector !== undefined)
|
|
77
|
-
(0, guards_1.assertFunction)(selector, 'selector');
|
|
78
|
-
return (0, minAsync_1.minAsync)(this._source, selector);
|
|
79
|
-
}
|
|
80
|
-
max(selector = comparers_1.identity) {
|
|
81
|
-
if (selector !== undefined)
|
|
82
|
-
(0, guards_1.assertFunction)(selector, 'selector');
|
|
83
|
-
return (0, maxAsync_1.maxAsync)(this._source, selector);
|
|
84
|
-
}
|
|
85
|
-
first(predicate) {
|
|
86
|
-
if (predicate !== undefined)
|
|
87
|
-
(0, guards_1.assertFunction)(predicate, 'predicate');
|
|
88
|
-
return (0, firstAsync_1.firstAsync)(this._source, predicate);
|
|
89
|
-
}
|
|
90
|
-
toArray() {
|
|
91
|
-
return (0, toArrayAsync_1.toArrayAsync)(this._source);
|
|
92
|
-
}
|
|
93
|
-
take(count) {
|
|
94
|
-
(0, guards_1.assertInteger)(count, 'count');
|
|
95
|
-
(0, guards_1.assertNonNegative)(count, 'count');
|
|
96
|
-
return new AsyncQuery((0, takeAsync_1.takeAsync)(this._source, count));
|
|
97
|
-
}
|
|
98
|
-
count(predicate) {
|
|
99
|
-
if (predicate !== undefined)
|
|
100
|
-
(0, guards_1.assertFunction)(predicate, 'predicate');
|
|
101
|
-
return (0, countAsync_1.countAsync)(this._source, predicate);
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
exports.AsyncQuery = AsyncQuery;
|
|
105
|
-
function fromAsync(source) {
|
|
106
|
-
return AsyncQuery.from(source);
|
|
107
|
-
}
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.allAsync = allAsync;
|
|
4
|
-
async function allAsync(source, predicate) {
|
|
5
|
-
for await (const item of source) {
|
|
6
|
-
if (!(await predicate(item))) {
|
|
7
|
-
return false;
|
|
8
|
-
}
|
|
9
|
-
}
|
|
10
|
-
return true;
|
|
11
|
-
}
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.anyAsync = anyAsync;
|
|
4
|
-
async function anyAsync(source, predicate) {
|
|
5
|
-
if (predicate) {
|
|
6
|
-
for await (const item of source) {
|
|
7
|
-
if (await predicate(item)) {
|
|
8
|
-
return true;
|
|
9
|
-
}
|
|
10
|
-
}
|
|
11
|
-
return false;
|
|
12
|
-
}
|
|
13
|
-
// If no predicate, check if source has any elements
|
|
14
|
-
const iterator = source[Symbol.asyncIterator]();
|
|
15
|
-
const result = await iterator.next();
|
|
16
|
-
return !result.done;
|
|
17
|
-
}
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.countAsync = countAsync;
|
|
4
|
-
async function countAsync(source, predicate) {
|
|
5
|
-
let count = 0;
|
|
6
|
-
for await (const item of source) {
|
|
7
|
-
if (!predicate || await predicate(item)) {
|
|
8
|
-
count++;
|
|
9
|
-
}
|
|
10
|
-
}
|
|
11
|
-
return count;
|
|
12
|
-
}
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.distinctAsync = distinctAsync;
|
|
4
|
-
async function* distinctAsync(source, keySelector) {
|
|
5
|
-
const seen = new Set();
|
|
6
|
-
for await (const item of source) {
|
|
7
|
-
const key = await keySelector(item);
|
|
8
|
-
if (!seen.has(key)) {
|
|
9
|
-
seen.add(key);
|
|
10
|
-
yield item;
|
|
11
|
-
}
|
|
12
|
-
}
|
|
13
|
-
}
|
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.firstAsync = firstAsync;
|
|
4
|
-
async function firstAsync(source, predicate) {
|
|
5
|
-
if (predicate) {
|
|
6
|
-
for await (const item of source) {
|
|
7
|
-
if (await predicate(item)) {
|
|
8
|
-
return item;
|
|
9
|
-
}
|
|
10
|
-
}
|
|
11
|
-
}
|
|
12
|
-
else {
|
|
13
|
-
const iterator = source[Symbol.asyncIterator]();
|
|
14
|
-
const result = await iterator.next();
|
|
15
|
-
if (!result.done) {
|
|
16
|
-
return result.value;
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
throw new Error('Sequence contains no matching element.');
|
|
20
|
-
}
|
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.maxAsync = maxAsync;
|
|
4
|
-
async function maxAsync(source, selector) {
|
|
5
|
-
let maxValue;
|
|
6
|
-
for await (const item of source) {
|
|
7
|
-
const value = await selector(item);
|
|
8
|
-
if (typeof value !== 'number' || !Number.isFinite(value)) {
|
|
9
|
-
throw new TypeError('maxAsync: value must be a finite number.');
|
|
10
|
-
}
|
|
11
|
-
if (maxValue === undefined || value > maxValue) {
|
|
12
|
-
maxValue = value;
|
|
13
|
-
}
|
|
14
|
-
}
|
|
15
|
-
return maxValue;
|
|
16
|
-
}
|
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.minAsync = minAsync;
|
|
4
|
-
async function minAsync(source, selector) {
|
|
5
|
-
let minValue;
|
|
6
|
-
for await (const item of source) {
|
|
7
|
-
const value = await selector(item);
|
|
8
|
-
if (typeof value !== 'number' || !Number.isFinite(value)) {
|
|
9
|
-
throw new TypeError('minAsync: value must be a finite number.');
|
|
10
|
-
}
|
|
11
|
-
if (minValue === undefined || value < minValue) {
|
|
12
|
-
minValue = value;
|
|
13
|
-
}
|
|
14
|
-
}
|
|
15
|
-
return minValue;
|
|
16
|
-
}
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.orderByAsync = orderByAsync;
|
|
4
|
-
async function* orderByAsync(source, keySelector, comparer) {
|
|
5
|
-
const buffer = [];
|
|
6
|
-
for await (const item of source) {
|
|
7
|
-
buffer.push(item);
|
|
8
|
-
}
|
|
9
|
-
// We need to resolve keys for all items to sort them
|
|
10
|
-
const itemsWithKeys = await Promise.all(buffer.map(async (item) => ({
|
|
11
|
-
item,
|
|
12
|
-
key: await keySelector(item),
|
|
13
|
-
})));
|
|
14
|
-
itemsWithKeys.sort((a, b) => comparer(a.key, b.key));
|
|
15
|
-
for (const { item } of itemsWithKeys) {
|
|
16
|
-
yield item;
|
|
17
|
-
}
|
|
18
|
-
}
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.selectAsync = selectAsync;
|
|
4
|
-
async function* selectAsync(source, selector) {
|
|
5
|
-
let index = 0;
|
|
6
|
-
for await (const item of source) {
|
|
7
|
-
yield await selector(item, index++);
|
|
8
|
-
}
|
|
9
|
-
}
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.skipAsync = skipAsync;
|
|
4
|
-
async function* skipAsync(source, count) {
|
|
5
|
-
let skipped = 0;
|
|
6
|
-
for await (const item of source) {
|
|
7
|
-
if (skipped < count) {
|
|
8
|
-
skipped++;
|
|
9
|
-
continue;
|
|
10
|
-
}
|
|
11
|
-
yield item;
|
|
12
|
-
}
|
|
13
|
-
}
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.sumAsync = sumAsync;
|
|
4
|
-
async function sumAsync(source, selector) {
|
|
5
|
-
let total = 0;
|
|
6
|
-
for await (const item of source) {
|
|
7
|
-
const value = await selector(item);
|
|
8
|
-
if (typeof value !== 'number' || !Number.isFinite(value)) {
|
|
9
|
-
throw new TypeError('sumAsync: value must be a finite number.');
|
|
10
|
-
}
|
|
11
|
-
total += value;
|
|
12
|
-
}
|
|
13
|
-
return total;
|
|
14
|
-
}
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.takeAsync = takeAsync;
|
|
4
|
-
async function* takeAsync(source, count) {
|
|
5
|
-
let taken = 0;
|
|
6
|
-
for await (const item of source) {
|
|
7
|
-
if (taken >= count)
|
|
8
|
-
break;
|
|
9
|
-
yield item;
|
|
10
|
-
taken++;
|
|
11
|
-
}
|
|
12
|
-
}
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.toArrayAsync = toArrayAsync;
|
|
4
|
-
async function toArrayAsync(source) {
|
|
5
|
-
const result = [];
|
|
6
|
-
for await (const item of source) {
|
|
7
|
-
result.push(item);
|
|
8
|
-
}
|
|
9
|
-
return result;
|
|
10
|
-
}
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.whereAsync = whereAsync;
|
|
4
|
-
async function* whereAsync(source, predicate) {
|
|
5
|
-
let index = 0;
|
|
6
|
-
for await (const item of source) {
|
|
7
|
-
if (await predicate(item, index++)) {
|
|
8
|
-
yield item;
|
|
9
|
-
}
|
|
10
|
-
}
|
|
11
|
-
}
|
|
@@ -1,48 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.fromJson = fromJson;
|
|
4
|
-
exports.fromJsonArray = fromJsonArray;
|
|
5
|
-
exports.fromJsonObject = fromJsonObject;
|
|
6
|
-
const Query_1 = require("../Query");
|
|
7
|
-
/**
|
|
8
|
-
* Parse a JSON string and return a Query over the result.
|
|
9
|
-
* - If the JSON is an array, returns a Query over the array elements.
|
|
10
|
-
* - If the JSON is an object, returns a Query over the object's entries as [key, value] tuples.
|
|
11
|
-
* - For primitive values, wraps them in a single-element array.
|
|
12
|
-
*/
|
|
13
|
-
function fromJson(jsonString) {
|
|
14
|
-
const parsed = JSON.parse(jsonString);
|
|
15
|
-
if (Array.isArray(parsed)) {
|
|
16
|
-
return Query_1.Query.from(parsed);
|
|
17
|
-
}
|
|
18
|
-
else if (parsed !== null && typeof parsed === 'object') {
|
|
19
|
-
return Query_1.Query.from(Object.entries(parsed));
|
|
20
|
-
}
|
|
21
|
-
else {
|
|
22
|
-
// Primitive value - wrap in array
|
|
23
|
-
return Query_1.Query.from([parsed]);
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
/**
|
|
27
|
-
* Parse a JSON string as an array and return a Query over its elements.
|
|
28
|
-
* Throws if the JSON is not an array.
|
|
29
|
-
*/
|
|
30
|
-
function fromJsonArray(jsonString) {
|
|
31
|
-
const parsed = JSON.parse(jsonString);
|
|
32
|
-
if (!Array.isArray(parsed)) {
|
|
33
|
-
throw new TypeError('JSON value is not an array');
|
|
34
|
-
}
|
|
35
|
-
return Query_1.Query.from(parsed);
|
|
36
|
-
}
|
|
37
|
-
/**
|
|
38
|
-
* Parse a JSON string as an object and return a Query over its entries.
|
|
39
|
-
* Each entry is a [key, value] tuple.
|
|
40
|
-
* Throws if the JSON is not an object.
|
|
41
|
-
*/
|
|
42
|
-
function fromJsonObject(jsonString) {
|
|
43
|
-
const parsed = JSON.parse(jsonString);
|
|
44
|
-
if (parsed === null || typeof parsed !== 'object' || Array.isArray(parsed)) {
|
|
45
|
-
throw new TypeError('JSON value is not an object');
|
|
46
|
-
}
|
|
47
|
-
return Query_1.Query.from(Object.entries(parsed));
|
|
48
|
-
}
|
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.any = any;
|
|
4
|
-
function any(source, predicate) {
|
|
5
|
-
if (predicate) {
|
|
6
|
-
for (const item of source) {
|
|
7
|
-
if (predicate(item)) {
|
|
8
|
-
return true;
|
|
9
|
-
}
|
|
10
|
-
}
|
|
11
|
-
return false;
|
|
12
|
-
}
|
|
13
|
-
// If no predicate, check if source has any elements
|
|
14
|
-
const iterator = source[Symbol.iterator]();
|
|
15
|
-
return !iterator.next().done;
|
|
16
|
-
}
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.count = count;
|
|
4
|
-
function count(source, predicate) {
|
|
5
|
-
let count = 0;
|
|
6
|
-
for (const item of source) {
|
|
7
|
-
if (!predicate || predicate(item)) {
|
|
8
|
-
count++;
|
|
9
|
-
}
|
|
10
|
-
}
|
|
11
|
-
return count;
|
|
12
|
-
}
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.distinct = distinct;
|
|
4
|
-
function* distinct(source, keySelector) {
|
|
5
|
-
const seen = new Set();
|
|
6
|
-
for (const item of source) {
|
|
7
|
-
const key = keySelector(item);
|
|
8
|
-
if (!seen.has(key)) {
|
|
9
|
-
seen.add(key);
|
|
10
|
-
yield item;
|
|
11
|
-
}
|
|
12
|
-
}
|
|
13
|
-
}
|
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.first = first;
|
|
4
|
-
function first(source, predicate) {
|
|
5
|
-
if (predicate) {
|
|
6
|
-
for (const item of source) {
|
|
7
|
-
if (predicate(item)) {
|
|
8
|
-
return item;
|
|
9
|
-
}
|
|
10
|
-
}
|
|
11
|
-
}
|
|
12
|
-
else {
|
|
13
|
-
const iterator = source[Symbol.iterator]();
|
|
14
|
-
const result = iterator.next();
|
|
15
|
-
if (!result.done) {
|
|
16
|
-
return result.value;
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
throw new Error('Sequence contains no matching element.');
|
|
20
|
-
}
|
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.max = max;
|
|
4
|
-
function max(source, selector) {
|
|
5
|
-
let maxValue;
|
|
6
|
-
for (const item of source) {
|
|
7
|
-
const value = selector(item);
|
|
8
|
-
if (typeof value !== 'number' || !Number.isFinite(value)) {
|
|
9
|
-
throw new TypeError('max: value must be a finite number.');
|
|
10
|
-
}
|
|
11
|
-
if (maxValue === undefined || value > maxValue) {
|
|
12
|
-
maxValue = value;
|
|
13
|
-
}
|
|
14
|
-
}
|
|
15
|
-
return maxValue;
|
|
16
|
-
}
|
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.min = min;
|
|
4
|
-
function min(source, selector) {
|
|
5
|
-
let minValue;
|
|
6
|
-
for (const item of source) {
|
|
7
|
-
const value = selector(item);
|
|
8
|
-
if (typeof value !== 'number' || !Number.isFinite(value)) {
|
|
9
|
-
throw new TypeError('min: value must be a finite number.');
|
|
10
|
-
}
|
|
11
|
-
if (minValue === undefined || value < minValue) {
|
|
12
|
-
minValue = value;
|
|
13
|
-
}
|
|
14
|
-
}
|
|
15
|
-
return minValue;
|
|
16
|
-
}
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.orderBy = orderBy;
|
|
4
|
-
function* orderBy(source, keySelector, comparer) {
|
|
5
|
-
const buffer = Array.from(source);
|
|
6
|
-
buffer.sort((a, b) => {
|
|
7
|
-
const keyA = keySelector(a);
|
|
8
|
-
const keyB = keySelector(b);
|
|
9
|
-
return comparer(keyA, keyB);
|
|
10
|
-
});
|
|
11
|
-
for (const item of buffer) {
|
|
12
|
-
yield item;
|
|
13
|
-
}
|
|
14
|
-
}
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.skip = skip;
|
|
4
|
-
function* skip(source, count) {
|
|
5
|
-
let skipped = 0;
|
|
6
|
-
for (const item of source) {
|
|
7
|
-
if (skipped < count) {
|
|
8
|
-
skipped++;
|
|
9
|
-
continue;
|
|
10
|
-
}
|
|
11
|
-
yield item;
|
|
12
|
-
}
|
|
13
|
-
}
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.sum = sum;
|
|
4
|
-
function sum(source, selector) {
|
|
5
|
-
let total = 0;
|
|
6
|
-
for (const item of source) {
|
|
7
|
-
const value = selector(item);
|
|
8
|
-
if (typeof value !== 'number' || !Number.isFinite(value)) {
|
|
9
|
-
throw new TypeError('sum: value must be a finite number.');
|
|
10
|
-
}
|
|
11
|
-
total += value;
|
|
12
|
-
}
|
|
13
|
-
return total;
|
|
14
|
-
}
|
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.defaultComparer = defaultComparer;
|
|
4
|
-
exports.identity = identity;
|
|
5
|
-
function defaultComparer(a, b) {
|
|
6
|
-
if (a === b)
|
|
7
|
-
return 0;
|
|
8
|
-
if (a === null || a === undefined)
|
|
9
|
-
return -1;
|
|
10
|
-
if (b === null || b === undefined)
|
|
11
|
-
return 1;
|
|
12
|
-
if (a < b)
|
|
13
|
-
return -1;
|
|
14
|
-
if (a > b)
|
|
15
|
-
return 1;
|
|
16
|
-
return 0;
|
|
17
|
-
}
|
|
18
|
-
function identity(item) {
|
|
19
|
-
return item;
|
|
20
|
-
}
|
package/dist/src/utils/guards.js
DELETED
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.isIterable = isIterable;
|
|
4
|
-
exports.isFunction = isFunction;
|
|
5
|
-
exports.assertIterable = assertIterable;
|
|
6
|
-
exports.assertFunction = assertFunction;
|
|
7
|
-
exports.assertInteger = assertInteger;
|
|
8
|
-
exports.assertNonNegative = assertNonNegative;
|
|
9
|
-
function isIterable(obj) {
|
|
10
|
-
return obj != null && typeof obj[Symbol.iterator] === 'function';
|
|
11
|
-
}
|
|
12
|
-
function isFunction(obj) {
|
|
13
|
-
return typeof obj === 'function';
|
|
14
|
-
}
|
|
15
|
-
function assertIterable(source, name = 'source') {
|
|
16
|
-
if (!isIterable(source)) {
|
|
17
|
-
throw new TypeError(`${name} must be an iterable.`);
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
function assertFunction(fn, name) {
|
|
21
|
-
if (!isFunction(fn)) {
|
|
22
|
-
throw new TypeError(`${name} must be a function.`);
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
function assertInteger(num, name) {
|
|
26
|
-
if (typeof num !== 'number' || !Number.isFinite(num) || !Number.isInteger(num)) {
|
|
27
|
-
throw new TypeError(`${name} must be a finite integer.`);
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
function assertNonNegative(num, name) {
|
|
31
|
-
if (num < 0) {
|
|
32
|
-
throw new TypeError(`${name} must be non-negative.`);
|
|
33
|
-
}
|
|
34
|
-
}
|