@knime/utils 1.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.
@@ -0,0 +1,22 @@
1
+ import { isValid, getYear, getMonth, getDate, set } from "date-fns";
2
+ /**
3
+ * Updates the date part (year, day and month) of a date object (base)
4
+ * time, offset (timezone) and so on will be left untouched.
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.
9
+ */
10
+ export default (base, date) => {
11
+ // ignore falsy dates
12
+ if (date) {
13
+ // allow invalid base and set time to 0
14
+ base = isValid(base) ? base : new Date(Date.UTC(0, 0));
15
+ return set(base, {
16
+ year: getYear(date),
17
+ month: getMonth(date),
18
+ date: getDate(date),
19
+ });
20
+ }
21
+ return base;
22
+ };
@@ -0,0 +1,19 @@
1
+ /**
2
+ * Updates the time part (hour, minute, second and millisecond) of a date object (base)
3
+ * date, offset (timezone) and so on will be left untouched.
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.
8
+ */
9
+ export default (base, date) => {
10
+ let d = new Date(base);
11
+ // ignore falsy dates
12
+ if (date) {
13
+ d.setHours(date.getHours());
14
+ d.setMinutes(date.getMinutes());
15
+ d.setSeconds(date.getSeconds());
16
+ d.setMilliseconds(date.getMilliseconds());
17
+ }
18
+ return d;
19
+ };
@@ -0,0 +1,3 @@
1
+ import muteConsole from "./muteConsole";
2
+
3
+ export { muteConsole };
@@ -0,0 +1,46 @@
1
+ /* eslint-disable no-console */
2
+
3
+ const methods = ["log", "error", "warn"];
4
+
5
+ /**
6
+ * Helper function to run code suppressing all console output
7
+ * @param {Function} f The function to run. Can be async.
8
+ * @returns {any} The original function's return value
9
+ */
10
+ export default (f) => {
11
+ let originals = methods.map((method) => console[method]);
12
+ methods.forEach((method) => {
13
+ console[method] = () => {};
14
+ });
15
+
16
+ let originalConsola;
17
+ if (global.consola) {
18
+ originalConsola = global.consola;
19
+ global.consola = consola.create({
20
+ level: -1,
21
+ });
22
+ }
23
+
24
+ const restoreOriginals = () => {
25
+ methods.forEach((method, i) => {
26
+ console[method] = originals[i];
27
+ });
28
+ if (originalConsola) {
29
+ global.consola = originalConsola;
30
+ }
31
+ };
32
+
33
+ if (f.constructor.name === "AsyncFunction") {
34
+ // async
35
+ return f().finally(restoreOriginals);
36
+ } else {
37
+ // sync
38
+ try {
39
+ return f();
40
+ } catch (e) {
41
+ throw e;
42
+ } finally {
43
+ restoreOriginals();
44
+ }
45
+ }
46
+ };