@heliosgraphics/utils 6.0.0-alpha.16 → 6.0.0-alpha.18

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/colors.ts CHANGED
@@ -1,5 +1,7 @@
1
1
  type TypeRGB = [number, number, number]
2
2
 
3
+ const HEX_COLOR_REGEX: RegExp = /^#(?:[0-9a-fA-F]{3}){1,2}$/
4
+
3
5
  const toHex = (c: unknown): string => {
4
6
  if (c === null || c === undefined) return "ff"
5
7
 
@@ -35,6 +37,10 @@ export const hexToRgb = (hex?: string | null): TypeRGB | null => {
35
37
  return [r, g, b]
36
38
  }
37
39
 
40
+ export const isHexColor = (value?: string | null): boolean => {
41
+ return Boolean(value && HEX_COLOR_REGEX.test(value))
42
+ }
43
+
38
44
  export const rgbToHex = (r: number | string = 255, g: number | string = 255, b: number | string = 255): string => {
39
45
  return `#${toHex(r)}${toHex(g)}${toHex(b)}`
40
46
  }
package/index.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  export { getClasses } from "./classnames"
2
2
  export { copyValue } from "./clipboard"
3
- export { hexToRgb, rgbToHex } from "./colors"
3
+ export { hexToRgb, isHexColor, rgbToHex } from "./colors"
4
4
  export { debounce } from "./debounce"
5
5
  export {
6
6
  getIsEqual,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@heliosgraphics/utils",
3
- "version": "6.0.0-alpha.16",
3
+ "version": "6.0.0-alpha.18",
4
4
  "type": "module",
5
5
  "sideEffects": false,
6
6
  "author": "Chris Puska <chris@puska.org>",
package/sanitize.ts CHANGED
@@ -25,6 +25,8 @@ const ALLOWED_TAGS = new Set([
25
25
  const ALLOWED_ATTRIBUTES = new Set(["class", "id", "title"])
26
26
 
27
27
  const SAFE_PROTOCOLS = /^(https?|mailto|tel|ftp):/i
28
+ const EMOJI_CLUSTER_LEFT_PATTERN: RegExp = /\p{Extended_Pictographic}(?:\uFE0F|[\u{1F3FB}-\u{1F3FF}])*$/u
29
+ const EMOJI_CLUSTER_RIGHT_PATTERN: RegExp = /^\p{Extended_Pictographic}/u
28
30
 
29
31
  interface SanitizeTextOptions {
30
32
  allowLinks?: boolean
@@ -85,6 +87,15 @@ const DANGEROUS_FUNCTIONS: Array<RegExp> = [
85
87
  /createElement\s*\(\s*["']script["']\s*\)/gi,
86
88
  ]
87
89
 
90
+ const stripUnsafeZeroWidthCharacter = (character: string, index: number, text: string): string => {
91
+ if (character !== "\u200D") return ""
92
+
93
+ const leftText: string = text.slice(0, index)
94
+ const rightText: string = text.slice(index + character.length)
95
+
96
+ return EMOJI_CLUSTER_LEFT_PATTERN.test(leftText) && EMOJI_CLUSTER_RIGHT_PATTERN.test(rightText) ? character : ""
97
+ }
98
+
88
99
  export const sanitizeText = (input: string = "", options: SanitizeTextOptions = {}): string => {
89
100
  if (!input) return ""
90
101
 
@@ -140,7 +151,7 @@ export const sanitizeText = (input: string = "", options: SanitizeTextOptions =
140
151
  }
141
152
 
142
153
  sanitized = sanitized
143
- .replace(/[\u200B-\u200D\uFEFF]/g, "")
154
+ .replace(/[\u200B-\u200D\uFEFF]/g, stripUnsafeZeroWidthCharacter)
144
155
  // eslint-disable-next-line no-control-regex
145
156
  .replace(/[\u0000-\u0008\u000B\u000C\u000E-\u001F\u007F-\u009F]/g, "")
146
157
  .replace(/[\u202A-\u202E]/g, "")