@reportman/engine 0.0.0-stage → 0.1.0-alpha.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 (244) hide show
  1. package/LICENSE +373 -0
  2. package/dist/.tsbuildinfo +1 -0
  3. package/dist/data/columns.d.ts +50 -0
  4. package/dist/data/columns.d.ts.map +1 -0
  5. package/dist/data/columns.js +385 -0
  6. package/dist/data/columns.js.map +1 -0
  7. package/dist/data/databaseinfo.d.ts +41 -0
  8. package/dist/data/databaseinfo.d.ts.map +1 -0
  9. package/dist/data/databaseinfo.js +145 -0
  10. package/dist/data/databaseinfo.js.map +1 -0
  11. package/dist/data/datainfo.d.ts +82 -0
  12. package/dist/data/datainfo.d.ts.map +1 -0
  13. package/dist/data/datainfo.js +449 -0
  14. package/dist/data/datainfo.js.map +1 -0
  15. package/dist/data/errors.d.ts +8 -0
  16. package/dist/data/errors.d.ts.map +1 -0
  17. package/dist/data/errors.js +14 -0
  18. package/dist/data/errors.js.map +1 -0
  19. package/dist/data/host.d.ts +132 -0
  20. package/dist/data/host.d.ts.map +1 -0
  21. package/dist/data/host.js +108 -0
  22. package/dist/data/host.js.map +1 -0
  23. package/dist/data/op.d.ts +21 -0
  24. package/dist/data/op.d.ts.map +1 -0
  25. package/dist/data/op.js +97 -0
  26. package/dist/data/op.js.map +1 -0
  27. package/dist/data/reportdataset.d.ts +116 -0
  28. package/dist/data/reportdataset.d.ts.map +1 -0
  29. package/dist/data/reportdataset.js +396 -0
  30. package/dist/data/reportdataset.js.map +1 -0
  31. package/dist/data/transfer.d.ts +46 -0
  32. package/dist/data/transfer.d.ts.map +1 -0
  33. package/dist/data/transfer.js +100 -0
  34. package/dist/data/transfer.js.map +1 -0
  35. package/dist/data/union.d.ts +88 -0
  36. package/dist/data/union.d.ts.map +1 -0
  37. package/dist/data/union.js +733 -0
  38. package/dist/data/union.js.map +1 -0
  39. package/dist/eval/chinesetables.d.ts +5 -0
  40. package/dist/eval/chinesetables.d.ts.map +1 -0
  41. package/dist/eval/chinesetables.js +8 -0
  42. package/dist/eval/chinesetables.js.map +1 -0
  43. package/dist/eval/evaluator.d.ts +100 -0
  44. package/dist/eval/evaluator.d.ts.map +1 -0
  45. package/dist/eval/evaluator.js +667 -0
  46. package/dist/eval/evaluator.js.map +1 -0
  47. package/dist/eval/exception.d.ts +9 -0
  48. package/dist/eval/exception.d.ts.map +1 -0
  49. package/dist/eval/exception.js +15 -0
  50. package/dist/eval/exception.js.map +1 -0
  51. package/dist/eval/fields.d.ts +60 -0
  52. package/dist/eval/fields.d.ts.map +1 -0
  53. package/dist/eval/fields.js +104 -0
  54. package/dist/eval/fields.js.map +1 -0
  55. package/dist/eval/functions.d.ts +27 -0
  56. package/dist/eval/functions.d.ts.map +1 -0
  57. package/dist/eval/functions.js +810 -0
  58. package/dist/eval/functions.js.map +1 -0
  59. package/dist/eval/identifiers.d.ts +100 -0
  60. package/dist/eval/identifiers.d.ts.map +1 -0
  61. package/dist/eval/identifiers.js +195 -0
  62. package/dist/eval/identifiers.js.map +1 -0
  63. package/dist/eval/parser.d.ts +47 -0
  64. package/dist/eval/parser.d.ts.map +1 -0
  65. package/dist/eval/parser.js +336 -0
  66. package/dist/eval/parser.js.map +1 -0
  67. package/dist/index.d.ts +52 -0
  68. package/dist/index.d.ts.map +1 -0
  69. package/dist/index.js +42 -0
  70. package/dist/index.js.map +1 -0
  71. package/dist/params/param.d.ts +92 -0
  72. package/dist/params/param.d.ts.map +1 -0
  73. package/dist/params/param.js +358 -0
  74. package/dist/params/param.js.map +1 -0
  75. package/dist/params/parameters.d.ts +87 -0
  76. package/dist/params/parameters.d.ts.map +1 -0
  77. package/dist/params/parameters.js +499 -0
  78. package/dist/params/parameters.js.map +1 -0
  79. package/dist/report/barcode/barcodeitem.d.ts +76 -0
  80. package/dist/report/barcode/barcodeitem.d.ts.map +1 -0
  81. package/dist/report/barcode/barcodeitem.js +864 -0
  82. package/dist/report/barcode/barcodeitem.js.map +1 -0
  83. package/dist/report/barcode/code128.d.ts +18 -0
  84. package/dist/report/barcode/code128.d.ts.map +1 -0
  85. package/dist/report/barcode/code128.js +187 -0
  86. package/dist/report/barcode/code128.js.map +1 -0
  87. package/dist/report/barcode/netutil.d.ts +30 -0
  88. package/dist/report/barcode/netutil.d.ts.map +1 -0
  89. package/dist/report/barcode/netutil.js +65 -0
  90. package/dist/report/barcode/netutil.js.map +1 -0
  91. package/dist/report/barcode/pdf417.d.ts +17 -0
  92. package/dist/report/barcode/pdf417.d.ts.map +1 -0
  93. package/dist/report/barcode/pdf417.js +782 -0
  94. package/dist/report/barcode/pdf417.js.map +1 -0
  95. package/dist/report/barcode/pdf417tables.d.ts +11 -0
  96. package/dist/report/barcode/pdf417tables.d.ts.map +1 -0
  97. package/dist/report/barcode/pdf417tables.js +880 -0
  98. package/dist/report/barcode/pdf417tables.js.map +1 -0
  99. package/dist/report/barcode/qrcode.d.ts +14 -0
  100. package/dist/report/barcode/qrcode.d.ts.map +1 -0
  101. package/dist/report/barcode/qrcode.js +214 -0
  102. package/dist/report/barcode/qrcode.js.map +1 -0
  103. package/dist/report/barcode/zxingwidth.d.ts +4 -0
  104. package/dist/report/barcode/zxingwidth.d.ts.map +1 -0
  105. package/dist/report/barcode/zxingwidth.js +247 -0
  106. package/dist/report/barcode/zxingwidth.js.map +1 -0
  107. package/dist/report/chart/chartidens.d.ts +70 -0
  108. package/dist/report/chart/chartidens.d.ts.map +1 -0
  109. package/dist/report/chart/chartidens.js +162 -0
  110. package/dist/report/chart/chartidens.js.map +1 -0
  111. package/dist/report/chart/chartitem.d.ts +110 -0
  112. package/dist/report/chart/chartitem.d.ts.map +1 -0
  113. package/dist/report/chart/chartitem.js +403 -0
  114. package/dist/report/chart/chartitem.js.map +1 -0
  115. package/dist/report/chart/series.d.ts +65 -0
  116. package/dist/report/chart/series.d.ts.map +1 -0
  117. package/dist/report/chart/series.js +74 -0
  118. package/dist/report/chart/series.js.map +1 -0
  119. package/dist/report/chart/vectorchart.d.ts +18 -0
  120. package/dist/report/chart/vectorchart.d.ts.map +1 -0
  121. package/dist/report/chart/vectorchart.js +356 -0
  122. package/dist/report/chart/vectorchart.js.map +1 -0
  123. package/dist/report/convert.d.ts +4 -0
  124. package/dist/report/convert.d.ts.map +1 -0
  125. package/dist/report/convert.js +162 -0
  126. package/dist/report/convert.js.map +1 -0
  127. package/dist/report/errors.d.ts +13 -0
  128. package/dist/report/errors.d.ts.map +1 -0
  129. package/dist/report/errors.js +20 -0
  130. package/dist/report/errors.js.map +1 -0
  131. package/dist/report/identifiers.d.ts +91 -0
  132. package/dist/report/identifiers.d.ts.map +1 -0
  133. package/dist/report/identifiers.js +404 -0
  134. package/dist/report/identifiers.js.map +1 -0
  135. package/dist/report/imageitem.d.ts +66 -0
  136. package/dist/report/imageitem.d.ts.map +1 -0
  137. package/dist/report/imageitem.js +190 -0
  138. package/dist/report/imageitem.js.map +1 -0
  139. package/dist/report/items.d.ts +219 -0
  140. package/dist/report/items.d.ts.map +1 -0
  141. package/dist/report/items.js +1015 -0
  142. package/dist/report/items.js.map +1 -0
  143. package/dist/report/report.d.ts +221 -0
  144. package/dist/report/report.d.ts.map +1 -0
  145. package/dist/report/report.js +1968 -0
  146. package/dist/report/report.js.map +1 -0
  147. package/dist/report/run.d.ts +87 -0
  148. package/dist/report/run.d.ts.map +1 -0
  149. package/dist/report/run.js +121 -0
  150. package/dist/report/run.js.map +1 -0
  151. package/dist/report/section.d.ts +86 -0
  152. package/dist/report/section.d.ts.map +1 -0
  153. package/dist/report/section.js +325 -0
  154. package/dist/report/section.js.map +1 -0
  155. package/dist/report/subreport.d.ts +49 -0
  156. package/dist/report/subreport.d.ts.map +1 -0
  157. package/dist/report/subreport.js +220 -0
  158. package/dist/report/subreport.js.map +1 -0
  159. package/dist/util/base64.d.ts +9 -0
  160. package/dist/util/base64.d.ts.map +1 -0
  161. package/dist/util/base64.js +60 -0
  162. package/dist/util/base64.js.map +1 -0
  163. package/dist/util/chars.d.ts +45 -0
  164. package/dist/util/chars.d.ts.map +1 -0
  165. package/dist/util/chars.js +194 -0
  166. package/dist/util/chars.js.map +1 -0
  167. package/dist/util/compositeformat.d.ts +3 -0
  168. package/dist/util/compositeformat.d.ts.map +1 -0
  169. package/dist/util/compositeformat.js +99 -0
  170. package/dist/util/compositeformat.js.map +1 -0
  171. package/dist/util/culture.d.ts +84 -0
  172. package/dist/util/culture.d.ts.map +1 -0
  173. package/dist/util/culture.js +59 -0
  174. package/dist/util/culture.js.map +1 -0
  175. package/dist/util/cultures.generated.d.ts +3 -0
  176. package/dist/util/cultures.generated.d.ts.map +1 -0
  177. package/dist/util/cultures.generated.js +1043 -0
  178. package/dist/util/cultures.generated.js.map +1 -0
  179. package/dist/util/dateformat.d.ts +21 -0
  180. package/dist/util/dateformat.d.ts.map +1 -0
  181. package/dist/util/dateformat.js +439 -0
  182. package/dist/util/dateformat.js.map +1 -0
  183. package/dist/util/dateparse.d.ts +12 -0
  184. package/dist/util/dateparse.d.ts.map +1 -0
  185. package/dist/util/dateparse.js +1212 -0
  186. package/dist/util/dateparse.js.map +1 -0
  187. package/dist/util/datetime.d.ts +70 -0
  188. package/dist/util/datetime.d.ts.map +1 -0
  189. package/dist/util/datetime.js +265 -0
  190. package/dist/util/datetime.js.map +1 -0
  191. package/dist/util/decimal.d.ts +54 -0
  192. package/dist/util/decimal.d.ts.map +1 -0
  193. package/dist/util/decimal.js +365 -0
  194. package/dist/util/decimal.js.map +1 -0
  195. package/dist/util/delphitext.d.ts +40 -0
  196. package/dist/util/delphitext.d.ts.map +1 -0
  197. package/dist/util/delphitext.js +408 -0
  198. package/dist/util/delphitext.js.map +1 -0
  199. package/dist/util/doubleutil.d.ts +30 -0
  200. package/dist/util/doubleutil.d.ts.map +1 -0
  201. package/dist/util/doubleutil.js +651 -0
  202. package/dist/util/doubleutil.js.map +1 -0
  203. package/dist/util/inflate.d.ts +7 -0
  204. package/dist/util/inflate.d.ts.map +1 -0
  205. package/dist/util/inflate.js +256 -0
  206. package/dist/util/inflate.js.map +1 -0
  207. package/dist/util/netexception.d.ts +59 -0
  208. package/dist/util/netexception.d.ts.map +1 -0
  209. package/dist/util/netexception.js +94 -0
  210. package/dist/util/netexception.js.map +1 -0
  211. package/dist/util/numberformat.d.ts +14 -0
  212. package/dist/util/numberformat.d.ts.map +1 -0
  213. package/dist/util/numberformat.js +828 -0
  214. package/dist/util/numberformat.js.map +1 -0
  215. package/dist/util/numberparse.d.ts +49 -0
  216. package/dist/util/numberparse.d.ts.map +1 -0
  217. package/dist/util/numberparse.js +568 -0
  218. package/dist/util/numberparse.js.map +1 -0
  219. package/dist/util/reportmanres.en.generated.d.ts +2 -0
  220. package/dist/util/reportmanres.en.generated.d.ts.map +1 -0
  221. package/dist/util/reportmanres.en.generated.js +2082 -0
  222. package/dist/util/reportmanres.en.generated.js.map +1 -0
  223. package/dist/util/reportmanres.es.generated.d.ts +2 -0
  224. package/dist/util/reportmanres.es.generated.d.ts.map +1 -0
  225. package/dist/util/reportmanres.es.generated.js +2082 -0
  226. package/dist/util/reportmanres.es.generated.js.map +1 -0
  227. package/dist/util/translator.d.ts +29 -0
  228. package/dist/util/translator.d.ts.map +1 -0
  229. package/dist/util/translator.js +79 -0
  230. package/dist/util/translator.js.map +1 -0
  231. package/dist/variant/coercion.d.ts +9 -0
  232. package/dist/variant/coercion.d.ts.map +1 -0
  233. package/dist/variant/coercion.js +298 -0
  234. package/dist/variant/coercion.js.map +1 -0
  235. package/dist/variant/context.d.ts +25 -0
  236. package/dist/variant/context.d.ts.map +1 -0
  237. package/dist/variant/context.js +22 -0
  238. package/dist/variant/context.js.map +1 -0
  239. package/dist/variant/variant.d.ts +193 -0
  240. package/dist/variant/variant.d.ts.map +1 -0
  241. package/dist/variant/variant.js +1229 -0
  242. package/dist/variant/variant.js.map +1 -0
  243. package/package.json +35 -4
  244. package/README.md +0 -3
