@thisisagile/easy 18.13.0 → 18.15.0

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 (151) hide show
  1. package/dist/{chunk-D5U4D63E.mjs → chunk-2O7APHCR.mjs} +4 -4
  2. package/dist/{chunk-T773NDGK.mjs → chunk-2RUIPTPW.mjs} +2 -2
  3. package/dist/{chunk-BS5KUNX2.mjs → chunk-3VYRMOJQ.mjs} +795 -787
  4. package/dist/chunk-3VYRMOJQ.mjs.map +1 -0
  5. package/dist/{chunk-NTK2UCHO.mjs → chunk-3XXOPMFK.mjs} +5 -5
  6. package/dist/{chunk-AHSUQCHS.mjs → chunk-55MJBJQI.mjs} +4 -4
  7. package/dist/{chunk-EPCIQ4J3.mjs → chunk-5D4ARYNW.mjs} +2 -2
  8. package/dist/{chunk-WJGWSFCJ.mjs → chunk-5DR6CXZ4.mjs} +3 -3
  9. package/dist/{chunk-M6I5JHJC.mjs → chunk-5REVEHXT.mjs} +2 -2
  10. package/dist/{chunk-W3LNVDFV.mjs → chunk-5VQFIOK7.mjs} +2 -2
  11. package/dist/{chunk-UIL3XRCV.mjs → chunk-5YD3FW65.mjs} +3 -3
  12. package/dist/{chunk-MH4VLBIT.mjs → chunk-7OKFVQ6R.mjs} +4 -4
  13. package/dist/{chunk-T7YVWWJF.mjs → chunk-AH3AKXOK.mjs} +4 -4
  14. package/dist/{chunk-3HEPK3TC.mjs → chunk-ANPIOQZC.mjs} +2 -2
  15. package/dist/{chunk-NWUALPMB.mjs → chunk-COQC24CB.mjs} +2 -2
  16. package/dist/{chunk-Z37TBPTK.mjs → chunk-CXQYWRP7.mjs} +2 -2
  17. package/dist/{chunk-OLL5B5NN.mjs → chunk-EHV2WRVW.mjs} +3 -3
  18. package/dist/{chunk-NOZLL54Z.mjs → chunk-EJQIAVVF.mjs} +2 -2
  19. package/dist/{chunk-23ZFESSO.mjs → chunk-ERD5JUYQ.mjs} +3 -3
  20. package/dist/{chunk-HWR7EEVI.mjs → chunk-GZX5DULM.mjs} +3 -3
  21. package/dist/{chunk-QMIBOIAF.mjs → chunk-HNGB5S6T.mjs} +2 -2
  22. package/dist/{chunk-6I4ZNNVM.mjs → chunk-IKF7DZVQ.mjs} +2 -2
  23. package/dist/{chunk-F2W64HKC.mjs → chunk-JMRCENVQ.mjs} +2 -2
  24. package/dist/{chunk-P2ZMGAQP.mjs → chunk-K7LXXBUR.mjs} +2 -2
  25. package/dist/{chunk-TN6LYAFX.mjs → chunk-KPAQ6GBD.mjs} +2 -2
  26. package/dist/{chunk-NL464DQQ.mjs → chunk-L3AWV2Z6.mjs} +3 -3
  27. package/dist/{chunk-PFLWEHXS.mjs → chunk-L7HJKDOM.mjs} +2 -2
  28. package/dist/{chunk-S5J3TYAZ.mjs → chunk-LC3MZH5U.mjs} +3 -3
  29. package/dist/{chunk-3JALCI3D.mjs → chunk-N354IJJ2.mjs} +3 -3
  30. package/dist/{chunk-CB3LF3XW.mjs → chunk-OOS2JAKJ.mjs} +5 -5
  31. package/dist/{chunk-HI4ZNU6Z.mjs → chunk-RATOUBU7.mjs} +2 -2
  32. package/dist/{chunk-4SO7UI5S.mjs → chunk-RHEXAQZX.mjs} +2 -2
  33. package/dist/{chunk-DMOWSGD3.mjs → chunk-TAEC3IRD.mjs} +2 -2
  34. package/dist/{chunk-SATFIUWH.mjs → chunk-TGLXWWEU.mjs} +2 -2
  35. package/dist/{chunk-7VJ3E3GP.mjs → chunk-XCQ3OU5Q.mjs} +2 -2
  36. package/dist/{chunk-5LVVM522.mjs → chunk-XSLUSOUM.mjs} +7 -7
  37. package/dist/data/Condition.mjs +3 -3
  38. package/dist/data/Database.mjs +2 -2
  39. package/dist/data/Field.mjs +3 -3
  40. package/dist/data/InMemoryGateway.mjs +3 -3
  41. package/dist/domain/Audit.mjs +7 -7
  42. package/dist/domain/Child.mjs +4 -4
  43. package/dist/domain/DateTime.mjs +3 -3
  44. package/dist/domain/Entity.mjs +9 -9
  45. package/dist/domain/Repo.mjs +3 -3
  46. package/dist/domain/Struct.mjs +3 -3
  47. package/dist/domain/Typo.mjs +3 -3
  48. package/dist/http/CacheControl.mjs +3 -3
  49. package/dist/http/ContentType.mjs +2 -2
  50. package/dist/http/HttpStatus.mjs +2 -2
  51. package/dist/http/HttpVerb.mjs +2 -2
  52. package/dist/http/LocalRequestStore.mjs +1 -1
  53. package/dist/http/RequestOptions.mjs +3 -3
  54. package/dist/http/RestResult.mjs +3 -3
  55. package/dist/http/toResponse.mjs +4 -4
  56. package/dist/index.js +10 -0
  57. package/dist/index.js.map +1 -1
  58. package/dist/process/App.mjs +2 -2
  59. package/dist/process/Manage.mjs +3 -3
  60. package/dist/process/Scope.mjs +2 -2
  61. package/dist/process/Search.mjs +3 -3
  62. package/dist/process/UseCase.mjs +1 -1
  63. package/dist/resources/Req.mjs +2 -2
  64. package/dist/services/Api.mjs +11 -11
  65. package/dist/services/ApiGateway.mjs +14 -14
  66. package/dist/services/AxiosProvider.mjs +9 -9
  67. package/dist/services/MappedRouteGateway.mjs +18 -18
  68. package/dist/services/RouteGateway.mjs +15 -15
  69. package/dist/services/ViewRouteGateway.mjs +17 -17
  70. package/dist/sql/Column.mjs +2 -2
  71. package/dist/sql/Count.mjs +4 -4
  72. package/dist/sql/Delete.mjs +3 -3
  73. package/dist/sql/Insert.mjs +3 -3
  74. package/dist/sql/Select.mjs +3 -3
  75. package/dist/sql/SqlQuery.mjs +2 -2
  76. package/dist/sql/Table.mjs +14 -14
  77. package/dist/sql/TableGateway.mjs +3 -3
  78. package/dist/sql/Update.mjs +3 -3
  79. package/dist/types/Cache.mjs +1 -1
  80. package/dist/types/CacheAge.mjs +3 -3
  81. package/dist/types/Case.mjs +1 -1
  82. package/dist/types/Context.mjs +1 -1
  83. package/dist/types/Enum.mjs +1 -1
  84. package/dist/types/Environment.mjs +1 -1
  85. package/dist/types/ErrorOrigin.mjs +1 -1
  86. package/dist/types/Exception.mjs +1 -1
  87. package/dist/types/Gateway.mjs +2 -2
  88. package/dist/types/IsEqual.mjs +1 -1
  89. package/dist/types/Json.mjs +2 -2
  90. package/dist/types/List.mjs +1 -1
  91. package/dist/types/Meta.mjs +1 -1
  92. package/dist/types/Number.mjs +2 -2
  93. package/dist/types/Object.mjs +1 -1
  94. package/dist/types/PageList.mjs +2 -2
  95. package/dist/types/Parser.mjs +1 -1
  96. package/dist/types/Repository.mjs +2 -2
  97. package/dist/types/Result.mjs +1 -1
  98. package/dist/types/Results.mjs +1 -1
  99. package/dist/types/Tag.mjs +1 -1
  100. package/dist/types/Template.mjs +1 -1
  101. package/dist/types/ToText.mjs +1 -1
  102. package/dist/types/Try.mjs +1 -1
  103. package/dist/types/Uri.mjs +1 -1
  104. package/dist/utils/If.d.ts +6 -0
  105. package/dist/utils/If.mjs +5 -1
  106. package/dist/utils/Mapper.mjs +3 -3
  107. package/dist/utils/Period.mjs +3 -3
  108. package/dist/utils/Promise.mjs +1 -1
  109. package/dist/utils/Seconds.mjs +2 -2
  110. package/dist/utils/Sentence.mjs +1 -1
  111. package/dist/utils/View.mjs +4 -4
  112. package/dist/validation/Contraints.mjs +2 -2
  113. package/dist/validation/Validate.mjs +1 -1
  114. package/dist/validation/When.mjs +1 -1
  115. package/package.json +1 -1
  116. package/src/utils/If.ts +16 -1
  117. package/dist/chunk-BS5KUNX2.mjs.map +0 -1
  118. /package/dist/{chunk-D5U4D63E.mjs.map → chunk-2O7APHCR.mjs.map} +0 -0
  119. /package/dist/{chunk-T773NDGK.mjs.map → chunk-2RUIPTPW.mjs.map} +0 -0
  120. /package/dist/{chunk-NTK2UCHO.mjs.map → chunk-3XXOPMFK.mjs.map} +0 -0
  121. /package/dist/{chunk-AHSUQCHS.mjs.map → chunk-55MJBJQI.mjs.map} +0 -0
  122. /package/dist/{chunk-EPCIQ4J3.mjs.map → chunk-5D4ARYNW.mjs.map} +0 -0
  123. /package/dist/{chunk-WJGWSFCJ.mjs.map → chunk-5DR6CXZ4.mjs.map} +0 -0
  124. /package/dist/{chunk-M6I5JHJC.mjs.map → chunk-5REVEHXT.mjs.map} +0 -0
  125. /package/dist/{chunk-W3LNVDFV.mjs.map → chunk-5VQFIOK7.mjs.map} +0 -0
  126. /package/dist/{chunk-UIL3XRCV.mjs.map → chunk-5YD3FW65.mjs.map} +0 -0
  127. /package/dist/{chunk-MH4VLBIT.mjs.map → chunk-7OKFVQ6R.mjs.map} +0 -0
  128. /package/dist/{chunk-T7YVWWJF.mjs.map → chunk-AH3AKXOK.mjs.map} +0 -0
  129. /package/dist/{chunk-3HEPK3TC.mjs.map → chunk-ANPIOQZC.mjs.map} +0 -0
  130. /package/dist/{chunk-NWUALPMB.mjs.map → chunk-COQC24CB.mjs.map} +0 -0
  131. /package/dist/{chunk-Z37TBPTK.mjs.map → chunk-CXQYWRP7.mjs.map} +0 -0
  132. /package/dist/{chunk-OLL5B5NN.mjs.map → chunk-EHV2WRVW.mjs.map} +0 -0
  133. /package/dist/{chunk-NOZLL54Z.mjs.map → chunk-EJQIAVVF.mjs.map} +0 -0
  134. /package/dist/{chunk-23ZFESSO.mjs.map → chunk-ERD5JUYQ.mjs.map} +0 -0
  135. /package/dist/{chunk-HWR7EEVI.mjs.map → chunk-GZX5DULM.mjs.map} +0 -0
  136. /package/dist/{chunk-QMIBOIAF.mjs.map → chunk-HNGB5S6T.mjs.map} +0 -0
  137. /package/dist/{chunk-6I4ZNNVM.mjs.map → chunk-IKF7DZVQ.mjs.map} +0 -0
  138. /package/dist/{chunk-F2W64HKC.mjs.map → chunk-JMRCENVQ.mjs.map} +0 -0
  139. /package/dist/{chunk-P2ZMGAQP.mjs.map → chunk-K7LXXBUR.mjs.map} +0 -0
  140. /package/dist/{chunk-TN6LYAFX.mjs.map → chunk-KPAQ6GBD.mjs.map} +0 -0
  141. /package/dist/{chunk-NL464DQQ.mjs.map → chunk-L3AWV2Z6.mjs.map} +0 -0
  142. /package/dist/{chunk-PFLWEHXS.mjs.map → chunk-L7HJKDOM.mjs.map} +0 -0
  143. /package/dist/{chunk-S5J3TYAZ.mjs.map → chunk-LC3MZH5U.mjs.map} +0 -0
  144. /package/dist/{chunk-3JALCI3D.mjs.map → chunk-N354IJJ2.mjs.map} +0 -0
  145. /package/dist/{chunk-CB3LF3XW.mjs.map → chunk-OOS2JAKJ.mjs.map} +0 -0
  146. /package/dist/{chunk-HI4ZNU6Z.mjs.map → chunk-RATOUBU7.mjs.map} +0 -0
  147. /package/dist/{chunk-4SO7UI5S.mjs.map → chunk-RHEXAQZX.mjs.map} +0 -0
  148. /package/dist/{chunk-DMOWSGD3.mjs.map → chunk-TAEC3IRD.mjs.map} +0 -0
  149. /package/dist/{chunk-SATFIUWH.mjs.map → chunk-TGLXWWEU.mjs.map} +0 -0
  150. /package/dist/{chunk-7VJ3E3GP.mjs.map → chunk-XCQ3OU5Q.mjs.map} +0 -0
  151. /package/dist/{chunk-5LVVM522.mjs.map → chunk-XSLUSOUM.mjs.map} +0 -0
