@odoo/o-spreadsheet 14.0.1 → 15.0.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 (203) hide show
  1. package/dist/o_spreadsheet.cjs.js +31302 -0
  2. package/dist/o_spreadsheet.d.ts +6604 -0
  3. package/dist/o_spreadsheet.esm.js +31257 -0
  4. package/dist/o_spreadsheet.iife.js +31304 -0
  5. package/dist/o_spreadsheet.iife.min.js +3071 -0
  6. package/package.json +57 -15
  7. package/.eslintrc.json +0 -20
  8. package/.husky/post-checkout +0 -9
  9. package/.husky/pre-commit +0 -39
  10. package/.prettierignore +0 -1
  11. package/astToFormula.patch +0 -0
  12. package/demo/data.js +0 -255
  13. package/demo/index.html +0 -9
  14. package/demo/libs/Chart.bundle.js +0 -20755
  15. package/demo/main.js +0 -101
  16. package/doc/readme.md +0 -46
  17. package/rollup.config.js +0 -23
  18. package/src/base_plugin.ts +0 -102
  19. package/src/components/autofill.ts +0 -146
  20. package/src/components/bottom_bar.ts +0 -199
  21. package/src/components/color_picker.ts +0 -150
  22. package/src/components/composer/autocomplete_dropdown.ts +0 -135
  23. package/src/components/composer/composer.ts +0 -487
  24. package/src/components/composer/content_editable_helper.ts +0 -146
  25. package/src/components/figures/chart.ts +0 -70
  26. package/src/components/figures/container.ts +0 -268
  27. package/src/components/figures/text_figure.ts +0 -36
  28. package/src/components/grid.ts +0 -635
  29. package/src/components/helpers/dom_helpers.ts +0 -7
  30. package/src/components/helpers/drag_and_drop.ts +0 -17
  31. package/src/components/icons.ts +0 -36
  32. package/src/components/index.ts +0 -1
  33. package/src/components/menu.ts +0 -288
  34. package/src/components/overlay.ts +0 -457
  35. package/src/components/scrollbar.ts +0 -22
  36. package/src/components/selection_input.ts +0 -168
  37. package/src/components/side_panel/cell_is_rule_editor.ts +0 -169
  38. package/src/components/side_panel/chart_panel.ts +0 -104
  39. package/src/components/side_panel/color_scale_rule_editor.ts +0 -145
  40. package/src/components/side_panel/conditional_formatting.ts +0 -396
  41. package/src/components/side_panel/side_panel.ts +0 -127
  42. package/src/components/side_panel/translations_terms.ts +0 -45
  43. package/src/components/spreadsheet.ts +0 -198
  44. package/src/components/top_bar.ts +0 -535
  45. package/src/constants.ts +0 -26
  46. package/src/data.ts +0 -149
  47. package/src/fonts.ts +0 -41
  48. package/src/formulas/compiler.ts +0 -258
  49. package/src/formulas/composer_tokenizer.ts +0 -70
  50. package/src/formulas/index.ts +0 -15
  51. package/src/formulas/normalize.ts +0 -52
  52. package/src/formulas/parser.ts +0 -280
  53. package/src/formulas/range_tokenizer.ts +0 -97
  54. package/src/formulas/tokenizer.ts +0 -242
  55. package/src/functions/arguments.ts +0 -114
  56. package/src/functions/dates.ts +0 -302
  57. package/src/functions/helpers.ts +0 -592
  58. package/src/functions/index.ts +0 -52
  59. package/src/functions/module_database.ts +0 -282
  60. package/src/functions/module_date.ts +0 -743
  61. package/src/functions/module_info.ts +0 -69
  62. package/src/functions/module_logical.ts +0 -208
  63. package/src/functions/module_lookup.ts +0 -185
  64. package/src/functions/module_math.ts +0 -1252
  65. package/src/functions/module_operators.ts +0 -291
  66. package/src/functions/module_statistical.ts +0 -882
  67. package/src/functions/module_text.ts +0 -370
  68. package/src/helpers/color.ts +0 -30
  69. package/src/helpers/coordinates.ts +0 -71
  70. package/src/helpers/grid_manipulation.ts +0 -68
  71. package/src/helpers/index.ts +0 -7
  72. package/src/helpers/misc.ts +0 -76
  73. package/src/helpers/numbers.ts +0 -117
  74. package/src/helpers/uuid.ts +0 -19
  75. package/src/helpers/zones.ts +0 -263
  76. package/src/history.ts +0 -186
  77. package/src/index.ts +0 -84
  78. package/src/model.ts +0 -261
  79. package/src/plugins/autofill.ts +0 -412
  80. package/src/plugins/chart.ts +0 -404
  81. package/src/plugins/clipboard.ts +0 -461
  82. package/src/plugins/conditional_format.ts +0 -445
  83. package/src/plugins/core.ts +0 -1502
  84. package/src/plugins/edition.ts +0 -157
  85. package/src/plugins/evaluation.ts +0 -428
  86. package/src/plugins/figures.ts +0 -123
  87. package/src/plugins/formatting.ts +0 -630
  88. package/src/plugins/highlight.ts +0 -187
  89. package/src/plugins/index.ts +0 -32
  90. package/src/plugins/merge.ts +0 -524
  91. package/src/plugins/renderer.ts +0 -571
  92. package/src/plugins/selection.ts +0 -562
  93. package/src/plugins/selection_inputs.ts +0 -326
  94. package/src/registries/autofill_modifiers.ts +0 -76
  95. package/src/registries/autofill_rules.ts +0 -98
  96. package/src/registries/figure_registry.ts +0 -30
  97. package/src/registries/index.ts +0 -7
  98. package/src/registries/menu_items_registry.ts +0 -110
  99. package/src/registries/menus/cell_menu_registry.ts +0 -79
  100. package/src/registries/menus/col_menu_registry.ts +0 -63
  101. package/src/registries/menus/index.ts +0 -6
  102. package/src/registries/menus/menu_items_actions.ts +0 -425
  103. package/src/registries/menus/row_menu_registry.ts +0 -63
  104. package/src/registries/menus/sheet_menu_registry.ts +0 -75
  105. package/src/registries/menus/topbar_menu_registry.ts +0 -204
  106. package/src/registries/side_panel_registry.ts +0 -26
  107. package/src/registries/topbar_component_registry.ts +0 -12
  108. package/src/registry.ts +0 -51
  109. package/src/translation.ts +0 -44
  110. package/src/types/autofill.ts +0 -48
  111. package/src/types/chart.ts +0 -26
  112. package/src/types/commands.ts +0 -716
  113. package/src/types/conditional_formatting.ts +0 -124
  114. package/src/types/env.ts +0 -13
  115. package/src/types/functions.ts +0 -35
  116. package/src/types/getters.ts +0 -103
  117. package/src/types/index.ts +0 -20
  118. package/src/types/misc.ts +0 -143
  119. package/src/types/rendering.ts +0 -41
  120. package/src/types/workbook_data.ts +0 -62
  121. package/tests/__mocks__/uuid.ts +0 -7
  122. package/tests/canvas.mock.ts +0 -34
  123. package/tests/components/__mocks__/content_editable_helper.ts +0 -79
  124. package/tests/components/__mocks__/scrollbar.ts +0 -18
  125. package/tests/components/__snapshots__/autocomplete_dropdown_test.ts.snap +0 -29
  126. package/tests/components/__snapshots__/bottom_bar_test.ts.snap +0 -75
  127. package/tests/components/__snapshots__/context_menu_test.ts.snap +0 -88
  128. package/tests/components/__snapshots__/grid_test.ts.snap +0 -60
  129. package/tests/components/__snapshots__/spreadsheet_test.ts.snap +0 -455
  130. package/tests/components/__snapshots__/top_bar_test.ts.snap +0 -705
  131. package/tests/components/autocomplete_dropdown_test.ts +0 -315
  132. package/tests/components/autofill_test.ts +0 -167
  133. package/tests/components/bottom_bar_test.ts +0 -262
  134. package/tests/components/chart_side_panel_test.ts +0 -123
  135. package/tests/components/composer_test.ts +0 -400
  136. package/tests/components/context_menu_test.ts +0 -825
  137. package/tests/components/grid_manipulation_test.ts +0 -163
  138. package/tests/components/grid_test.ts +0 -581
  139. package/tests/components/overlay_test.ts +0 -541
  140. package/tests/components/selection_input_test.ts +0 -259
  141. package/tests/components/side_panel_test.ts +0 -148
  142. package/tests/components/spreadsheet_test.ts +0 -194
  143. package/tests/components/top_bar_test.ts +0 -427
  144. package/tests/data_test.ts +0 -175
  145. package/tests/dom_helper.ts +0 -42
  146. package/tests/formulas/__snapshots__/compiler_test.ts.snap +0 -302
  147. package/tests/formulas/compiler_test.ts +0 -266
  148. package/tests/formulas/composer_tokenizer_test.ts +0 -137
  149. package/tests/formulas/expressions_test.ts +0 -57
  150. package/tests/formulas/formulas_test.ts +0 -80
  151. package/tests/formulas/normalize_test.ts +0 -82
  152. package/tests/formulas/parser_test.ts +0 -224
  153. package/tests/formulas/range_tokenizer_test.ts +0 -187
  154. package/tests/formulas/tokenizer_test.ts +0 -164
  155. package/tests/functions/arguments_test.ts +0 -141
  156. package/tests/functions/dates_test.ts +0 -1081
  157. package/tests/functions/functions_test.ts +0 -53
  158. package/tests/functions/helper_test.ts +0 -98
  159. package/tests/functions/module_database_test.ts +0 -311
  160. package/tests/functions/module_date_test.ts +0 -941
  161. package/tests/functions/module_info_test.ts +0 -139
  162. package/tests/functions/module_logical_test.ts +0 -369
  163. package/tests/functions/module_lookup_test.ts +0 -541
  164. package/tests/functions/module_math_test.ts +0 -2482
  165. package/tests/functions/module_operators_test.ts +0 -955
  166. package/tests/functions/module_statistical_test.ts +0 -2014
  167. package/tests/functions/module_text_test.ts +0 -667
  168. package/tests/helpers/coordinates_test.ts +0 -25
  169. package/tests/helpers/misc_test.ts +0 -95
  170. package/tests/helpers/numbers_test.ts +0 -196
  171. package/tests/helpers/zones_test.ts +0 -97
  172. package/tests/helpers.ts +0 -384
  173. package/tests/menu_items_registry_test.ts +0 -540
  174. package/tests/model/plugins/__snapshots__/chart_test.ts.snap +0 -90
  175. package/tests/model/plugins/chart_test.ts +0 -562
  176. package/tests/model_test.ts +0 -82
  177. package/tests/plugins/__snapshots__/conditional_formatting_test.ts.snap +0 -131
  178. package/tests/plugins/__snapshots__/renderer_test.ts.snap +0 -371
  179. package/tests/plugins/autofill_test.ts +0 -457
  180. package/tests/plugins/borders_test.ts +0 -295
  181. package/tests/plugins/clipboard_test.ts +0 -1180
  182. package/tests/plugins/conditional_formatting_test.ts +0 -1253
  183. package/tests/plugins/core_test.ts +0 -614
  184. package/tests/plugins/edition_test.ts +0 -100
  185. package/tests/plugins/evaluation_async_test.ts +0 -332
  186. package/tests/plugins/evaluation_dates_test.ts +0 -79
  187. package/tests/plugins/evaluation_test.ts +0 -901
  188. package/tests/plugins/figures_test.ts +0 -327
  189. package/tests/plugins/formatting_test.ts +0 -391
  190. package/tests/plugins/grid_manipulation_test.ts +0 -1366
  191. package/tests/plugins/highlight_test.ts +0 -413
  192. package/tests/plugins/history_test.ts +0 -142
  193. package/tests/plugins/import_export_test.ts +0 -276
  194. package/tests/plugins/merges_test.ts +0 -490
  195. package/tests/plugins/navigation_test.ts +0 -181
  196. package/tests/plugins/plugins/evaluation_test.ts +0 -111
  197. package/tests/plugins/renderer_test.ts +0 -598
  198. package/tests/plugins/resizing_test.ts +0 -152
  199. package/tests/plugins/selection_input_test.ts +0 -409
  200. package/tests/plugins/selection_test.ts +0 -442
  201. package/tests/plugins/sheets_test.ts +0 -643
  202. package/tests/plugins/style_test.ts +0 -90
  203. package/tsconfig.json +0 -22
