@knime/utils 1.0.0 → 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md ADDED
@@ -0,0 +1,8 @@
1
+ # @knime/utils
2
+
3
+ ## 1.0.1
4
+
5
+ ### Patch Changes
6
+
7
+ - 05c9114: refactor utils to TS
8
+ - 0c594bd: improve exports of utils package (related to hotkeys)
package/README.md CHANGED
@@ -15,7 +15,7 @@ npm install @knime/utils
15
15
  To use it in your project, you can import it as follows:
16
16
 
17
17
  ```javascript
18
- import { copyText } from "@knime/utils";
18
+ import { capitalize } from "@knime/utils";
19
19
  ```
20
20
 
21
21
  # Join the Community!
package/package.json CHANGED
@@ -1,19 +1,16 @@
1
1
  {
2
2
  "name": "@knime/utils",
3
- "version": "1.0.0",
3
+ "version": "1.0.1",
4
4
  "description": "Internal utility functions and constants shared across @knime packages.",
5
5
  "homepage": "https://knime.github.io/webapps-common/",
6
6
  "license": "GPL 3 and Additional Permissions according to Sec. 7 (SEE the file LICENSE)",
7
7
  "author": "KNIME AG, Zurich, Switzerland",
8
8
  "type": "module",
9
- "scripts": {
10
- "test:unit": "TZ='Europe/Berlin' vitest"
11
- },
12
9
  "dependencies": {
13
- "@knime/styles": "1.0.0",
14
10
  "consola": "2.15.3",
15
11
  "date-fns": "2.30.0",
16
- "date-fns-tz": "2.0.0"
12
+ "date-fns-tz": "2.0.0",
13
+ "@knime/styles": "1.0.0"
17
14
  },
18
15
  "main": "src/index.ts",
19
16
  "module": "src/index.ts",
@@ -36,5 +33,8 @@
36
33
  },
37
34
  "devDependencies": {
38
35
  "flush-promises": "^1.0.2"
36
+ },
37
+ "scripts": {
38
+ "test:unit": "TZ='Europe/Berlin' vitest"
39
39
  }
40
- }
40
+ }
@@ -0,0 +1,45 @@
1
+ /**
2
+ * Uppercases first letter of string
3
+ * @param string to be capitalized first string, e.g. "workflow"
4
+ * @return Capitalized String, e.g. "Workflow"
5
+ */
6
+ export const capitalize = (input: string) =>
7
+ input.charAt(0).toUpperCase() + input.substring(1);
8
+
9
+ const formats = {
10
+ customFormat: (input: string, delimiter: string) =>
11
+ capitalize(input.replace(delimiter, " ").toLowerCase()),
12
+
13
+ kebabFormat: (input: string) => formats.customFormat(input, "-"),
14
+ snakeFormat: (input: string) => formats.customFormat(input, "_"),
15
+
16
+ camelPascalCase: (input: string) =>
17
+ capitalize(
18
+ (input.match(/([A-Z]?[^A-Z]*)/g) ?? [])
19
+ .slice(0, -1)
20
+ .join(" ")
21
+ .toLowerCase(),
22
+ ),
23
+ } as const;
24
+
25
+ type CaseFormatterOptions = {
26
+ string: string;
27
+ delimiter?: string;
28
+ format?: keyof typeof formats;
29
+ };
30
+
31
+ export const caseFormatter = ({
32
+ string,
33
+ delimiter,
34
+ format,
35
+ }: CaseFormatterOptions) => {
36
+ if (delimiter) {
37
+ return formats.customFormat(string, delimiter);
38
+ }
39
+
40
+ if (format) {
41
+ return formats[format](string, delimiter ?? "");
42
+ }
43
+
44
+ return capitalize(string);
45
+ };
@@ -4,13 +4,18 @@ import updateDate from "./updateDate";
4
4
 
5
5
  /**
6
6
  * Check if date is before min.
7
- * @param {Date} date
8
- * @param {Date | null} min
9
- * @param {Boolean} checkDate - check date part (year, day, month).
10
- * @param {Boolean} checkTime - check time part (hour, minutes etc.).
11
- * @returns {Boolean} whether limit is kept or not.
7
+ * @param date
8
+ * @param min
9
+ * @param checkDate - check date part (year, day, month).
10
+ * @param checkTime - check time part (hour, minutes etc.).
11
+ * @returns whether limit is kept or not.
12
12
  */
