@castlabs/ui 7.15.1 → 7.17.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.
@@ -57,6 +57,7 @@ declare module '@castlabs/ui/dist/castlabs-ui.module.js' {
57
57
  sort: (col?: number, order?: string) => 'ASC' | 'DESC' | 'NONE',
58
58
  }
59
59
  export function clDebounce (callback: any, ms?: number, key?: string): void
60
+ export function clSanitizeId (id: string): string
60
61
 
61
62
  export function clRandomString (length?: number, words?: string[]): string
62
63
  export function clRandomList (length?: number, words?: string[]): string[]
@@ -1,4 +1,4 @@
1
- /* @castlabs/ui v7.15.1 */
1
+ /* @castlabs/ui v7.17.0 */
2
2
 
3
3
  /*!
4
4
  * Bootstrap v5.3.8 (https://getbootstrap.com/)
@@ -1151,33 +1151,44 @@ export function clFormatFilesize (bytes) {
1151
1151
  return `${Math.floor(bytes / 1024 / 1024 / 1024)}GB`
1152
1152
  }
1153
1153
 
1154
+ /**
1155
+ * Format an UTC date into our UI version.
1156
+ *
1157
+ * @param {Date} date Date to format.
1158
+ * @return {string} Formatted date, e.g. '01 Aug 2021'.
1159
+ */
1160
+ export function clFormatDateUTC (date) {
1161
+ // we use toISOString() to discard timezone information in date object
1162
+ const [yyyy, mm, dd] = date.toISOString().split(/[-T:.Z]/)
1163
+ const mmm = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'][
1164
+ mm - 1
1165
+ ]
1166
+ return `${dd} ${mmm} ${yyyy}`
1167
+ }
1168
+
1154
1169
  /**
1155
1170
  * Format a date into our UI version.
1156
1171
  *
1172
+ * @deprecated use clFormatDateUTC instead to avoid unexpected auto-timezone-shifts.
1173
+ *
1157
1174
  * @param {Date} date Date to format.
1158
1175
  * @return {string} Formatted date, e.g. '01 Aug 2021'.
1159
1176
  */
1160
1177
  export function clFormatDate (date) {
1161
- const yyyy = new Intl.DateTimeFormat('en', { year: 'numeric' }).format(date)
1162
- const mmm = new Intl.DateTimeFormat('en', { month: 'short' }).format(date)
1163
- const dd = new Intl.DateTimeFormat('en', { day: '2-digit' }).format(date)
1164
- return `${dd} ${mmm} ${yyyy}`
1178
+ return clFormatDateUTC(date)
1165
1179
  }
1166
1180
 
1167
1181
  /**
1168
1182
  * Format a time into our UI version.
1169
1183
  *
1170
- * @param {Date} date Date to format. Only hours/minutes will be used.
1184
+ * @param {Date} date Date to format, assumed to be UTC.
1171
1185
  * @param {boolean} seconds If true, seconds will be appended.
1172
1186
  * @return {string} Formatted date, e.g. '18:21' or '18:21:59' (incl. seconds).
1173
1187
  */
1174
1188
  export function clFormatTimeUTC (date, seconds = false) {
1175
- const [hh, mm, ss] = [
1176
- ('' + date.getUTCHours()).padStart(2, '0'),
1177
- ('' + date.getUTCMinutes()).padStart(2, '0'),
1178
- ('' + date.getUTCSeconds()).padStart(2, '0')
1179
- ]
1180
- return seconds ? `${hh}:${mm}:${ss}` : `${hh}:${mm}`
1189
+ // we use toISOString() to discard timezone information in date object
1190
+ const utc = date.toISOString().split(/[-T:.Z]/)
1191
+ return seconds ? `${utc[3]}:${utc[4]}:${utc[5]}` : `${utc[3]}:${utc[4]}`
1181
1192
  }
1182
1193
 
1183
1194
  /**
@@ -1188,7 +1199,7 @@ export function clFormatTimeUTC (date, seconds = false) {
1188
1199
  * @return {string} Formatted date, e.g. '01 Aug 2021 18:21' or '01 Aug 2021 18:21:59' (incl. seconds).
1189
1200
  */
1190
1201
  export function clFormatDateTimeUTC (date, seconds = false) {
1191
- return `${clFormatDate(date)} ${clFormatTimeUTC(date, seconds)}`
1202
+ return `${clFormatDateUTC(date)} ${clFormatTimeUTC(date, seconds)}`
1192
1203
  }
1193
1204
 
1194
1205
  /**
@@ -1511,6 +1522,13 @@ export function clTableSorterObjects (
1511
1522
  // --- misc --------------------------------------------------------------------
1512
1523
  // -----------------------------------------------------------------------------
1513
1524
 
1525
+ /**
1526
+ * Remove characters from user-provided IDs that can’t appear in our UUIDs or (CS) urns.
1527
+ */
1528
+ export function clSanitizeId (id) {
1529
+ return id.replace(/[^A-Za-z0-9:_-]*/g, '')
1530
+ }
1531
+
1514
1532
  /**
1515
1533
  * Debounce a method to be called not faster than x ms.
1516
1534
  *