@@ -1,114 +0,0 @@
1
- import { _lt } from "../translation";
2
- import { Arg, ArgType } from "../types";
3
-
4
- //------------------------------------------------------------------------------
5
- // Arg description DSL
6
- //------------------------------------------------------------------------------
7
-
8
- const ARG_REGEXP = /(.*?)\((.*?)\)(.*)/;
9
- const ARG_TYPES: ArgType[] = [
10
- "ANY",
11
- "BOOLEAN",
12
- "DATE",
13
- "NUMBER",
14
- "STRING",
15
- "RANGE",
16
- "RANGE<BOOLEAN>",
17
- "RANGE<DATE>",
18
- "RANGE<NUMBER>",
19
- "RANGE<STRING>",
20
- ];
21
-
22
- /**
23
- * This function is meant to be used as a tag for a template strings.
24
- *
25
- * Its job is to convert a textual description of the list of arguments into an
26
- * actual array of Arg, suitable for consumption.
27
- */
28
- export function args(strings: string): Arg[] {
29
- let lines = strings.split("\n");
30
- const result: Arg[] = [];
31
- for (let l of lines) {
32
- l = l.trim();
33
- if (l) {
34
- result.push(makeArg(l));
35
- }
36
- }
37
- return result;
38
- }
39
-
40
- function makeArg(str: string): Arg {
41
- let parts = str.match(ARG_REGEXP)!;
42
- let name = parts[1].trim();
43
- let types: ArgType[] = [];
44
- let isOptional = false;
45
- let isRepeating = false;
46
- let isLazy = false;
47
- let defaultVal;
48
-
49
- for (let param of parts[2].split(",")) {
50
- const key = param.trim().toUpperCase();
51
- let type = ARG_TYPES.find((t) => key === t);
52
- if (type) {
53
- types.push(type);
54
- } else if (key === "RANGE<ANY>") {
55
- types.push("RANGE");
56
- } else if (key === "OPTIONAL") {
57
- isOptional = true;
58
- } else if (key === "REPEATING") {
59
- isRepeating = true;
60
- } else if (key === "LAZY") {
61
- isLazy = true;
62
- } else if (key.startsWith("DEFAULT=")) {
63
- const value = param.trim().slice(8);
64
- defaultVal = value[0] === '"' ? value.slice(1, -1) : parseFloat(value);
65
- }
66
- }
67
- let description = parts[3].trim();
68
- const result: Arg = {
69
- name,
70
- description,
71
- type: types,
72
- };
73
- if (isOptional) {
74
- result.optional = true;
75
- }
76
- if (isRepeating) {
77
- result.repeating = true;
78
- }
79
- if (isLazy) {
80
- result.lazy = true;
81
- }
82
- if (defaultVal !== undefined) {
83
- result.default = defaultVal;
84
- }
85
- return result;
86
- }
87
-
88
- //------------------------------------------------------------------------------
89
- // Argument validation
90
- //------------------------------------------------------------------------------
91
-
92
- export function validateArguments(args: Arg[]) {
93
- let previousArgRepeating: boolean | undefined = false;
94
- let previousArgOptional: boolean | undefined = false;
95
- for (let current of args) {
96
- if (previousArgRepeating) {
97
- throw new Error(
98
- _lt(
99
- "Function ${name} has at least 2 arguments that are repeating. The maximum repeating arguments is 1."
100
- )
101
- );
102
- }
103
-
104
- if (previousArgOptional && !current.optional) {
105
- throw new Error(
106
- _lt(
107
- "Function ${name} has at mandatory arguments declared after optional ones. All optional arguments must be after all mandatory arguments."
108
- )
109
- );
110
- }
111
- previousArgRepeating = current.repeating;
112
- previousArgOptional = current.optional;
113
- }
114
- }
@@ -1,302 +0,0 @@
1
- // -----------------------------------------------------------------------------
2
- // Date Type
3
- // -----------------------------------------------------------------------------
4
- import { _lt } from "../translation";
5
- import { toNumber } from "./helpers";
6
-
7
- /**
8
- * All Spreadsheet dates are internally stored as an object with two values:
9
- * - value (number), which represent the number of day till 30/12/1899
10
- * - format (string), which keep the information on how the date was defined
11
- */
12
- export interface InternalDate {
13
- value: number;
14
- format: string;
15
- jsDate: Date;
16
- }
17
-
18
- // -----------------------------------------------------------------------------
19
- // Parsing
20
- // -----------------------------------------------------------------------------
21
-
22
- const CURRENT_MILLENIAL = 2000; // note: don't forget to update this in 2999
23
- const CURRENT_YEAR = new Date().getFullYear();
24
- const INITIAL_1900_DAY = new Date(1899, 11, 30) as any;
25
- const INITIAL_JS_DAY = new Date(0) as any;
26
- const DATE_JS_1900_OFFSET = INITIAL_JS_DAY - INITIAL_1900_DAY;
27
-
28
- const mdyDateRegexp = /^\d{1,2}(\/|-|\s)\d{1,2}((\/|-|\s)\d{1,4})?$/;
29
- const ymdDateRegexp = /^\d{3,4}(\/|-|\s)\d{1,2}(\/|-|\s)\d{1,2}$/;
30
- const timeRegexp = /((\d+(:\d+)?(:\d+)?\s*(AM|PM))|(\d+:\d+(:\d+)?))$/;
31
-
32
- export function parseDateTime(str: string): InternalDate | null {
33
- str = str.trim();
34
-
35
- let time;
36
- const timeMatch = str.match(timeRegexp);
37
- if (timeMatch) {
38
- time = parseTime(timeMatch[0]);
39
- if (time === null) {
40
- return null;
41
- }
42
- str = str.replace(timeMatch[0], "").trim();
43
- }
44
-
45
- let date;
46
- const mdyDateMatch = str.match(mdyDateRegexp);
47
- const ymdDateMatch = str.match(ymdDateRegexp);
48
- if (mdyDateMatch || ymdDateMatch) {
49
- let dateMatch;
50
- if (mdyDateMatch) {
51
- dateMatch = mdyDateMatch[0];
52
- date = parseDate(dateMatch, "mdy");
53
- } else {
54
- dateMatch = ymdDateMatch![0];
55
- date = parseDate(dateMatch, "ymd");
56
- }
57
- if (date === null) {
58
- return null;
59
- }
60
- str = str.replace(dateMatch, "").trim();
61
- }
62
-
63
- if (str !== "" || !(date || time)) {
64
- return null;
65
- }
66
-
67
- if (date && time) {
68
- return {
69
- value: date.value + time.value,
70
- format: date.format + " " + (time.format === "hhhh:mm:ss" ? "hh:mm:ss" : time.format),
71
- jsDate: new Date(
72
- date.jsDate.getFullYear() + time.jsDate.getFullYear() - 1899,
73
- date.jsDate.getMonth() + time.jsDate.getMonth() - 11,
74
- date.jsDate.getDate() + time.jsDate.getDate() - 30,
75
- date.jsDate.getHours() + time.jsDate.getHours(),
76
- date.jsDate.getMinutes() + time.jsDate.getMinutes(),
77
- date.jsDate.getSeconds() + time.jsDate.getSeconds()
78
- ),
79
- };
80
- }
81
-
82
- return date || time;
83
- }
84
-
85
- function parseDate(str: string, dateFormat: string): InternalDate | null {
86
- const isMDY = dateFormat === "mdy";
87
- const isYMD = dateFormat === "ymd";
88
- if (isMDY || isYMD) {
89
- const parts: string[] = str.split(/\/|-|\s/);
90
- const monthIndex = isMDY ? 0 : 1;
91
- const dayIndex = isMDY ? 1 : 2;
92
- const yearIndex = isMDY ? 2 : 0;
93
- const month = Number(parts[monthIndex]);
94
- const day = Number(parts[dayIndex]);
95
- const leadingZero =
96
- (parts[monthIndex].length === 2 && month < 10) || (parts[dayIndex].length === 2 && day < 10);
97
- const year = parts[yearIndex] ? inferYear(parts[yearIndex]) : CURRENT_YEAR;
98
- const jsDate = new Date(year, month - 1, day);
99
- const sep = str.match(/\/|-|\s/)![0];
100
- if (jsDate.getMonth() !== month - 1 || jsDate.getDate() !== day) {
101
- // invalid date
102
- return null;
103
- }
104
- const delta = (jsDate as any) - INITIAL_1900_DAY;
105
-
106
- let format = leadingZero ? `mm${sep}dd` : `m${sep}d`;
107
- if (parts[yearIndex]) {
108
- format = isMDY ? format + sep + "yyyy" : "yyyy" + sep + format;
109
- }
110
- return {
111
- value: Math.round(delta / 86400000),
112
- format: format,
113
- jsDate,
114
- };
115
- }
116
- return null;
117
- }
118
-
119
- function inferYear(str: string): number {
120
- const nbr = Number(str);
121
- switch (str.length) {
122
- case 1:
123
- return CURRENT_MILLENIAL + nbr;
124
- case 2:
125
- const offset = CURRENT_MILLENIAL + nbr > CURRENT_YEAR + 10 ? -100 : 0;
126
- const base = CURRENT_MILLENIAL + offset;
127
- return base + nbr;
128
- case 3:
129
- case 4:
130
- return nbr;
131
- }
132
- return 0;
133
- }
134
-
135
- function parseTime(str: string): InternalDate | null {
136
- str = str.trim();
137
- if (timeRegexp.test(str)) {
138
- const isAM = /AM/i.test(str);
139
- const isPM = /PM/i.test(str);
140
- const strTime = isAM || isPM ? str.substring(0, str.length - 2).trim() : str;
141
- const parts: string[] = strTime.split(/:/);
142
- const isMinutes = parts.length >= 2;
143
- const isSeconds = parts.length === 3;
144
- let hours = Number(parts[0]);
145
- let minutes = isMinutes ? Number(parts[1]) : 0;
146
- let seconds = isSeconds ? Number(parts[2]) : 0;
147
- let format = isSeconds ? "hh:mm:ss" : "hh:mm";
148
-
149
- if (isAM || isPM) {
150
- format += " a";
151
- } else if (!isMinutes) {
152
- return null;
153
- }
154
-
155
- if (hours >= 12 && isAM) {
156
- hours -= 12;
157
- } else if (hours < 12 && isPM) {
158
- hours += 12;
159
- }
160
-
161
- minutes += Math.floor(seconds / 60);
162
- seconds %= 60;
163
- hours += Math.floor(minutes / 60);
164
- minutes %= 60;
165
-
166
- if (hours >= 24) {
167
- format = "hhhh:mm:ss";
168
- }
169
-
170
- const jsDate = new Date(1899, 11, 30, hours, minutes, seconds);
171
-
172
- return {
173
- value: hours / 24 + minutes / 1440 + seconds / 86400,
174
- format: format,
175
- jsDate: jsDate,
176
- };
177
- }
178
- return null;
179
- }
180
-
181
- // -----------------------------------------------------------------------------
182
- // Conversion
183
- // -----------------------------------------------------------------------------
184
-
185
- function numberToDate(value: number): Date {
186
- const truncValue = Math.trunc(value);
187
- let date = new Date(truncValue * 86400 * 1000 - DATE_JS_1900_OFFSET);
188
-
189
- let time = value - truncValue;
190
- time = time < 0 ? 1 + time : time;
191
-
192
- const hours = Math.round(time * 24);
193
- const minutes = Math.round((time - hours / 24) * 24 * 60);
194
- const seconds = Math.round((time - hours / 24 - minutes / 24 / 60) * 24 * 60 * 60);
195
-
196
- date.setHours(hours);
197
- date.setMinutes(minutes);
198
- date.setSeconds(seconds);
199
-
200
- return date;
201
- }
202
-
203
- export function toNativeDate(date: any): Date {
204
- if (typeof date === "object" && date !== null) {
205
- if (!date.jsDate) {
206
- date.jsDate = new Date(date.value * 86400 * 1000 - DATE_JS_1900_OFFSET);
207
- }
208
- return date.jsDate;
209
- }
210
- if (typeof date === "string") {
211
- let result = parseDateTime(date);
212
- if (result !== null && result.jsDate) {
213
- return result.jsDate;
214
- }
215
- }
216
- return numberToDate(toNumber(date));
217
- }
218
-
219
- // -----------------------------------------------------------------------------
220
- // Formatting
221
- // -----------------------------------------------------------------------------
222
-
223
- export function formatDateTime(date: Omit<InternalDate, "jsDate">, format?: string): string {
224
- // TODO: unify the format functions for date and datetime
225
- // This requires some code to 'parse' or 'tokenize' the format, keep it in a
226
- // cache, and use it in a single mapping, that recognizes the special list
227
- // of tokens dd,d,m,y,h, ... and preserves the rest
228
-
229
- const dateTimeFormat = format || date.format;
230
- const jsDate = toNativeDate(date);
231
- const indexH = dateTimeFormat.indexOf("h");
232
- let strDate = "";
233
- let strTime = "";
234
- if (indexH > 0) {
235
- strDate = formatJSDate(jsDate, dateTimeFormat.substring(0, indexH - 1));
236
- strTime = formatJSTime(jsDate, dateTimeFormat.substring(indexH));
237
- } else if (indexH === 0) {
238
- strTime = formatJSTime(jsDate, dateTimeFormat);
239
- } else if (indexH < 0) {
240
- strDate = formatJSDate(jsDate, dateTimeFormat);
241
- }
242
- return strDate + (strDate && strTime ? " " : "") + strTime;
243
- }
244
-
245
- function formatJSDate(date: Date, format: string): string {
246
- const sep = format.match(/\/|-|\s/)![0];
247
- const parts = format.split(sep);
248
- return parts
249
- .map((p) => {
250
- switch (p) {
251
- case "d":
252
- return date.getDate();
253
- case "dd":
254
- return date.getDate().toString().padStart(2, "0");
255
- case "m":
256
- return date.getMonth() + 1;
257
- case "mm":
258
- return String(date.getMonth() + 1).padStart(2, "0");
259
- case "yyyy":
260
- return date.getFullYear();
261
- default:
262
- throw new Error(_lt("invalid format"));
263
- }
264
- })
265
- .join(sep);
266
- }
267
-
268
- function formatJSTime(date: Date, format: string): string {
269
- let parts = format.split(/:|\s/);
270
-
271
- const dateHours = date.getHours();
272
- const isMeridian = parts[parts.length - 1] === "a";
273
- let hours = dateHours;
274
- let meridian = "";
275
- if (isMeridian) {
276
- hours = hours === 0 ? 12 : hours > 12 ? hours - 12 : hours;
277
- meridian = dateHours >= 12 ? " PM" : " AM";
278
- parts.pop();
279
- }
280
-
281
- return (
282
- parts
283
- .map((p) => {
284
- switch (p) {
285
- case "hhhh":
286
- const helapsedHours = Math.floor(
287
- (date.getTime() - INITIAL_1900_DAY) / (60 * 60 * 1000)
288
- );
289
- return helapsedHours.toString();
290
- case "hh":
291
- return hours.toString().padStart(2, "0");
292
- case "mm":
293
- return date.getMinutes().toString().padStart(2, "0");
294
- case "ss":
295
- return date.getSeconds().toString().padStart(2, "0");
296
- default:
297
- throw new Error("invalid format");
298
- }
299
- })
300
- .join(":") + meridian
301
- );
302
- }