13
- export const isBeforeMinDate = (date, min, checkDate, checkTime) => {
13
+ export const isBeforeMinDate = (
14
+ date: Date,
15
+ min: Date | null,
16
+ checkDate: boolean,
17
+ checkTime: boolean,
18
+ ) => {
14
19
  if (min) {
15
20
  if (checkTime && checkDate) {
16
21
  return isBefore(date, min);
@@ -31,13 +36,18 @@ export const isBeforeMinDate = (date, min, checkDate, checkTime) => {
31
36
 
32
37
  /**
33
38
  * Check
34
- * @param {Date} date - date to check.
35
- * @param {Date | null} max - maximum date.
36
- * @param {Boolean} checkDate - check date part (year, day, month).
37
- * @param {Boolean} checkTime - check time part (hour, minutes etc.).
38
- * @returns {Boolean} whether limit is kept or not.
39
+ * @param date - date to check.
40
+ * @param max - maximum date.
41
+ * @param checkDate - check date part (year, day, month).
42
+ * @param checkTime - check time part (hour, minutes etc.).
43
+ * @returns whether limit is kept or not.
39
44
  */
40
- export const isAfterMaxDate = (date, max, checkDate, checkTime) => {
45
+ export const isAfterMaxDate = (
46
+ date: Date,
47
+ max: Date | null,
48
+ checkDate: boolean,
49
+ checkTime: boolean,
50
+ ) => {
41
51
  if (max) {
42
52
  if (checkTime && checkDate) {
43
53
  return isAfter(date, max);
package/src/format.ts ADDED
@@ -0,0 +1,98 @@
1
+ import getLocalTimeZone from "./localTimezone";
2
+ /**
3
+ * Converts the specified date String to human readable text containing the day, month, and year.
4
+ * Example: '2018-07-31T09:44:31+00:00' => 'Jul 31, 2018'
5
+ *
6
+ * @param dateString String representation of the Date to format
7
+ * @param useTimeZone to set the timeZone where the user is located, and display local date and time
8
+ * @returns human readable date String
9
+ */
10
+ export const formatDateString = (
11
+ dateString: string | number,
12
+ useTimeZone = false,
13
+ ) => {
14
+ const date = new Date(dateString);
15
+
16
+ // @ts-ignore
17
+ if (isNaN(date)) {
18
+ throw Error("Invalid Date format");
19
+ }
20
+
21
+ const baseDateOptions: Intl.DateTimeFormatOptions = {
22
+ day: "numeric",
23
+ month: "short",
24
+ year: "numeric",
25
+ };
26
+
27
+ // This only works for the default locale of en-US, as support of other locals in node js might not be available
28
+ // See for example this issue https://github.com/nodejs/node/issues/8500
29
+ const dateOptions: Intl.DateTimeFormatOptions = useTimeZone
30
+ ? { ...baseDateOptions, timeZone: getLocalTimeZone() }
31
+ : baseDateOptions;
32
+
33
+ return date.toLocaleDateString("en-US", dateOptions);
34
+ };
35
+
36
+ /**
37
+ * Converts the specified time String to human readable text containing the hour (12-hour) and minute.
38
+ * Example: '2018-07-31T09:44:31+00:00' => '9:44 AM'
39
+ *
40
+ * @param timeString String representation of the Date (including time) to format
41
+ * @param useTimeZone to set the timeZone where the user is located, and display local date and time
42
+ * @returns human readable time String
43
+ */
44
+ export const formatTimeString = (
45
+ timeString: string | number,
46
+ useTimeZone = false,
47
+ ) => {
48
+ const time = new Date(timeString);
49
+
50
+ // @ts-ignore
51
+ if (isNaN(time)) {
52
+ throw Error("Invalid Date format");
53
+ }
54
+
55
+ // This only works for the default locale of en-US, as support of other locals in node js might not be available
56
+ // See for example this issue https://github.com/nodejs/node/issues/8500
57
+ const baseTimeOptions: Intl.DateTimeFormatOptions = {
58
+ hour: "numeric",
59
+ minute: "2-digit",
60
+ };
61
+
62
+ const timeOptions: Intl.DateTimeFormatOptions = useTimeZone
63
+ ? { ...baseTimeOptions, timeZone: getLocalTimeZone() }
64
+ : baseTimeOptions;
65
+
66
+ return time.toLocaleTimeString("en-US", timeOptions);
67
+ };
68
+
69
+ /**
70
+ * Converts the specified time String to human readable text containing the date (day, month, year) and time (hour,
71
+ * minute; 12-hour).
72
+ * Example: '2018-07-31T09:44:31+00:00' => 'Jul 31, 2018 9:44 AM'
73
+ *
74
+ * @param dateTimeString String representation of the Date (including time and date) to format
75
+ * @param useTimeZone to set the timeZone where the user is located, and display local date and time
76
+ * @returns human readable date/time String
77
+ */
78
+ export const formatDateTimeString = (
79
+ dateTimeString: string | number,
80
+ useTimeZone = false,
81
+ ) => {
82
+ // eslint-disable-line arrow-body-style
83
+ return `${formatDateString(dateTimeString, useTimeZone)} ${formatTimeString(
84
+ dateTimeString,
85
+ useTimeZone,
86
+ )}`;
87
+ };
88
+
89
+ /**
90
+ * Converts the specified date and time to the user time zone with option to display (day, month, year) only or with time (hour,
91
+ * minute; 12-hour).
92
+ *
93
+ * @param showTime String representation of the Date to convert and format
94
+ * @param String | number} date String representation of the Date to convert and format
95
+ * @returns human readable date/time String converted to user time zone
96
+ */
97
+ export const formatLocalDateTimeString = (date: string, showTime: boolean) =>
98
+ showTime ? formatDateTimeString(date, true) : formatDateString(date, true);
package/src/index.ts CHANGED
@@ -1,12 +1,14 @@
1
1
  import { capitalize, caseFormatter } from "./capitalize";
2
- import { copyText } from "./copyText";
3
2
  import debounce from "./debounce";
4
3
  import { filters } from "./filters";
5
4
  import truncateString from "./truncateString";
6
5
  import getWrappedAroundIndex from "./getWrappedAroundIndex";
7
6
  import sleep from "./sleep";
8
7
  import { icons, isIconExisting } from "./fileTypeIcons";
9
- import svgWithTitle from "./svgWithTitle";
8
+
9
+ // eslint-disable-next-line import/extensions
10
+ import svgWithTitle from "./svgWithTitle.js";
11
+
10
12
  import updateDate from "./updateDate";
11
13
  import { isAfterMaxDate, isBeforeMinDate } from "./dateMinMaxCheck";
12
14
  import {
@@ -18,16 +20,30 @@ import {
18
20
 
19
21
  import getLocalTimeZone from "./localTimezone";
20
22
  import numIntegerDigits from "./numIntegerDigits";
21
- import { formatHotkeys } from "./formatHotkeys";
22
- import type { Hotkey, KnownHotkey } from "./formatHotkeys";
23
+
24
+ import {
25
+ formatHotkeys,
26
+ formatHotkey,
27
+ getDefaultSeparator,
28
+ } from "./formatHotkeys";
29
+
30
+ import type {
31
+ Hotkey as _Hotkey,
32
+ KnownHotkey as _KnownHotkey,
33
+ } from "./formatHotkeys";
34
+
23
35
  import * as navigator from "./navigator";
24
36
 
25
- export type { Hotkey, KnownHotkey };
37
+ const hotkeys = { formatHotkeys, formatHotkey, getDefaultSeparator };
38
+
39
+ export namespace HotkeysNS {
40
+ export type Hotkey = _Hotkey;
41
+ export type KnownHotkey = _KnownHotkey;
42
+ }
26
43
 
27
44
  export {
28
45
  capitalize,
29
46
  caseFormatter,
30
- copyText,
31
47
  debounce,
32
48
  filters,
33
49
  truncateString,
@@ -45,6 +61,6 @@ export {
45
61
  getLocalTimeZone,
46
62
  isBeforeMinDate,
47
63
  numIntegerDigits,
48
- formatHotkeys,
64
+ hotkeys,
49
65
  navigator,
50
66
  };
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * Get the users local timezone.
3
3
  *
4
- * @returns {String} current users timezone (browser based) tz db string (e. g. Europe/Berlin).
4
+ * @returns current users timezone (browser based) tz db string (e. g. Europe/Berlin).
5
5
  */
6
6
  // eslint-disable-next-line new-cap
7
7
  export default () => Intl.DateTimeFormat().resolvedOptions().timeZone;
@@ -0,0 +1,8 @@
1
+ /**
2
+ * Count the number of integer digits for a given number.
3
+ *
4
+ * @param input number.
5
+ * @returns number of integer digits x has. 1234 = 4 but 1234.44 is also 4.
6
+ */
7
+ export default (input: number) =>
8
+ Math.max(Math.floor(Math.log10(Math.abs(input))), 0) + 1;
@@ -3,11 +3,11 @@ import { isValid, getYear, getMonth, getDate, set } from "date-fns";
3
3
  * Updates the date part (year, day and month) of a date object (base)
4
4
  * time, offset (timezone) and so on will be left untouched.
5
5
  *
6
- * @param {Date} base - Date which will be used (copied) to set day, month and year.
7
- * @param {Date | null} date - Extract day, month and year from this date object.
8
- * @returns {Date} Copy of base with day, month and year of date.
6
+ * @param base - Date which will be used (copied) to set day, month and year.
7
+ * @param date - Extract day, month and year from this date object.
8
+ * @returns Copy of base with day, month and year of date.
9
9
  */
10
- export default (base, date) => {
10
+ export default (base: Date, date: Date | null) => {
11
11
  // ignore falsy dates
12
12
  if (date) {
13
13
  // allow invalid base and set time to 0
@@ -18,5 +18,6 @@ export default (base, date) => {
18
18
  date: getDate(date),
19
19
  });
20
20
  }
21
+
21
22
  return base;
22
23
  };
@@ -2,12 +2,12 @@
2
2
  * Updates the time part (hour, minute, second and millisecond) of a date object (base)
3
3
  * date, offset (timezone) and so on will be left untouched.
4
4
  *
5
- * @param {Date} base - Date which will be used (copied) to set day, month and year.
6
- * @param {Date | null} date - Extract hour, minute, second and millisecond from this date object.
7
- * @returns {Date} Copy of base with hour, minute, second and millisecond changed.
5
+ * @param base - Date which will be used (copied) to set day, month and year.
6
+ * @param date - Extract hour, minute, second and millisecond from this date object.
7
+ * @returns Copy of base with hour, minute, second and millisecond changed.
8
8
  */
9
- export default (base, date) => {
10
- let d = new Date(base);
9
+ export default (base: Date, date: Date | null) => {
10
+ const d = new Date(base);
11
11
  // ignore falsy dates
12
12
  if (date) {
13
13
  d.setHours(date.getHours());
package/src/capitalize.js DELETED
@@ -1,33 +0,0 @@
1
- /**
2
- * Uppercases first letter of string
3
- * @param {String} str to be capitalized first string, e.g. 'workflow'
4
- * @return {String} Capitalized String, e.g. Workflow
5
- */
6
- export const capitalize = (str) =>
7
- str.charAt(0).toUpperCase() + str.substring(1);
8
-
9
- const formats = {
10
- customFormat: (str, delimiter) =>
11
- capitalize(str.replace(delimiter, " ").toLowerCase()),
12
- kebabFormat: (str) => formats.customFormat(str, "-"),
13
- snakeFormat: (str) => formats.customFormat(str, "_"),
14
- camelPascalCase: (str) =>
15
- capitalize(
16
- str
17
- .match(/([A-Z]?[^A-Z]*)/g)
18
- .slice(0, -1)
19
- .join(" ")
20
- .toLowerCase(),
21
- ),
22
- [undefined]: (str) => capitalize(str), // eslint-disable-line no-undefined
23
- };
24
-
25
- export const caseFormatter = ({ string, delimiter, format }) => {
26
- if (delimiter) {
27
- return formats.customFormat(string, delimiter);
28
- }
29
- if (formats) {
30
- return formats[format](string);
31
- }
32
- return capitalize(string);
33
- };
package/src/copyText.js DELETED
@@ -1,10 +0,0 @@
1
- export const copyText = (text) => {
2
- let tmpElement = document.createElement("textarea");
3
- tmpElement.style = "position:fixed;pointer-events:none;opacity:0;";
4
- tmpElement.setAttribute("readonly", "");
5
- tmpElement.value = text;
6
- document.body.appendChild(tmpElement);
7
- tmpElement.select();
8
- document.execCommand("copy");
9
- document.body.removeChild(tmpElement);
10
- };
package/src/format.js DELETED
@@ -1,70 +0,0 @@
1
- import getLocalTimeZone from "./localTimezone";
2
- /**
3
- * Converts the specified date String to human readable text containing the day, month, and year.
4
- * Example: '2018-07-31T09:44:31+00:00' => 'Jul 31, 2018'
5
- *
6
- * @param {Boolean} useTimeZone to set the timeZone where the user is located, and display local date and time
7
- * @param {String | number} dateString String representation of the Date to format
8
- * @returns {String} human readable date String
9
- */
10
- export const formatDateString = (dateString, useTimeZone = false) => {
11
- let date = new Date(dateString);
12
- if (isNaN(date)) {
13
- throw Error("Invalid Date format");
14
- }
15
- // This only works for the default locale of en-US, as support of other locals in node js might not be available
16
- // See for example this issue https://github.com/nodejs/node/issues/8500
17
- let dateOptions = { day: "numeric", month: "short", year: "numeric" };
18
- if (useTimeZone) {
19
- dateOptions.timeZone = getLocalTimeZone();
20
- }
21
- return date.toLocaleDateString("en-US", dateOptions);
22
- };
23
- /**
24
- * Converts the specified time String to human readable text containing the hour (12-hour) and minute.
25
- * Example: '2018-07-31T09:44:31+00:00' => '9:44 AM'
26
- *
27
- * @param {Boolean} useTimeZone to set the timeZone where the user is located, and display local date and time
28
- * @param {String | number} timeString String representation of the Date (including time) to format
29
- * @returns {String} human readable time String
30
- */
31
- export const formatTimeString = (timeString, useTimeZone = false) => {
32
- let time = new Date(timeString);
33
- if (isNaN(time)) {
34
- throw Error("Invalid Date format");
35
- }
36
- // This only works for the default locale of en-US, as support of other locals in node js might not be available
37
- // See for example this issue https://github.com/nodejs/node/issues/8500
38
-
39
- let timeOptions = { hour: "numeric", minute: "2-digit" };
40
- if (useTimeZone) {
41
- timeOptions.timeZone = getLocalTimeZone();
42
- }
43
-
44
- return time.toLocaleTimeString("en-US", timeOptions);
45
- };
46
- /**
47
- * Converts the specified time String to human readable text containing the date (day, month, year) and time (hour,
48
- * minute; 12-hour).
49
- * Example: '2018-07-31T09:44:31+00:00' => 'Jul 31, 2018 9:44 AM'
50
- *
51
- * @param {Boolean} useTimeZone to set the timeZone where the user is located, and display local date and time
52
- * @param {String | number} dateTimeString String representation of the Date (including time and date) to format
53
- * @returns {String} human readable date/time String
54
- */
55
- export const formatDateTimeString = (dateTimeString, useTimeZone = false) => {
56
- // eslint-disable-line arrow-body-style
57
- return `${formatDateString(dateTimeString, useTimeZone)} ${formatTimeString(
58
- dateTimeString,
59
- useTimeZone,
60
- )}`;
61
- };
62
- /**
63
- * Converts the specified date and time to the user time zone with option to display (day, month, year) only or with time (hour,
64
- * minute; 12-hour).
65
- * @param {Boolean} showTime String representation of the Date to convert and format
66
- * @param {String | number} date String representation of the Date to convert and format
67
- * @returns {String} human readable date/time String converted to user time zone
68
- */
69
- export const formatLocalDateTimeString = (date, showTime) =>
70
- showTime ? formatDateTimeString(date, true) : formatDateString(date, true);
@@ -1,7 +0,0 @@
1
- /**
2
- * Count the number of integer digits for a given number.
3
- *
4
- * @param {Number} x - input number.
5
- * @returns {Number} - number of integer digits x has. 1234 = 4 but 1234.44 is also 4.
6
- */
7
- export default (x) => Math.max(Math.floor(Math.log10(Math.abs(x))), 0) + 1;
File without changes