@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.
Files changed (67) hide show
  1. package/LICENSE +674 -0
  2. package/README.md +96 -0
  3. package/data/api-futures.d.ts +4 -0
  4. package/data/api-futures.js +21 -0
  5. package/data/repositories/ConsoleLoggerRepository.d.ts +6 -0
  6. package/data/repositories/ConsoleLoggerRepository.js +16 -0
  7. package/data/repositories/ProgramD2Repository.d.ts +7 -0
  8. package/data/repositories/ProgramD2Repository.js +69 -0
  9. package/data/repositories/ProgramLoggerD2Repository.d.ts +19 -0
  10. package/data/repositories/ProgramLoggerD2Repository.js +122 -0
  11. package/domain/entities/Async.d.ts +1 -0
  12. package/domain/entities/Async.js +2 -0
  13. package/domain/entities/Base.d.ts +17 -0
  14. package/domain/entities/Base.js +15 -0
  15. package/domain/entities/Log.d.ts +5 -0
  16. package/domain/entities/Log.js +2 -0
  17. package/domain/entities/LoggerConfig.d.ts +25 -0
  18. package/domain/entities/LoggerConfig.js +2 -0
  19. package/domain/entities/generic/Collection.d.ts +82 -0
  20. package/domain/entities/generic/Collection.js +287 -0
  21. package/domain/entities/generic/Either.d.ts +49 -0
  22. package/domain/entities/generic/Either.js +71 -0
  23. package/domain/entities/generic/Future.d.ts +45 -0
  24. package/domain/entities/generic/Future.js +276 -0
  25. package/domain/entities/generic/HashMap.d.ts +42 -0
  26. package/domain/entities/generic/HashMap.js +149 -0
  27. package/domain/entities/generic/Rec.d.ts +30 -0
  28. package/domain/entities/generic/Rec.js +73 -0
  29. package/domain/entities/generic/Struct.d.ts +36 -0
  30. package/domain/entities/generic/Struct.js +51 -0
  31. package/domain/entities/generic/__tests__/Collection.spec.d.ts +1 -0
  32. package/domain/entities/generic/__tests__/Collection.spec.js +284 -0
  33. package/domain/entities/generic/__tests__/Future.spec.d.ts +8 -0
  34. package/domain/entities/generic/__tests__/Future.spec.js +556 -0
  35. package/domain/entities/generic/__tests__/HashMap.spec.d.ts +1 -0
  36. package/domain/entities/generic/__tests__/HashMap.spec.js +170 -0
  37. package/domain/entities/generic/__tests__/Rec.spec.d.ts +1 -0
  38. package/domain/entities/generic/__tests__/Rec.spec.js +37 -0
  39. package/domain/entities/generic/__tests__/Struct.spec.d.ts +1 -0
  40. package/domain/entities/generic/__tests__/Struct.spec.js +37 -0
  41. package/domain/repositories/LoggerRepository.d.ts +5 -0
  42. package/domain/repositories/LoggerRepository.js +2 -0
  43. package/domain/repositories/ProgramRepository.d.ts +5 -0
  44. package/domain/repositories/ProgramRepository.js +2 -0
  45. package/domain/usecases/CheckConfigProgramLoggerUseCase.d.ts +8 -0
  46. package/domain/usecases/CheckConfigProgramLoggerUseCase.js +13 -0
  47. package/domain/usecases/LogMessageUseCase.d.ts +8 -0
  48. package/domain/usecases/LogMessageUseCase.js +17 -0
  49. package/index.d.ts +17 -0
  50. package/index.js +124 -0
  51. package/package.json +64 -0
  52. package/scripts/cli.d.ts +1 -0
  53. package/scripts/cli.js +45 -0
  54. package/scripts/commands/consoleLogger.d.ts +31 -0
  55. package/scripts/commands/consoleLogger.js +96 -0
  56. package/scripts/commands/programLogger.d.ts +39 -0
  57. package/scripts/commands/programLogger.js +130 -0
  58. package/scripts/common.d.ts +29 -0
  59. package/scripts/common.js +128 -0
  60. package/scripts/index.d.ts +1 -0
  61. package/scripts/index.js +4 -0
  62. package/types/d2-api.d.ts +4 -0
  63. package/types/d2-api.js +7 -0
  64. package/utils/log.d.ts +10 -0
  65. package/utils/log.js +22 -0
  66. package/utils/ts-utils.d.ts +31 -0
  67. package/utils/ts-utils.js +81 -0
