@trebco/treb 23.6.2 → 25.0.0-rc1

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 (217) hide show
  1. package/.eslintignore +8 -0
  2. package/.eslintrc.js +164 -0
  3. package/README-shadow-DOM.md +88 -0
  4. package/README.md +37 -130
  5. package/api-config.json +29 -0
  6. package/api-generator/api-generator-types.ts +82 -0
  7. package/api-generator/api-generator.ts +1172 -0
  8. package/api-generator/package.json +3 -0
  9. package/build/treb-spreadsheet.mjs +14 -0
  10. package/{treb.d.ts → build/treb.d.ts} +293 -299
  11. package/esbuild-custom-element.mjs +336 -0
  12. package/esbuild.js +305 -0
  13. package/package.json +43 -14
  14. package/treb-base-types/package.json +5 -0
  15. package/treb-base-types/src/api_types.ts +36 -0
  16. package/treb-base-types/src/area.ts +583 -0
  17. package/treb-base-types/src/basic_types.ts +45 -0
  18. package/treb-base-types/src/cell.ts +612 -0
  19. package/treb-base-types/src/cells.ts +1066 -0
  20. package/treb-base-types/src/color.ts +124 -0
  21. package/treb-base-types/src/import.ts +71 -0
  22. package/treb-base-types/src/index-standalone.ts +29 -0
  23. package/treb-base-types/src/index.ts +42 -0
  24. package/treb-base-types/src/layout.ts +47 -0
  25. package/treb-base-types/src/localization.ts +187 -0
  26. package/treb-base-types/src/rectangle.ts +145 -0
  27. package/treb-base-types/src/render_text.ts +72 -0
  28. package/treb-base-types/src/style.ts +545 -0
  29. package/treb-base-types/src/table.ts +109 -0
  30. package/treb-base-types/src/text_part.ts +54 -0
  31. package/treb-base-types/src/theme.ts +608 -0
  32. package/treb-base-types/src/union.ts +152 -0
  33. package/treb-base-types/src/value-type.ts +164 -0
  34. package/treb-base-types/style/resizable.css +59 -0
  35. package/treb-calculator/modern.tsconfig.json +11 -0
  36. package/treb-calculator/package.json +5 -0
  37. package/treb-calculator/src/calculator.ts +2546 -0
  38. package/treb-calculator/src/complex-math.ts +558 -0
  39. package/treb-calculator/src/dag/array-vertex.ts +198 -0
  40. package/treb-calculator/src/dag/graph.ts +951 -0
  41. package/treb-calculator/src/dag/leaf_vertex.ts +118 -0
  42. package/treb-calculator/src/dag/spreadsheet_vertex.ts +327 -0
  43. package/treb-calculator/src/dag/spreadsheet_vertex_base.ts +44 -0
  44. package/treb-calculator/src/dag/vertex.ts +352 -0
  45. package/treb-calculator/src/descriptors.ts +162 -0
  46. package/treb-calculator/src/expression-calculator.ts +1069 -0
  47. package/treb-calculator/src/function-error.ts +103 -0
  48. package/treb-calculator/src/function-library.ts +103 -0
  49. package/treb-calculator/src/functions/base-functions.ts +1214 -0
  50. package/treb-calculator/src/functions/checkbox.ts +164 -0
  51. package/treb-calculator/src/functions/complex-functions.ts +253 -0
  52. package/treb-calculator/src/functions/finance-functions.ts +399 -0
  53. package/treb-calculator/src/functions/information-functions.ts +102 -0
  54. package/treb-calculator/src/functions/matrix-functions.ts +182 -0
  55. package/treb-calculator/src/functions/sparkline.ts +335 -0
  56. package/treb-calculator/src/functions/statistics-functions.ts +350 -0
  57. package/treb-calculator/src/functions/text-functions.ts +298 -0
  58. package/treb-calculator/src/index.ts +27 -0
  59. package/treb-calculator/src/notifier-types.ts +59 -0
  60. package/treb-calculator/src/primitives.ts +428 -0
  61. package/treb-calculator/src/utilities.ts +305 -0
  62. package/treb-charts/package.json +5 -0
  63. package/treb-charts/src/chart-functions.ts +156 -0
  64. package/treb-charts/src/chart-types.ts +230 -0
  65. package/treb-charts/src/chart.ts +1288 -0
  66. package/treb-charts/src/index.ts +24 -0
  67. package/treb-charts/src/main.ts +37 -0
  68. package/treb-charts/src/rectangle.ts +52 -0
  69. package/treb-charts/src/renderer.ts +1841 -0
  70. package/treb-charts/src/util.ts +122 -0
  71. package/treb-charts/style/charts.scss +221 -0
  72. package/treb-charts/style/old-charts.scss +250 -0
  73. package/treb-embed/markup/layout.html +137 -0
  74. package/treb-embed/markup/toolbar.html +175 -0
  75. package/treb-embed/modern.tsconfig.json +25 -0
  76. package/treb-embed/src/custom-element/content-types.d.ts +18 -0
  77. package/treb-embed/src/custom-element/global.d.ts +11 -0
  78. package/treb-embed/src/custom-element/spreadsheet-constructor.ts +1227 -0
  79. package/treb-embed/src/custom-element/treb-global.ts +44 -0
  80. package/treb-embed/src/custom-element/treb-spreadsheet-element.ts +52 -0
  81. package/treb-embed/src/embedded-spreadsheet.ts +5362 -0
  82. package/treb-embed/src/index.ts +16 -0
  83. package/treb-embed/src/language-model.ts +41 -0
  84. package/treb-embed/src/options.ts +320 -0
  85. package/treb-embed/src/progress-dialog.ts +228 -0
  86. package/treb-embed/src/selection-state.ts +16 -0
  87. package/treb-embed/src/spinner.ts +42 -0
  88. package/treb-embed/src/toolbar-message.ts +96 -0
  89. package/treb-embed/src/types.ts +167 -0
  90. package/treb-embed/style/autocomplete.scss +103 -0
  91. package/treb-embed/style/dark-theme.scss +114 -0
  92. package/treb-embed/style/defaults.scss +36 -0
  93. package/treb-embed/style/dialog.scss +181 -0
  94. package/treb-embed/style/dropdown-select.scss +101 -0
  95. package/treb-embed/style/formula-bar.scss +193 -0
  96. package/treb-embed/style/grid.scss +374 -0
  97. package/treb-embed/style/layout.scss +424 -0
  98. package/treb-embed/style/mouse-mask.scss +67 -0
  99. package/treb-embed/style/note.scss +92 -0
  100. package/treb-embed/style/overlay-editor.scss +102 -0
  101. package/treb-embed/style/spinner.scss +92 -0
  102. package/treb-embed/style/tab-bar.scss +228 -0
  103. package/treb-embed/style/table.scss +80 -0
  104. package/treb-embed/style/theme-defaults.scss +444 -0
  105. package/treb-embed/style/toolbar.scss +416 -0
  106. package/treb-embed/style/tooltip.scss +68 -0
  107. package/treb-embed/style/treb-icons.scss +130 -0
  108. package/treb-embed/style/treb-spreadsheet-element.scss +20 -0
  109. package/treb-embed/style/z-index.scss +43 -0
  110. package/treb-export/docs/charts.md +68 -0
  111. package/treb-export/modern.tsconfig.json +19 -0
  112. package/treb-export/package.json +4 -0
  113. package/treb-export/src/address-type.ts +77 -0
  114. package/treb-export/src/base-template.ts +22 -0
  115. package/treb-export/src/column-width.ts +85 -0
  116. package/treb-export/src/drawing2/chart-template-components2.ts +389 -0
  117. package/treb-export/src/drawing2/chart2.ts +282 -0
  118. package/treb-export/src/drawing2/column-chart-template2.ts +521 -0
  119. package/treb-export/src/drawing2/donut-chart-template2.ts +296 -0
  120. package/treb-export/src/drawing2/drawing2.ts +355 -0
  121. package/treb-export/src/drawing2/embedded-image.ts +71 -0
  122. package/treb-export/src/drawing2/scatter-chart-template2.ts +555 -0
  123. package/treb-export/src/export-worker/export-worker.ts +99 -0
  124. package/treb-export/src/export-worker/index-modern.ts +22 -0
  125. package/treb-export/src/export2.ts +2204 -0
  126. package/treb-export/src/import2.ts +882 -0
  127. package/treb-export/src/relationship.ts +36 -0
  128. package/treb-export/src/shared-strings2.ts +128 -0
  129. package/treb-export/src/template-2.ts +22 -0
  130. package/treb-export/src/unescape_xml.ts +47 -0
  131. package/treb-export/src/workbook-sheet2.ts +182 -0
  132. package/treb-export/src/workbook-style2.ts +1285 -0
  133. package/treb-export/src/workbook-theme2.ts +88 -0
  134. package/treb-export/src/workbook2.ts +491 -0
  135. package/treb-export/src/xml-utils.ts +201 -0
  136. package/treb-export/template/base/[Content_Types].xml +2 -0
  137. package/treb-export/template/base/_rels/.rels +2 -0
  138. package/treb-export/template/base/docProps/app.xml +2 -0
  139. package/treb-export/template/base/docProps/core.xml +12 -0
  140. package/treb-export/template/base/xl/_rels/workbook.xml.rels +2 -0
  141. package/treb-export/template/base/xl/sharedStrings.xml +2 -0
  142. package/treb-export/template/base/xl/styles.xml +2 -0
  143. package/treb-export/template/base/xl/theme/theme1.xml +2 -0
  144. package/treb-export/template/base/xl/workbook.xml +2 -0
  145. package/treb-export/template/base/xl/worksheets/sheet1.xml +2 -0
  146. package/treb-export/template/base.xlsx +0 -0
  147. package/treb-format/package.json +8 -0
  148. package/treb-format/src/format.test.ts +213 -0
  149. package/treb-format/src/format.ts +942 -0
  150. package/treb-format/src/format_cache.ts +199 -0
  151. package/treb-format/src/format_parser.ts +723 -0
  152. package/treb-format/src/index.ts +25 -0
  153. package/treb-format/src/number_format_section.ts +100 -0
  154. package/treb-format/src/value_parser.ts +337 -0
  155. package/treb-grid/package.json +5 -0
  156. package/treb-grid/src/editors/autocomplete.ts +394 -0
  157. package/treb-grid/src/editors/autocomplete_matcher.ts +260 -0
  158. package/treb-grid/src/editors/formula_bar.ts +473 -0
  159. package/treb-grid/src/editors/formula_editor_base.ts +910 -0
  160. package/treb-grid/src/editors/overlay_editor.ts +511 -0
  161. package/treb-grid/src/index.ts +37 -0
  162. package/treb-grid/src/layout/base_layout.ts +2618 -0
  163. package/treb-grid/src/layout/grid_layout.ts +299 -0
  164. package/treb-grid/src/layout/rectangle_cache.ts +86 -0
  165. package/treb-grid/src/render/selection-renderer.ts +414 -0
  166. package/treb-grid/src/render/svg_header_overlay.ts +93 -0
  167. package/treb-grid/src/render/svg_selection_block.ts +187 -0
  168. package/treb-grid/src/render/tile_renderer.ts +2122 -0
  169. package/treb-grid/src/types/annotation.ts +216 -0
  170. package/treb-grid/src/types/border_constants.ts +34 -0
  171. package/treb-grid/src/types/clipboard_data.ts +31 -0
  172. package/treb-grid/src/types/data_model.ts +334 -0
  173. package/treb-grid/src/types/drag_mask.ts +81 -0
  174. package/treb-grid/src/types/grid.ts +7743 -0
  175. package/treb-grid/src/types/grid_base.ts +3644 -0
  176. package/treb-grid/src/types/grid_command.ts +470 -0
  177. package/treb-grid/src/types/grid_events.ts +124 -0
  178. package/treb-grid/src/types/grid_options.ts +97 -0
  179. package/treb-grid/src/types/grid_selection.ts +60 -0
  180. package/treb-grid/src/types/named_range.ts +369 -0
  181. package/treb-grid/src/types/scale-control.ts +202 -0
  182. package/treb-grid/src/types/serialize_options.ts +72 -0
  183. package/treb-grid/src/types/set_range_options.ts +52 -0
  184. package/treb-grid/src/types/sheet.ts +3099 -0
  185. package/treb-grid/src/types/sheet_types.ts +95 -0
  186. package/treb-grid/src/types/tab_bar.ts +464 -0
  187. package/treb-grid/src/types/tile.ts +59 -0
  188. package/treb-grid/src/types/update_flags.ts +75 -0
  189. package/treb-grid/src/util/dom_utilities.ts +44 -0
  190. package/treb-grid/src/util/fontmetrics2.ts +179 -0
  191. package/treb-grid/src/util/ua.ts +104 -0
  192. package/treb-logo.svg +18 -0
  193. package/treb-parser/package.json +5 -0
  194. package/treb-parser/src/csv-parser.ts +122 -0
  195. package/treb-parser/src/index.ts +25 -0
  196. package/treb-parser/src/md-parser.ts +526 -0
  197. package/treb-parser/src/parser-types.ts +397 -0
  198. package/treb-parser/src/parser.test.ts +298 -0
  199. package/treb-parser/src/parser.ts +2673 -0
  200. package/treb-utils/package.json +5 -0
  201. package/treb-utils/src/dispatch.ts +57 -0
  202. package/treb-utils/src/event_source.ts +147 -0
  203. package/treb-utils/src/ievent_source.ts +33 -0
  204. package/treb-utils/src/index.ts +31 -0
  205. package/treb-utils/src/measurement.ts +174 -0
  206. package/treb-utils/src/resizable.ts +160 -0
  207. package/treb-utils/src/scale.ts +137 -0
  208. package/treb-utils/src/serialize_html.ts +124 -0
  209. package/treb-utils/src/template.ts +70 -0
  210. package/treb-utils/src/validate_uri.ts +61 -0
  211. package/tsconfig.json +10 -0
  212. package/tsproject.json +30 -0
  213. package/util/license-plugin-esbuild.js +86 -0
  214. package/util/list-css-vars.sh +46 -0
  215. package/README-esm.md +0 -37
  216. package/treb-bundle.css +0 -2
  217. package/treb-bundle.mjs +0 -15