@@ -0,0 +1,733 @@
1
+ // Port of the unions of datasets of Reportman.Reporting.DataInfo (BuildUnion and its helpers): a
2
+ // dataset of the MyBase connection made of other datasets of the report (DataUnions), as the
3
+ // Delphi engine builds it (TRpDataInfoItem.Connect, CombineAddDataset, CombineParallel). The
4
+ // DataTables of C# are UnionTable here: columns with their .NET types, rows of stored values.
5
+ //
6
+ // Texts are ordered and matched as a DataView without case sensitivity orders them in C#:
7
+ // CompareInfo.Compare(a, b, IgnoreCase | IgnoreKanaType | IgnoreWidth) under the culture of the
8
+ // run. .NET 5+ compares with ICU (on Windows too) and gives IgnoreCase the secondary strength,
9
+ // with no custom rules when kana type and width are ignored as well: that is an ICU collator of
10
+ // the culture with sensitivity 'accent', what Intl.Collator gives. Known limits: the ICU version
11
+ // of Node and the one .NET loads may differ (new characters, CLDR changes of a locale); V8 turns
12
+ // on the normalization of its collators and .NET does not, so texts with unusual sequences of
13
+ // combining marks (not in FCD form) may compare differently. The in-memory datasets of the Delphi
14
+ // engine compare texts with the functions of Windows (NLS), not ICU: around punctuation the
15
+ // order of Delphi may differ from the order of C#, which this port follows.
16
+ import { collatorFor, toUpperNet } from '../util/chars.js';
17
+ import { formatDateTime } from '../util/dateformat.js';
18
+ import { RpDateTime } from '../util/datetime.js';
19
+ import { CsDecimal } from '../util/decimal.js';
20
+ import { tryParseDateTime } from '../util/dateparse.js';
21
+ import { argumentException, formatException, NamedException, NetException, NetMessages, overflowException } from '../util/netexception.js';
22
+ import { formatDecimal, formatDouble } from '../util/numberformat.js';
23
+ import { convertDecimalToInt32, convertDecimalToInt64, convertDoubleToInt32, convertDoubleToInt64, NumberStyles, parseDecimal, parseDouble, tryParseBoolean, tryParseInt32, tryParseInt64, } from '../util/numberparse.js';
24
+ import { storedDouble, storeValue } from './columns.js';
25
+ import { duplicateNameException } from './errors.js';
26
+ import { ResultView } from './reportdataset.js';
27
+ const NET_NAMES = {
28
+ String: 'String',
29
+ Int64: 'Int64',
30
+ Int32: 'Int32',
31
+ Int16: 'Int16',
32
+ Byte: 'Byte',
33
+ Boolean: 'Boolean',
34
+ Double: 'Double',
35
+ Single: 'Single',
36
+ Decimal: 'Decimal',
37
+ DateTime: 'DateTime',
38
+ Binary: 'Byte[]',
39
+ };
40
+ const collators = new Map();
41
+ /** The comparer of texts of a culture: IgnoreCase | IgnoreKanaType | IgnoreWidth (see above). */
42
+ export function unionCollator(culture) {
43
+ let c = collators.get(culture.name);
44
+ if (c === undefined) {
45
+ c = collatorFor(culture.name, { usage: 'sort', sensitivity: 'accent' });
46
+ collators.set(culture.name, c);
47
+ }
48
+ return c;
49
+ }
50
+ /** Strings.FromSemiColon: the parts between semicolons, empty ones too ("" gives one part). */
51
+ export function fromSemiColon(text) {
52
+ return text.split(';');
53
+ }
54
+ /** Strings.RemoveBlanks. */
55
+ export function removeBlanks(list) {
56
+ return list.filter((s) => s.length > 0);
57
+ }
58
+ /** DataTable.Clone: the columns, no rows. */
59
+ export function cloneTable(table) {
60
+ return { columns: table.columns.map((c) => ({ name: c.name, type: c.type, maxLength: -1 })), rows: [] };
61
+ }
62
+ /**
63
+ * DataColumnCollection.IndexOf: the column with that name, else the only one with that name
64
+ * without regard to case (-1 when there is none, or more than one).
65
+ */
66
+ export function indexOfColumn(table, name) {
67
+ if (name.length === 0)
68
+ return -1;
69
+ const cols = table.columns;
70
+ for (let i = 0; i < cols.length; i++)
71
+ if (cols[i].name === name)
72
+ return i;
73
+ const upper = toUpperNet(name);
74
+ let found = -1;
75
+ for (let i = 0; i < cols.length; i++) {
76
+ if (toUpperNet(cols[i].name) === upper) {
77
+ if (found >= 0)
78
+ return -1;
79
+ found = i;
80
+ }
81
+ }
82
+ return found;
83
+ }
84
+ /** DataColumnCollection.Add(name, type): its ordinal; a name already there fails. */
85
+ function addColumn(table, name, type) {
86
+ if (table.columns.some((c) => c.name === name))
87
+ throw duplicateNameException(name);
88
+ table.columns.push({ name, type, maxLength: -1 });
89
+ return table.columns.length - 1;
90
+ }
91
+ /** DataRow[i] of a column index that is not in the table. */
92
+ function columnOutOfRange(index) {
93
+ return new NetException('IndexOutOfRangeException', `Cannot find column ${index}.`);
94
+ }
95
+ /** The boxed value of a cell: Object columns keep values of any type, known by their JavaScript type. */
96
+ function boxedOf(column, value) {
97
+ if (value === null)
98
+ return null;
99
+ if (column.type !== 'Object')
100
+ return { type: column.type, value };
101
+ switch (typeof value) {
102
+ case 'string':
103
+ return { type: 'String', value };
104
+ case 'bigint':
105
+ return { type: 'Int64', value };
106
+ case 'boolean':
107
+ return { type: 'Boolean', value };
108
+ case 'number':
109
+ return { type: Number.isInteger(value) && Math.abs(value) <= 2147483647 ? 'Int32' : 'Double', value };
110
+ }
111
+ if (value instanceof Uint8Array)
112
+ return { type: 'Binary', value };
113
+ if (value instanceof CsDecimal)
114
+ return { type: 'Decimal', value };
115
+ return { type: 'DateTime', value };
116
+ }
117
+ /** row[i] of a table, boxed (a column that is not there fails as DataRow[i]). */
118
+ function cell(table, row, index) {
119
+ if (index < 0 || index >= table.columns.length)
120
+ throw columnOutOfRange(index);
121
+ return boxedOf(table.columns[index], row[index] ?? null);
122
+ }
123
+ function isInteger(t) {
124
+ return t === 'Int64' || t === 'Int32' || t === 'Int16' || t === 'Byte';
125
+ }
126
+ function isFloat(t) {
127
+ return t === 'Double' || t === 'Single';
128
+ }
129
+ /** IsUnionNumber: the integer types, float, double and decimal (not bool). */
130
+ function isNumber(t) {
131
+ return isInteger(t) || isFloat(t) || t === 'Decimal';
132
+ }
133
+ /** A value that is not IConvertible (byte[]) given to Convert.ToXxx(object). */
134
+ function notConvertible() {
135
+ return new NetException('InvalidCastException', "Unable to cast object of type 'System.Byte[]' to type 'System.IConvertible'.");
136
+ }
137
+ function invalidCast(from, to) {
138
+ return new NetException('InvalidCastException', NetMessages.invalidCast(NET_NAMES[from], to));
139
+ }
140
+ /** Convert.ToString(value, InvariantCulture). */
141
+ function invariantText(v, ctx) {
142
+ switch (v.type) {
143
+ case 'String':
144
+ return v.value;
145
+ case 'Int64':
146
+ case 'Int32':
147
+ case 'Int16':
148
+ case 'Byte':
149
+ return String(v.value);
150
+ case 'Double':
151
+ case 'Single':
152
+ // A Single prints as the double it is stored as (the engine keeps floats as doubles)
153
+ return formatDouble(v.value, '', ctx.invariant.number);
154
+ case 'Decimal':
155
+ return formatDecimal(v.value, '', ctx.invariant.number);
156
+ case 'Boolean':
157
+ return v.value ? 'True' : 'False';
158
+ case 'DateTime':
159
+ return formatDateTime(v.value, '', ctx.invariant);
160
+ case 'Binary':
161
+ return 'System.Byte[]';
162
+ }
163
+ }
164
+ /** IConvertible.ToInt64(InvariantCulture). */
165
+ function toInt64(v, ctx) {
166
+ switch (v.type) {
167
+ case 'Int64':
168
+ return v.value;
169
+ case 'Int32':
170
+ case 'Int16':
171
+ case 'Byte':
172
+ return BigInt(v.value);
173
+ case 'Double':
174
+ case 'Single':
175
+ return convertDoubleToInt64(v.value);
176
+ case 'Decimal':
177
+ return convertDecimalToInt64(v.value);
178
+ case 'Boolean':
179
+ return v.value ? 1n : 0n;
180
+ case 'String': {
181
+ const r = tryParseInt64(v.value, NumberStyles.Integer, ctx.invariant.number);
182
+ if (r === undefined)
183
+ throw formatException(NetMessages.parseFailed(v.value));
184
+ return r;
185
+ }
186
+ case 'DateTime':
187
+ throw invalidCast('DateTime', 'Int64');
188
+ case 'Binary':
189
+ throw notConvertible();
190
+ }
191
+ }
192
+ /** IConvertible.ToInt32 / ToInt16 / ToByte(InvariantCulture). */
193
+ function toSmallInteger(v, ctx, to) {
194
+ let r;
195
+ switch (v.type) {
196
+ case 'Int64': {
197
+ const b = v.value;
198
+ if (b < -2147483648n || b > 2147483647n)
199
+ throw overflowException(overflowMessage(to));
200
+ r = Number(b);
201
+ break;
202
+ }
203
+ case 'Int32':
204
+ case 'Int16':
205
+ case 'Byte':
206
+ r = v.value;
207
+ break;
208
+ case 'Double':
209
+ case 'Single':
210
+ // Convert.ToInt16(double) is ToInt16(ToInt32(value)): out of the Int32 range it fails as Int32
211
+ r = convertDoubleToInt32(v.value);
212
+ break;
213
+ case 'Decimal':
214
+ r = convertDecimalToInt32(v.value);
215
+ break;
216
+ case 'Boolean':
217
+ r = v.value ? 1 : 0;
218
+ break;
219
+ case 'String': {
220
+ const p = tryParseInt32(v.value, NumberStyles.Integer, ctx.invariant.number);
221
+ if (p === undefined)
222
+ throw formatException(NetMessages.parseFailed(v.value));
223
+ r = p;
224
+ break;
225
+ }
226
+ case 'DateTime':
227
+ throw invalidCast('DateTime', to);
228
+ case 'Binary':
229
+ throw notConvertible();
230
+ }
231
+ const [min, max] = to === 'Int32' ? [-2147483648, 2147483647] : to === 'Int16' ? [-32768, 32767] : [0, 255];
232
+ if (r < min || r > max)
233
+ throw overflowException(overflowMessage(to));
234
+ return r;
235
+ }
236
+ function overflowMessage(to) {
237
+ if (to === 'Int32')
238
+ return NetMessages.overflowInt32;
239
+ if (to === 'Byte')
240
+ return NetMessages.overflowByte;
241
+ return 'Value was either too large or too small for an Int16.';
242
+ }
243
+ /** IConvertible.ToDouble(InvariantCulture). */
244
+ function toDouble(v, ctx) {
245
+ switch (v.type) {
246
+ case 'Int64':
247
+ return Number(v.value);
248
+ case 'Int32':
249
+ case 'Int16':
250
+ case 'Byte':
251
+ case 'Double':
252
+ case 'Single':
253
+ return v.value;
254
+ case 'Decimal':
255
+ return v.value.toDouble();
256
+ case 'Boolean':
257
+ return v.value ? 1 : 0;
258
+ case 'String':
259
+ return parseDouble(v.value, NumberStyles.Float | NumberStyles.AllowThousands, ctx.invariant.number);
260
+ case 'DateTime':
261
+ throw invalidCast('DateTime', 'Double');
262
+ case 'Binary':
263
+ throw notConvertible();
264
+ }
265
+ }
266
+ /** IConvertible.ToDecimal(InvariantCulture). */
267
+ function toDecimal(v, ctx) {
268
+ switch (v.type) {
269
+ case 'Int64':
270
+ case 'Int32':
271
+ case 'Int16':
272
+ case 'Byte':
273
+ return CsDecimal.fromInteger(v.value);
274
+ case 'Double':
275
+ return CsDecimal.fromDouble(v.value);
276
+ case 'Single':
277
+ // Convert.ToDecimal(float) keeps the 7 significant digits of a float
278
+ return CsDecimal.fromDouble(Number(v.value.toPrecision(7)));
279
+ case 'Decimal':
280
+ return v.value;
281
+ case 'Boolean':
282
+ return v.value ? CsDecimal.one : CsDecimal.zero;
283
+ case 'String':
284
+ return parseDecimal(v.value, NumberStyles.Number, ctx.invariant.number);
285
+ case 'DateTime':
286
+ throw invalidCast('DateTime', 'Decimal');
287
+ case 'Binary':
288
+ throw notConvertible();
289
+ }
290
+ }
291
+ /**
292
+ * ConvertUnionValue: a value converted to the type of the column it goes to (as a Variant
293
+ * assigned to a field of the Delphi engine is): the value itself when it is of that type,
294
+ * Convert.ToString(value, InvariantCulture) for a text column, otherwise
295
+ * Convert.ChangeType(value, type, InvariantCulture). The value goes into a row of a union, so it
296
+ * is the value the DataTable gives back (a double -0.0 is 0.0).
297
+ */
298
+ export function convertUnionValue(v, type, ctx) {
299
+ const r = changeType(v, type, ctx);
300
+ return (type === 'Double' || type === 'Single') && typeof r === 'number' ? storedDouble(r) : r;
301
+ }
302
+ function changeType(v, type, ctx) {
303
+ if (v === null)
304
+ return null;
305
+ if (type === 'Object' || v.type === type)
306
+ return v.value;
307
+ if (type === 'String')
308
+ return invariantText(v, ctx);
309
+ // Convert.ChangeType of a value that is not IConvertible
310
+ if (v.type === 'Binary')
311
+ throw new NetException('InvalidCastException', 'Object must implement IConvertible.');
312
+ switch (type) {
313
+ case 'Int64':
314
+ return toInt64(v, ctx);
315
+ case 'Int32':
316
+ case 'Int16':
317
+ case 'Byte':
318
+ return toSmallInteger(v, ctx, type);
319
+ case 'Double':
320
+ return toDouble(v, ctx);
321
+ case 'Single':
322
+ return Math.fround(toDouble(v, ctx));
323
+ case 'Decimal':
324
+ return toDecimal(v, ctx);
325
+ case 'Boolean':
326
+ switch (v.type) {
327
+ case 'String': {
328
+ const b = tryParseBoolean(v.value);
329
+ if (b === undefined)
330
+ throw formatException(`String '${v.value}' was not recognized as a valid Boolean.`);
331
+ return b;
332
+ }
333
+ case 'Decimal':
334
+ return !v.value.isZero;
335
+ case 'Int64':
336
+ return v.value !== 0n;
337
+ case 'DateTime':
338
+ throw invalidCast('DateTime', 'Boolean');
339
+ default:
340
+ return v.value !== 0;
341
+ }
342
+ case 'DateTime':
343
+ if (v.type === 'String') {
344
+ const d = tryParseDateTime(v.value, ctx.invariant, ctx.now);
345
+ if (d === undefined)
346
+ throw formatException(`String '${v.value}' was not recognized as a valid DateTime.`);
347
+ return d;
348
+ }
349
+ throw invalidCast(v.type, 'DateTime');
350
+ case 'Binary':
351
+ throw new NetException('InvalidCastException', NetMessages.invalidCast('System.' + NET_NAMES[v.type], 'System.Byte[]'));
352
+ }
353
+ }
354
+ /**
355
+ * AddUnionValues: the sum of two values of a grouped union (the + of two Variants of the Delphi
356
+ * engine): texts are joined, a date moves by a number of days, numbers are added.
357
+ */
358
+ function addUnionValues(a, b, ctx) {
359
+ // Convert.ToDouble(object) etc.: a byte[] is not IConvertible
360
+ const plain = (v) => {
361
+ if (v.type === 'Binary')
362
+ throw notConvertible();
363
+ return v;
364
+ };
365
+ if (a.type === 'String' || b.type === 'String') {
366
+ if (a.type === 'String' && b.type === 'String')
367
+ return { type: 'String', value: a.value + b.value };
368
+ return { type: 'Double', value: toDouble(plain(a), ctx) + toDouble(plain(b), ctx) };
369
+ }
370
+ // Two dates fail: Convert.ToDouble(DateTime) is an invalid cast
371
+ if (a.type === 'DateTime')
372
+ return { type: 'DateTime', value: RpDateTime.fromOADate(a.value.toOADate() + toDouble(plain(b), ctx)) };
373
+ if (b.type === 'DateTime')
374
+ return { type: 'DateTime', value: RpDateTime.fromOADate(toDouble(plain(a), ctx) + b.value.toOADate()) };
375
+ if (isFloat(a.type) || isFloat(b.type))
376
+ return { type: 'Double', value: toDouble(plain(a), ctx) + toDouble(plain(b), ctx) };
377
+ if (a.type === 'Decimal' || b.type === 'Decimal') {
378
+ const sum = toDecimal(plain(a), ctx).add(toDecimal(plain(b), ctx));
379
+ return { type: 'Decimal', value: sum };
380
+ }
381
+ // C# parity: long addition without overflow checking
382
+ return { type: 'Int64', value: BigInt.asIntN(64, toInt64(plain(a), ctx) + toInt64(plain(b), ctx)) };
383
+ }
384
+ /** double.CompareTo: NaN first. */
385
+ function compareDouble(a, b) {
386
+ if (a < b)
387
+ return -1;
388
+ if (a > b)
389
+ return 1;
390
+ if (a === b)
391
+ return 0;
392
+ if (Number.isNaN(a))
393
+ return Number.isNaN(b) ? 0 : -1;
394
+ return 1;
395
+ }
396
+ /** string.CompareOrdinal (its sign). */
397
+ function compareOrdinal(a, b) {
398
+ const n = Math.min(a.length, b.length);
399
+ for (let i = 0; i < n; i++) {
400
+ const d = a.charCodeAt(i) - b.charCodeAt(i);
401
+ if (d !== 0)
402
+ return d < 0 ? -1 : 1;
403
+ }
404
+ return a.length === b.length ? 0 : a.length < b.length ? -1 : 1;
405
+ }
406
+ function compareText(collator, a, b) {
407
+ if (a === b)
408
+ return 0;
409
+ const r = collator.compare(a, b);
410
+ return r < 0 ? -1 : r > 0 ? 1 : 0;
411
+ }
412
+ /**
413
+ * CompareUnionValues: the order of two values of an index of a union: nulls first, numbers by
414
+ * value, texts by the culture without regard to case, dates by time, and other values by their
415
+ * invariant text.
416
+ */
417
+ function compareUnionValues(a, b, ctx) {
418
+ if (a === null || b === null)
419
+ return a === null && b === null ? 0 : a === null ? -1 : 1;
420
+ if (isNumber(a.type) && isNumber(b.type)) {
421
+ if (isFloat(a.type) || isFloat(b.type))
422
+ return compareDouble(toDouble(a, ctx), toDouble(b, ctx));
423
+ if (a.type !== 'Decimal' && b.type !== 'Decimal') {
424
+ const x = toInt64(a, ctx);
425
+ const y = toInt64(b, ctx);
426
+ return x < y ? -1 : x > y ? 1 : 0;
427
+ }
428
+ return toDecimal(a, ctx).compareTo(toDecimal(b, ctx));
429
+ }
430
+ if (a.type === 'String' && b.type === 'String')
431
+ return compareText(unionCollator(ctx.culture), a.value, b.value);
432
+ if (a.type === b.type) {
433
+ // IComparable of the same type (byte[] is not)
434
+ if (a.type === 'DateTime')
435
+ return a.value.compareTo(b.value);
436
+ if (a.type === 'Boolean')
437
+ return a.value === b.value ? 0 : a.value ? 1 : -1;
438
+ }
439
+ return compareOrdinal(invariantText(a, ctx), invariantText(b, ctx));
440
+ }
441
+ function unionValuesEqual(a, b, ctx) {
442
+ return compareUnionValues(a, b, ctx) === 0;
443
+ }
444
+ /**
445
+ * OrderedRows: the rows of a table in the order of an index on some of its columns (the rows with
446
+ * the same values keep their order), the order a TClientDataSet with IndexFieldNames goes through
447
+ * them. firstColumn (-1: none) goes before the others.
448
+ */
449
+ function orderedRowsBy(table, firstColumn, columns, ctx) {
450
+ const rows = [...table.rows];
451
+ if (firstColumn < 0 && columns.length === 0)
452
+ return rows;
453
+ const keys = firstColumn >= 0 ? [firstColumn, ...columns] : [...columns];
454
+ // Array.prototype.sort is stable: the position breaks the ties, as in C#
455
+ rows.sort((a, b) => {
456
+ for (const key of keys) {
457
+ const r = compareUnionValues(cell(table, a, key), cell(table, b, key), ctx);
458
+ if (r !== 0)
459
+ return r;
460
+ }
461
+ return 0;
462
+ });
463
+ return rows;
464
+ }
465
+ /** OrderedRows by the names of the index fields. */
466
+ export function orderedRows(table, indexFields, ctx) {
467
+ const columns = [];
468
+ for (const field of indexFields) {
469
+ const column = indexOfColumn(table, field);
470
+ if (column < 0)
471
+ throw new NamedException("Field '" + field + "' not found in the union", field);
472
+ columns.push(column);
473
+ }
474
+ return orderedRowsBy(table, -1, columns, ctx);
475
+ }
476
+ /**
477
+ * ExtractUnionFields: the dataset alias of a union entry, "ALIAS" or "ALIAS-FIELD1;FIELD2", and
478
+ * its common fields in upper case (the first '-' ends the alias).
479
+ */
480
+ export function extractUnionFields(entry) {
481
+ const index = entry.indexOf('-');
482
+ if (index < 0)
483
+ return { alias: entry, commonFields: [] };
484
+ return { alias: entry.substring(0, index), commonFields: fromSemiColon(entry.substring(index + 1)).map(toUpperNet) };
485
+ }
486
+ /**
487
+ * CombineAddDataset: adds the rows of a dataset to the result of a union. The result takes the
488
+ * columns of the first one, and the values of each row fill its columns by position. With
489
+ * grouping, a row whose index fields (by position) have the values of a row already there adds
490
+ * its other values to it (numbers are added, texts joined) instead.
491
+ */
492
+ export function combineAddDataset(client, data, group, indexFields, alias, fewerFieldsText, ctx) {
493
+ const result = client ?? cloneTable(data);
494
+ if (data.columns.length > result.columns.length)
495
+ throw new NamedException(fewerFieldsText + ' - ' + alias, alias);
496
+ let keyColumns = [];
497
+ if (group) {
498
+ if (indexFields.length === 0)
499
+ throw new NamedException('A grouped union needs the index fields (MyBaseIndexFields) of ' + alias, alias);
500
+ keyColumns = indexFields.map((field) => {
501
+ const k = indexOfColumn(result, field);
502
+ if (k < 0)
503
+ throw new NamedException("Field '" + field + "' not found in " + alias, alias);
504
+ return k;
505
+ });
506
+ }
507
+ const columns = result.columns;
508
+ for (const row of data.rows) {
509
+ let target = null;
510
+ if (group) {
511
+ for (const existing of result.rows) {
512
+ let same = true;
513
+ for (const k of keyColumns) {
514
+ const key = convertUnionValue(cell(data, row, k), columns[k].type, ctx);
515
+ if (!unionValuesEqual(cell(result, existing, k), boxedOf(columns[k], key), ctx)) {
516
+ same = false;
517
+ break;
518
+ }
519
+ }
520
+ if (same) {
521
+ target = existing;
522
+ break;
523
+ }
524
+ }
525
+ }
526
+ if (target !== null) {
527
+ for (let i = 0; i < data.columns.length; i++) {
528
+ const value = cell(data, row, i);
529
+ if (keyColumns.includes(i) || value === null)
530
+ continue;
531
+ const current = cell(result, target, i);
532
+ if (current === null)
533
+ target[i] = convertUnionValue(value, columns[i].type, ctx);
534
+ else
535
+ target[i] = convertUnionValue(addUnionValues(current, value, ctx), columns[i].type, ctx);
536
+ }
537
+ }
538
+ else {
539
+ const newRow = new Array(columns.length).fill(null);
540
+ for (let i = 0; i < data.columns.length; i++)
541
+ newRow[i] = convertUnionValue(cell(data, row, i), columns[i].type, ctx);
542
+ result.rows.push(newRow);
543
+ }
544
+ }
545
+ return result;
546
+ }
547
+ /**
548
+ * CombineParallel: a parallel union. The columns of the first dataset, an integer column named as
549
+ * the prefix (0 for the rows only the first one has, 1 for the rows the second one reaches), and
550
+ * the columns of the second one with the prefix but its common fields. With common fields each
551
+ * row of the second dataset fills the first row of the first one with the same values that it
552
+ * has not filled yet, or a new row; the result is ordered by the prefix column and then by the
553
+ * common fields. Without them the rows are paired by position.
554
+ */
555
+ export function combineParallel(data1, rows1, data2, prefix, commonFields, originalFields, ctx) {
556
+ const result = cloneTable(data1);
557
+ const prefixColumn = addColumn(result, prefix, 'Int32');
558
+ const targets = [];
559
+ for (const column of data2.columns) {
560
+ const name = column.name;
561
+ if (!commonFields.includes(toUpperNet(name)))
562
+ targets.push(addColumn(result, prefix + name, column.type));
563
+ else {
564
+ const target = indexOfColumn(result, name);
565
+ if (target < 0)
566
+ throw new NamedException("Field '" + name + "' not found in the first dataset of the union", name);
567
+ targets.push(target);
568
+ }
569
+ }
570
+ const keyColumns = [];
571
+ const keySources = [];
572
+ for (let k = 0; k < originalFields.length; k++) {
573
+ const column = indexOfColumn(result, originalFields[k]);
574
+ if (column < 0)
575
+ throw new NamedException("Field '" + originalFields[k] + "' not found in the union", originalFields[k]);
576
+ keyColumns.push(column);
577
+ // BuildUnion has checked that there are as many common fields as original ones
578
+ const common = commonFields[k];
579
+ const source = indexOfColumn(data2, common);
580
+ if (source < 0)
581
+ throw new NamedException("Field '" + common + "' not found in the dataset of the union", common);
582
+ keySources.push(source);
583
+ }
584
+ const width = result.columns.length;
585
+ for (const row of rows1) {
586
+ const newRow = new Array(width).fill(null);
587
+ for (let i = 0; i < data1.columns.length; i++)
588
+ newRow[i] = row[i] ?? null;
589
+ newRow[prefixColumn] = 0;
590
+ result.rows.push(newRow);
591
+ }
592
+ let position = 0;
593
+ for (const row of data2.rows) {
594
+ let target = null;
595
+ if (keyColumns.length > 0) {
596
+ // C# goes through the rows ordered by the prefix column and the keys and takes the first
597
+ // one not filled whose keys are equal: in a stable order that is the first such row of the
598
+ // table, so the rows are not sorted for each one
599
+ for (const candidate of result.rows) {
600
+ if (candidate[prefixColumn] !== 0)
601
+ continue;
602
+ let same = true;
603
+ for (let k = 0; k < keyColumns.length; k++) {
604
+ const column = result.columns[keyColumns[k]];
605
+ const key = convertUnionValue(cell(data2, row, keySources[k]), column.type, ctx);
606
+ if (!unionValuesEqual(cell(result, candidate, keyColumns[k]), boxedOf(column, key), ctx)) {
607
+ same = false;
608
+ break;
609
+ }
610
+ }
611
+ if (same) {
612
+ target = candidate;
613
+ break;
614
+ }
615
+ }
616
+ }
617
+ else if (position < result.rows.length)
618
+ target = result.rows[position];
619
+ if (target === null) {
620
+ target = new Array(width).fill(null);
621
+ result.rows.push(target);
622
+ }
623
+ for (let i = 0; i < data2.columns.length; i++)
624
+ target[targets[i]] = convertUnionValue(cell(data2, row, i), result.columns[targets[i]].type, ctx);
625
+ target[prefixColumn] = 1;
626
+ position++;
627
+ }
628
+ if (keyColumns.length === 0)
629
+ return result;
630
+ return { columns: result.columns, rows: orderedRowsBy(result, prefixColumn, keyColumns, ctx) };
631
+ }
632
+ // ---------------------------------------------------------------------------------------------
633
+ // The DataView of a union
634
+ /** DataTable.Compare of two texts: trailing spaces (and ideographic spaces) do not count. */
635
+ function trimEndSpaces(s) {
636
+ let n = s.length;
637
+ while (n > 0 && (s.charCodeAt(n - 1) === 0x20 || s.charCodeAt(n - 1) === 0x3000))
638
+ n--;
639
+ return n === s.length ? s : s.substring(0, n);
640
+ }
641
+ /** The order of two values of a column as the DataView index compares them (DBNull first). */
642
+ function viewCompare(column, a, b, ctx) {
643
+ if (a === null || b === null)
644
+ return a === null && b === null ? 0 : a === null ? -1 : 1;
645
+ switch (column.type) {
646
+ case 'String':
647
+ return compareText(unionCollator(ctx.culture), trimEndSpaces(a), trimEndSpaces(b));
648
+ case 'Binary': {
649
+ // ObjectStorage.CompareWithFamilies of two arrays: the length, then element by element
650
+ const x = a;
651
+ const y = b;
652
+ if (x.length !== y.length)
653
+ return x.length < y.length ? -1 : 1;
654
+ for (let i = 0; i < x.length; i++)
655
+ if (x[i] !== y[i])
656
+ return x[i] < y[i] ? -1 : 1;
657
+ return 0;
658
+ }
659
+ default:
660
+ return compareUnionValues(boxedOf(column, a), boxedOf(column, b), ctx);
661
+ }
662
+ }
663
+ /** DataTable.ParseSortString: the columns of a sort ("A,B DESC"); a name that is not a column fails. */
664
+ function parseSortString(table, sort) {
665
+ const fields = [];
666
+ if (sort.length === 0)
667
+ return fields;
668
+ for (const part of sort.split(',')) {
669
+ let current = part.trim();
670
+ const upper = current.toUpperCase();
671
+ let descending = false;
672
+ if (current.length >= 5 && upper.endsWith(' ASC'))
673
+ current = current.substring(0, current.length - 4).trim();
674
+ else if (current.length >= 6 && upper.endsWith(' DESC')) {
675
+ descending = true;
676
+ current = current.substring(0, current.length - 5).trim();
677
+ }
678
+ if (current.startsWith('[')) {
679
+ if (current.endsWith(']'))
680
+ current = current.substring(1, current.length - 1);
681
+ else
682
+ throw argumentException(`${part} isn't a valid Sort string entry.`);
683
+ }
684
+ const column = indexOfColumn(table, current);
685
+ if (column < 0)
686
+ throw new NetException('IndexOutOfRangeException', `Cannot find column ${current}.`);
687
+ fields.push({ column, descending });
688
+ }
689
+ return fields;
690
+ }
691
+ /**
692
+ * The DataView of a union: the rows of its table sorted by the columns of its sort (the rows with
693
+ * the same values in the order of the table), and FindRows on them.
694
+ */
695
+ export class UnionView extends ResultView {
696
+ sort;
697
+ ctx;
698
+ constructor(table, sort, ctx) {
699
+ const fields = parseSortString(table, sort);
700
+ const rows = [...table.rows];
701
+ if (fields.length > 0)
702
+ rows.sort((a, b) => {
703
+ for (const f of fields) {
704
+ const r = viewCompare(table.columns[f.column], a[f.column] ?? null, b[f.column] ?? null, ctx);
705
+ if (r !== 0)
706
+ return f.descending ? -r : r;
707
+ }
708
+ return 0;
709
+ });
710
+ super(table.columns, rows);
711
+ this.sort = fields;
712
+ this.ctx = ctx;
713
+ }
714
+ /**
715
+ * DataView.FindRows: the rows whose sort columns have the values of the key, each value
716
+ * converted to the type of its column first.
717
+ */
718
+ findRows(key) {
719
+ const n = this.sort.length;
720
+ if (n === 0)
721
+ return super.findRows(key);
722
+ if (key.length === 0 || key.length !== n)
723
+ throw argumentException(`Expecting ${n} value(s) for the key being indexed, but received ${key.length} value(s).`);
724
+ if (this.rows.length === 0)
725
+ return [];
726
+ const ctx = this.ctx;
727
+ // DataColumn.ConvertValue: the key in the type of its column (the culture of the table)
728
+ const values = this.sort.map((f, i) => storeValue(this.columns[f.column], key[i], ctx.culture, ctx.culture));
729
+ // The rows with equal keys are together in the view, in the order of the table
730
+ return this.rows.filter((row) => this.sort.every((f, i) => viewCompare(this.columns[f.column], row[f.column] ?? null, values[i], ctx) === 0));
731
+ }
732
+ }
733
+ //# sourceMappingURL=union.js.map