@@ -0,0 +1,287 @@
1
+ "use strict";
2
+ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
3
+ if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
4
+ if (ar || !(i in from)) {
5
+ if (!ar) ar = Array.prototype.slice.call(from, 0, i);
6
+ ar[i] = from[i];
7
+ }
8
+ }
9
+ return to.concat(ar || Array.prototype.slice.call(from));
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.Collection = void 0;
13
+ var HashMap_1 = require("./HashMap");
14
+ /**
15
+ * Wrap a collection of values, expanding methods for Javascript Arrays. An example:
16
+ *
17
+ * ```
18
+ * import _ from "./Collection";
19
+ *
20
+ * const values = _(["1", "2", "3", "3", "4"])
21
+ * .map(x => parseInt(x))
22
+ * .filter(x => x > 1)
23
+ * .uniq()
24
+ * .reverse()
25
+ * .value(); // [4, 3, 2]
26
+ * ```
27
+ */
28
+ var Collection = /** @class */ (function () {
29
+ function Collection(values) {
30
+ this.toArray = this.value;
31
+ this.filter = this.select;
32
+ this.all = this.every;
33
+ this.any = this.some;
34
+ this.keyBy = this.indexBy;
35
+ this.xs = values;
36
+ }
37
+ /* Builders */
38
+ Collection.from = function (xs) {
39
+ return new Collection(xs);
40
+ };
41
+ Collection.range = function (start, end, step) {
42
+ if (step === void 0) { step = 1; }
43
+ var output = [];
44
+ for (var idx = start; idx < end; idx = idx + step)
45
+ output.push(idx);
46
+ return Collection.from(output);
47
+ };
48
+ /* Unwrappers */
49
+ Collection.prototype.value = function () {
50
+ return this.xs;
51
+ };
52
+ Object.defineProperty(Collection.prototype, "size", {
53
+ get: function () {
54
+ return this.xs.length;
55
+ },
56
+ enumerable: false,
57
+ configurable: true
58
+ });
59
+ /* Methods that return a Collection */
60
+ Collection.prototype.map = function (fn) {
61
+ return _c(this.xs.map(fn));
62
+ };
63
+ Collection.prototype.flatten = function () {
64
+ return _c(this.xs.flat());
65
+ };
66
+ Collection.prototype.flatMap = function (fn) {
67
+ return _c(this.xs.flatMap(function (x) { return fn(x).toArray(); }));
68
+ };
69
+ Collection.prototype.select = function (pred) {
70
+ return _c(this.xs.filter(pred));
71
+ };
72
+ Collection.prototype.reject = function (pred) {
73
+ return _c(this.xs.filter(function (x) { return !pred(x); }));
74
+ };
75
+ Collection.prototype.enumerate = function () {
76
+ return _c(this.xs.map(function (x, idx) { return [idx, x]; }));
77
+ };
78
+ Collection.prototype.compact = function () {
79
+ return this.reject(function (x) { return x === undefined || x === null; });
80
+ };
81
+ Collection.prototype.compactMap = function (fn) {
82
+ return this.map(fn).compact();
83
+ };
84
+ Collection.prototype.append = function (x) {
85
+ return _c(this.xs.concat([x]));
86
+ };
87
+ Collection.prototype.includes = function (x) {
88
+ return this.xs.includes(x);
89
+ };
90
+ Collection.prototype.every = function (pred) {
91
+ return this.xs.every(pred);
92
+ };
93
+ Collection.prototype.some = function (pred) {
94
+ return this.xs.some(pred);
95
+ };
96
+ Collection.prototype.find = function (pred, options) {
97
+ if (options === void 0) { options = {}; }
98
+ return this.xs.find(pred) || (options === null || options === void 0 ? void 0 : options.or);
99
+ };
100
+ Collection.prototype.sort = function () {
101
+ return this.sortWith(defaultCompareFn);
102
+ };
103
+ Collection.prototype.reverse = function () {
104
+ return _c(__spreadArray([], this.xs, true).reverse());
105
+ };
106
+ Collection.prototype.sortWith = function (compareFn) {
107
+ return _c(this.xs.slice().sort(compareFn));
108
+ };
109
+ Collection.prototype.sortBy = function (fn, options) {
110
+ if (options === void 0) { options = {}; }
111
+ var compareFn = options.compareFn || defaultCompareFn;
112
+ // TODO: Schwartzian transform: decorate + sort tuple + undecorate
113
+ return this.sortWith(function (a, b) { return compareFn(fn(a), fn(b)); });
114
+ };
115
+ Collection.prototype.orderBy = function (items) {
116
+ return this.sortWith(function (a, b) {
117
+ return compareArray(a, b, items);
118
+ });
119
+ };
120
+ Collection.prototype.first = function () {
121
+ return this.xs[0];
122
+ };
123
+ Collection.prototype.last = function () {
124
+ return this.xs[this.xs.length - 1];
125
+ };
126
+ Collection.prototype.sum = function () {
127
+ return this.xs.reduce(function (acc, x) { return acc + Number(x); }, 0);
128
+ };
129
+ Collection.prototype.take = function (n) {
130
+ return _c(this.xs.slice(0, n));
131
+ };
132
+ Collection.prototype.drop = function (n) {
133
+ return _c(this.xs.slice(n));
134
+ };
135
+ Collection.prototype.pairwise = function () {
136
+ var _this = this;
137
+ var n = 2;
138
+ return _c(this.xs
139
+ .slice(0, this.xs.length - n + 1)
140
+ .map(function (_x, idx) { return [_this.xs[idx], _this.xs[idx + 1]]; }));
141
+ };
142
+ Collection.prototype.prepend = function (x) {
143
+ return _c(__spreadArray([x], this.xs, true));
144
+ };
145
+ Collection.prototype.tap = function (fn) {
146
+ fn(this);
147
+ return this;
148
+ };
149
+ Collection.prototype.splitAt = function (indexes) {
150
+ var _this = this;
151
+ return _c(indexes)
152
+ .prepend(0)
153
+ .append(this.xs.length)
154
+ .pairwise()
155
+ .map(function (_a) {
156
+ var i1 = _a[0], i2 = _a[1];
157
+ return _c(_this.xs.slice(i1, i2));
158
+ });
159
+ };
160
+ Collection.prototype.thru = function (fn) {
161
+ return fn(this);
162
+ };
163
+ Collection.prototype.join = function (char) {
164
+ return this.xs.join(char);
165
+ };
166
+ Collection.prototype.get = function (idx) {
167
+ return this.xs[idx];
168
+ };
169
+ Collection.prototype.getMany = function (idxs) {
170
+ var _this = this;
171
+ return _c(idxs.map(function (idx) { return _this.xs[idx]; }));
172
+ };
173
+ Collection.prototype.intersperse = function (value) {
174
+ return this.flatMap(function (x) { return _c([x, value]); }).thru(function (cs) { return cs.take(cs.size - 1); });
175
+ };
176
+ Collection.prototype.uniq = function () {
177
+ return this.uniqBy(function (x) { return x; });
178
+ };
179
+ Collection.prototype.uniqBy = function (mapper) {
180
+ var seen = new Set();
181
+ var output = [];
182
+ for (var _i = 0, _a = this.xs; _i < _a.length; _i++) {
183
+ var item = _a[_i];
184
+ var mapped = mapper(item);
185
+ if (!seen.has(mapped)) {
186
+ output.push(item);
187
+ seen.add(mapped);
188
+ }
189
+ }
190
+ return _c(output);
191
+ };
192
+ Collection.prototype.reduce = function (mapper, initialAcc) {
193
+ return this.xs.reduce(mapper, initialAcc);
194
+ };
195
+ Collection.prototype.chunk = function (size) {
196
+ var _this = this;
197
+ return Collection.range(0, this.xs.length, size).map(function (index) {
198
+ return _this.xs.slice(index, index + size);
199
+ });
200
+ };
201
+ Collection.prototype.cartesian = function () {
202
+ var _a = this.xs, ys = _a[0], zss = _a.slice(1);
203
+ if (!ys) {
204
+ return _c([[]]);
205
+ }
206
+ else {
207
+ return _c(ys).flatMap(function (x) {
208
+ return _c(zss)
209
+ .cartesian()
210
+ .map(function (zs) { return __spreadArray([x], zs, true); });
211
+ });
212
+ }
213
+ };
214
+ // forEach(fn: (value: T) => void): void
215
+ Collection.prototype.zipLongest = function (xs) {
216
+ var _this = this;
217
+ var max = Math.max(this.size, xs.size);
218
+ var pairs = Collection.range(0, max)
219
+ .map(function (i) { return [_this.xs[i], xs.xs[i]]; })
220
+ .value();
221
+ return _c(pairs);
222
+ };
223
+ Collection.prototype.zip = function (xs) {
224
+ var _this = this;
225
+ var min = Math.min(this.size, xs.size);
226
+ var pairs = Collection.range(0, min)
227
+ .map(function (i) { return [_this.xs[i], xs.xs[i]]; })
228
+ .value();
229
+ return _c(pairs);
230
+ };
231
+ /* Methods that return HashMap */
232
+ Collection.prototype.indexBy = function (grouperFn) {
233
+ var initialValue = HashMap_1.HashMap.empty();
234
+ return this.reduce(function (acc, x) {
235
+ var key = grouperFn(x);
236
+ return acc.set(key, x);
237
+ }, initialValue);
238
+ };
239
+ Collection.prototype.groupBy = function (grouperFn) {
240
+ var map = this.reduce(function (acc, value) {
241
+ var key = grouperFn(value);
242
+ var valuesForKey = acc.get(key) || [];
243
+ valuesForKey.push(value);
244
+ return acc.set(key, valuesForKey);
245
+ }, new Map());
246
+ return HashMap_1.HashMap.fromPairs(Array.from(map.entries()));
247
+ };
248
+ Collection.prototype.groupFromMap = function (pairGrouperFn) {
249
+ var map = this.reduce(function (acc, x) {
250
+ var _a = pairGrouperFn(x), key = _a[0], value = _a[1];
251
+ var valuesForKey = acc.get(key) || [];
252
+ valuesForKey.push(value);
253
+ return acc.set(key, valuesForKey);
254
+ }, new Map());
255
+ return HashMap_1.HashMap.fromPairs(Array.from(map.entries()));
256
+ };
257
+ Collection.prototype.toHashMap = function (toPairFn) {
258
+ var pairs = this.map(toPairFn).toArray();
259
+ return HashMap_1.HashMap.fromPairs(pairs);
260
+ };
261
+ /* Predicates */
262
+ Collection.prototype.isEmpty = function () {
263
+ return this.xs.length === 0;
264
+ };
265
+ Collection.prototype.isNotEmpty = function () {
266
+ return !this.isEmpty();
267
+ };
268
+ return Collection;
269
+ }());
270
+ exports.Collection = Collection;
271
+ function defaultCompareFn(a, b, direction) {
272
+ if (direction === void 0) { direction = "asc"; }
273
+ var _a = direction === "asc" ? [1, -1] : [-1, 1], value1 = _a[0], value2 = _a[1];
274
+ return a > b ? value1 : b > a ? value2 : 0;
275
+ }
276
+ function compareArray(a, b, items) {
277
+ var item = items[0];
278
+ if (!item)
279
+ return 0;
280
+ var mapper = item[0], direction = item[1];
281
+ var res = defaultCompareFn(mapper(a), mapper(b), direction);
282
+ return res !== 0 ? res : compareArray(a, b, items.slice(1));
283
+ }
284
+ function _c(xs) {
285
+ return Collection.from(xs);
286
+ }
287
+ exports.default = _c;
@@ -0,0 +1,49 @@
1
+ /**
2
+ * Either a success value or an error. Example:
3
+ *
4
+ * ```
5
+ * Either.success<{ message: string }, string>("9")
6
+ * .map(s => parseInt(s))
7
+ * .flatMap(x => {
8
+ * return x > 0 ? Either.success(Math.sqrt(x)) : Either.error({ message: "negative!" });
9
+ * })
10
+ * .match({
11
+ * success: x => console.log(`Value is ${x}`),
12
+ * error: error => console.error(`Some error: ${error.message}`),
13
+ * }); // prints `Value is 3`
14
+ * ```
15
+ */
16
+ export declare class Either<Error, Data> {
17
+ value: EitherValue<Error, Data>;
18
+ constructor(value: EitherValue<Error, Data>);
19
+ match<Res>(matchObj: MatchObject<Error, Data, Res>): Res;
20
+ isError(): this is this & {
21
+ value: EitherValueError<Error>;
22
+ };
23
+ isSuccess(): this is this & {
24
+ value: EitherValueSuccess<Data>;
25
+ };
26
+ map<Data1>(fn: (data: Data) => Data1): Either<Error, Data1>;
27
+ mapError<Error1>(fn: (error: Error) => Error1): Either<Error1, Data>;
28
+ flatMap<Data1>(fn: (data: Data) => Either<Error, Data1>): Either<Error, Data1>;
29
+ flatMapError<Error1>(fn: (error: Error) => Either<Error1, Data>): Either<Error1, Data>;
30
+ static error<Error>(error: Error): Either<Error, never>;
31
+ static success<Error, Data>(data: Data): Either<Error, Data>;
32
+ static map2<Error, Res, Data1, Data2>([either1, either2]: [Either<Error, Data1>, Either<Error, Data2>], fn: (data1: Data1, data2: Data2) => Res): Either<Error, Res>;
33
+ }
34
+ type EitherValueError<Error> = {
35
+ type: "error";
36
+ error: Error;
37
+ data?: never;
38
+ };
39
+ type EitherValueSuccess<Data> = {
40
+ type: "success";
41
+ error?: never;
42
+ data: Data;
43
+ };
44
+ type EitherValue<Error, Data> = EitherValueError<Error> | EitherValueSuccess<Data>;
45
+ type MatchObject<Error, Data, Res> = {
46
+ success: (data: Data) => Res;
47
+ error: (error: Error) => Res;
48
+ };
49
+ export {};
@@ -0,0 +1,71 @@
1
+ "use strict";
2
+ /**
3
+ * Either a success value or an error. Example:
4
+ *
5
+ * ```
6
+ * Either.success<{ message: string }, string>("9")
7
+ * .map(s => parseInt(s))
8
+ * .flatMap(x => {
9
+ * return x > 0 ? Either.success(Math.sqrt(x)) : Either.error({ message: "negative!" });
10
+ * })
11
+ * .match({
12
+ * success: x => console.log(`Value is ${x}`),
13
+ * error: error => console.error(`Some error: ${error.message}`),
14
+ * }); // prints `Value is 3`
15
+ * ```
16
+ */
17
+ Object.defineProperty(exports, "__esModule", { value: true });
18
+ exports.Either = void 0;
19
+ var Either = /** @class */ (function () {
20
+ function Either(value) {
21
+ this.value = value;
22
+ }
23
+ Either.prototype.match = function (matchObj) {
24
+ switch (this.value.type) {
25
+ case "success":
26
+ return matchObj.success(this.value.data);
27
+ case "error":
28
+ return matchObj.error(this.value.error);
29
+ }
30
+ };
31
+ Either.prototype.isError = function () {
32
+ return this.value.type === "error";
33
+ };
34
+ Either.prototype.isSuccess = function () {
35
+ return this.value.type === "success";
36
+ };
37
+ Either.prototype.map = function (fn) {
38
+ return this.flatMap(function (data) { return new Either({ type: "success", data: fn(data) }); });
39
+ };
40
+ Either.prototype.mapError = function (fn) {
41
+ return this.flatMapError(function (error) { return new Either({ type: "error", error: fn(error) }); });
42
+ };
43
+ Either.prototype.flatMap = function (fn) {
44
+ var _this = this;
45
+ return this.match({
46
+ success: function (data) { return fn(data); },
47
+ error: function () { return _this; },
48
+ });
49
+ };
50
+ Either.prototype.flatMapError = function (fn) {
51
+ var _this = this;
52
+ return this.match({
53
+ success: function () { return _this; },
54
+ error: function (error) { return fn(error); },
55
+ });
56
+ };
57
+ Either.error = function (error) {
58
+ return new Either({ type: "error", error: error });
59
+ };
60
+ Either.success = function (data) {
61
+ return new Either({ type: "success", data: data });
62
+ };
63
+ Either.map2 = function (_a, fn) {
64
+ var either1 = _a[0], either2 = _a[1];
65
+ return either1.flatMap(function (data1) {
66
+ return either2.map(function (data2) { return fn(data1, data2); });
67
+ });
68
+ };
69
+ return Either;
70
+ }());
71
+ exports.Either = Either;
@@ -0,0 +1,45 @@
1
+ import { FutureData } from "../../../data/api-futures";
2
+ /**
3
+ * Futures are async values similar to promises, with some differences:
4
+ * - Futures are only executed when their method `run` is called.
5
+ * - Futures are cancellable (thus, they can be easily used in a `React.useEffect`, for example).
6
+ * - Futures have fully typed errors. Subclass Error if you need full stack traces.
7
+ * - You may still use async/await monad-style blocks (check Future.block).
8
+ *
9
+ * More info: https://github.com/EyeSeeTea/know-how/wiki/Async-futures
10
+ */
11
+ export declare class Future<E, D> {
12
+ private _promise;
13
+ private constructor();
14
+ static success<E, D>(data: D): Future<E, D>;
15
+ static error<E, D>(error: E): Future<E, D>;
16
+ static fromPromise<Data>(promise: Promise<Data>): FutureData<Data>;
17
+ static fromComputation<E, D>(computation: (resolve: (value: D) => void, reject: (error: E) => void) => Cancel): Future<E, D>;
18
+ run(onSuccess: (data: D) => void, onError: (error: E) => void): Cancel;
19
+ map<U>(fn: (data: D) => U): Future<E, U>;
20
+ mapError<E2>(fn: (error: E) => E2): Future<E2, D>;
21
+ flatMap<U, E>(fn: (data: D) => Future<U, E>): Future<U, E>;
22
+ flatMapError<E2>(fn: (error: E) => Future<E2, D>): Future<E2, D>;
23
+ chain<U, E>(fn: (data: D) => Future<U, E>): Future<U, E>;
24
+ toPromise(): Promise<D>;
25
+ static join2<E, T, S>(async1: Future<E, T>, async2: Future<E, S>): Future<E, [T, S]>;
26
+ static joinObj<Obj extends Record<string, Future<any, any>>>(obj: Obj, options?: ParallelOptions): Future<Obj[keyof Obj] extends Future<infer E, any> ? E : never, {
27
+ [K in keyof Obj]: Obj[K] extends Future<any, infer U> ? U : never;
28
+ }>;
29
+ static sequential<E, D>(asyncs: Future<E, D>[]): Future<E, D[]>;
30
+ static parallel<E, D>(asyncs: Future<E, D>[], options: ParallelOptions): Future<E, D[]>;
31
+ static sleep(ms: number): Future<any, number>;
32
+ static void(): Future<unknown, undefined>;
33
+ static block<E, U>(blockFn: (capture: CaptureAsync<E>) => Promise<U>): Future<E, U>;
34
+ static block_<E>(): <U>(blockFn: (capture: CaptureAsync<E>) => Promise<U>) => Future<E, U>;
35
+ }
36
+ export type Cancel = (() => void) | undefined;
37
+ interface CaptureAsync<E> {
38
+ <D>(async: Future<E, D>): Promise<D>;
39
+ throw: (error: E) => never;
40
+ }
41
+ type ParallelOptions = {
42
+ concurrency: number;
43
+ };
44
+ export declare function getJSON<U>(url: string): Future<TypeError | SyntaxError, U>;
45
+ export {};