@deephaven/jsapi-utils 0.12.1-beta.6

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 (59) hide show
  1. package/LICENSE +176 -0
  2. package/README.md +23 -0
  3. package/dist/DateUtils.d.ts +82 -0
  4. package/dist/DateUtils.d.ts.map +1 -0
  5. package/dist/DateUtils.js +284 -0
  6. package/dist/DateUtils.js.map +1 -0
  7. package/dist/Formatter.d.ts +65 -0
  8. package/dist/Formatter.d.ts.map +1 -0
  9. package/dist/Formatter.js +166 -0
  10. package/dist/Formatter.js.map +1 -0
  11. package/dist/FormatterUtils.d.ts +24 -0
  12. package/dist/FormatterUtils.d.ts.map +1 -0
  13. package/dist/FormatterUtils.js +40 -0
  14. package/dist/FormatterUtils.js.map +1 -0
  15. package/dist/TableUtils.d.ts +194 -0
  16. package/dist/TableUtils.d.ts.map +1 -0
  17. package/dist/TableUtils.js +1326 -0
  18. package/dist/TableUtils.js.map +1 -0
  19. package/dist/formatters/BooleanColumnFormatter.d.ts +7 -0
  20. package/dist/formatters/BooleanColumnFormatter.d.ts.map +1 -0
  21. package/dist/formatters/BooleanColumnFormatter.js +26 -0
  22. package/dist/formatters/BooleanColumnFormatter.js.map +1 -0
  23. package/dist/formatters/CharColumnFormatter.d.ts +7 -0
  24. package/dist/formatters/CharColumnFormatter.d.ts.map +1 -0
  25. package/dist/formatters/CharColumnFormatter.js +15 -0
  26. package/dist/formatters/CharColumnFormatter.js.map +1 -0
  27. package/dist/formatters/DateTimeColumnFormatter.d.ts +40 -0
  28. package/dist/formatters/DateTimeColumnFormatter.d.ts.map +1 -0
  29. package/dist/formatters/DateTimeColumnFormatter.js +123 -0
  30. package/dist/formatters/DateTimeColumnFormatter.js.map +1 -0
  31. package/dist/formatters/DecimalColumnFormatter.d.ts +64 -0
  32. package/dist/formatters/DecimalColumnFormatter.d.ts.map +1 -0
  33. package/dist/formatters/DecimalColumnFormatter.js +128 -0
  34. package/dist/formatters/DecimalColumnFormatter.js.map +1 -0
  35. package/dist/formatters/DefaultColumnFormatter.d.ts +6 -0
  36. package/dist/formatters/DefaultColumnFormatter.d.ts.map +1 -0
  37. package/dist/formatters/DefaultColumnFormatter.js +14 -0
  38. package/dist/formatters/DefaultColumnFormatter.js.map +1 -0
  39. package/dist/formatters/IntegerColumnFormatter.d.ts +60 -0
  40. package/dist/formatters/IntegerColumnFormatter.d.ts.map +1 -0
  41. package/dist/formatters/IntegerColumnFormatter.js +121 -0
  42. package/dist/formatters/IntegerColumnFormatter.js.map +1 -0
  43. package/dist/formatters/StringColumnFormatter.d.ts +7 -0
  44. package/dist/formatters/StringColumnFormatter.d.ts.map +1 -0
  45. package/dist/formatters/StringColumnFormatter.js +12 -0
  46. package/dist/formatters/StringColumnFormatter.js.map +1 -0
  47. package/dist/formatters/TableColumnFormatter.d.ts +44 -0
  48. package/dist/formatters/TableColumnFormatter.d.ts.map +1 -0
  49. package/dist/formatters/TableColumnFormatter.js +65 -0
  50. package/dist/formatters/TableColumnFormatter.js.map +1 -0
  51. package/dist/formatters/index.d.ts +10 -0
  52. package/dist/formatters/index.d.ts.map +1 -0
  53. package/dist/formatters/index.js +10 -0
  54. package/dist/formatters/index.js.map +1 -0
  55. package/dist/index.d.ts +6 -0
  56. package/dist/index.d.ts.map +1 -0
  57. package/dist/index.js +6 -0
  58. package/dist/index.js.map +1 -0
  59. package/package.json +45 -0