@@ -0,0 +1,164 @@
1
+ /*
2
+ * This file is part of TREB.
3
+ *
4
+ * TREB is free software: you can redistribute it and/or modify it under the
5
+ * terms of the GNU General Public License as published by the Free Software
6
+ * Foundation, either version 3 of the License, or (at your option) any
7
+ * later version.
8
+ *
9
+ * TREB is distributed in the hope that it will be useful, but WITHOUT ANY
10
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11
+ * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12
+ * details.
13
+ *
14
+ * You should have received a copy of the GNU General Public License along
15
+ * with TREB. If not, see <https://www.gnu.org/licenses/>.
16
+ *
17
+ * Copyright 2022-2023 trebco, llc.
18
+ * info@treb.app
19
+ *
20
+ */
21
+
22
+ import { RenderFunctionOptions, ClickFunctionOptions, ClickFunctionResult, Style, RenderFunctionResult } from 'treb-base-types';
23
+
24
+ export const ClickCheckbox = (options: ClickFunctionOptions): ClickFunctionResult => {
25
+ const { x, y, width, height, cell } = options;
26
+ const result: ClickFunctionResult = {};
27
+
28
+ const offset = 3;
29
+ const scaled = Math.round(16 * (options.scale || 1));
30
+
31
+ // console.info('options', options);
32
+
33
+ if (cell && width && height && x && y) {
34
+
35
+ const box = {
36
+ x: offset,
37
+ y: height - offset - scaled,
38
+ }
39
+
40
+ if (cell.style) {
41
+ switch (cell.style.vertical_align) {
42
+ case Style.VerticalAlign.Top:
43
+ box.y = offset;
44
+ break;
45
+
46
+ case Style.VerticalAlign.Middle:
47
+ box.y = Math.round((height - scaled) / 2);
48
+ break;
49
+
50
+ }
51
+
52
+ switch (cell.style.horizontal_align) {
53
+ case Style.HorizontalAlign.Right:
54
+ box.x = Math.round(width - offset - scaled);
55
+ break;
56
+
57
+ case Style.HorizontalAlign.Center:
58
+ box.x = Math.round((width - scaled) / 2);
59
+ break;
60
+
61
+ }
62
+ }
63
+
64
+ if (x >= box.x && x <= box.x + scaled && y >= box.y && y <= box.y + scaled) {
65
+ result.value = `=Checkbox(${cell.calculated ? 'FALSE' : 'TRUE'})`;
66
+ result.block_selection = true;
67
+ }
68
+ }
69
+
70
+ return result;
71
+ };
72
+
73
+ export const RenderCheckbox = (options: RenderFunctionOptions): RenderFunctionResult => {
74
+
75
+ const {context, width, height, cell} = options;
76
+ const scale = options.scale || 1;
77
+
78
+ context.lineJoin = 'round';
79
+ context.lineCap = 'round';
80
+
81
+ const offset = 3 * scale;
82
+ const scaled = 16 * scale;
83
+
84
+ let x = offset;
85
+ let y = height - offset - scaled;
86
+
87
+ if (cell.style) {
88
+ switch (cell.style.vertical_align) {
89
+ case Style.VerticalAlign.Top:
90
+ y = offset;
91
+ break;
92
+
93
+ case Style.VerticalAlign.Middle:
94
+ y = // Math.round
95
+ ((height - scaled) / 2);
96
+ break;
97
+
98
+ }
99
+
100
+ switch (cell.style.horizontal_align) {
101
+ case Style.HorizontalAlign.Right:
102
+ x = // Math.round
103
+ (width - offset - scaled);
104
+ break;
105
+
106
+ case Style.HorizontalAlign.Center:
107
+ x = // Math.round
108
+ ((width - scaled) / 2);
109
+ break;
110
+
111
+ }
112
+ }
113
+
114
+ x = Math.floor(x) + .5;
115
+ y = Math.floor(y) + .5;
116
+
117
+ const x2 = Math.floor(x + scaled) + .5;
118
+ const y2 = Math.floor(y + scaled) + .5;
119
+
120
+ if (cell && cell.calculated) {
121
+ context.lineWidth = .5;
122
+ context.fillStyle = context.strokeStyle;
123
+ context.beginPath();
124
+
125
+ context.moveTo(x, y);
126
+ context.lineTo(x + 16 * scale, y);
127
+ context.lineTo(x + 16 * scale, y + 16 * scale);
128
+ context.lineTo(x, y + 16 * scale);
129
+ context.closePath();
130
+
131
+ context.moveTo(x + 15 * scale, y + 4 * scale);
132
+ for (const point of [
133
+ [13.59, 2.58],
134
+ [6, 10.17],
135
+ [2.41, 6.59],
136
+ [1, 8],
137
+ [6, 13],
138
+ ]) {
139
+ context.lineTo(x + point[0] * scale, y + point[1] * scale);
140
+ }
141
+ context.closePath();
142
+ context.fill();
143
+
144
+ }
145
+ else {
146
+
147
+ context.lineWidth = 1;
148
+ context.lineJoin = 'round';
149
+
150
+ context.beginPath();
151
+ context.moveTo(x, y);
152
+ context.lineTo(x2, y);
153
+ context.lineTo(x2, y2);
154
+ context.lineTo(x, y2);
155
+ context.closePath();
156
+ context.stroke();
157
+
158
+ // context.lineWidth = Math.max(2, 2 * scale);
159
+ // context.strokeRect(x, y, 16 * scale, 16 * scale);
160
+ }
161
+
162
+ return { handled: true }; // painted
163
+
164
+ };
@@ -0,0 +1,253 @@
1
+ /*
2
+ * This file is part of TREB.
3
+ *
4
+ * TREB is free software: you can redistribute it and/or modify it under the
5
+ * terms of the GNU General Public License as published by the Free Software
6
+ * Foundation, either version 3 of the License, or (at your option) any
7
+ * later version.
8
+ *
9
+ * TREB is distributed in the hope that it will be useful, but WITHOUT ANY
10
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11
+ * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12
+ * details.
13
+ *
14
+ * You should have received a copy of the GNU General Public License along
15
+ * with TREB. If not, see <https://www.gnu.org/licenses/>.
16
+ *
17
+ * Copyright 2022-2023 trebco, llc.
18
+ * info@treb.app
19
+ *
20
+ */
21
+
22
+ import type { FunctionMap } from '../descriptors';
23
+ import { IsComplex, NumberUnion, UnionValue, ValueType } from 'treb-base-types';
24
+ import * as Utils from '../utilities';
25
+ import { ValueError } from '../function-error';
26
+ import { RectangularToPolar } from '../complex-math';
27
+
28
+ export const ComplexFunctionLibrary: FunctionMap = {
29
+
30
+ IsComplex: {
31
+ description: 'Returns true if the reference is a complex number',
32
+ arguments: [{
33
+ name: 'Reference',
34
+ metadata: true, /* OK with array metadata */
35
+ }],
36
+ fn: Utils.ApplyAsArray((ref: UnionValue): UnionValue => {
37
+ return {
38
+ type: ValueType.boolean,
39
+ value: ref?.value && IsComplex(ref.value.value),
40
+ };
41
+ }),
42
+ },
43
+
44
+
45
+ Real: {
46
+ description: 'Returns the real part of a complex number',
47
+ arguments: [
48
+ { boxed: true },
49
+ ],
50
+ fn: Utils.ApplyAsArray((ref: UnionValue): UnionValue => {
51
+ if (ref.type === ValueType.number) {
52
+ return { ...ref };
53
+ }
54
+ if (ref.type === ValueType.complex) {
55
+ return {
56
+ type: ValueType.number,
57
+ value: ref.value.real || 0,
58
+ };
59
+ }
60
+ if (ref.type === ValueType.undefined || (ref.type === ValueType.string && ref.value === '')) {
61
+ return {
62
+ type: ValueType.number,
63
+ value: 0,
64
+ };
65
+ }
66
+ return ValueError();
67
+ }),
68
+ },
69
+
70
+ Imaginary: {
71
+ description: 'Returns the imaginary part of a complex number (as real)',
72
+ arguments: [
73
+ { boxed: true },
74
+ ],
75
+ fn: Utils.ApplyAsArray((ref: UnionValue): UnionValue => {
76
+ if (ref.type === ValueType.complex) {
77
+ return {
78
+ type: ValueType.number,
79
+ value: ref.value.imaginary || 0,
80
+ };
81
+ }
82
+ if (ref.type === ValueType.number ||
83
+ ref.type === ValueType.undefined ||
84
+ (ref.type === ValueType.string && ref.value === '')) {
85
+ return {
86
+ type: ValueType.number,
87
+ value: 0,
88
+ };
89
+ }
90
+ return ValueError();
91
+ }),
92
+ },
93
+
94
+ Conjugate: {
95
+ description: 'Returns the conjugate of a complex number',
96
+ arguments: [
97
+ { boxed: true },
98
+ ],
99
+ fn: Utils.ApplyAsArray((arg: UnionValue): UnionValue => {
100
+ if (arg.type === ValueType.complex) {
101
+ return {
102
+ type: ValueType.complex,
103
+ value: {
104
+ real: arg.value.real,
105
+ imaginary: -arg.value.imaginary,
106
+ },
107
+ };
108
+ }
109
+ else if (arg.type === ValueType.number || arg.type === ValueType.undefined || !arg.value) {
110
+ return {
111
+ type: ValueType.number, value: arg.value || 0,
112
+ };
113
+ }
114
+ else {
115
+ return ValueError();
116
+ }
117
+ }),
118
+ },
119
+
120
+ Arg: {
121
+ description: 'Returns the principal argument of a complex number (in radians)',
122
+ arguments: [
123
+ { boxed: true },
124
+ ],
125
+ fn: Utils.ApplyAsArray((ref: UnionValue): UnionValue => {
126
+
127
+ if (ref.type === ValueType.complex) {
128
+ return {
129
+ type: ValueType.number,
130
+ value: Math.atan2(ref.value.imaginary, ref.value.real),
131
+ }
132
+ }
133
+
134
+ if (ref.type === ValueType.number ||
135
+ ref.type === ValueType.undefined ||
136
+ (ref.type === ValueType.string && ref.value === '')) {
137
+ return {
138
+ type: ValueType.number,
139
+ value: Math.atan2(0, (ref as NumberUnion).value || 0), // this is clumsy now because typing has _improved_
140
+ }
141
+ }
142
+
143
+ return ValueError();
144
+ }),
145
+ },
146
+
147
+ Rectangular: {
148
+ description: 'Converts a complex number in polar form to rectangular form',
149
+ arguments: [
150
+ { name: 'r' },
151
+ { name: 'θ in radians' },
152
+ ],
153
+ fn: (r = 0, theta = 0): UnionValue => {
154
+ return {
155
+ type: ValueType.complex,
156
+ value: {
157
+ real: r * Math.cos(theta),
158
+ imaginary: r * Math.sin(theta),
159
+ },
160
+ }
161
+ },
162
+ },
163
+
164
+ Complex: {
165
+ description: 'Ensures that the given value will be treated as a complex number',
166
+ arguments: [
167
+ { boxed: true },
168
+ ],
169
+
170
+ // FIXME: this should use flatten? not sure
171
+
172
+ fn: Utils.ApplyAsArray((a: UnionValue): UnionValue => {
173
+
174
+ if (a.type === ValueType.complex) {
175
+ return a;
176
+ }
177
+
178
+ if (a.type === ValueType.number || a.type === ValueType.undefined || !a.value) {
179
+ return {
180
+ type: ValueType.complex,
181
+ value: {
182
+ imaginary: 0,
183
+ real: a.value || 0,
184
+ },
185
+ }
186
+ }
187
+
188
+ return ValueError();
189
+
190
+ }),
191
+ },
192
+
193
+ /**
194
+ * unfortunately we can't override the log function because the complex
195
+ * log function has a different meaning even when applied to reals, i.e.
196
+ * Log(a + 0i) !== ln(a)
197
+ *
198
+ * note that Log(0) is undefined -- we need to return an error here, but
199
+ * what error? let's do #VALUE
200
+ *
201
+ */
202
+ ComplexLog: {
203
+ description: 'Returns the principal value Log(z) of a complex number z',
204
+ arguments: [
205
+ { boxed: true },
206
+ ],
207
+ fn: Utils.ApplyAsArray((a: UnionValue): UnionValue => {
208
+
209
+ // real -> complex
210
+ if (a.type === ValueType.number) {
211
+
212
+ if (!a.value) {
213
+ return ValueError();
214
+ }
215
+
216
+ a = {
217
+ type: ValueType.complex,
218
+ value: {
219
+ real: a.value,
220
+ imaginary: 0,
221
+ },
222
+ };
223
+ }
224
+
225
+ // other zero -> complex
226
+ else if (a.type === ValueType.undefined || (a.type === ValueType.string && a.value === '')) {
227
+ return ValueError();
228
+ }
229
+
230
+ if (a.type === ValueType.complex) {
231
+
232
+ // from polar form, the principal value is
233
+ // Log z = ln r + iθ
234
+
235
+ const polar = RectangularToPolar(a.value);
236
+ const value = {
237
+ real: Math.log(polar.r),
238
+ imaginary: polar.theta,
239
+ };
240
+
241
+ return (value.imaginary) ?
242
+ { type: ValueType.complex, value } :
243
+ { type: ValueType.number, value: value.real };
244
+
245
+ }
246
+
247
+ return ValueError();
248
+
249
+ }),
250
+ },
251
+
252
+ };
253
+