@eyeseetea/d2-logger 0.1.0-beta.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/LICENSE +674 -0
- package/README.md +96 -0
- package/data/api-futures.d.ts +4 -0
- package/data/api-futures.js +21 -0
- package/data/repositories/ConsoleLoggerRepository.d.ts +6 -0
- package/data/repositories/ConsoleLoggerRepository.js +16 -0
- package/data/repositories/ProgramD2Repository.d.ts +7 -0
- package/data/repositories/ProgramD2Repository.js +69 -0
- package/data/repositories/ProgramLoggerD2Repository.d.ts +19 -0
- package/data/repositories/ProgramLoggerD2Repository.js +122 -0
- package/domain/entities/Async.d.ts +1 -0
- package/domain/entities/Async.js +2 -0
- package/domain/entities/Base.d.ts +17 -0
- package/domain/entities/Base.js +15 -0
- package/domain/entities/Log.d.ts +5 -0
- package/domain/entities/Log.js +2 -0
- package/domain/entities/LoggerConfig.d.ts +25 -0
- package/domain/entities/LoggerConfig.js +2 -0
- package/domain/entities/generic/Collection.d.ts +82 -0
- package/domain/entities/generic/Collection.js +287 -0
- package/domain/entities/generic/Either.d.ts +49 -0
- package/domain/entities/generic/Either.js +71 -0
- package/domain/entities/generic/Future.d.ts +45 -0
- package/domain/entities/generic/Future.js +276 -0
- package/domain/entities/generic/HashMap.d.ts +42 -0
- package/domain/entities/generic/HashMap.js +149 -0
- package/domain/entities/generic/Rec.d.ts +30 -0
- package/domain/entities/generic/Rec.js +73 -0
- package/domain/entities/generic/Struct.d.ts +36 -0
- package/domain/entities/generic/Struct.js +51 -0
- package/domain/entities/generic/__tests__/Collection.spec.d.ts +1 -0
- package/domain/entities/generic/__tests__/Collection.spec.js +284 -0
- package/domain/entities/generic/__tests__/Future.spec.d.ts +8 -0
- package/domain/entities/generic/__tests__/Future.spec.js +556 -0
- package/domain/entities/generic/__tests__/HashMap.spec.d.ts +1 -0
- package/domain/entities/generic/__tests__/HashMap.spec.js +170 -0
- package/domain/entities/generic/__tests__/Rec.spec.d.ts +1 -0
- package/domain/entities/generic/__tests__/Rec.spec.js +37 -0
- package/domain/entities/generic/__tests__/Struct.spec.d.ts +1 -0
- package/domain/entities/generic/__tests__/Struct.spec.js +37 -0
- package/domain/repositories/LoggerRepository.d.ts +5 -0
- package/domain/repositories/LoggerRepository.js +2 -0
- package/domain/repositories/ProgramRepository.d.ts +5 -0
- package/domain/repositories/ProgramRepository.js +2 -0
- package/domain/usecases/CheckConfigProgramLoggerUseCase.d.ts +8 -0
- package/domain/usecases/CheckConfigProgramLoggerUseCase.js +13 -0
- package/domain/usecases/LogMessageUseCase.d.ts +8 -0
- package/domain/usecases/LogMessageUseCase.js +17 -0
- package/index.d.ts +17 -0
- package/index.js +124 -0
- package/package.json +64 -0
- package/scripts/cli.d.ts +1 -0
- package/scripts/cli.js +45 -0
- package/scripts/commands/consoleLogger.d.ts +31 -0
- package/scripts/commands/consoleLogger.js +96 -0
- package/scripts/commands/programLogger.d.ts +39 -0
- package/scripts/commands/programLogger.js +130 -0
- package/scripts/common.d.ts +29 -0
- package/scripts/common.js +128 -0
- package/scripts/index.d.ts +1 -0
- package/scripts/index.js +4 -0
- package/types/d2-api.d.ts +4 -0
- package/types/d2-api.js +7 -0
- package/utils/log.d.ts +10 -0
- package/utils/log.js +22 -0
- package/utils/ts-utils.d.ts +31 -0
- package/utils/ts-utils.js +81 -0
|
@@ -0,0 +1,276 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
26
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
27
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
28
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
29
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
30
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
31
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
32
|
+
});
|
|
33
|
+
};
|
|
34
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
35
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
36
|
+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
37
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
38
|
+
function step(op) {
|
|
39
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
40
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
41
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
42
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
43
|
+
switch (op[0]) {
|
|
44
|
+
case 0: case 1: t = op; break;
|
|
45
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
46
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
47
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
48
|
+
default:
|
|
49
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
50
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
51
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
52
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
53
|
+
if (t[2]) _.ops.pop();
|
|
54
|
+
_.trys.pop(); continue;
|
|
55
|
+
}
|
|
56
|
+
op = body.call(thisArg, _);
|
|
57
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
58
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
62
|
+
exports.getJSON = exports.Future = void 0;
|
|
63
|
+
var rcpromise = __importStar(require("real-cancellable-promise"));
|
|
64
|
+
var real_cancellable_promise_1 = require("real-cancellable-promise");
|
|
65
|
+
/**
|
|
66
|
+
* Futures are async values similar to promises, with some differences:
|
|
67
|
+
* - Futures are only executed when their method `run` is called.
|
|
68
|
+
* - Futures are cancellable (thus, they can be easily used in a `React.useEffect`, for example).
|
|
69
|
+
* - Futures have fully typed errors. Subclass Error if you need full stack traces.
|
|
70
|
+
* - You may still use async/await monad-style blocks (check Future.block).
|
|
71
|
+
*
|
|
72
|
+
* More info: https://github.com/EyeSeeTea/know-how/wiki/Async-futures
|
|
73
|
+
*/
|
|
74
|
+
var Future = /** @class */ (function () {
|
|
75
|
+
function Future(_promise) {
|
|
76
|
+
this._promise = _promise;
|
|
77
|
+
}
|
|
78
|
+
Future.success = function (data) {
|
|
79
|
+
return new Future(function () { return rcpromise.CancellablePromise.resolve(data); });
|
|
80
|
+
};
|
|
81
|
+
Future.error = function (error) {
|
|
82
|
+
return new Future(function () { return rcpromise.CancellablePromise.reject(error); });
|
|
83
|
+
};
|
|
84
|
+
Future.fromPromise = function (promise) {
|
|
85
|
+
return Future.fromComputation(function (resolve, reject) {
|
|
86
|
+
promise.then(resolve).catch(function (err) { return reject(err ? err.message : "Unknown error"); });
|
|
87
|
+
return function () { };
|
|
88
|
+
});
|
|
89
|
+
};
|
|
90
|
+
Future.fromComputation = function (computation) {
|
|
91
|
+
var cancel = function () { };
|
|
92
|
+
return new Future(function () {
|
|
93
|
+
var promise = new Promise(function (resolve, reject) {
|
|
94
|
+
cancel = computation(resolve, function (error) { return reject(error); });
|
|
95
|
+
});
|
|
96
|
+
return new rcpromise.CancellablePromise(promise, cancel || (function () { }));
|
|
97
|
+
});
|
|
98
|
+
};
|
|
99
|
+
Future.prototype.run = function (onSuccess, onError) {
|
|
100
|
+
return this._promise().then(onSuccess, function (err) {
|
|
101
|
+
if (err instanceof rcpromise.Cancellation) {
|
|
102
|
+
// no-op
|
|
103
|
+
}
|
|
104
|
+
else {
|
|
105
|
+
onError(err);
|
|
106
|
+
}
|
|
107
|
+
}).cancel;
|
|
108
|
+
};
|
|
109
|
+
Future.prototype.map = function (fn) {
|
|
110
|
+
var _this = this;
|
|
111
|
+
return new Future(function () { return _this._promise().then(fn); });
|
|
112
|
+
};
|
|
113
|
+
Future.prototype.mapError = function (fn) {
|
|
114
|
+
var _this = this;
|
|
115
|
+
return new Future(function () {
|
|
116
|
+
return _this._promise().catch(function (error) {
|
|
117
|
+
throw fn(error);
|
|
118
|
+
});
|
|
119
|
+
});
|
|
120
|
+
};
|
|
121
|
+
Future.prototype.flatMap = function (fn) {
|
|
122
|
+
var _this = this;
|
|
123
|
+
return new Future(function () { return _this._promise().then(function (data) { return fn(data)._promise(); }); });
|
|
124
|
+
};
|
|
125
|
+
Future.prototype.flatMapError = function (fn) {
|
|
126
|
+
var _this = this;
|
|
127
|
+
return new Future(function () { return _this._promise().catch(function (error) { return fn(error)._promise(); }); });
|
|
128
|
+
};
|
|
129
|
+
Future.prototype.chain = function (fn) {
|
|
130
|
+
return this.flatMap(fn);
|
|
131
|
+
};
|
|
132
|
+
Future.prototype.toPromise = function () {
|
|
133
|
+
return this._promise();
|
|
134
|
+
};
|
|
135
|
+
Future.join2 = function (async1, async2) {
|
|
136
|
+
return new Future(function () {
|
|
137
|
+
return rcpromise.CancellablePromise.all([async1._promise(), async2._promise()]);
|
|
138
|
+
});
|
|
139
|
+
};
|
|
140
|
+
Future.joinObj = function (obj, options) {
|
|
141
|
+
if (options === void 0) { options = { concurrency: 1 }; }
|
|
142
|
+
var asyncs = Object.values(obj);
|
|
143
|
+
return Future.parallel(asyncs, options).map(function (values) {
|
|
144
|
+
var keys = Object.keys(obj);
|
|
145
|
+
var pairs = keys.map(function (key, idx) { return [key, values[idx]]; });
|
|
146
|
+
return Object.fromEntries(pairs);
|
|
147
|
+
});
|
|
148
|
+
};
|
|
149
|
+
Future.sequential = function (asyncs) {
|
|
150
|
+
var _this = this;
|
|
151
|
+
return Future.block(function ($) { return __awaiter(_this, void 0, void 0, function () {
|
|
152
|
+
var output, _i, asyncs_1, async, res;
|
|
153
|
+
return __generator(this, function (_a) {
|
|
154
|
+
switch (_a.label) {
|
|
155
|
+
case 0:
|
|
156
|
+
output = [];
|
|
157
|
+
_i = 0, asyncs_1 = asyncs;
|
|
158
|
+
_a.label = 1;
|
|
159
|
+
case 1:
|
|
160
|
+
if (!(_i < asyncs_1.length)) return [3 /*break*/, 4];
|
|
161
|
+
async = asyncs_1[_i];
|
|
162
|
+
return [4 /*yield*/, $(async)];
|
|
163
|
+
case 2:
|
|
164
|
+
res = _a.sent();
|
|
165
|
+
output.push(res);
|
|
166
|
+
_a.label = 3;
|
|
167
|
+
case 3:
|
|
168
|
+
_i++;
|
|
169
|
+
return [3 /*break*/, 1];
|
|
170
|
+
case 4: return [2 /*return*/, output];
|
|
171
|
+
}
|
|
172
|
+
});
|
|
173
|
+
}); });
|
|
174
|
+
};
|
|
175
|
+
Future.parallel = function (asyncs, options) {
|
|
176
|
+
var _this = this;
|
|
177
|
+
return new Future(function () {
|
|
178
|
+
return rcpromise.buildCancellablePromise(function ($) { return __awaiter(_this, void 0, void 0, function () {
|
|
179
|
+
var queue, output, _loop_1, _i, _a, _b, idx, async;
|
|
180
|
+
return __generator(this, function (_c) {
|
|
181
|
+
switch (_c.label) {
|
|
182
|
+
case 0:
|
|
183
|
+
queue = [];
|
|
184
|
+
output = new Array(asyncs.length);
|
|
185
|
+
_loop_1 = function (idx, async) {
|
|
186
|
+
var queueItem$;
|
|
187
|
+
return __generator(this, function (_d) {
|
|
188
|
+
switch (_d.label) {
|
|
189
|
+
case 0:
|
|
190
|
+
queueItem$ = async._promise().then(function (res) {
|
|
191
|
+
queue.splice(queue.indexOf(queueItem$), 1);
|
|
192
|
+
output[idx] = res;
|
|
193
|
+
});
|
|
194
|
+
queue.push(queueItem$);
|
|
195
|
+
if (!(queue.length >= options.concurrency)) return [3 /*break*/, 2];
|
|
196
|
+
return [4 /*yield*/, $(rcpromise.CancellablePromise.race(queue))];
|
|
197
|
+
case 1:
|
|
198
|
+
_d.sent();
|
|
199
|
+
_d.label = 2;
|
|
200
|
+
case 2: return [2 /*return*/];
|
|
201
|
+
}
|
|
202
|
+
});
|
|
203
|
+
};
|
|
204
|
+
_i = 0, _a = asyncs.map(function (async, idx) { return [idx, async]; });
|
|
205
|
+
_c.label = 1;
|
|
206
|
+
case 1:
|
|
207
|
+
if (!(_i < _a.length)) return [3 /*break*/, 4];
|
|
208
|
+
_b = _a[_i], idx = _b[0], async = _b[1];
|
|
209
|
+
return [5 /*yield**/, _loop_1(idx, async)];
|
|
210
|
+
case 2:
|
|
211
|
+
_c.sent();
|
|
212
|
+
_c.label = 3;
|
|
213
|
+
case 3:
|
|
214
|
+
_i++;
|
|
215
|
+
return [3 /*break*/, 1];
|
|
216
|
+
case 4: return [4 /*yield*/, $(rcpromise.CancellablePromise.all(queue))];
|
|
217
|
+
case 5:
|
|
218
|
+
_c.sent();
|
|
219
|
+
return [2 /*return*/, output];
|
|
220
|
+
}
|
|
221
|
+
});
|
|
222
|
+
}); });
|
|
223
|
+
});
|
|
224
|
+
};
|
|
225
|
+
Future.sleep = function (ms) {
|
|
226
|
+
return new Future(function () { return rcpromise.CancellablePromise.delay(ms); }).map(function () { return ms; });
|
|
227
|
+
};
|
|
228
|
+
Future.void = function () {
|
|
229
|
+
return Future.success(undefined);
|
|
230
|
+
};
|
|
231
|
+
Future.block = function (blockFn) {
|
|
232
|
+
return new Future(function () {
|
|
233
|
+
return rcpromise.buildCancellablePromise(function (capturePromise) {
|
|
234
|
+
var captureAsync = function (async) {
|
|
235
|
+
return capturePromise(async._promise());
|
|
236
|
+
};
|
|
237
|
+
captureAsync.throw = function (error) {
|
|
238
|
+
throw error;
|
|
239
|
+
};
|
|
240
|
+
return blockFn(captureAsync);
|
|
241
|
+
});
|
|
242
|
+
});
|
|
243
|
+
};
|
|
244
|
+
Future.block_ = function () {
|
|
245
|
+
return function (blockFn) {
|
|
246
|
+
return Future.block(blockFn);
|
|
247
|
+
};
|
|
248
|
+
};
|
|
249
|
+
return Future;
|
|
250
|
+
}());
|
|
251
|
+
exports.Future = Future;
|
|
252
|
+
function getJSON(url) {
|
|
253
|
+
var abortController = new AbortController();
|
|
254
|
+
return Future.fromComputation(function (resolve, reject) {
|
|
255
|
+
// exceptions: TypeError | DOMException[name=AbortError]
|
|
256
|
+
fetch(url, { method: "get", signal: abortController.signal })
|
|
257
|
+
.then(function (res) { return res.json(); }) // exceptions: SyntaxError
|
|
258
|
+
.then(function (data) { return resolve(data); })
|
|
259
|
+
.catch(function (error) {
|
|
260
|
+
if (isNamedError(error) && error.name === "AbortError") {
|
|
261
|
+
throw new real_cancellable_promise_1.Cancellation();
|
|
262
|
+
}
|
|
263
|
+
else if (error instanceof TypeError || error instanceof SyntaxError) {
|
|
264
|
+
reject(error);
|
|
265
|
+
}
|
|
266
|
+
else {
|
|
267
|
+
reject(new TypeError("Unknown error"));
|
|
268
|
+
}
|
|
269
|
+
});
|
|
270
|
+
return function () { return abortController.abort(); };
|
|
271
|
+
});
|
|
272
|
+
}
|
|
273
|
+
exports.getJSON = getJSON;
|
|
274
|
+
function isNamedError(error) {
|
|
275
|
+
return Boolean(error && typeof error === "object" && "name" in error);
|
|
276
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { Collection } from "./Collection";
|
|
2
|
+
import * as imap from "typed-immutable-map";
|
|
3
|
+
/**
|
|
4
|
+
* Like ES6 Map, but immutable.
|
|
5
|
+
*
|
|
6
|
+
* ```
|
|
7
|
+
* import HashMap from "./HashMap";
|
|
8
|
+
*
|
|
9
|
+
* const map = HashMap.fromObject({ x: 1, y: 2, z: 3 })
|
|
10
|
+
* .invert()
|
|
11
|
+
* .pickBy(([key, value]) => key > 1)
|
|
12
|
+
* .values(); // ["y", "z"]
|
|
13
|
+
*/
|
|
14
|
+
export declare class HashMap<K, V> {
|
|
15
|
+
protected _map: imap.HashMap<K, V>;
|
|
16
|
+
protected constructor(_map: imap.HashMap<K, V>);
|
|
17
|
+
static empty<K, V>(): HashMap<K, V>;
|
|
18
|
+
static fromPairs<K, V>(pairs: Array<[K, V]>): HashMap<K, V>;
|
|
19
|
+
static fromObject<K extends keyof any, V>(obj: Record<K, V>): HashMap<K, V>;
|
|
20
|
+
get(key: K): V | undefined;
|
|
21
|
+
set(key: K, value: V): HashMap<K, V>;
|
|
22
|
+
equals(map: HashMap<K, V>): boolean;
|
|
23
|
+
keys(): K[];
|
|
24
|
+
values(): V[];
|
|
25
|
+
toPairs(): Array<[K, V]>;
|
|
26
|
+
get size(): number;
|
|
27
|
+
pick(keys: K[]): HashMap<K, V>;
|
|
28
|
+
pickBy(pred: (pair: readonly [K, V]) => boolean): HashMap<K, V>;
|
|
29
|
+
omit(keys: K[]): HashMap<K, V>;
|
|
30
|
+
omitBy(pred: (pair: readonly [K, V]) => boolean): HashMap<K, V>;
|
|
31
|
+
toCollection(): Collection<[K, V]>;
|
|
32
|
+
hasKey(key: K): boolean;
|
|
33
|
+
invert(): HashMap<V, K>;
|
|
34
|
+
invertMulti(): HashMap<V, K[]>;
|
|
35
|
+
mapValues<V2>(mapper: (pair: [K, V]) => V2): HashMap<K, V2>;
|
|
36
|
+
mapKeys<K2>(_mapper: (pair: [K, V]) => K2): HashMap<K2, V>;
|
|
37
|
+
merge(other: HashMap<K, V>): HashMap<K, V>;
|
|
38
|
+
forEach(fn: (pair: readonly [K, V]) => void): void;
|
|
39
|
+
toObject(): ToObject<K, V>;
|
|
40
|
+
}
|
|
41
|
+
type ToObject<K, V> = K extends keyof any ? Record<K, V> : Record<string, V>;
|
|
42
|
+
export {};
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
+
exports.HashMap = void 0;
|
|
27
|
+
var Collection_1 = require("./Collection");
|
|
28
|
+
var imap = __importStar(require("typed-immutable-map"));
|
|
29
|
+
/**
|
|
30
|
+
* Like ES6 Map, but immutable.
|
|
31
|
+
*
|
|
32
|
+
* ```
|
|
33
|
+
* import HashMap from "./HashMap";
|
|
34
|
+
*
|
|
35
|
+
* const map = HashMap.fromObject({ x: 1, y: 2, z: 3 })
|
|
36
|
+
* .invert()
|
|
37
|
+
* .pickBy(([key, value]) => key > 1)
|
|
38
|
+
* .values(); // ["y", "z"]
|
|
39
|
+
*/
|
|
40
|
+
/* Immutable Hash Map. Keys and values can be of any type. */
|
|
41
|
+
var HashMap = /** @class */ (function () {
|
|
42
|
+
function HashMap(_map) {
|
|
43
|
+
this._map = _map;
|
|
44
|
+
}
|
|
45
|
+
/* Constructors */
|
|
46
|
+
HashMap.empty = function () {
|
|
47
|
+
return new HashMap(imap.empty());
|
|
48
|
+
};
|
|
49
|
+
HashMap.fromPairs = function (pairs) {
|
|
50
|
+
return new HashMap(imap.fromIterable(pairs));
|
|
51
|
+
};
|
|
52
|
+
HashMap.fromObject = function (obj) {
|
|
53
|
+
return new HashMap(imap.fromObject(obj));
|
|
54
|
+
};
|
|
55
|
+
/* Methods */
|
|
56
|
+
HashMap.prototype.get = function (key) {
|
|
57
|
+
return imap.get(key, this._map);
|
|
58
|
+
};
|
|
59
|
+
HashMap.prototype.set = function (key, value) {
|
|
60
|
+
var updated = imap.set(key, value, this._map);
|
|
61
|
+
return new HashMap(updated);
|
|
62
|
+
};
|
|
63
|
+
HashMap.prototype.equals = function (map) {
|
|
64
|
+
var _this = this;
|
|
65
|
+
var mapsHaveEqualSize = function () { return _this.size === map.size; };
|
|
66
|
+
var allValuesEqual = function () { return _this.keys().every(function (k) { return _this.get(k) === map.get(k); }); };
|
|
67
|
+
return mapsHaveEqualSize() && allValuesEqual();
|
|
68
|
+
};
|
|
69
|
+
HashMap.prototype.keys = function () {
|
|
70
|
+
return Array.from(imap.keys(this._map));
|
|
71
|
+
};
|
|
72
|
+
HashMap.prototype.values = function () {
|
|
73
|
+
return Array.from(imap.values(this._map));
|
|
74
|
+
};
|
|
75
|
+
HashMap.prototype.toPairs = function () {
|
|
76
|
+
return Array.from(imap.entries(this._map));
|
|
77
|
+
};
|
|
78
|
+
Object.defineProperty(HashMap.prototype, "size", {
|
|
79
|
+
get: function () {
|
|
80
|
+
return this._map.size;
|
|
81
|
+
},
|
|
82
|
+
enumerable: false,
|
|
83
|
+
configurable: true
|
|
84
|
+
});
|
|
85
|
+
HashMap.prototype.pick = function (keys) {
|
|
86
|
+
return this.pickBy(function (_a) {
|
|
87
|
+
var key = _a[0], _value = _a[1];
|
|
88
|
+
return keys.includes(key);
|
|
89
|
+
});
|
|
90
|
+
};
|
|
91
|
+
HashMap.prototype.pickBy = function (pred) {
|
|
92
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
93
|
+
return new HashMap(imap.filter(function (value, key) { return pred([key, value]); }, this._map));
|
|
94
|
+
};
|
|
95
|
+
HashMap.prototype.omit = function (keys) {
|
|
96
|
+
return this.pickBy(function (_a) {
|
|
97
|
+
var key = _a[0], _value = _a[1];
|
|
98
|
+
return !keys.includes(key);
|
|
99
|
+
});
|
|
100
|
+
};
|
|
101
|
+
HashMap.prototype.omitBy = function (pred) {
|
|
102
|
+
return this.pickBy(function (_a) {
|
|
103
|
+
var key = _a[0], value = _a[1];
|
|
104
|
+
return !pred([key, value]);
|
|
105
|
+
});
|
|
106
|
+
};
|
|
107
|
+
HashMap.prototype.toCollection = function () {
|
|
108
|
+
return Collection_1.Collection.from(this.toPairs());
|
|
109
|
+
};
|
|
110
|
+
HashMap.prototype.hasKey = function (key) {
|
|
111
|
+
return imap.has(key, this._map);
|
|
112
|
+
};
|
|
113
|
+
HashMap.prototype.invert = function () {
|
|
114
|
+
var invertedPairs = this.toPairs().map(function (_a) {
|
|
115
|
+
var key = _a[0], value = _a[1];
|
|
116
|
+
return [value, key];
|
|
117
|
+
});
|
|
118
|
+
return HashMap.fromPairs(invertedPairs);
|
|
119
|
+
};
|
|
120
|
+
HashMap.prototype.invertMulti = function () {
|
|
121
|
+
return this.toCollection().groupFromMap(function (_a) {
|
|
122
|
+
var key = _a[0], value = _a[1];
|
|
123
|
+
return [value, key];
|
|
124
|
+
});
|
|
125
|
+
};
|
|
126
|
+
HashMap.prototype.mapValues = function (mapper) {
|
|
127
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
128
|
+
return new HashMap(imap.map(function (value, key) { return mapper([key, value]); }, this._map));
|
|
129
|
+
};
|
|
130
|
+
HashMap.prototype.mapKeys = function (_mapper) {
|
|
131
|
+
var pairs = this.toPairs().map(function (_a) {
|
|
132
|
+
var key = _a[0], value = _a[1];
|
|
133
|
+
return [_mapper([key, value]), value];
|
|
134
|
+
});
|
|
135
|
+
return HashMap.fromPairs(pairs);
|
|
136
|
+
};
|
|
137
|
+
HashMap.prototype.merge = function (other) {
|
|
138
|
+
return HashMap.fromPairs(this.toPairs().concat(other.toPairs()));
|
|
139
|
+
};
|
|
140
|
+
HashMap.prototype.forEach = function (fn) {
|
|
141
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
142
|
+
imap.forEach(function (value, key) { return fn([key, value]); }, this._map);
|
|
143
|
+
};
|
|
144
|
+
HashMap.prototype.toObject = function () {
|
|
145
|
+
return imap.toObject(this._map);
|
|
146
|
+
};
|
|
147
|
+
return HashMap;
|
|
148
|
+
}());
|
|
149
|
+
exports.HashMap = HashMap;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Expand methods for Javascript objects. An example:
|
|
3
|
+
*
|
|
4
|
+
* ```
|
|
5
|
+
* import _r from "./Rec";
|
|
6
|
+
*
|
|
7
|
+
* const obj = _r({ x: 1, y: 2, s: "hello" })
|
|
8
|
+
* .pick(["x", "y"])
|
|
9
|
+
* .merge(_r({ z: 3 }))
|
|
10
|
+
* .value(); // { x: 1, y: 2, z: 3}
|
|
11
|
+
* ```
|
|
12
|
+
*/
|
|
13
|
+
export declare class Rec<T extends BaseObj> {
|
|
14
|
+
protected obj: T;
|
|
15
|
+
protected constructor(obj: T);
|
|
16
|
+
static from<T extends BaseObj>(obj: T): Rec<T>;
|
|
17
|
+
keys(): Array<keyof T>;
|
|
18
|
+
values(): Array<T[keyof T]>;
|
|
19
|
+
toObject(): T;
|
|
20
|
+
value(): T;
|
|
21
|
+
pick<K extends keyof T>(keys: K[]): Rec<Pick<T, K>>;
|
|
22
|
+
omit<K extends keyof T>(keys: K[]): Rec<Omit<T, K>>;
|
|
23
|
+
pickBy(filter: (key: keyof T) => boolean): Rec<Partial<T>>;
|
|
24
|
+
omitBy(filter: (key: keyof T) => boolean): Rec<Partial<T>>;
|
|
25
|
+
merge<T2 extends BaseObj>(rec2: Rec<T2>): Rec<Merge<T, T2>>;
|
|
26
|
+
}
|
|
27
|
+
export default function _r<T extends BaseObj>(obj: T): Rec<T>;
|
|
28
|
+
type Merge<T1, T2> = Omit<T1, keyof T2> & T2;
|
|
29
|
+
type BaseObj = Record<string, unknown>;
|
|
30
|
+
export {};
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Expand methods for Javascript objects. An example:
|
|
4
|
+
*
|
|
5
|
+
* ```
|
|
6
|
+
* import _r from "./Rec";
|
|
7
|
+
*
|
|
8
|
+
* const obj = _r({ x: 1, y: 2, s: "hello" })
|
|
9
|
+
* .pick(["x", "y"])
|
|
10
|
+
* .merge(_r({ z: 3 }))
|
|
11
|
+
* .value(); // { x: 1, y: 2, z: 3}
|
|
12
|
+
* ```
|
|
13
|
+
*/
|
|
14
|
+
var __assign = (this && this.__assign) || function () {
|
|
15
|
+
__assign = Object.assign || function(t) {
|
|
16
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
17
|
+
s = arguments[i];
|
|
18
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
19
|
+
t[p] = s[p];
|
|
20
|
+
}
|
|
21
|
+
return t;
|
|
22
|
+
};
|
|
23
|
+
return __assign.apply(this, arguments);
|
|
24
|
+
};
|
|
25
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
+
exports.Rec = void 0;
|
|
27
|
+
var Rec = /** @class */ (function () {
|
|
28
|
+
function Rec(obj) {
|
|
29
|
+
this.obj = obj;
|
|
30
|
+
}
|
|
31
|
+
Rec.from = function (obj) {
|
|
32
|
+
return new Rec(obj);
|
|
33
|
+
};
|
|
34
|
+
Rec.prototype.keys = function () {
|
|
35
|
+
return Object.keys(this.obj);
|
|
36
|
+
};
|
|
37
|
+
Rec.prototype.values = function () {
|
|
38
|
+
return Object.values(this.obj);
|
|
39
|
+
};
|
|
40
|
+
Rec.prototype.toObject = function () {
|
|
41
|
+
return this.obj;
|
|
42
|
+
};
|
|
43
|
+
Rec.prototype.value = function () {
|
|
44
|
+
return this.obj;
|
|
45
|
+
};
|
|
46
|
+
Rec.prototype.pick = function (keys) {
|
|
47
|
+
return this.pickBy(function (key) { return keys.includes(key); });
|
|
48
|
+
};
|
|
49
|
+
Rec.prototype.omit = function (keys) {
|
|
50
|
+
return this.pickBy(function (key) { return !keys.includes(key); });
|
|
51
|
+
};
|
|
52
|
+
Rec.prototype.pickBy = function (filter) {
|
|
53
|
+
var pairs = Object.entries(this.obj);
|
|
54
|
+
var filtered = Object.fromEntries(pairs.filter(function (_a) {
|
|
55
|
+
var k = _a[0], _v = _a[1];
|
|
56
|
+
return filter(k);
|
|
57
|
+
}));
|
|
58
|
+
return new Rec(filtered);
|
|
59
|
+
};
|
|
60
|
+
Rec.prototype.omitBy = function (filter) {
|
|
61
|
+
return this.pickBy(function (key) { return !filter(key); });
|
|
62
|
+
};
|
|
63
|
+
Rec.prototype.merge = function (rec2) {
|
|
64
|
+
var merged = __assign(__assign({}, this.obj), rec2.obj);
|
|
65
|
+
return new Rec(merged);
|
|
66
|
+
};
|
|
67
|
+
return Rec;
|
|
68
|
+
}());
|
|
69
|
+
exports.Rec = Rec;
|
|
70
|
+
function _r(obj) {
|
|
71
|
+
return Rec.from(obj);
|
|
72
|
+
}
|
|
73
|
+
exports.default = _r;
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Base class for typical classes with attributes. Features: create, update.
|
|
3
|
+
*
|
|
4
|
+
* ```
|
|
5
|
+
* class Counter extends Struct<{ id: Id; value: number }>() {
|
|
6
|
+
* add(value: number): Counter {
|
|
7
|
+
* return this._update({ value: this.value + value });
|
|
8
|
+
* }
|
|
9
|
+
* }
|
|
10
|
+
*
|
|
11
|
+
* const counter1 = Counter.create({ id: "some-counter", value: 1 });
|
|
12
|
+
* const counter2 = counter1._update({ value: 2 });
|
|
13
|
+
* ```
|
|
14
|
+
*/
|
|
15
|
+
export declare function Struct<Attrs>(): {
|
|
16
|
+
new (values: Attrs): Attrs & {
|
|
17
|
+
_getAttributes(): Attrs;
|
|
18
|
+
_update(partialAttrs: Partial<Attrs>): this;
|
|
19
|
+
};
|
|
20
|
+
create: <U extends {
|
|
21
|
+
_getAttributes(): Attrs;
|
|
22
|
+
_update(partialAttrs: Partial<Attrs>): this;
|
|
23
|
+
}>(this: new (attrs: Attrs) => U, attrs: Attrs) => U;
|
|
24
|
+
};
|
|
25
|
+
declare const GenericStruct: {
|
|
26
|
+
new (values: unknown): {
|
|
27
|
+
_getAttributes(): unknown;
|
|
28
|
+
_update(partialAttrs: Partial<unknown>): this;
|
|
29
|
+
};
|
|
30
|
+
create: <U extends {
|
|
31
|
+
_getAttributes(): unknown;
|
|
32
|
+
_update(partialAttrs: Partial<unknown>): this;
|
|
33
|
+
}>(this: new (attrs: unknown) => U, attrs: unknown) => U;
|
|
34
|
+
};
|
|
35
|
+
export type GenericStructInstance = InstanceType<typeof GenericStruct>;
|
|
36
|
+
export {};
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Base class for typical classes with attributes. Features: create, update.
|
|
4
|
+
*
|
|
5
|
+
* ```
|
|
6
|
+
* class Counter extends Struct<{ id: Id; value: number }>() {
|
|
7
|
+
* add(value: number): Counter {
|
|
8
|
+
* return this._update({ value: this.value + value });
|
|
9
|
+
* }
|
|
10
|
+
* }
|
|
11
|
+
*
|
|
12
|
+
* const counter1 = Counter.create({ id: "some-counter", value: 1 });
|
|
13
|
+
* const counter2 = counter1._update({ value: 2 });
|
|
14
|
+
* ```
|
|
15
|
+
*/
|
|
16
|
+
var __assign = (this && this.__assign) || function () {
|
|
17
|
+
__assign = Object.assign || function(t) {
|
|
18
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
19
|
+
s = arguments[i];
|
|
20
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
21
|
+
t[p] = s[p];
|
|
22
|
+
}
|
|
23
|
+
return t;
|
|
24
|
+
};
|
|
25
|
+
return __assign.apply(this, arguments);
|
|
26
|
+
};
|
|
27
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
28
|
+
exports.Struct = void 0;
|
|
29
|
+
function Struct() {
|
|
30
|
+
var Base = /** @class */ (function () {
|
|
31
|
+
function Base(_attributes) {
|
|
32
|
+
Object.assign(this, _attributes);
|
|
33
|
+
}
|
|
34
|
+
Base.prototype._getAttributes = function () {
|
|
35
|
+
var _this = this;
|
|
36
|
+
var entries = Object.getOwnPropertyNames(this).map(function (key) { return [key, _this[key]]; });
|
|
37
|
+
return Object.fromEntries(entries);
|
|
38
|
+
};
|
|
39
|
+
Base.prototype._update = function (partialAttrs) {
|
|
40
|
+
var ParentClass = this.constructor;
|
|
41
|
+
return new ParentClass(__assign(__assign({}, this._getAttributes()), partialAttrs));
|
|
42
|
+
};
|
|
43
|
+
Base.create = function (attrs) {
|
|
44
|
+
return new this(attrs);
|
|
45
|
+
};
|
|
46
|
+
return Base;
|
|
47
|
+
}());
|
|
48
|
+
return Base;
|
|
49
|
+
}
|
|
50
|
+
exports.Struct = Struct;
|
|
51
|
+
var GenericStruct = Struct();
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|