@knime/utils 1.9.1 → 1.10.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.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,17 @@
1
1
  # @knime/utils
2
2
 
3
+ ## 1.10.0
4
+
5
+ ### Minor Changes
6
+
7
+ - ea3b7e1: Add util to format file-sizes
8
+
9
+ ## 1.9.2
10
+
11
+ ### Patch Changes
12
+
13
+ - 6d57731: Add option to allow hyperlinks to the sanitization util.
14
+
3
15
  ## 1.9.1
4
16
 
5
17
  ### Patch Changes
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@knime/utils",
3
- "version": "1.9.1",
3
+ "version": "1.10.0",
4
4
  "description": "Internal utility functions and constants shared across @knime packages.",
5
5
  "repository": {
6
6
  "type": "git",
@@ -14,7 +14,8 @@
14
14
  "dependencies": {
15
15
  "date-fns": "2.30.0",
16
16
  "date-fns-tz": "2.0.0",
17
- "dompurify": "^3.2.7"
17
+ "dompurify": "^3.2.7",
18
+ "filesize": "10.0.6"
18
19
  },
19
20
  "peerDependencies": {
20
21
  "consola": "3.x"
package/src/format.ts CHANGED
@@ -1,3 +1,5 @@
1
+ import { filesize } from "filesize";
2
+
1
3
  import getLocalTimeZone from "./localTimezone";
2
4
  /**
3
5
  * Converts the specified date String to human readable text containing the day, month, and year.
@@ -95,3 +97,17 @@ export const formatDateTimeString = (
95
97
  */
96
98
  export const formatLocalDateTimeString = (date: string, showTime: boolean) =>
97
99
  showTime ? formatDateTimeString(date, true) : formatDateString(date, true);
100
+
101
+ export const formatFileSize = (bytes: number, decimals = 1): string => {
102
+ const safeBytes = Number.isFinite(bytes) && bytes >= 0 ? bytes : 0;
103
+ const safeDecimals =
104
+ Number.isFinite(decimals) && decimals >= 0 ? Math.floor(decimals) : 0;
105
+
106
+ return filesize(safeBytes, {
107
+ output: "string",
108
+ base: 2,
109
+ standard: "iec",
110
+ round: safeDecimals,
111
+ pad: true,
112
+ }) as string;
113
+ };
package/src/index.ts CHANGED
@@ -5,6 +5,7 @@ import { filters } from "./filters";
5
5
  import {
6
6
  formatDateString,
7
7
  formatDateTimeString,
8
+ formatFileSize,
8
9
  formatLocalDateTimeString,
9
10
  formatTimeString,
10
11
  } from "./format";
@@ -55,6 +56,7 @@ export {
55
56
  filters,
56
57
  formatDateString,
57
58
  formatDateTimeString,
59
+ formatFileSize,
58
60
  getDeepActiveElement,
59
61
  formatLocalDateTimeString,
60
62
  formatTimeString,
@@ -35,16 +35,28 @@ const ALLOWED_TAGS = [
35
35
  /**
36
36
  * Sanitizes Raw HTML and only allow specific tags to be used, as per OWASP standards.
37
37
  * @param rawHTML
38
+ * @param options.allowStyleAttribute - Allow the style attribute on elements
39
+ * @param options.allowHyperlinks - Allow anchor tags with href attribute
38
40
  * @returns sanitized html
39
41
  */
40
42
  const sanitizeHTML = (
41
43
  rawHTML: string,
42
- options: { allowStyleAttribute: boolean } = { allowStyleAttribute: false },
44
+ options: { allowStyleAttribute?: boolean; allowHyperlinks?: boolean } = {
45
+ allowStyleAttribute: false,
46
+ allowHyperlinks: false,
47
+ },
43
48
  ) => {
44
- const { allowStyleAttribute } = options;
45
- const ALLOWED_ATTR = allowStyleAttribute ? ["style"] : [];
49
+ const { allowStyleAttribute, allowHyperlinks } = options;
50
+ const ALLOWED_ATTR = [
51
+ ...(allowStyleAttribute ? ["style"] : []),
52
+ ...(allowHyperlinks ? ["href"] : []),
53
+ ];
54
+ const allowedTags = allowHyperlinks ? [...ALLOWED_TAGS, "a"] : ALLOWED_TAGS;
46
55
 
47
- return DomPurify.sanitize(rawHTML, { ALLOWED_TAGS, ALLOWED_ATTR });
56
+ return DomPurify.sanitize(rawHTML, {
57
+ ALLOWED_TAGS: allowedTags,
58
+ ALLOWED_ATTR,
59
+ });
48
60
  };
49
61
 
50
62
  /**