@@ -42,959 +42,955 @@ import {
42
42
  isTrue
43
43
  } from "./chunk-AAND4MKF.mjs";
44
44
 
45
- // src/types/Case.ts
46
- var CaseBuilder = class {
47
- constructor(v) {
48
- this.v = v;
49
- }
50
- is = {
51
- true: (pred, out) => this.case(pred, out),
52
- false: (pred, out) => this.case(!ofGet(pred, this.v), out),
53
- truthy: (prop, out) => this.case(!!prop(this.v), out),
54
- falsy: (prop, out) => this.case(!prop(this.v), out),
55
- defined: (prop, out) => this.case(isDefined(prop(this.v)), out),
56
- empty: (prop, out) => this.case(isEmpty(prop(this.v)), out),
57
- valid: (prop, out) => this.case(validate(prop(this.v)).isValid, out),
58
- in: (prop, out) => this.case(ofGet(prop, this.v).includes(this.v), out),
59
- type: (guard, out) => this.type(guard, out),
60
- not: {
61
- true: (pred, out) => this.case(!ofGet(pred, this.v), out),
62
- false: (pred, out) => this.case(pred, out),
63
- truthy: (prop, out) => this.case(!prop(this.v), out),
64
- falsy: (prop, out) => this.case(!!prop(this.v), out),
65
- defined: (prop, out) => this.case(!isDefined(prop(this.v)), out),
66
- empty: (prop, out) => this.case(!isEmpty(prop(this.v)), out),
67
- valid: (prop, out) => this.case(!validate(prop(this.v)).isValid, out),
68
- in: (prop, out) => this.case(!ofGet(prop, this.v).includes(this.v), out),
69
- type: (guard, out) => this.case((v) => !guard(v), out)
70
- }
71
- };
72
- if = this.is;
73
- case(pred, out) {
74
- return new Case(this.v).case(pred, out);
75
- }
76
- type(guard, out) {
77
- return new Case(this.v).type(guard, out);
78
- }
79
- equals(value, out) {
80
- return new Case(this.v).equals(value, out);
81
- }
82
- };
83
- var Case = class {
84
- constructor(value, outcome) {
85
- this.value = value;
86
- this.outcome = outcome;
87
- }
88
- is = {
89
- true: (pred, out) => this.case(pred, out),
90
- false: (pred, out) => this.case(!ofGet(pred, this.value), out),
91
- truthy: (prop, out) => this.case(!!prop(this.value), out),
92
- falsy: (prop, out) => this.case(!prop(this.value), out),
93
- defined: (prop, out) => this.case(isDefined(prop(this.value)), out),
94
- empty: (prop, out) => this.case(isEmpty(prop(this.value)), out),
95
- valid: (prop, out) => this.case(validate(prop(this.value)).isValid, out),
96
- in: (prop, out) => this.case(ofGet(prop, this.value).includes(this.value), out),
97
- type: (guard, out) => this.type(guard, out),
98
- not: {
99
- true: (pred, out) => this.case(!ofGet(pred, this.value), out),
100
- false: (pred, out) => this.case(pred, out),
101
- truthy: (prop, out) => this.case(!prop(this.value), out),
102
- falsy: (prop, out) => this.case(!!prop(this.value), out),
103
- defined: (prop, out) => this.case(!isDefined(prop(this.value)), out),
104
- empty: (prop, out) => this.case(!isEmpty(prop(this.value)), out),
105
- valid: (prop, out) => this.case(!validate(prop(this.value)).isValid, out),
106
- in: (prop, out) => this.case(!ofGet(prop, this.value).includes(this.value), out),
107
- type: (guard, out) => this.case((v) => !guard(v), out)
108
- }
109
- };
110
- if = this.is;
111
- case(pred, out) {
112
- return tryTo(pred, this.value).is.true().map(() => ofGet(out, this.value)).map((res) => new Found(this.value, res)).or(this);
113
- }
114
- type(guard, out) {
115
- return tryTo(guard, this.value).is.true().map(() => out(this.value)).map((res) => new Found(this.value, res)).or(this);
116
- }
117
- equals(value, out) {
118
- return this.case(this.value === value, out);
119
- }
120
- else(alt) {
121
- return ofGet(alt, this.value);
122
- }
123
- };
124
- var Found = class extends Case {
125
- constructor(value, outcome) {
126
- super(value, outcome);
127
- this.value = value;
128
- this.outcome = outcome;
129
- }
130
- case(pred, out) {
131
- return this;
132
- }
133
- type(guard, out) {
134
- return this;
135
- }
136
- equals(value, out) {
137
- return this;
138
- }
139
- else(alt) {
140
- return this.outcome;
141
- }
142
- };
143
- var choose = (value) => new CaseBuilder(value);
144
-
145
- // src/types/IsEqual.ts
146
- var isEqualArray = (one, another) => choose([one, another]).case(([o, a]) => !isArray(o) || !isArray(a), false).case(([o, a]) => o?.length !== a?.length, false).else(([o, a]) => !o.some((v, i) => !isEqual(v, a[i])));
147
- var isEqualObject = (one, another) => choose([Object.keys(one), Object.keys(another)]).case(([keysO, keysA]) => keysO.length !== keysA.length, false).case(([keysO, keysA]) => keysO.some((k) => !keysA.includes(k)), false).case(([keysO]) => keysO.some((k) => !isEqual(one[k], another[k])), false).else(true);
148
- var isEqual = (one, another) => choose([one, another]).case(([o, a]) => o === a, true).case(
149
- ([o, a]) => isArray(o) || isArray(a),
150
- ([o, a]) => isEqualArray(o, a)
151
- ).case(
152
- ([o, a]) => o == null || a == null || !isObject(o) && !isObject(a),
153
- ([o, a]) => o !== o && a !== a
154
- ).else(([o, a]) => isEqualObject(o, a));
155
-
156
- // src/utils/If.ts
157
- function ifTrue(o, f, alt) {
158
- return isTrue(o) ? ofConstruct(f, o) : ofConstruct(alt, o);
159
- }
160
- function ifFalse(o, f, alt) {
161
- return !isTrue(o) ? ofConstruct(f, o) : ofConstruct(alt, o);
162
- }
163
- function ifDefined(o, f, alt) {
164
- return isDefined(o) ? ofConstruct(f, o) : ofConstruct(alt);
45
+ // src/types/Object.ts
46
+ function entries(subject) {
47
+ return ifDefined(
48
+ subject,
49
+ () => toList([...Object.entries(subject), ...Object.entries(Object.getPrototypeOf(subject) ?? {})]),
50
+ () => toList()
51
+ );
165
52
  }
166
- function ifNotEmpty(o, f = (o2) => o2, alt) {
167
- return isNotEmpty(o) ? ofConstruct(f, o) : ofConstruct(alt, o);
53
+ function values(subject) {
54
+ return toList([...Object.values(subject), ...Object.values(Object.getPrototypeOf(subject) ?? {})]);
168
55
  }
169
- function ifEqual(one, another, f, alt) {
170
- return isEqual(one, another) ? ofConstruct(f) : ofConstruct(alt);
56
+ function keys(subject) {
57
+ return toList([...Object.keys(subject), ...Object.keys(Object.getPrototypeOf(subject) ?? {})]);
171
58
  }
172
- function ifEither(os, f = (o) => o, alt) {
173
- return use(
174
- toList(os).first((o) => isPresent(o)),
175
- (o) => isDefined(o) ? ofConstruct(f, o) : ofConstruct(alt, o)
59
+ function extractKeys(obj, keys2) {
60
+ return keys2.filter((k) => k in obj).reduce(
61
+ (acc, key) => {
62
+ acc.keys[key] = obj[key];
63
+ delete acc[key];
64
+ return acc;
65
+ },
66
+ { keys: {}, ...obj }
176
67
  );
177
68
  }
178
69
 
179
- // src/types/List.ts
180
- var List = class extends Array {
181
- get ids() {
182
- return this.mapDefined((i) => i.id);
183
- }
184
- get indexed() {
185
- return this.map((v, index) => ({ ...v, index }));
186
- }
187
- isSubSetOf(...items) {
188
- return this.diff(items).length === 0;
189
- }
190
- isSuperSetOf(...items) {
191
- return this.length > items.length && toList(...items).isSubSetOf(...this);
192
- }
193
- isIntersectingWith(...items) {
194
- return this.intersect(items).length > 0;
195
- }
196
- isDisjointWith(...items) {
197
- return !this.isIntersectingWith(...items);
198
- }
199
- areEqual(...items) {
200
- return this.isSubSetOf(...items) && toList(...items).isSubSetOf(...this);
201
- }
202
- asc(p) {
203
- return toList(...this).sort((e1, e2) => ofProperty(e1, p) > ofProperty(e2, p) ? 1 : -1);
204
- }
205
- desc(p) {
206
- return toList(...this).sort((e1, e2) => ofProperty(e1, p) < ofProperty(e2, p) ? 1 : -1);
70
+ // src/utils/Promise.ts
71
+ var resolve = (subject) => Promise.resolve(subject);
72
+ var reject = (e) => Promise.reject(e);
73
+ var tuple = {
74
+ 2: (first, second) => Promise.all([first, second]),
75
+ 3: (first, second, third) => Promise.all([first, second, third]),
76
+ 4: (first, second, third, forth) => Promise.all([first, second, third, forth]),
77
+ 5: (first, second, third, forth, fifth) => Promise.all([first, second, third, forth, fifth]),
78
+ all: (first, second) => Promise.all([first, Promise.all(second)]),
79
+ spread: (first, ...second) => Promise.all([first, Promise.all(toArray(second))]),
80
+ list: (list) => Promise.all([...list]).then(toList),
81
+ settled: (list) => Promise.allSettled([...list]).then((rs) => ({
82
+ fulfilled: toList(...rs.filter((r) => r.status === "fulfilled").map((r) => r.value)),
83
+ rejected: toList(...rs.filter((r) => r.status === "rejected").map((r) => asString(r.reason)))
84
+ })),
85
+ object: (obj) => use(keys(obj), (ks) => tuple.list(values(obj)).then((vs) => vs.reduce((acc, v, i) => on(acc, (a) => a[ks[i]] = v), {})))
86
+ };
87
+ var tuple2 = tuple[2];
88
+ var tuple3 = tuple[3];
89
+ var tuple4 = tuple[4];
90
+ var tuple5 = tuple[5];
91
+ var tupleO = tuple.object;
92
+ var settled = tuple.settled;
93
+
94
+ // src/types/Meta.ts
95
+ import "reflect-metadata";
96
+ var ClassMeta = class {
97
+ constructor(subject, data = (subject.prototype ?? subject).constructor) {
98
+ this.subject = subject;
99
+ this.data = data;
207
100
  }
208
- first(p, params) {
209
- return p ? this.find(p, params) : this[0];
101
+ get = (key) => Reflect.getMetadata(key, this.data);
102
+ set = (key, value) => {
103
+ Reflect.defineMetadata(key, value, this.data);
104
+ return value;
105
+ };
106
+ entries = () => entries(this.subject);
107
+ parse = (f, options = {}) => {
108
+ const { initial = {}, skipUndefined = false } = options;
109
+ return this.entries().reduce((a, [k, val]) => use(f(val), (v) => isDefined(v) || !skipUndefined ? on(a, (a2) => a2[k] = v) : a), initial);
110
+ };
111
+ properties = (key) => toList([...Object.getOwnPropertyNames(this.subject), ...Object.getOwnPropertyNames(Object.getPrototypeOf(this.subject))]).map((p) => this.property(p)).filter((p) => key ? p.get(key) : p);
112
+ keys = (key) => this.properties().map((p) => p.get(key)).reduce((list, u) => u ? list.add(u) : list, toList());
113
+ values = () => values(this.subject);
114
+ property = (property) => new PropertyMeta(this.subject, property);
115
+ };
116
+ var PropertyMeta = class {
117
+ constructor(subject, property, data = Reflect.getMetadata(property, subject)) {
118
+ this.subject = subject;
119
+ this.property = property;
120
+ this.data = data;
210
121
  }
211
- firstValue(f, alt) {
212
- return ifDefined(
213
- this.first((t) => !!f(t)),
214
- f,
215
- (v) => ofGet(alt, v)
216
- );
122
+ get value() {
123
+ return this.subject[this.property];
217
124
  }
218
- isFirst(value) {
219
- return value === this.first();
125
+ get = (key) => isDefined(this.data) && isDefined(this.data[key]) ? this.data[key] : void 0;
126
+ set = (key, value) => {
127
+ Reflect.defineMetadata(this.property, { ...this.data, [key]: value }, this.subject);
128
+ return value;
129
+ };
130
+ };
131
+ var meta = (subject) => new ClassMeta(subject ?? {});
132
+
133
+ // src/types/Enum.ts
134
+ var isEnumConstructor = (c) => isFunction(c) && c.isEnum;
135
+ var Enum = class {
136
+ constructor(name, id = name.toLowerCase(), code = id.toString()) {
137
+ this.name = name;
138
+ this.id = id;
139
+ this.code = code;
220
140
  }
221
- next(p, params) {
222
- return p ? this[this.findIndex(p, params) + 1] : this[0];
141
+ static isEnum = true;
142
+ get isValid() {
143
+ return isDefined(this.id);
223
144
  }
224
- prev(p, params) {
225
- return p ? this[this.findIndex(p, params) - 1] : this[0];
145
+ static all() {
146
+ return meta(this.allTuple()).values();
226
147
  }
227
- last(p, params) {
228
- return p ? this.filter(p, params).last() : this[this.length - 1];
148
+ static filter(p, params) {
149
+ return this.all().filter(p, params);
229
150
  }
230
- isLast(value) {
231
- return value === this.last();
151
+ static first(p, params) {
152
+ return this.all().first(p, params);
232
153
  }
233
- overlaps(...items) {
234
- return toList(...items).some((i) => this.some((t) => i === t));
154
+ static byIds(ids = []) {
155
+ return toList(ids).mapDefined((id) => this.byId(id)).distinct();
235
156
  }
236
- diff(others) {
237
- return this.filter((i) => !others.includes(i));
157
+ static byId(id, alt) {
158
+ return this.allTuple()[id] ?? ofGet(alt);
238
159
  }
239
- diffByKey(others, key) {
240
- return this.filter((i) => !others.some((o) => o[key] === i[key]));
160
+ static allTuple() {
161
+ return meta(this).get(`all-${this.name}`) ?? meta(this).set(`all-${this.name}`, meta(this).values().filter(isEnum).toObject("id"));
241
162
  }
242
- symmetricDiff(others) {
243
- return this.diff(others).concat(toList(...others).diff(this));
163
+ equals(other) {
164
+ return this.id === (isEnum(other) ? other.id : other);
244
165
  }
245
- symmetricDiffByKey(others, key) {
246
- return this.diffByKey(others, key).concat(toList(...others).diffByKey(this, key));
166
+ isIn(...items) {
167
+ return items.some((i) => this.equals(i));
247
168
  }
248
- intersect(others) {
249
- return this.filter((i) => others.includes(i));
169
+ toJSON() {
170
+ return this.id;
250
171
  }
251
- intersectByKey(others, key) {
252
- return this.filter((i) => others.some((o) => o[key] === i[key]));
172
+ toString() {
173
+ return this.id.toString();
253
174
  }
254
- intersectBy(others, f) {
255
- return this.filter((i) => others.some((o) => f(i, o)));
175
+ };
176
+ var isEnum = (e) => isDefined(e) && e instanceof Enum && isAn(e, "name", "id", "code");
177
+
178
+ // src/types/Try.ts
179
+ var Try = class _Try {
180
+ is = {
181
+ defined: (prop) => this.filter((v) => isDefined(prop ? prop(v) : v)),
182
+ empty: (prop) => this.filter((v) => isEmpty(prop ? prop(v) : v)),
183
+ valid: (prop) => this.filter((v) => validate(prop ? prop(v) : v).isValid),
184
+ true: (prop) => this.filter((v) => isTrue(prop ? prop(v) : v)),
185
+ false: (prop) => this.filter((v) => !isTrue(prop ? prop(v) : v)),
186
+ not: {
187
+ defined: (prop) => this.filter((v) => !isDefined(prop ? prop(v) : v)),
188
+ empty: (prop) => this.filter((v) => !isEmpty(prop ? prop(v) : v)),
189
+ valid: (prop) => this.filter((v) => !validate(prop ? prop(v) : v).isValid)
190
+ }
191
+ };
192
+ if = this.is;
193
+ static of = (c, ...args) => {
194
+ try {
195
+ const out = ofGet(c, ...args);
196
+ return new Success(out instanceof _Try ? out.value : out);
197
+ } catch (e) {
198
+ return new Failure(e);
199
+ }
200
+ };
201
+ };
202
+ var Success = class extends Try {
203
+ constructor(value) {
204
+ super();
205
+ this.value = value;
256
206
  }
257
- accumulate(...keys2) {
258
- return this.map((d, i, arr) => {
259
- const acc = keys2.reduce((acc2, v) => {
260
- acc2[v] = i === 0 ? d[v] : arr[i - 1][v] + d[v];
261
- return acc2;
262
- }, d);
263
- arr[i] = acc;
264
- return acc;
265
- });
207
+ get error() {
208
+ throw new Error("No error found");
266
209
  }
267
- toJSON() {
268
- return this.reduce((a, i) => {
269
- a.push(JSON.parse(JSON.stringify(i ?? {})));
270
- return a;
271
- }, new Array());
210
+ get isValid() {
211
+ return true;
272
212
  }
273
- map(f, params) {
274
- return toList(super.map(f, params));
213
+ map(f) {
214
+ return tryTo(f, this.value);
275
215
  }
276
- flatMap(f, params) {
277
- return toList(super.flatMap(f, params));
216
+ recover(f) {
217
+ return this;
278
218
  }
279
- mapDefined(f, params) {
280
- return this.map(f, params).defined();
219
+ recoverFrom(type, f) {
220
+ return this;
281
221
  }
282
- mapAsync(f) {
283
- return Promise.all(super.map((e) => f(e))).then((a) => toList(a));
222
+ accept(f) {
223
+ return tryTo(() => {
224
+ f(this.value);
225
+ return this;
226
+ });
284
227
  }
285
- mapSerial(f) {
286
- return super.reduce((p, item) => p.then((results) => on(results, async (r) => r.push(await f(item)))), Promise.resolve(toList()));
228
+ filter(predicate) {
229
+ return tryTo(() => {
230
+ if (predicate(this.value))
231
+ return this;
232
+ throw new Error(`Applying filter(${predicate.toString()}) failed.`);
233
+ });
287
234
  }
288
- distinct() {
289
- return this.filter((i, index) => this.indexOf(i) === index);
235
+ or(value) {
236
+ return this.value;
290
237
  }
291
- distinctByKey(key) {
292
- const seen = /* @__PURE__ */ new Set();
293
- return this.filter((item) => !seen.has(item[key]) && seen.add(item[key]));
238
+ orElse(value) {
239
+ return this.value;
294
240
  }
295
- distinctByValue() {
296
- const seen = /* @__PURE__ */ new Set();
297
- return this.filter((item) => !seen.has(JSON.stringify(item)) && seen.add(JSON.stringify(item)));
241
+ orThrow(_error) {
242
+ return this.value;
298
243
  }
299
- filter(p, params) {
300
- return toList(super.filter(p, params));
244
+ };
245
+ var Failure = class _Failure extends Try {
246
+ constructor(error) {
247
+ super();
248
+ this.error = error;
301
249
  }
302
- sum(p) {
303
- return this.reduce((sum, i) => sum + p(i), 0);
250
+ get value() {
251
+ throw this.error;
304
252
  }
305
- max(p) {
306
- return this.desc(p).first();
253
+ get isValid() {
254
+ return false;
307
255
  }
308
- min(p) {
309
- return this.asc(p).first();
256
+ map(f) {
257
+ return new _Failure(this.error);
310
258
  }
311
- byId(id) {
312
- return this.first((i) => equals(i.id, id));
259
+ recover(f) {
260
+ return tryTo(f, this.error);
313
261
  }
314
- byKey(key, value) {
315
- return this.first((i) => i[key] == value);
262
+ recoverFrom(type, f) {
263
+ return tryTo(() => this.error instanceof type ? this.recover(f) : this);
316
264
  }
317
- add(...items) {
318
- super.push(...toArray(...items));
265
+ accept(f) {
319
266
  return this;
320
267
  }
321
- concat(...items) {
322
- return toList(super.concat(...items));
268
+ filter(predicate) {
269
+ return this;
323
270
  }
324
- reverse() {
325
- return toList(super.reverse());
271
+ or(value) {
272
+ return ofGet(value);
326
273
  }
327
- splice(start, deleteCount, ...items) {
328
- return toList(super.splice(start, deleteCount, ...items));
274
+ orElse(value) {
275
+ return ofGet(value);
329
276
  }
330
- remove(item) {
331
- const index = this.indexOf(item);
332
- if (index > -1) {
333
- this.splice(index, 1);
334
- }
335
- return this;
277
+ orThrow(error) {
278
+ throw ofConstruct(error);
336
279
  }
337
- move(sourceId, destinationId) {
338
- return this.moveOn("id", sourceId, destinationId);
280
+ };
281
+ var tryTo = (c, ...args) => Try.of(c, ...args);
282
+
283
+ // src/types/Template.ts
284
+ var Template = class {
285
+ constructor(template2, subject = {}, options = {}) {
286
+ this.template = template2;
287
+ this.subject = subject;
288
+ this.options = options;
339
289
  }
340
- moveOn(key, sourceId, destinationId) {
341
- const source = this.findIndex((i) => equals(i[key], sourceId));
342
- const dest = this.findIndex((i) => equals(i[key], destinationId));
343
- return on(toList(...this), (r) => r.splice(dest, 0, ...r.splice(source, 1)));
290
+ knownPrefixes = /* @__PURE__ */ new Set(["typeof", "property", "actual", "subject"]);
291
+ toString = () => {
292
+ return entries(this.options).reduce((t, [k]) => this.option(t, k), this.object()).replace(" ", " ");
293
+ };
294
+ props = (tmpl, key, result = toList()) => {
295
+ const i1 = tmpl.indexOf(`{${key}`);
296
+ if (i1 < 0) {
297
+ return result;
298
+ }
299
+ const i2 = tmpl.indexOf("}", i1);
300
+ return this.props(tmpl.slice(i2 + 1), key, result.add(tmpl.substring(i1 + 1, i2)));
301
+ };
302
+ object = () => tryTo(() => this.template.replace(/\{this\./g, "{")).map((t) => t.replace(/\{([^{}]+)}/g, (match, p) => this.knownPrefixes.has(p.split(".")[0]) ? match : textValue(this.subject, p))).or("");
303
+ option = (tmpl, prop) => {
304
+ return this.props(tmpl, prop).reduce((t, p) => t.replace(`{${p}}`, textValue(this.options, p)), tmpl);
305
+ };
306
+ };
307
+ function template(tmpl, subject, options = {}) {
308
+ return new Template(asString(tmpl), subject, {
309
+ typeof: toName(subject),
310
+ subject: text(JSON.stringify(subject)),
311
+ ...options
312
+ });
313
+ }
314
+
315
+ // src/types/ToText.ts
316
+ var ToText = class {
317
+ constructor(subject) {
318
+ this.subject = subject;
344
319
  }
345
- replace(key, item) {
346
- const index = this.findIndex((i) => i[key] === item?.[key]);
347
- ifTrue(index != -1, () => this.splice(index, 1, item));
348
- return this;
320
+ get cap() {
321
+ return this.map((s) => capitalize(s?.toLowerCase()));
349
322
  }
350
- switch(item) {
351
- return this.includes(item) ? this.remove(item) : this.add(item);
323
+ get capFirst() {
324
+ return this.map((s) => capitalize(s));
352
325
  }
353
- switchOn(item, on2) {
354
- return use(
355
- typeof on2 === "function" ? this.find(on2) : use(traverse(item, on2), (v) => this.find((i) => traverse(i, on2) === v)),
356
- (i) => i ? this.remove(i) : this.add(item)
326
+ get title() {
327
+ return this.map(
328
+ (s) => s.split(" ").map((w) => text(w).cap).join(" ")
357
329
  );
358
330
  }
359
- defined() {
360
- return this.reduce((l, v) => isDefined(v) ? l.add(v) : l, toList());
331
+ get pascal() {
332
+ return this.title.replace(" ", "");
361
333
  }
362
- toObject(key, options = {}) {
363
- return this.reduce((o, i) => {
364
- o[i[key]] = i;
365
- if (options.deleteKey)
366
- delete o[i[key]][key];
367
- return o;
368
- }, {});
334
+ get lower() {
335
+ return this.map((s) => s.toLowerCase());
369
336
  }
370
- toObjectList(key) {
371
- return this.reduce(
372
- (a, t) => {
373
- const k = t[key];
374
- a[k] = a[k] ?? toList();
375
- a[k].push(t);
376
- return a;
377
- },
378
- {}
337
+ get upper() {
338
+ return this.map((s) => s.toUpperCase());
339
+ }
340
+ get camel() {
341
+ return this.title.trim.map((s) => `${s.charAt(0).toLowerCase()}${s.slice(1)}`);
342
+ }
343
+ get kebab() {
344
+ return this.lower.replace(" ", "-");
345
+ }
346
+ get strictKebab() {
347
+ return this.map((s) => s.replace(/[^a-z\d]+/gi, " ").trim()).kebab;
348
+ }
349
+ get slug() {
350
+ return this.map(
351
+ (s) => s.replace(/ß/g, "ss").normalize("NFKD").replace(/[\u0300-\u036F]/g, "").toLowerCase().replace(/[^a-z\d]+/g, "-").replace(/(^-)|(-$)/g, "")
379
352
  );
380
353
  }
381
- orElse(...alt) {
382
- return !isEmpty(this) ? this : !isEmpty(...alt) ? toList(...alt) : void 0;
354
+ get snake() {
355
+ return this.upper.replace(" ", "_");
383
356
  }
384
- weave(insertFrom, interval) {
385
- for (let i = interval, n = 0; i <= this.length && n < insertFrom.length; i += interval + 1) {
386
- this.splice(i, 0, insertFrom[n++]);
387
- }
388
- return this;
357
+ get plural() {
358
+ return this.ifLike("") ?? this.add("s");
389
359
  }
390
- slice(start, end) {
391
- return toList(super.slice(start, end));
360
+ get space() {
361
+ return this.map((s) => s.replace(/([a-z0-9])([A-Z])/g, "$1 $2").replace(/[_-]/g, " "));
392
362
  }
393
- none(p) {
394
- return !this.some(p);
363
+ get sentence() {
364
+ return this.isEmpty ? this : this.map((s) => `${s.charAt(0).toUpperCase()}${s.slice(1)}.`);
395
365
  }
396
- chunk(chunkSize) {
397
- return this.reduce((acc, _, index) => index % chunkSize === 0 ? on(acc, (a) => a.push(this.slice(index, index + chunkSize))) : acc, toList());
366
+ get initials() {
367
+ return this.map(
368
+ (s) => s.split(" ").map((w) => w[0]).join("")
369
+ );
398
370
  }
399
- //we needed to add U because of a Typescript issue with generics
400
- update(p, val) {
401
- return this.map((v, i, a) => p(v, i, a) ? ofGet(val, v, i, a) : v);
371
+ get trim() {
372
+ return this.map((s) => s.replace(/[- ,_#]/g, ""));
402
373
  }
403
- updateFirst(p, val) {
404
- const index = this.findIndex(p);
405
- return this.update((t, i) => p(t, i, this) && i == index, val);
374
+ get trimSentence() {
375
+ return this.map(
376
+ (s) => s.replace(/[\u200B-\u200D\uFEFF]/g, "").replace(/[\s\u00A0]+/g, " ").replace(/\s+([.,;:!?])/g, "$1").trim()
377
+ );
406
378
  }
407
- updateFirstById(id, val) {
408
- return this.updateFirst((i) => equals(i?.id, id), val);
379
+ get isEmpty() {
380
+ return isEmpty(this.toString());
409
381
  }
410
- updateById(id, val) {
411
- return this.update((i) => equals(i?.id, id), val);
382
+ parse = (subject, options = {}) => text(template(this.subject, subject, { typeof: toName(subject), ...options }));
383
+ is = (...others) => others.some((o) => this.toString() === text(o).toString());
384
+ equals = this.is;
385
+ like = (...others) => others.some((o) => this.trim.lower.toString().includes(text(o).trim.lower.toString()));
386
+ isLike = (...others) => others.some((o) => this.trim.lower.is(text(o).trim.lower));
387
+ ifLike = (...others) => this.isLike(...others) ? this : void 0;
388
+ endsWith = (end) => this.subject.endsWith(asString(end));
389
+ startsWith = (end) => this.subject.startsWith(asString(end));
390
+ first = (n) => this.map((s) => s.substring(0, n));
391
+ last = (n) => this.map((s) => s.substring(s.length - n));
392
+ map = (func) => text(ofGet(func, this.subject));
393
+ replace = (search, replace) => this.map((s) => replaceAll(s, search, replace));
394
+ add = (add, separator = "") => this.map((s) => isNotEmpty(add) ? `${s}${separator}${text(add)}` : s);
395
+ with = (separator, ...other) => this.map(
396
+ (s) => toList(s).add(...other.map((u) => text(u).toString())).filter((s2) => isNotEmpty(s2)).join(separator)
397
+ );
398
+ split = (separator = " ") => toList(this.subject.split(separator));
399
+ toString() {
400
+ return this.subject;
412
401
  }
413
- groupBy(by) {
414
- return toList(
415
- Array.from(this.reduce((m, v, i, a) => use(by(v, i, a), (key) => m.set(key, (m.get(key) ?? toList()).add(v))), /* @__PURE__ */ new Map()).values())
416
- );
402
+ toJSON() {
403
+ return this.subject;
417
404
  }
418
405
  };
419
- var toList = (...items) => new List().add(...items);
420
- var isList = (l) => isDefined(l) && isArray(l) && isA(l, "first", "last", "asc", "desc");
421
- var asList = (c, items = []) => toList(toArray(items).map((i) => new c(i)));
422
- var maxValue = (l, key) => l.desc(key).first()?.[key];
423
- var minValue = (l, key) => l.asc(key).first()?.[key];
424
-
425
- // src/types/Object.ts
426
- function entries(subject) {
427
- return ifDefined(
428
- subject,
429
- () => toList([...Object.entries(subject), ...Object.entries(Object.getPrototypeOf(subject) ?? {})]),
430
- () => toList()
431
- );
432
- }
433
- function values(subject) {
434
- return toList([...Object.values(subject), ...Object.values(Object.getPrototypeOf(subject) ?? {})]);
435
- }
436
- function keys(subject) {
437
- return toList([...Object.keys(subject), ...Object.keys(Object.getPrototypeOf(subject) ?? {})]);
406
+ function text(subject, alt = "") {
407
+ const sub = subject ? asString(subject) : alt;
408
+ return new ToText(sub !== "[object Object]" ? sub : "");
438
409
  }
439
- function extractKeys(obj, keys2) {
440
- return keys2.filter((k) => k in obj).reduce(
441
- (acc, key) => {
442
- acc.keys[key] = obj[key];
443
- delete acc[key];
444
- return acc;
445
- },
446
- { keys: {}, ...obj }
447
- );
410
+ function textValue(subject, prop) {
411
+ const p = prop.split(".");
412
+ const root = subject?.[p[0]];
413
+ const initial = typeof root === "object" && root !== null ? root : text(root);
414
+ return p.splice(1).reduce((t, s) => (typeof t === "object" && t !== null ? t : text(t))?.[s], initial)?.toString() ?? "";
448
415
  }
449
416
 
450
- // src/utils/Promise.ts
451
- var resolve = (subject) => Promise.resolve(subject);
452
- var reject = (e) => Promise.reject(e);
453
- var tuple = {
454
- 2: (first, second) => Promise.all([first, second]),
455
- 3: (first, second, third) => Promise.all([first, second, third]),
456
- 4: (first, second, third, forth) => Promise.all([first, second, third, forth]),
457
- 5: (first, second, third, forth, fifth) => Promise.all([first, second, third, forth, fifth]),
458
- all: (first, second) => Promise.all([first, Promise.all(second)]),
459
- spread: (first, ...second) => Promise.all([first, Promise.all(toArray(second))]),
460
- list: (list) => Promise.all([...list]).then(toList),
461
- settled: (list) => Promise.allSettled([...list]).then((rs) => ({
462
- fulfilled: toList(...rs.filter((r) => r.status === "fulfilled").map((r) => r.value)),
463
- rejected: toList(...rs.filter((r) => r.status === "rejected").map((r) => asString(r.reason)))
464
- })),
465
- object: (obj) => use(keys(obj), (ks) => tuple.list(values(obj)).then((vs) => vs.reduce((acc, v, i) => on(acc, (a) => a[ks[i]] = v), {})))
417
+ // src/types/Exception.ts
418
+ var Exception = class _Exception extends Enum {
419
+ constructor(message, id, reason) {
420
+ super(message, id ?? text(message).pascal.toString());
421
+ this.message = message;
422
+ this.reason = reason;
423
+ }
424
+ static AlreadyExists = new _Exception("Subject already exists");
425
+ static DoesNotExist = new _Exception("Does not exist");
426
+ static IsMissingId = new _Exception("Subject is missing an id");
427
+ static IsNotImplemented = new _Exception("Is not implemented");
428
+ static IsNotValid = new _Exception("Is not valid");
429
+ static Unknown = new _Exception("Unknown error");
430
+ static CouldNotExecute = (target) => new _Exception(`Could not execute ${target}.`, "CouldNotExecute");
431
+ static CouldNotValidate = (target) => new _Exception(`Could not validate ${target}.`, "CouldNotValidate");
432
+ static EnvironmentVariableNotFound = (variable) => new _Exception(`Environment variable ${text(variable).upper} could not be found.`, "EnvironmentVariableNotFound");
433
+ because = (reason) => new _Exception(this.message, this.id, reason);
466
434
  };
467
- var tuple2 = tuple[2];
468
- var tuple3 = tuple[3];
469
- var tuple4 = tuple[4];
470
- var tuple5 = tuple[5];
471
- var tupleO = tuple.object;
472
- var settled = tuple.settled;
435
+ var isException = (e, t) => e instanceof Exception && (isDefined(t) ? e.equals(asString(t)) : true);
436
+ var isDoesNotExist = (e) => e instanceof Exception && Exception.DoesNotExist.equals(e);
473
437
 
474
- // src/types/Meta.ts
475
- import "reflect-metadata";
476
- var ClassMeta = class {
477
- constructor(subject, data = (subject.prototype ?? subject).constructor) {
438
+ // src/validation/When.ts
439
+ var When = class _When {
440
+ constructor(subject, valid = true, results) {
478
441
  this.subject = subject;
479
- this.data = data;
442
+ this.valid = valid;
443
+ this.results = results;
480
444
  }
481
- get = (key) => Reflect.getMetadata(key, this.data);
482
- set = (key, value) => {
483
- Reflect.defineMetadata(key, value, this.data);
484
- return value;
485
- };
486
- entries = () => entries(this.subject);
487
- parse = (f, options = {}) => {
488
- const { initial = {}, skipUndefined = false } = options;
489
- return this.entries().reduce((a, [k, val]) => use(f(val), (v) => isDefined(v) || !skipUndefined ? on(a, (a2) => a2[k] = v) : a), initial);
490
- };
491
- properties = (key) => toList([...Object.getOwnPropertyNames(this.subject), ...Object.getOwnPropertyNames(Object.getPrototypeOf(this.subject))]).map((p) => this.property(p)).filter((p) => key ? p.get(key) : p);
492
- keys = (key) => this.properties().map((p) => p.get(key)).reduce((list, u) => u ? list.add(u) : list, toList());
493
- values = () => values(this.subject);
494
- property = (property) => new PropertyMeta(this.subject, property);
495
- };
496
- var PropertyMeta = class {
497
- constructor(subject, property, data = Reflect.getMetadata(property, subject)) {
498
- this.subject = subject;
499
- this.property = property;
500
- this.data = data;
445
+ get not() {
446
+ return this.clone(!this.valid);
501
447
  }
502
- get value() {
503
- return this.subject[this.property];
448
+ get and() {
449
+ return this.clone(this.valid);
504
450
  }
505
- get = (key) => isDefined(this.data) && isDefined(this.data[key]) ? this.data[key] : void 0;
506
- set = (key, value) => {
507
- Reflect.defineMetadata(this.property, { ...this.data, [key]: value }, this.subject);
508
- return value;
509
- };
451
+ get isDefined() {
452
+ return this.clone(this.valid === isDefined(this.subject));
453
+ }
454
+ get isEmpty() {
455
+ return this.clone(this.valid === isEmpty(this.subject));
456
+ }
457
+ get isTrue() {
458
+ return this.clone(this.valid === !!this.subject);
459
+ }
460
+ get isValid() {
461
+ this.results = validate(this.subject);
462
+ return this.clone(this.valid === this.results.isValid);
463
+ }
464
+ isInstance = (c) => this.clone(this.valid === this.subject instanceof c);
465
+ with = (pred) => this.clone(this.valid === ofGet(pred, this.subject));
466
+ contains = (property) => this.clone(this.valid === isDefined(ofGet(property, this.subject)));
467
+ in = (...items) => this.clone(this.valid === isIn(this.subject, toArray(...items)));
468
+ is = (item) => this.clone(this.valid === (this.subject === item));
469
+ reject = (error) => !this.valid ? resolve(this.subject) : reject(ofGet(error, this.subject) ?? this.results ?? Exception.Unknown);
470
+ recover = (f) => resolve(!this.valid ? this.subject : f(this.subject));
471
+ clone = (result = true) => new _When(this.subject, result, this.results);
510
472
  };
511
- var meta = (subject) => new ClassMeta(subject ?? {});
473
+ var when = (subject) => new When(subject);
512
474
 
513
- // src/types/Enum.ts
514
- var isEnumConstructor = (c) => isFunction(c) && c.isEnum;
515
- var Enum = class {
516
- constructor(name, id = name.toLowerCase(), code = id.toString()) {
517
- this.name = name;
518
- this.id = id;
519
- this.code = code;
475
+ // src/types/Context.ts
476
+ var DotEnvContext = class {
477
+ domain = process.env.DOMAIN ?? "easy";
478
+ name = process.env.ENVIRONMENT_NAME ?? "";
479
+ host = process.env.HOST ?? "";
480
+ port = Number.parseInt(process.env.PORT ?? "8080");
481
+ app = process.env.APP ?? "";
482
+ get = (key, alt) => {
483
+ const k = text(key).map((k2) => k2.replace(/([a-z])([A-Z])/g, "$1 $2")).snake.toString();
484
+ return process.env[k] ?? alt;
485
+ };
486
+ };
487
+ var BaseRequestContext = class {
488
+ state = {};
489
+ get token() {
490
+ return this.get("token");
520
491
  }
521
- static isEnum = true;
522
- get isValid() {
523
- return isDefined(this.id);
492
+ set token(token) {
493
+ this.set("token", token);
494
+ }
495
+ get identity() {
496
+ return this.token;
497
+ }
498
+ get jwt() {
499
+ return this.get("jwt");
500
+ }
501
+ set jwt(jwt) {
502
+ this.set("jwt", jwt);
503
+ }
504
+ get correlationId() {
505
+ return this.get("correlationId");
506
+ }
507
+ set correlationId(id) {
508
+ this.set("correlationId", id);
509
+ }
510
+ get lastError() {
511
+ return this.get("lastError");
512
+ }
513
+ set lastError(error) {
514
+ this.set("lastError", error);
515
+ }
516
+ get(key) {
517
+ return this.state[key];
524
518
  }
525
- static all() {
526
- return meta(this.allTuple()).values();
519
+ set(key, value) {
520
+ return this.state[key] = value;
527
521
  }
528
- static filter(p, params) {
529
- return this.all().filter(p, params);
522
+ create = (f) => f();
523
+ wrap = (f) => f();
524
+ };
525
+ var BaseContext = class extends BaseRequestContext {
526
+ };
527
+ var Context = class {
528
+ constructor(state = {}) {
529
+ this.state = state;
530
+ this.state = {
531
+ ...{
532
+ env: new DotEnvContext(),
533
+ request: new BaseRequestContext(),
534
+ other: {}
535
+ },
536
+ ...this.state
537
+ };
530
538
  }
531
- static first(p, params) {
532
- return this.all().first(p, params);
539
+ get env() {
540
+ return this.state.env;
533
541
  }
534
- static byIds(ids = []) {
535
- return toList(ids).mapDefined((id) => this.byId(id)).distinct();
542
+ set env(env) {
543
+ this.state.env = env;
536
544
  }
537
- static byId(id, alt) {
538
- return this.allTuple()[id] ?? ofGet(alt);
545
+ get request() {
546
+ return this.state.request;
539
547
  }
540
- static allTuple() {
541
- return meta(this).get(`all-${this.name}`) ?? meta(this).set(`all-${this.name}`, meta(this).values().filter(isEnum).toObject("id"));
548
+ set request(request) {
549
+ this.state.request = request;
542
550
  }
543
- equals(other) {
544
- return this.id === (isEnum(other) ? other.id : other);
551
+ get other() {
552
+ return this.state.other;
545
553
  }
546
- isIn(...items) {
547
- return items.some((i) => this.equals(i));
554
+ };
555
+ var ctx = new Context();
556
+
557
+ // src/types/Result.ts
558
+ var toResult = (message, location, domain = ctx.env.domain) => ({
559
+ message: message.toString(),
560
+ location: location?.toString(),
561
+ domain: domain?.toString()
562
+ });
563
+ var isResult = (r) => isA(r, "message");
564
+
565
+ // src/types/Results.ts
566
+ var parse = (...rs) => toArray(...rs).map((r) => isResult(r) ? r : toResult(r));
567
+ var Results = class {
568
+ results;
569
+ constructor(...rs) {
570
+ this.results = parse(...rs);
548
571
  }
549
- toJSON() {
550
- return this.id;
572
+ get length() {
573
+ return this.results.length;
551
574
  }
552
- toString() {
553
- return this.id.toString();
575
+ get isValid() {
576
+ return this.results.length === 0;
554
577
  }
578
+ add = (...rs) => toResults(...this.results, ...parse(...rs));
555
579
  };
556
- var isEnum = (e) => isDefined(e) && e instanceof Enum && isAn(e, "name", "id", "code");
580
+ var toResults = (...r) => new Results(...r);
581
+ var isResults = (r) => isDefined(r) && r instanceof Results;
557
582
 
558
- // src/types/Template.ts
559
- var Template = class {
560
- constructor(template2, subject = {}, options = {}) {
561
- this.template = template2;
562
- this.subject = subject;
563
- this.options = options;
583
+ // src/validation/Validate.ts
584
+ var asResults = (subject, template2, options = {}) => toResults(toResult(text(template2).parse(subject, options), toName(subject)));
585
+ var validators = (subject) => meta(subject).keys("constraint").reduce((list, vs) => list.add(vs), toList());
586
+ var runValidator = (v, subject) => {
587
+ try {
588
+ const actual = isFunction(subject[v.property]) ? subject[v.property]() : subject[v.property];
589
+ const constraint = v.constraint(actual);
590
+ return isResults(constraint) ? constraint : !constraint ? asResults(subject, v.text, {
591
+ ...v,
592
+ actual
593
+ }) : toResults();
594
+ } catch (e) {
595
+ return asResults(subject, asString(e));
564
596
  }
565
- knownPrefixes = /* @__PURE__ */ new Set(["typeof", "property", "actual", "subject"]);
566
- toString = () => {
567
- return entries(this.options).reduce((t, [k]) => this.option(t, k), this.object()).replace(" ", " ");
568
- };
569
- props = (tmpl, key, result = toList()) => {
570
- const i1 = tmpl.indexOf(`{${key}`);
571
- if (i1 < 0) {
572
- return result;
597
+ };
598
+ var constraints = (subject) => validators(subject).map((vs) => runValidator(vs, subject)).reduce((rs, r) => rs.add(...r.results), toResults());
599
+ var validate = (subject) => choose(subject).is.not.defined(
600
+ (s) => s,
601
+ () => toResults("Subject is not defined.")
602
+ ).type(isEnum, (e) => e.isValid ? toResults() : asResults(e, "This is not a valid {typeof}.")).type(isValue, (v) => v.isValid ? toResults() : asResults(v, "This is not a valid {typeof}.")).type(isArray, (a) => a.map((i) => validate(i)).reduce((rs, r) => rs.add(...r.results), toResults())).type(isValidatable, (v) => constraints(v)).else(toResults());
603
+ var validateReject = (subject) => when(subject).not.isValid.reject();
604
+ var isValid = (t) => validate(t).isValid;
605
+
606
+ // src/types/Case.ts
607
+ var CaseBuilder = class {
608
+ constructor(v) {
609
+ this.v = v;
610
+ }
611
+ is = {
612
+ true: (pred, out) => this.case(pred, out),
613
+ false: (pred, out) => this.case(!ofGet(pred, this.v), out),
614
+ truthy: (prop, out) => this.case(!!prop(this.v), out),
615
+ falsy: (prop, out) => this.case(!prop(this.v), out),
616
+ defined: (prop, out) => this.case(isDefined(prop(this.v)), out),
617
+ empty: (prop, out) => this.case(isEmpty(prop(this.v)), out),
618
+ valid: (prop, out) => this.case(validate(prop(this.v)).isValid, out),
619
+ in: (prop, out) => this.case(ofGet(prop, this.v).includes(this.v), out),
620
+ type: (guard, out) => this.type(guard, out),
621
+ not: {
622
+ true: (pred, out) => this.case(!ofGet(pred, this.v), out),
623
+ false: (pred, out) => this.case(pred, out),
624
+ truthy: (prop, out) => this.case(!prop(this.v), out),
625
+ falsy: (prop, out) => this.case(!!prop(this.v), out),
626
+ defined: (prop, out) => this.case(!isDefined(prop(this.v)), out),
627
+ empty: (prop, out) => this.case(!isEmpty(prop(this.v)), out),
628
+ valid: (prop, out) => this.case(!validate(prop(this.v)).isValid, out),
629
+ in: (prop, out) => this.case(!ofGet(prop, this.v).includes(this.v), out),
630
+ type: (guard, out) => this.case((v) => !guard(v), out)
573
631
  }
574
- const i2 = tmpl.indexOf("}", i1);
575
- return this.props(tmpl.slice(i2 + 1), key, result.add(tmpl.substring(i1 + 1, i2)));
576
- };
577
- object = () => tryTo(() => this.template.replace(/\{this\./g, "{")).map((t) => t.replace(/\{([^{}]+)}/g, (match, p) => this.knownPrefixes.has(p.split(".")[0]) ? match : textValue(this.subject, p))).or("");
578
- option = (tmpl, prop) => {
579
- return this.props(tmpl, prop).reduce((t, p) => t.replace(`{${p}}`, textValue(this.options, p)), tmpl);
580
632
  };
633
+ if = this.is;
634
+ case(pred, out) {
635
+ return new Case(this.v).case(pred, out);
636
+ }
637
+ type(guard, out) {
638
+ return new Case(this.v).type(guard, out);
639
+ }
640
+ equals(value, out) {
641
+ return new Case(this.v).equals(value, out);
642
+ }
581
643
  };
582
- function template(tmpl, subject, options = {}) {
583
- return new Template(asString(tmpl), subject, {
584
- typeof: toName(subject),
585
- subject: text(JSON.stringify(subject)),
586
- ...options
587
- });
588
- }
589
-
590
- // src/types/ToText.ts
591
- var ToText = class {
592
- constructor(subject) {
593
- this.subject = subject;
644
+ var Case = class {
645
+ constructor(value, outcome) {
646
+ this.value = value;
647
+ this.outcome = outcome;
594
648
  }
595
- get cap() {
596
- return this.map((s) => capitalize(s?.toLowerCase()));
649
+ is = {
650
+ true: (pred, out) => this.case(pred, out),
651
+ false: (pred, out) => this.case(!ofGet(pred, this.value), out),
652
+ truthy: (prop, out) => this.case(!!prop(this.value), out),
653
+ falsy: (prop, out) => this.case(!prop(this.value), out),
654
+ defined: (prop, out) => this.case(isDefined(prop(this.value)), out),
655
+ empty: (prop, out) => this.case(isEmpty(prop(this.value)), out),
656
+ valid: (prop, out) => this.case(validate(prop(this.value)).isValid, out),
657
+ in: (prop, out) => this.case(ofGet(prop, this.value).includes(this.value), out),
658
+ type: (guard, out) => this.type(guard, out),
659
+ not: {
660
+ true: (pred, out) => this.case(!ofGet(pred, this.value), out),
661
+ false: (pred, out) => this.case(pred, out),
662
+ truthy: (prop, out) => this.case(!prop(this.value), out),
663
+ falsy: (prop, out) => this.case(!!prop(this.value), out),
664
+ defined: (prop, out) => this.case(!isDefined(prop(this.value)), out),
665
+ empty: (prop, out) => this.case(!isEmpty(prop(this.value)), out),
666
+ valid: (prop, out) => this.case(!validate(prop(this.value)).isValid, out),
667
+ in: (prop, out) => this.case(!ofGet(prop, this.value).includes(this.value), out),
668
+ type: (guard, out) => this.case((v) => !guard(v), out)
669
+ }
670
+ };
671
+ if = this.is;
672
+ case(pred, out) {
673
+ return tryTo(pred, this.value).is.true().map(() => ofGet(out, this.value)).map((res) => new Found(this.value, res)).or(this);
597
674
  }
598
- get capFirst() {
599
- return this.map((s) => capitalize(s));
675
+ type(guard, out) {
676
+ return tryTo(guard, this.value).is.true().map(() => out(this.value)).map((res) => new Found(this.value, res)).or(this);
600
677
  }
601
- get title() {
602
- return this.map(
603
- (s) => s.split(" ").map((w) => text(w).cap).join(" ")
604
- );
678
+ equals(value, out) {
679
+ return this.case(this.value === value, out);
605
680
  }
606
- get pascal() {
607
- return this.title.replace(" ", "");
681
+ else(alt) {
682
+ return ofGet(alt, this.value);
608
683
  }
609
- get lower() {
610
- return this.map((s) => s.toLowerCase());
684
+ };
685
+ var Found = class extends Case {
686
+ constructor(value, outcome) {
687
+ super(value, outcome);
688
+ this.value = value;
689
+ this.outcome = outcome;
611
690
  }
612
- get upper() {
613
- return this.map((s) => s.toUpperCase());
691
+ case(pred, out) {
692
+ return this;
614
693
  }
615
- get camel() {
616
- return this.title.trim.map((s) => `${s.charAt(0).toLowerCase()}${s.slice(1)}`);
694
+ type(guard, out) {
695
+ return this;
696
+ }
697
+ equals(value, out) {
698
+ return this;
699
+ }
700
+ else(alt) {
701
+ return this.outcome;
702
+ }
703
+ };
704
+ var choose = (value) => new CaseBuilder(value);
705
+
706
+ // src/types/IsEqual.ts
707
+ var isEqualArray = (one, another) => choose([one, another]).case(([o, a]) => !isArray(o) || !isArray(a), false).case(([o, a]) => o?.length !== a?.length, false).else(([o, a]) => !o.some((v, i) => !isEqual(v, a[i])));
708
+ var isEqualObject = (one, another) => choose([Object.keys(one), Object.keys(another)]).case(([keysO, keysA]) => keysO.length !== keysA.length, false).case(([keysO, keysA]) => keysO.some((k) => !keysA.includes(k)), false).case(([keysO]) => keysO.some((k) => !isEqual(one[k], another[k])), false).else(true);
709
+ var isEqual = (one, another) => choose([one, another]).case(([o, a]) => o === a, true).case(
710
+ ([o, a]) => isArray(o) || isArray(a),
711
+ ([o, a]) => isEqualArray(o, a)
712
+ ).case(
713
+ ([o, a]) => o == null || a == null || !isObject(o) && !isObject(a),
714
+ ([o, a]) => o !== o && a !== a
715
+ ).else(([o, a]) => isEqualObject(o, a));
716
+
717
+ // src/utils/If.ts
718
+ function ifTrue(o, f, alt) {
719
+ return isTrue(o) ? ofConstruct(f, o) : ofConstruct(alt, o);
720
+ }
721
+ function ifFalse(o, f, alt) {
722
+ return !isTrue(o) ? ofConstruct(f, o) : ofConstruct(alt, o);
723
+ }
724
+ function ifDefined(o, f, alt) {
725
+ return isDefined(o) ? ofConstruct(f, o) : ofConstruct(alt);
726
+ }
727
+ function ifNotEmpty(o, f = (o2) => o2, alt) {
728
+ return isNotEmpty(o) ? ofConstruct(f, o) : ofConstruct(alt, o);
729
+ }
730
+ function ifEmpty(o, f = (o2) => o2, alt) {
731
+ return isEmpty(o) ? ofConstruct(f, o) : ofConstruct(alt, o);
732
+ }
733
+ function given(o, key, alt) {
734
+ return isDefined(o) && key in o ? o[key] : ofGet(alt);
735
+ }
736
+ function ifEqual(one, another, f, alt) {
737
+ return isEqual(one, another) ? ofConstruct(f) : ofConstruct(alt);
738
+ }
739
+ function ifEither(os, f = (o) => o, alt) {
740
+ return use(
741
+ toList(os).first((o) => isPresent(o)),
742
+ (o) => isDefined(o) ? ofConstruct(f, o) : ofConstruct(alt, o)
743
+ );
744
+ }
745
+
746
+ // src/types/List.ts
747
+ var List8 = class extends Array {
748
+ get ids() {
749
+ return this.mapDefined((i) => i.id);
617
750
  }
618
- get kebab() {
619
- return this.lower.replace(" ", "-");
751
+ get indexed() {
752
+ return this.map((v, index) => ({ ...v, index }));
620
753
  }
621
- get strictKebab() {
622
- return this.map((s) => s.replace(/[^a-z\d]+/gi, " ").trim()).kebab;
754
+ isSubSetOf(...items) {
755
+ return this.diff(items).length === 0;
623
756
  }
624
- get slug() {
625
- return this.map(
626
- (s) => s.replace(/ß/g, "ss").normalize("NFKD").replace(/[\u0300-\u036F]/g, "").toLowerCase().replace(/[^a-z\d]+/g, "-").replace(/(^-)|(-$)/g, "")
627
- );
757
+ isSuperSetOf(...items) {
758
+ return this.length > items.length && toList(...items).isSubSetOf(...this);
628
759
  }
629
- get snake() {
630
- return this.upper.replace(" ", "_");
760
+ isIntersectingWith(...items) {
761
+ return this.intersect(items).length > 0;
631
762
  }
632
- get plural() {
633
- return this.ifLike("") ?? this.add("s");
763
+ isDisjointWith(...items) {
764
+ return !this.isIntersectingWith(...items);
634
765
  }
635
- get space() {
636
- return this.map((s) => s.replace(/([a-z0-9])([A-Z])/g, "$1 $2").replace(/[_-]/g, " "));
766
+ areEqual(...items) {
767
+ return this.isSubSetOf(...items) && toList(...items).isSubSetOf(...this);
637
768
  }
638
- get sentence() {
639
- return this.isEmpty ? this : this.map((s) => `${s.charAt(0).toUpperCase()}${s.slice(1)}.`);
769
+ asc(p) {
770
+ return toList(...this).sort((e1, e2) => ofProperty(e1, p) > ofProperty(e2, p) ? 1 : -1);
640
771
  }
641
- get initials() {
642
- return this.map(
643
- (s) => s.split(" ").map((w) => w[0]).join("")
644
- );
772
+ desc(p) {
773
+ return toList(...this).sort((e1, e2) => ofProperty(e1, p) < ofProperty(e2, p) ? 1 : -1);
645
774
  }
646
- get trim() {
647
- return this.map((s) => s.replace(/[- ,_#]/g, ""));
775
+ first(p, params) {
776
+ return p ? this.find(p, params) : this[0];
648
777
  }
649
- get trimSentence() {
650
- return this.map(
651
- (s) => s.replace(/[\u200B-\u200D\uFEFF]/g, "").replace(/[\s\u00A0]+/g, " ").replace(/\s+([.,;:!?])/g, "$1").trim()
778
+ firstValue(f, alt) {
779
+ return ifDefined(
780
+ this.first((t) => !!f(t)),
781
+ f,
782
+ (v) => ofGet(alt, v)
652
783
  );
653
784
  }
654
- get isEmpty() {
655
- return isEmpty(this.toString());
656
- }
657
- parse = (subject, options = {}) => text(template(this.subject, subject, { typeof: toName(subject), ...options }));
658
- is = (...others) => others.some((o) => this.toString() === text(o).toString());
659
- equals = this.is;
660
- like = (...others) => others.some((o) => this.trim.lower.toString().includes(text(o).trim.lower.toString()));
661
- isLike = (...others) => others.some((o) => this.trim.lower.is(text(o).trim.lower));
662
- ifLike = (...others) => this.isLike(...others) ? this : void 0;
663
- endsWith = (end) => this.subject.endsWith(asString(end));
664
- startsWith = (end) => this.subject.startsWith(asString(end));
665
- first = (n) => this.map((s) => s.substring(0, n));
666
- last = (n) => this.map((s) => s.substring(s.length - n));
667
- map = (func) => text(ofGet(func, this.subject));
668
- replace = (search, replace) => this.map((s) => replaceAll(s, search, replace));
669
- add = (add, separator = "") => this.map((s) => isNotEmpty(add) ? `${s}${separator}${text(add)}` : s);
670
- with = (separator, ...other) => this.map(
671
- (s) => toList(s).add(...other.map((u) => text(u).toString())).filter((s2) => isNotEmpty(s2)).join(separator)
672
- );
673
- split = (separator = " ") => toList(this.subject.split(separator));
674
- toString() {
675
- return this.subject;
676
- }
677
- toJSON() {
678
- return this.subject;
679
- }
680
- };
681
- function text(subject, alt = "") {
682
- const sub = subject ? asString(subject) : alt;
683
- return new ToText(sub !== "[object Object]" ? sub : "");
684
- }
685
- function textValue(subject, prop) {
686
- const p = prop.split(".");
687
- const root = subject?.[p[0]];
688
- const initial = typeof root === "object" && root !== null ? root : text(root);
689
- return p.splice(1).reduce((t, s) => (typeof t === "object" && t !== null ? t : text(t))?.[s], initial)?.toString() ?? "";
690
- }
691
-
692
- // src/types/Exception.ts
693
- var Exception = class _Exception extends Enum {
694
- constructor(message, id, reason) {
695
- super(message, id ?? text(message).pascal.toString());
696
- this.message = message;
697
- this.reason = reason;
785
+ isFirst(value) {
786
+ return value === this.first();
698
787
  }
699
- static AlreadyExists = new _Exception("Subject already exists");
700
- static DoesNotExist = new _Exception("Does not exist");
701
- static IsMissingId = new _Exception("Subject is missing an id");
702
- static IsNotImplemented = new _Exception("Is not implemented");
703
- static IsNotValid = new _Exception("Is not valid");
704
- static Unknown = new _Exception("Unknown error");
705
- static CouldNotExecute = (target) => new _Exception(`Could not execute ${target}.`, "CouldNotExecute");
706
- static CouldNotValidate = (target) => new _Exception(`Could not validate ${target}.`, "CouldNotValidate");
707
- static EnvironmentVariableNotFound = (variable) => new _Exception(`Environment variable ${text(variable).upper} could not be found.`, "EnvironmentVariableNotFound");
708
- because = (reason) => new _Exception(this.message, this.id, reason);
709
- };
710
- var isException = (e, t) => e instanceof Exception && (isDefined(t) ? e.equals(asString(t)) : true);
711
- var isDoesNotExist = (e) => e instanceof Exception && Exception.DoesNotExist.equals(e);
712
-
713
- // src/validation/When.ts
714
- var When = class _When {
715
- constructor(subject, valid = true, results) {
716
- this.subject = subject;
717
- this.valid = valid;
718
- this.results = results;
788
+ next(p, params) {
789
+ return p ? this[this.findIndex(p, params) + 1] : this[0];
719
790
  }
720
- get not() {
721
- return this.clone(!this.valid);
791
+ prev(p, params) {
792
+ return p ? this[this.findIndex(p, params) - 1] : this[0];
722
793
  }
723
- get and() {
724
- return this.clone(this.valid);
794
+ last(p, params) {
795
+ return p ? this.filter(p, params).last() : this[this.length - 1];
725
796
  }
726
- get isDefined() {
727
- return this.clone(this.valid === isDefined(this.subject));
797
+ isLast(value) {
798
+ return value === this.last();
728
799
  }
729
- get isEmpty() {
730
- return this.clone(this.valid === isEmpty(this.subject));
800
+ overlaps(...items) {
801
+ return toList(...items).some((i) => this.some((t) => i === t));
731
802
  }
732
- get isTrue() {
733
- return this.clone(this.valid === !!this.subject);
803
+ diff(others) {
804
+ return this.filter((i) => !others.includes(i));
734
805
  }
735
- get isValid() {
736
- this.results = validate(this.subject);
737
- return this.clone(this.valid === this.results.isValid);
806
+ diffByKey(others, key) {
807
+ return this.filter((i) => !others.some((o) => o[key] === i[key]));
738
808
  }
739
- isInstance = (c) => this.clone(this.valid === this.subject instanceof c);
740
- with = (pred) => this.clone(this.valid === ofGet(pred, this.subject));
741
- contains = (property) => this.clone(this.valid === isDefined(ofGet(property, this.subject)));
742
- in = (...items) => this.clone(this.valid === isIn(this.subject, toArray(...items)));
743
- is = (item) => this.clone(this.valid === (this.subject === item));
744
- reject = (error) => !this.valid ? resolve(this.subject) : reject(ofGet(error, this.subject) ?? this.results ?? Exception.Unknown);
745
- recover = (f) => resolve(!this.valid ? this.subject : f(this.subject));
746
- clone = (result = true) => new _When(this.subject, result, this.results);
747
- };
748
- var when = (subject) => new When(subject);
749
-
750
- // src/types/Context.ts
751
- var DotEnvContext = class {
752
- domain = process.env.DOMAIN ?? "easy";
753
- name = process.env.ENVIRONMENT_NAME ?? "";
754
- host = process.env.HOST ?? "";
755
- port = Number.parseInt(process.env.PORT ?? "8080");
756
- app = process.env.APP ?? "";
757
- get = (key, alt) => {
758
- const k = text(key).map((k2) => k2.replace(/([a-z])([A-Z])/g, "$1 $2")).snake.toString();
759
- return process.env[k] ?? alt;
760
- };
761
- };
762
- var BaseRequestContext = class {
763
- state = {};
764
- get token() {
765
- return this.get("token");
809
+ symmetricDiff(others) {
810
+ return this.diff(others).concat(toList(...others).diff(this));
766
811
  }
767
- set token(token) {
768
- this.set("token", token);
812
+ symmetricDiffByKey(others, key) {
813
+ return this.diffByKey(others, key).concat(toList(...others).diffByKey(this, key));
769
814
  }
770
- get identity() {
771
- return this.token;
815
+ intersect(others) {
816
+ return this.filter((i) => others.includes(i));
772
817
  }
773
- get jwt() {
774
- return this.get("jwt");
818
+ intersectByKey(others, key) {
819
+ return this.filter((i) => others.some((o) => o[key] === i[key]));
775
820
  }
776
- set jwt(jwt) {
777
- this.set("jwt", jwt);
821
+ intersectBy(others, f) {
822
+ return this.filter((i) => others.some((o) => f(i, o)));
778
823
  }
779
- get correlationId() {
780
- return this.get("correlationId");
824
+ accumulate(...keys2) {
825
+ return this.map((d, i, arr) => {
826
+ const acc = keys2.reduce((acc2, v) => {
827
+ acc2[v] = i === 0 ? d[v] : arr[i - 1][v] + d[v];
828
+ return acc2;
829
+ }, d);
830
+ arr[i] = acc;
831
+ return acc;
832
+ });
781
833
  }
782
- set correlationId(id) {
783
- this.set("correlationId", id);
834
+ toJSON() {
835
+ return this.reduce((a, i) => {
836
+ a.push(JSON.parse(JSON.stringify(i ?? {})));
837
+ return a;
838
+ }, new Array());
784
839
  }
785
- get lastError() {
786
- return this.get("lastError");
840
+ map(f, params) {
841
+ return toList(super.map(f, params));
787
842
  }
788
- set lastError(error) {
789
- this.set("lastError", error);
843
+ flatMap(f, params) {
844
+ return toList(super.flatMap(f, params));
790
845
  }
791
- get(key) {
792
- return this.state[key];
846
+ mapDefined(f, params) {
847
+ return this.map(f, params).defined();
793
848
  }
794
- set(key, value) {
795
- return this.state[key] = value;
849
+ mapAsync(f) {
850
+ return Promise.all(super.map((e) => f(e))).then((a) => toList(a));
796
851
  }
797
- create = (f) => f();
798
- wrap = (f) => f();
799
- };
800
- var BaseContext = class extends BaseRequestContext {
801
- };
802
- var Context = class {
803
- constructor(state = {}) {
804
- this.state = state;
805
- this.state = {
806
- ...{
807
- env: new DotEnvContext(),
808
- request: new BaseRequestContext(),
809
- other: {}
810
- },
811
- ...this.state
812
- };
852
+ mapSerial(f) {
853
+ return super.reduce((p, item) => p.then((results) => on(results, async (r) => r.push(await f(item)))), Promise.resolve(toList()));
813
854
  }
814
- get env() {
815
- return this.state.env;
855
+ distinct() {
856
+ return this.filter((i, index) => this.indexOf(i) === index);
816
857
  }
817
- set env(env) {
818
- this.state.env = env;
858
+ distinctByKey(key) {
859
+ const seen = /* @__PURE__ */ new Set();
860
+ return this.filter((item) => !seen.has(item[key]) && seen.add(item[key]));
819
861
  }
820
- get request() {
821
- return this.state.request;
862
+ distinctByValue() {
863
+ const seen = /* @__PURE__ */ new Set();
864
+ return this.filter((item) => !seen.has(JSON.stringify(item)) && seen.add(JSON.stringify(item)));
822
865
  }
823
- set request(request) {
824
- this.state.request = request;
866
+ filter(p, params) {
867
+ return toList(super.filter(p, params));
825
868
  }
826
- get other() {
827
- return this.state.other;
869
+ sum(p) {
870
+ return this.reduce((sum, i) => sum + p(i), 0);
828
871
  }
829
- };
830
- var ctx = new Context();
831
-
832
- // src/types/Result.ts
833
- var toResult = (message, location, domain = ctx.env.domain) => ({
834
- message: message.toString(),
835
- location: location?.toString(),
836
- domain: domain?.toString()
837
- });
838
- var isResult = (r) => isA(r, "message");
839
-
840
- // src/types/Results.ts
841
- var parse = (...rs) => toArray(...rs).map((r) => isResult(r) ? r : toResult(r));
842
- var Results = class {
843
- results;
844
- constructor(...rs) {
845
- this.results = parse(...rs);
872
+ max(p) {
873
+ return this.desc(p).first();
846
874
  }
847
- get length() {
848
- return this.results.length;
875
+ min(p) {
876
+ return this.asc(p).first();
849
877
  }
850
- get isValid() {
851
- return this.results.length === 0;
878
+ byId(id) {
879
+ return this.first((i) => equals(i.id, id));
852
880
  }
853
- add = (...rs) => toResults(...this.results, ...parse(...rs));
854
- };
855
- var toResults = (...r) => new Results(...r);
856
- var isResults = (r) => isDefined(r) && r instanceof Results;
857
-
858
- // src/validation/Validate.ts
859
- var asResults = (subject, template2, options = {}) => toResults(toResult(text(template2).parse(subject, options), toName(subject)));
860
- var validators = (subject) => meta(subject).keys("constraint").reduce((list, vs) => list.add(vs), toList());
861
- var runValidator = (v, subject) => {
862
- try {
863
- const actual = isFunction(subject[v.property]) ? subject[v.property]() : subject[v.property];
864
- const constraint = v.constraint(actual);
865
- return isResults(constraint) ? constraint : !constraint ? asResults(subject, v.text, {
866
- ...v,
867
- actual
868
- }) : toResults();
869
- } catch (e) {
870
- return asResults(subject, asString(e));
881
+ byKey(key, value) {
882
+ return this.first((i) => i[key] == value);
871
883
  }
872
- };
873
- var constraints = (subject) => validators(subject).map((vs) => runValidator(vs, subject)).reduce((rs, r) => rs.add(...r.results), toResults());
874
- var validate = (subject) => choose(subject).is.not.defined(
875
- (s) => s,
876
- () => toResults("Subject is not defined.")
877
- ).type(isEnum, (e) => e.isValid ? toResults() : asResults(e, "This is not a valid {typeof}.")).type(isValue, (v) => v.isValid ? toResults() : asResults(v, "This is not a valid {typeof}.")).type(isArray, (a) => a.map((i) => validate(i)).reduce((rs, r) => rs.add(...r.results), toResults())).type(isValidatable, (v) => constraints(v)).else(toResults());
878
- var validateReject = (subject) => when(subject).not.isValid.reject();
879
- var isValid = (t) => validate(t).isValid;
880
-
881
- // src/types/Try.ts
882
- var Try = class _Try {
883
- is = {
884
- defined: (prop) => this.filter((v) => isDefined(prop ? prop(v) : v)),
885
- empty: (prop) => this.filter((v) => isEmpty(prop ? prop(v) : v)),
886
- valid: (prop) => this.filter((v) => validate(prop ? prop(v) : v).isValid),
887
- true: (prop) => this.filter((v) => isTrue(prop ? prop(v) : v)),
888
- false: (prop) => this.filter((v) => !isTrue(prop ? prop(v) : v)),
889
- not: {
890
- defined: (prop) => this.filter((v) => !isDefined(prop ? prop(v) : v)),
891
- empty: (prop) => this.filter((v) => !isEmpty(prop ? prop(v) : v)),
892
- valid: (prop) => this.filter((v) => !validate(prop ? prop(v) : v).isValid)
893
- }
894
- };
895
- if = this.is;
896
- static of = (c, ...args) => {
897
- try {
898
- const out = ofGet(c, ...args);
899
- return new Success(out instanceof _Try ? out.value : out);
900
- } catch (e) {
901
- return new Failure(e);
902
- }
903
- };
904
- };
905
- var Success = class extends Try {
906
- constructor(value) {
907
- super();
908
- this.value = value;
884
+ add(...items) {
885
+ super.push(...toArray(...items));
886
+ return this;
909
887
  }
910
- get error() {
911
- throw new Error("No error found");
888
+ concat(...items) {
889
+ return toList(super.concat(...items));
912
890
  }
913
- get isValid() {
914
- return true;
891
+ reverse() {
892
+ return toList(super.reverse());
915
893
  }
916
- map(f) {
917
- return tryTo(f, this.value);
894
+ splice(start, deleteCount, ...items) {
895
+ return toList(super.splice(start, deleteCount, ...items));
918
896
  }
919
- recover(f) {
897
+ remove(item) {
898
+ const index = this.indexOf(item);
899
+ if (index > -1) {
900
+ this.splice(index, 1);
901
+ }
920
902
  return this;
921
903
  }
922
- recoverFrom(type, f) {
923
- return this;
904
+ move(sourceId, destinationId) {
905
+ return this.moveOn("id", sourceId, destinationId);
924
906
  }
925
- accept(f) {
926
- return tryTo(() => {
927
- f(this.value);
928
- return this;
929
- });
907
+ moveOn(key, sourceId, destinationId) {
908
+ const source = this.findIndex((i) => equals(i[key], sourceId));
909
+ const dest = this.findIndex((i) => equals(i[key], destinationId));
910
+ return on(toList(...this), (r) => r.splice(dest, 0, ...r.splice(source, 1)));
930
911
  }
931
- filter(predicate) {
932
- return tryTo(() => {
933
- if (predicate(this.value))
934
- return this;
935
- throw new Error(`Applying filter(${predicate.toString()}) failed.`);
936
- });
912
+ replace(key, item) {
913
+ const index = this.findIndex((i) => i[key] === item?.[key]);
914
+ ifTrue(index != -1, () => this.splice(index, 1, item));
915
+ return this;
937
916
  }
938
- or(value) {
939
- return this.value;
917
+ switch(item) {
918
+ return this.includes(item) ? this.remove(item) : this.add(item);
940
919
  }
941
- orElse(value) {
942
- return this.value;
920
+ switchOn(item, on2) {
921
+ return use(
922
+ typeof on2 === "function" ? this.find(on2) : use(traverse(item, on2), (v) => this.find((i) => traverse(i, on2) === v)),
923
+ (i) => i ? this.remove(i) : this.add(item)
924
+ );
943
925
  }
944
- orThrow(_error) {
945
- return this.value;
926
+ defined() {
927
+ return this.reduce((l, v) => isDefined(v) ? l.add(v) : l, toList());
946
928
  }
947
- };
948
- var Failure = class _Failure extends Try {
949
- constructor(error) {
950
- super();
951
- this.error = error;
929
+ toObject(key, options = {}) {
930
+ return this.reduce((o, i) => {
931
+ o[i[key]] = i;
932
+ if (options.deleteKey)
933
+ delete o[i[key]][key];
934
+ return o;
935
+ }, {});
952
936
  }
953
- get value() {
954
- throw this.error;
937
+ toObjectList(key) {
938
+ return this.reduce(
939
+ (a, t) => {
940
+ const k = t[key];
941
+ a[k] = a[k] ?? toList();
942
+ a[k].push(t);
943
+ return a;
944
+ },
945
+ {}
946
+ );
955
947
  }
956
- get isValid() {
957
- return false;
948
+ orElse(...alt) {
949
+ return !isEmpty(this) ? this : !isEmpty(...alt) ? toList(...alt) : void 0;
958
950
  }
959
- map(f) {
960
- return new _Failure(this.error);
951
+ weave(insertFrom, interval) {
952
+ for (let i = interval, n = 0; i <= this.length && n < insertFrom.length; i += interval + 1) {
953
+ this.splice(i, 0, insertFrom[n++]);
954
+ }
955
+ return this;
961
956
  }
962
- recover(f) {
963
- return tryTo(f, this.error);
957
+ slice(start, end) {
958
+ return toList(super.slice(start, end));
964
959
  }
965
- recoverFrom(type, f) {
966
- return tryTo(() => this.error instanceof type ? this.recover(f) : this);
960
+ none(p) {
961
+ return !this.some(p);
967
962
  }
968
- accept(f) {
969
- return this;
963
+ chunk(chunkSize) {
964
+ return this.reduce((acc, _, index) => index % chunkSize === 0 ? on(acc, (a) => a.push(this.slice(index, index + chunkSize))) : acc, toList());
970
965
  }
971
- filter(predicate) {
972
- return this;
966
+ //we needed to add U because of a Typescript issue with generics
967
+ update(p, val) {
968
+ return this.map((v, i, a) => p(v, i, a) ? ofGet(val, v, i, a) : v);
973
969
  }
974
- or(value) {
975
- return ofGet(value);
970
+ updateFirst(p, val) {
971
+ const index = this.findIndex(p);
972
+ return this.update((t, i) => p(t, i, this) && i == index, val);
976
973
  }
977
- orElse(value) {
978
- return ofGet(value);
974
+ updateFirstById(id, val) {
975
+ return this.updateFirst((i) => equals(i?.id, id), val);
979
976
  }
980
- orThrow(error) {
981
- throw ofConstruct(error);
977
+ updateById(id, val) {
978
+ return this.update((i) => equals(i?.id, id), val);
979
+ }
980
+ groupBy(by) {
981
+ return toList(
982
+ Array.from(this.reduce((m, v, i, a) => use(by(v, i, a), (key) => m.set(key, (m.get(key) ?? toList()).add(v))), /* @__PURE__ */ new Map()).values())
983
+ );
982
984
  }
983
985
  };
984
- var tryTo = (c, ...args) => Try.of(c, ...args);
986
+ var toList = (...items) => new List8().add(...items);
987
+ var isList = (l) => isDefined(l) && isArray(l) && isA(l, "first", "last", "asc", "desc");
988
+ var asList = (c, items = []) => toList(toArray(items).map((i) => new c(i)));
989
+ var maxValue = (l, key) => l.desc(key).first()?.[key];
990
+ var minValue = (l, key) => l.asc(key).first()?.[key];
985
991
 
986
992
  export {
987
- Try,
988
- tryTo,
989
- choose,
990
- isEqual,
991
- ifTrue,
992
- ifFalse,
993
- ifDefined,
994
- ifNotEmpty,
995
- ifEqual,
996
- ifEither,
997
- List,
993
+ List8 as List,
998
994
  toList,
999
995
  isList,
1000
996
  asList,
@@ -1017,6 +1013,8 @@ export {
1017
1013
  isEnumConstructor,
1018
1014
  Enum,
1019
1015
  isEnum,
1016
+ Try,
1017
+ tryTo,
1020
1018
  Template,
1021
1019
  template,
1022
1020
  ToText,
@@ -1040,6 +1038,16 @@ export {
1040
1038
  asResults,
1041
1039
  validate,
1042
1040
  validateReject,
1043
- isValid
1041
+ isValid,
1042
+ choose,
1043
+ isEqual,
1044
+ ifTrue,
1045
+ ifFalse,
1046
+ ifDefined,
1047
+ ifNotEmpty,
1048
+ ifEmpty,
1049
+ given,
1050
+ ifEqual,
1051
+ ifEither
1044
1052
  };
1045
- //# sourceMappingURL=chunk-BS5KUNX2.mjs.map
1053
+ //# sourceMappingURL=chunk-3VYRMOJQ.mjs.map