package/LICENSE ADDED
@@ -0,0 +1,176 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
package/README.md ADDED
@@ -0,0 +1,23 @@
1
+ # @deephaven/jsapi-utils
2
+
3
+ A library with some JS utility methods for interacting with the JSAPI.
4
+
5
+ ## Install
6
+
7
+ ```bash
8
+ npm install --save @deephaven/jsapi-utils
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ ```javascript
14
+ import { TableUtils } from '@deephaven/jsapi-utils';
15
+
16
+ if (TableUtils.isDateType(columnType)) {
17
+ console.log('Date type', columnType);
18
+ } else if (TableUtils.isNumberType(columnType)) {
19
+ console.log('Number type', columnType);
20
+ } else {
21
+ console.log('Unrecognized type', columnType);
22
+ }
23
+ ```
@@ -0,0 +1,82 @@
1
+ import type { DateWrapper } from '@deephaven/jsapi-shim';
2
+ interface DateParts<T> {
3
+ year: T;
4
+ month: T;
5
+ date: T;
6
+ hours: T;
7
+ minutes: T;
8
+ seconds: T;
9
+ nanos: T;
10
+ }
11
+ export declare class DateUtils {
12
+ static FULL_DATE_FORMAT: string;
13
+ static months: string[];
14
+ /**
15
+ *
16
+ * @param timeZone The time zone to parse this time in. E.g. America/New_York
17
+ * @param year The year for the date
18
+ * @param month The month, starting at 0
19
+ * @param day The day, starting at 1
20
+ * @param hour The hours
21
+ * @param minute The minutes
22
+ * @param second The seconds
23
+ * @param ns The nanoseconds
24
+ */
25
+ static makeDateWrapper(timeZone: string, year: number, month?: number, day?: number, hour?: number, minute?: number, second?: number, ns?: number): DateWrapper;
26
+ /**
27
+ * Takes the string the user entered and returns the next nanos value
28
+ * @param nanoString The nano string to get the next one of
29
+ * @returns The value of the next nanos
30
+ */
31
+ static getNextNanos(nanoString: string): number;
32
+ /**
33
+ * @param components The string components that were parsed from the original string
34
+ * @param values The values that were parsed from the components
35
+ * @param timeZone The time zone to parse the date in. E.g. America/New_York
36
+ * @returns Returns the DateWrapper for the next date, or null if a full date was passed in
37
+ */
38
+ static getNextDate(components: DateParts<string>, values: DateParts<number>, timeZone: string): DateWrapper | null;
39
+ /**
40
+ * Get the JS month value for the provided string.
41
+ * Matches digits or a month name (eg. '1', '01', 'jan', 'january' should all work)
42
+ * @param monthString The string to parse to a JS month value
43
+ * @returns number The JS month value, which starts at 0 for january, or NaN if nothing could be parsed
44
+ */
45
+ static parseMonth(monthString: string): number;
46
+ /**
47
+ * Parse a date object out of the provided string segments.
48
+ * Also using `parseMonth` to get month names like Aug/August rather than
49
+ * simply doing `parseInt`.
50
+ * @param yearString The year part of the string
51
+ * @param monthString The month part of the string
52
+ * @param dayString The day part of the string
53
+ * @param hourString The hour part of the string
54
+ * @param minuteString The minute part of the string
55
+ * @param secondString The second part of the string
56
+ * @param nanoString The milli part of the string
57
+ */
58
+ static parseDateValues(yearString: string, monthString: string, dayString: string, hourString: string, minuteString: string, secondString: string, nanoString: string): DateParts<number> | null;
59
+ /**
60
+ * Parse out a date time string into it's string components.
61
+ * Anything that is not captured in the string will be undefined.
62
+ *
63
+ * @param dateTimeString The date time string to parse
64
+ * @returns Containing the date time components
65
+ */
66
+ static parseDateTimeString(dateTimeString: string): DateParts<string>;
67
+ /**
68
+ * Parses the date range provided from a string of text.
69
+ * @param text The string to parse the date from. Can be a keyword like "today", or in the format "2018-08-04"
70
+ * @param timeZone The time zone to parse this range in. E.g. America/New_York
71
+ * @returns A tuple with the start and end value/null for that date range, or both null
72
+ */
73
+ static parseDateRange(text: string, timeZone: string): [DateWrapper, DateWrapper | null] | [null, null];
74
+ /**
75
+ * Gets the Js Date object from the provided DateWrapper.
76
+ * In unit test, DateWrapper is just a number provided in millis, so handles that case.
77
+ * @param dateWrapper The DateWrapper object, or time in millis
78
+ */
79
+ static getJsDate(dateWrapper: DateWrapper | number): Date;
80
+ }
81
+ export default DateUtils;
82
+ //# sourceMappingURL=DateUtils.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"DateUtils.d.ts","sourceRoot":"","sources":["../src/DateUtils.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAC;AAEzD,UAAU,SAAS,CAAC,CAAC;IACnB,IAAI,EAAE,CAAC,CAAC;IACR,KAAK,EAAE,CAAC,CAAC;IACT,IAAI,EAAE,CAAC,CAAC;IACR,KAAK,EAAE,CAAC,CAAC;IACT,OAAO,EAAE,CAAC,CAAC;IACX,OAAO,EAAE,CAAC,CAAC;IACX,KAAK,EAAE,CAAC,CAAC;CACV;AAED,qBAAa,SAAS;IACpB,MAAM,CAAC,gBAAgB,SAAmC;IAE1D,MAAM,CAAC,MAAM,WAaX;IAEF;;;;;;;;;;OAUG;IACH,MAAM,CAAC,eAAe,CACpB,QAAQ,EAAE,MAAM,EAChB,IAAI,EAAE,MAAM,EACZ,KAAK,SAAI,EACT,GAAG,SAAI,EACP,IAAI,SAAI,EACR,MAAM,SAAI,EACV,MAAM,SAAI,EACV,EAAE,SAAI,GACL,WAAW;IAiBd;;;;OAIG;IACH,MAAM,CAAC,YAAY,CAAC,UAAU,EAAE,MAAM,GAAG,MAAM;IAQ/C;;;;;OAKG;IACH,MAAM,CAAC,WAAW,CAChB,UAAU,EAAE,SAAS,CAAC,MAAM,CAAC,EAC7B,MAAM,EAAE,SAAS,CAAC,MAAM,CAAC,EACzB,QAAQ,EAAE,MAAM,GACf,WAAW,GAAG,IAAI;IA4CrB;;;;;OAKG;IACH,MAAM,CAAC,UAAU,CAAC,WAAW,EAAE,MAAM,GAAG,MAAM;IAqB9C;;;;;;;;;;;OAWG;IACH,MAAM,CAAC,eAAe,CACpB,UAAU,EAAE,MAAM,EAClB,WAAW,EAAE,MAAM,EACnB,SAAS,EAAE,MAAM,EACjB,UAAU,EAAE,MAAM,EAClB,YAAY,EAAE,MAAM,EACpB,YAAY,EAAE,MAAM,EACpB,UAAU,EAAE,MAAM,GACjB,SAAS,CAAC,MAAM,CAAC,GAAG,IAAI;IAwB3B;;;;;;OAMG;IACH,MAAM,CAAC,mBAAmB,CAAC,cAAc,EAAE,MAAM,GAAG,SAAS,CAAC,MAAM,CAAC;IAiCrE;;;;;OAKG;IACH,MAAM,CAAC,cAAc,CACnB,IAAI,EAAE,MAAM,EACZ,QAAQ,EAAE,MAAM,GACf,CAAC,WAAW,EAAE,WAAW,GAAG,IAAI,CAAC,GAAG,CAAC,IAAI,EAAE,IAAI,CAAC;IAwFnD;;;;OAIG;IACH,MAAM,CAAC,SAAS,CAAC,WAAW,EAAE,WAAW,GAAG,MAAM,GAAG,IAAI;CAM1D;AAED,eAAe,SAAS,CAAC"}
@@ -0,0 +1,284 @@
1
+ function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
2
+
3
+ import dh from '@deephaven/jsapi-shim';
4
+ export class DateUtils {
5
+ /**
6
+ *
7
+ * @param timeZone The time zone to parse this time in. E.g. America/New_York
8
+ * @param year The year for the date
9
+ * @param month The month, starting at 0
10
+ * @param day The day, starting at 1
11
+ * @param hour The hours
12
+ * @param minute The minutes
13
+ * @param second The seconds
14
+ * @param ns The nanoseconds
15
+ */
16
+ static makeDateWrapper(timeZone, year) {
17
+ var month = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 0;
18
+ var day = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 1;
19
+ var hour = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : 0;
20
+ var minute = arguments.length > 5 && arguments[5] !== undefined ? arguments[5] : 0;
21
+ var second = arguments.length > 6 && arguments[6] !== undefined ? arguments[6] : 0;
22
+ var ns = arguments.length > 7 && arguments[7] !== undefined ? arguments[7] : 0;
23
+ var yearString = "".concat(year).padStart(4, '0');
24
+ var monthString = "".concat(month + 1).padStart(2, '0');
25
+ var dayString = "".concat(day).padStart(2, '0');
26
+ var hourString = "".concat(hour).padStart(2, '0');
27
+ var minuteString = "".concat(minute).padStart(2, '0');
28
+ var secondString = "".concat(second).padStart(2, '0');
29
+ var nanoString = "".concat(ns).padStart(9, '0');
30
+ var dateString = "".concat(yearString, "-").concat(monthString, "-").concat(dayString, " ").concat(hourString, ":").concat(minuteString, ":").concat(secondString, ".").concat(nanoString);
31
+ return dh.i18n.DateTimeFormat.parse(DateUtils.FULL_DATE_FORMAT, dateString, dh.i18n.TimeZone.getTimeZone(timeZone));
32
+ }
33
+ /**
34
+ * Takes the string the user entered and returns the next nanos value
35
+ * @param nanoString The nano string to get the next one of
36
+ * @returns The value of the next nanos
37
+ */
38
+
39
+
40
+ static getNextNanos(nanoString) {
41
+ var sigNanos = parseInt(nanoString, 10); // Get the zeroes needed for padding before adding one so we handle overflow properly.
42
+
43
+ var zeros = '0'.repeat(9 - nanoString.length);
44
+ var nextNanoString = "".concat(sigNanos + 1).concat(zeros);
45
+ return parseInt(nextNanoString, 10);
46
+ }
47
+ /**
48
+ * @param components The string components that were parsed from the original string
49
+ * @param values The values that were parsed from the components
50
+ * @param timeZone The time zone to parse the date in. E.g. America/New_York
51
+ * @returns Returns the DateWrapper for the next date, or null if a full date was passed in
52
+ */
53
+
54
+
55
+ static getNextDate(components, values, timeZone) {
56
+ var {
57
+ year,
58
+ month,
59
+ date,
60
+ hours,
61
+ minutes,
62
+ seconds,
63
+ nanos
64
+ } = values;
65
+
66
+ if (components.nanos != null) {
67
+ if (components.nanos.length === 9) {
68
+ // They want an exact match
69
+ return null;
70
+ }
71
+
72
+ nanos = DateUtils.getNextNanos(components.nanos);
73
+
74
+ if (nanos > 999999999) {
75
+ // There's an overflow, add it to the seconds manually
76
+ seconds += 1;
77
+ nanos = 0;
78
+ }
79
+ } else if (components.seconds != null) {
80
+ seconds += 1;
81
+ } else if (components.minutes != null) {
82
+ minutes += 1;
83
+ } else if (components.hours != null) {
84
+ hours += 1;
85
+ } else if (components.date != null) {
86
+ date += 1;
87
+ } else if (components.month != null) {
88
+ month += 1;
89
+ } else {
90
+ year += 1;
91
+ } // Use the JS date to handle overflow rather than doing our own logic
92
+ // Because handling leap years and stuff is a pain
93
+ // Still need to add nanos after, and the overflow from that is already added to seconds above
94
+
95
+
96
+ var jsDate = new Date(year, month, date, hours, minutes, seconds);
97
+ return DateUtils.makeDateWrapper(timeZone, jsDate.getFullYear(), jsDate.getMonth(), jsDate.getDate(), jsDate.getHours(), jsDate.getMinutes(), jsDate.getSeconds(), nanos);
98
+ }
99
+ /**
100
+ * Get the JS month value for the provided string.
101
+ * Matches digits or a month name (eg. '1', '01', 'jan', 'january' should all work)
102
+ * @param monthString The string to parse to a JS month value
103
+ * @returns number The JS month value, which starts at 0 for january, or NaN if nothing could be parsed
104
+ */
105
+
106
+
107
+ static parseMonth(monthString) {
108
+ var month = parseInt(monthString, 10);
109
+
110
+ if (!Number.isNaN(month)) {
111
+ if (month >= 1 && month <= 12) {
112
+ return month - 1;
113
+ }
114
+
115
+ return NaN;
116
+ }
117
+
118
+ var cleanMonthString = monthString.trim().toLowerCase();
119
+
120
+ if (cleanMonthString.length >= 3) {
121
+ for (var i = 0; i < DateUtils.months.length; i += 1) {
122
+ if (DateUtils.months[i].startsWith(cleanMonthString)) {
123
+ return i;
124
+ }
125
+ }
126
+ }
127
+
128
+ return NaN;
129
+ }
130
+ /**
131
+ * Parse a date object out of the provided string segments.
132
+ * Also using `parseMonth` to get month names like Aug/August rather than
133
+ * simply doing `parseInt`.
134
+ * @param yearString The year part of the string
135
+ * @param monthString The month part of the string
136
+ * @param dayString The day part of the string
137
+ * @param hourString The hour part of the string
138
+ * @param minuteString The minute part of the string
139
+ * @param secondString The second part of the string
140
+ * @param nanoString The milli part of the string
141
+ */
142
+
143
+
144
+ static parseDateValues(yearString, monthString, dayString, hourString, minuteString, secondString, nanoString) {
145
+ var year = parseInt(yearString, 10);
146
+ var month = monthString != null ? this.parseMonth(monthString) : 0;
147
+ var date = dayString != null ? parseInt(dayString, 10) : 1;
148
+ var hours = hourString != null ? parseInt(hourString, 10) : 0;
149
+ var minutes = minuteString != null ? parseInt(minuteString, 10) : 0;
150
+ var seconds = secondString != null ? parseInt(secondString, 10) : 0;
151
+ var nanos = nanoString != null ? parseInt(nanoString.padEnd(9, '0'), 10) : 0;
152
+
153
+ if (Number.isNaN(year) || Number.isNaN(month) || Number.isNaN(date) || Number.isNaN(hours) || Number.isNaN(minutes) || Number.isNaN(seconds) || Number.isNaN(nanos)) {
154
+ return null;
155
+ }
156
+
157
+ return {
158
+ year,
159
+ month,
160
+ date,
161
+ hours,
162
+ minutes,
163
+ seconds,
164
+ nanos
165
+ };
166
+ }
167
+ /**
168
+ * Parse out a date time string into it's string components.
169
+ * Anything that is not captured in the string will be undefined.
170
+ *
171
+ * @param dateTimeString The date time string to parse
172
+ * @returns Containing the date time components
173
+ */
174
+
175
+
176
+ static parseDateTimeString(dateTimeString) {
177
+ var regex = /\s*(\d{4})([-./]([\da-z]+))?([-./](\d{1,2}))?([tT\s](\d{2})([:](\d{2}))?([:](\d{2}))?([.](\d{1,9}))?)?(.*)/;
178
+ var result = regex.exec(dateTimeString);
179
+
180
+ if (result == null) {
181
+ throw new Error("Unexpected date string: ".concat(dateTimeString));
182
+ }
183
+
184
+ var [, year,, month,, date,, hours,, minutes,, seconds,, nanos, overflow] = result;
185
+
186
+ if (overflow != null && overflow.length > 0) {
187
+ throw new Error("Unexpected characters after date string '".concat(dateTimeString, "': ").concat(overflow));
188
+ }
189
+
190
+ return {
191
+ year,
192
+ month,
193
+ date,
194
+ hours,
195
+ minutes,
196
+ seconds,
197
+ nanos
198
+ };
199
+ }
200
+ /**
201
+ * Parses the date range provided from a string of text.
202
+ * @param text The string to parse the date from. Can be a keyword like "today", or in the format "2018-08-04"
203
+ * @param timeZone The time zone to parse this range in. E.g. America/New_York
204
+ * @returns A tuple with the start and end value/null for that date range, or both null
205
+ */
206
+
207
+
208
+ static parseDateRange(text, timeZone) {
209
+ var cleanText = text.trim().toLowerCase();
210
+
211
+ if (cleanText.length === 0) {
212
+ throw new Error('Cannot parse date range from empty string');
213
+ }
214
+
215
+ if (cleanText === 'null') {
216
+ return [null, null];
217
+ }
218
+
219
+ if (cleanText === 'today') {
220
+ var now = new Date(Date.now());
221
+
222
+ var _startDate = DateUtils.makeDateWrapper(timeZone, now.getFullYear(), now.getMonth(), now.getDate());
223
+
224
+ var _endDate = DateUtils.makeDateWrapper(timeZone, now.getFullYear(), now.getMonth(), now.getDate() + 1);
225
+
226
+ return [_startDate, _endDate];
227
+ }
228
+
229
+ if (cleanText === 'yesterday') {
230
+ var _now = new Date(Date.now());
231
+
232
+ var _startDate2 = DateUtils.makeDateWrapper(timeZone, _now.getFullYear(), _now.getMonth(), _now.getDate() - 1);
233
+
234
+ var _endDate2 = DateUtils.makeDateWrapper(timeZone, _now.getFullYear(), _now.getMonth(), _now.getDate());
235
+
236
+ return [_startDate2, _endDate2];
237
+ }
238
+
239
+ if (cleanText === 'now') {
240
+ var _now2 = new Date(Date.now());
241
+
242
+ var date = dh.DateWrapper.ofJsDate(_now2);
243
+ return [date, null];
244
+ }
245
+
246
+ var components = DateUtils.parseDateTimeString(cleanText);
247
+
248
+ if (components.year == null && components.month == null && components.date == null) {
249
+ throw new Error("Unable to extract year, month, or day ".concat(cleanText));
250
+ }
251
+
252
+ var values = DateUtils.parseDateValues(components.year, components.month, components.date, components.hours, components.minutes, components.seconds, components.nanos);
253
+
254
+ if (values == null) {
255
+ throw new Error("Unable to extract date values from ".concat(components));
256
+ }
257
+
258
+ var startDate = DateUtils.makeDateWrapper(timeZone, values.year, values.month, values.date, values.hours, values.minutes, values.seconds, values.nanos);
259
+ var endDate = DateUtils.getNextDate(components, values, timeZone);
260
+ return [startDate, endDate];
261
+ }
262
+ /**
263
+ * Gets the Js Date object from the provided DateWrapper.
264
+ * In unit test, DateWrapper is just a number provided in millis, so handles that case.
265
+ * @param dateWrapper The DateWrapper object, or time in millis
266
+ */
267
+
268
+
269
+ static getJsDate(dateWrapper) {
270
+ if (typeof dateWrapper === 'number') {
271
+ return new Date(dateWrapper);
272
+ }
273
+
274
+ return dateWrapper.asDate();
275
+ }
276
+
277
+ }
278
+
279
+ _defineProperty(DateUtils, "FULL_DATE_FORMAT", 'yyyy-MM-dd HH:mm:ss.SSSSSSSSS');
280
+
281
+ _defineProperty(DateUtils, "months", ['january', 'february', 'march', 'april', 'may', 'june', 'july', 'august', 'september', 'october', 'november', 'december']);
282
+
283
+ export default DateUtils;
284
+ //# sourceMappingURL=DateUtils.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/DateUtils.ts"],"names":["dh","DateUtils","makeDateWrapper","timeZone","year","month","day","hour","minute","second","ns","yearString","padStart","monthString","dayString","hourString","minuteString","secondString","nanoString","dateString","i18n","DateTimeFormat","parse","FULL_DATE_FORMAT","TimeZone","getTimeZone","getNextNanos","sigNanos","parseInt","zeros","repeat","length","nextNanoString","getNextDate","components","values","date","hours","minutes","seconds","nanos","jsDate","Date","getFullYear","getMonth","getDate","getHours","getMinutes","getSeconds","parseMonth","Number","isNaN","NaN","cleanMonthString","trim","toLowerCase","i","months","startsWith","parseDateValues","padEnd","parseDateTimeString","dateTimeString","regex","result","exec","Error","overflow","parseDateRange","text","cleanText","now","startDate","endDate","DateWrapper","ofJsDate","getJsDate","dateWrapper","asDate"],"mappings":";;AAAA,OAAOA,EAAP,MAAe,uBAAf;AAaA,OAAO,MAAMC,SAAN,CAAgB;AAkBrB;AACF;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACwB,SAAfC,eAAe,CACpBC,QADoB,EAEpBC,IAFoB,EASP;A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AA1C,CAAhB;AACA,WAAO,CAACqE,SAAD,EAAYC,OAAZ,CAAP;AACD;AAED;AACF;AACA;AACA;AACA;;;AACkB,SAATG,SAAS,CAACC,WAAD,EAA0C;AACxD,QAAI,OAAOA,WAAP,KAAuB,QAA3B,EAAqC;AACnC,aAAO,IAAInC,IAAJ,CAASmC,WAAT,CAAP;AACD;;AACD,WAAOA,WAAW,CAACC,MAAZ,EAAP;AACD;;AApVoB;;gBAAV7E,S,sBACe,+B;;gBADfA,S,YAGK,CACd,SADc,EAEd,UAFc,EAGd,OAHc,EAId,OAJc,EAKd,KALc,EAMd,MANc,EAOd,MAPc,EAQd,QARc,EASd,WATc,EAUd,SAVc,EAWd,UAXc,EAYd,UAZc,C;;AAoVlB,eAAeA,SAAf","sourcesContent":["import dh from '@deephaven/jsapi-shim';\nimport type { DateWrapper } from '@deephaven/jsapi-shim';\n\ninterface DateParts<T> {\n year: T;\n month: T;\n date: T;\n hours: T;\n minutes: T;\n seconds: T;\n nanos: T;\n}\n\nexport class DateUtils {\n static FULL_DATE_FORMAT = 'yyyy-MM-dd HH:mm:ss.SSSSSSSSS';\n\n static months = [\n 'january',\n 'february',\n 'march',\n 'april',\n 'may',\n 'june',\n 'july',\n 'august',\n 'september',\n 'october',\n 'november',\n 'december',\n ];\n\n /**\n *\n * @param timeZone The time zone to parse this time in. E.g. America/New_York\n * @param year The year for the date\n * @param month The month, starting at 0\n * @param day The day, starting at 1\n * @param hour The hours\n * @param minute The minutes\n * @param second The seconds\n * @param ns The nanoseconds\n */\n static makeDateWrapper(\n timeZone: string,\n year: number,\n month = 0,\n day = 1,\n hour = 0,\n minute = 0,\n second = 0,\n ns = 0\n ): DateWrapper {\n const yearString = `${year}`.padStart(4, '0');\n const monthString = `${month + 1}`.padStart(2, '0');\n const dayString = `${day}`.padStart(2, '0');\n const hourString = `${hour}`.padStart(2, '0');\n const minuteString = `${minute}`.padStart(2, '0');\n const secondString = `${second}`.padStart(2, '0');\n const nanoString = `${ns}`.padStart(9, '0');\n\n const dateString = `${yearString}-${monthString}-${dayString} ${hourString}:${minuteString}:${secondString}.${nanoString}`;\n return dh.i18n.DateTimeFormat.parse(\n DateUtils.FULL_DATE_FORMAT,\n dateString,\n dh.i18n.TimeZone.getTimeZone(timeZone)\n );\n }\n\n /**\n * Takes the string the user entered and returns the next nanos value\n * @param nanoString The nano string to get the next one of\n * @returns The value of the next nanos\n */\n static getNextNanos(nanoString: string): number {\n const sigNanos = parseInt(nanoString, 10);\n // Get the zeroes needed for padding before adding one so we handle overflow properly.\n const zeros = '0'.repeat(9 - nanoString.length);\n const nextNanoString = `${sigNanos + 1}${zeros}`;\n return parseInt(nextNanoString, 10);\n }\n\n /**\n * @param components The string components that were parsed from the original string\n * @param values The values that were parsed from the components\n * @param timeZone The time zone to parse the date in. E.g. America/New_York\n * @returns Returns the DateWrapper for the next date, or null if a full date was passed in\n */\n static getNextDate(\n components: DateParts<string>,\n values: DateParts<number>,\n timeZone: string\n ): DateWrapper | null {\n let { year, month, date, hours, minutes, seconds, nanos } = values;\n\n if (components.nanos != null) {\n if (components.nanos.length === 9) {\n // They want an exact match\n return null;\n }\n nanos = DateUtils.getNextNanos(components.nanos);\n if (nanos > 999999999) {\n // There's an overflow, add it to the seconds manually\n seconds += 1;\n nanos = 0;\n }\n } else if (components.seconds != null) {\n seconds += 1;\n } else if (components.minutes != null) {\n minutes += 1;\n } else if (components.hours != null) {\n hours += 1;\n } else if (components.date != null) {\n date += 1;\n } else if (components.month != null) {\n month += 1;\n } else {\n year += 1;\n }\n\n // Use the JS date to handle overflow rather than doing our own logic\n // Because handling leap years and stuff is a pain\n // Still need to add nanos after, and the overflow from that is already added to seconds above\n const jsDate = new Date(year, month, date, hours, minutes, seconds);\n return DateUtils.makeDateWrapper(\n timeZone,\n jsDate.getFullYear(),\n jsDate.getMonth(),\n jsDate.getDate(),\n jsDate.getHours(),\n jsDate.getMinutes(),\n jsDate.getSeconds(),\n nanos\n );\n }\n\n /**\n * Get the JS month value for the provided string.\n * Matches digits or a month name (eg. '1', '01', 'jan', 'january' should all work)\n * @param monthString The string to parse to a JS month value\n * @returns number The JS month value, which starts at 0 for january, or NaN if nothing could be parsed\n */\n static parseMonth(monthString: string): number {\n const month = parseInt(monthString, 10);\n if (!Number.isNaN(month)) {\n if (month >= 1 && month <= 12) {\n return month - 1;\n }\n return NaN;\n }\n\n const cleanMonthString = monthString.trim().toLowerCase();\n if (cleanMonthString.length >= 3) {\n for (let i = 0; i < DateUtils.months.length; i += 1) {\n if (DateUtils.months[i].startsWith(cleanMonthString)) {\n return i;\n }\n }\n }\n\n return NaN;\n }\n\n /**\n * Parse a date object out of the provided string segments.\n * Also using `parseMonth` to get month names like Aug/August rather than\n * simply doing `parseInt`.\n * @param yearString The year part of the string\n * @param monthString The month part of the string\n * @param dayString The day part of the string\n * @param hourString The hour part of the string\n * @param minuteString The minute part of the string\n * @param secondString The second part of the string\n * @param nanoString The milli part of the string\n */\n static parseDateValues(\n yearString: string,\n monthString: string,\n dayString: string,\n hourString: string,\n minuteString: string,\n secondString: string,\n nanoString: string\n ): DateParts<number> | null {\n const year = parseInt(yearString, 10);\n const month = monthString != null ? this.parseMonth(monthString) : 0;\n const date = dayString != null ? parseInt(dayString, 10) : 1;\n const hours = hourString != null ? parseInt(hourString, 10) : 0;\n const minutes = minuteString != null ? parseInt(minuteString, 10) : 0;\n const seconds = secondString != null ? parseInt(secondString, 10) : 0;\n const nanos =\n nanoString != null ? parseInt(nanoString.padEnd(9, '0'), 10) : 0;\n if (\n Number.isNaN(year) ||\n Number.isNaN(month) ||\n Number.isNaN(date) ||\n Number.isNaN(hours) ||\n Number.isNaN(minutes) ||\n Number.isNaN(seconds) ||\n Number.isNaN(nanos)\n ) {\n return null;\n }\n\n return { year, month, date, hours, minutes, seconds, nanos };\n }\n\n /**\n * Parse out a date time string into it's string components.\n * Anything that is not captured in the string will be undefined.\n *\n * @param dateTimeString The date time string to parse\n * @returns Containing the date time components\n */\n static parseDateTimeString(dateTimeString: string): DateParts<string> {\n const regex = /\\s*(\\d{4})([-./]([\\da-z]+))?([-./](\\d{1,2}))?([tT\\s](\\d{2})([:](\\d{2}))?([:](\\d{2}))?([.](\\d{1,9}))?)?(.*)/;\n const result = regex.exec(dateTimeString);\n if (result == null) {\n throw new Error(`Unexpected date string: ${dateTimeString}`);\n }\n\n const [\n ,\n year,\n ,\n month,\n ,\n date,\n ,\n hours,\n ,\n minutes,\n ,\n seconds,\n ,\n nanos,\n overflow,\n ] = result;\n if (overflow != null && overflow.length > 0) {\n throw new Error(\n `Unexpected characters after date string '${dateTimeString}': ${overflow}`\n );\n }\n\n return { year, month, date, hours, minutes, seconds, nanos };\n }\n\n /**\n * Parses the date range provided from a string of text.\n * @param text The string to parse the date from. Can be a keyword like \"today\", or in the format \"2018-08-04\"\n * @param timeZone The time zone to parse this range in. E.g. America/New_York\n * @returns A tuple with the start and end value/null for that date range, or both null\n */\n static parseDateRange(\n text: string,\n timeZone: string\n ): [DateWrapper, DateWrapper | null] | [null, null] {\n const cleanText = text.trim().toLowerCase();\n if (cleanText.length === 0) {\n throw new Error('Cannot parse date range from empty string');\n }\n\n if (cleanText === 'null') {\n return [null, null];\n }\n\n if (cleanText === 'today') {\n const now = new Date(Date.now());\n const startDate = DateUtils.makeDateWrapper(\n timeZone,\n now.getFullYear(),\n now.getMonth(),\n now.getDate()\n );\n const endDate = DateUtils.makeDateWrapper(\n timeZone,\n now.getFullYear(),\n now.getMonth(),\n now.getDate() + 1\n );\n return [startDate, endDate];\n }\n\n if (cleanText === 'yesterday') {\n const now = new Date(Date.now());\n const startDate = DateUtils.makeDateWrapper(\n timeZone,\n now.getFullYear(),\n now.getMonth(),\n now.getDate() - 1\n );\n const endDate = DateUtils.makeDateWrapper(\n timeZone,\n now.getFullYear(),\n now.getMonth(),\n now.getDate()\n );\n return [startDate, endDate];\n }\n\n if (cleanText === 'now') {\n const now = new Date(Date.now());\n const date = dh.DateWrapper.ofJsDate(now);\n return [date, null];\n }\n\n const components = DateUtils.parseDateTimeString(cleanText);\n if (\n components.year == null &&\n components.month == null &&\n components.date == null\n ) {\n throw new Error(`Unable to extract year, month, or day ${cleanText}`);\n }\n\n const values = DateUtils.parseDateValues(\n components.year,\n components.month,\n components.date,\n components.hours,\n components.minutes,\n components.seconds,\n components.nanos\n );\n\n if (values == null) {\n throw new Error(`Unable to extract date values from ${components}`);\n }\n\n const startDate = DateUtils.makeDateWrapper(\n timeZone,\n values.year,\n values.month,\n values.date,\n values.hours,\n values.minutes,\n values.seconds,\n values.nanos\n );\n\n const endDate = DateUtils.getNextDate(components, values, timeZone);\n return [startDate, endDate];\n }\n\n /**\n * Gets the Js Date object from the provided DateWrapper.\n * In unit test, DateWrapper is just a number provided in millis, so handles that case.\n * @param dateWrapper The DateWrapper object, or time in millis\n */\n static getJsDate(dateWrapper: DateWrapper | number): Date {\n if (typeof dateWrapper === 'number') {\n return new Date(dateWrapper);\n }\n return dateWrapper.asDate();\n }\n}\n\nexport default DateUtils;\n"],"file":"DateUtils.js"}