@cyberalien/svg-utils 0.1.2 → 0.1.4

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/lib/css/hash.d.ts CHANGED
@@ -1,6 +1,6 @@
1
- import { CSSRules } from "./types.js";
1
+ import { CSSHashOptions, CSSRules } from "./types.js";
2
2
  /**
3
3
  * Get class name for CSS rules
4
4
  */
5
- declare function createCSSClassName(rules: CSSRules, prefix?: string): string;
5
+ declare function createCSSClassName(rules: CSSRules, prefix?: string, options?: CSSHashOptions): string;
6
6
  export { createCSSClassName };
package/lib/css/hash.js CHANGED
@@ -5,12 +5,13 @@ const length = 6;
5
5
  /**
6
6
  * Get class name for CSS rules
7
7
  */
8
- function createCSSClassName(rules, prefix = "") {
8
+ function createCSSClassName(rules, prefix = "", options) {
9
9
  const sorted = sortObject(rules);
10
10
  return getUniqueHash(sorted, {
11
11
  css: true,
12
12
  length,
13
- prefix
13
+ prefix,
14
+ ...options
14
15
  });
15
16
  }
16
17
 
@@ -1,3 +1,4 @@
1
+ import { UniqueHashOptions } from "../helpers/hash/types.js";
1
2
  /**
2
3
  * CSS rules
3
4
  */
@@ -13,4 +14,8 @@ interface CSSKeyframes {
13
14
  prop: string;
14
15
  frames: CSSKeyframe[];
15
16
  }
16
- export { CSSKeyframe, CSSKeyframes, CSSRules };
17
+ /**
18
+ * Hash options
19
+ */
20
+ type CSSHashOptions = Partial<Pick<UniqueHashOptions, 'length' | 'lengths' | 'throwOnCollision'>>;
21
+ export { CSSHashOptions, CSSKeyframe, CSSKeyframes, CSSRules };
@@ -1,7 +1,7 @@
1
1
  interface UniqueHashOptions {
2
2
  prefix?: string;
3
3
  css: boolean;
4
- length: number;
4
+ length: number | ((content: string) => number);
5
5
  lengths?: Record<string, number>;
6
6
  throwOnCollision?: boolean;
7
7
  }
@@ -24,7 +24,8 @@ function getUniqueHash(data, options) {
24
24
  const str = typeof data === "string" ? data : JSON.stringify(sortObject(data));
25
25
  const hasPrefix = !!prefix;
26
26
  const values = hashString(str);
27
- let hash = hashToString(values, css, hasPrefix, length);
27
+ const defaultLength = typeof length === "function" ? length(str) : length;
28
+ let hash = hashToString(values, css, hasPrefix, defaultLength);
28
29
  if (lengths?.[hash]) hash = hashToString(values, css, hasPrefix, lengths[hash]);
29
30
  const cache = hasPrefix ? uniqueWithPrefixHashes : uniqueHashes;
30
31
  const result = `${prefix}${hash}`;
package/lib/index.d.ts CHANGED
@@ -1,12 +1,12 @@
1
1
  import { ClassProp, classProps, defaultClassProp } from "./classname/const.js";
2
2
  import { splitClassName, toggleClassName } from "./classname/toggle.js";
3
+ import { UniqueHashOptions } from "./helpers/hash/types.js";
3
4
  import { ParsedXMLNode, ParsedXMLTagElement, ParsedXMLTextElement, StringifyXMLOptions } from "./xml/types.js";
4
5
  import { ConvertSVGContentOptions, ConvertedSVGContent } from "./svg-css/types.js";
5
6
  import { createCSSClassName } from "./css/hash.js";
6
7
  import { stringifyCSSKeyframes, stringifyCSSRules, stringifyCSSSelector } from "./css/stringify.js";
7
8
  import { hashString } from "./helpers/hash/hash.js";
8
9
  import { hashToString } from "./helpers/hash/stringify.js";
9
- import { UniqueHashOptions } from "./helpers/hash/types.js";
10
10
  import { getUniqueHash } from "./helpers/hash/unique.js";
11
11
  import { cloneObject } from "./helpers/misc/clone.js";
12
12
  import { compareSets, compareValues } from "./helpers/misc/compare.js";
@@ -8,7 +8,7 @@ import { convertSVGRootToCSS } from "./root.js";
8
8
  function convertSVGContentToCSSRules(content, options) {
9
9
  const root = parseXMLContent(content);
10
10
  if (!root) return { content };
11
- const classes = convertSVGRootToCSS(root, options?.classNamePrefix);
11
+ const classes = convertSVGRootToCSS(root, options?.classNamePrefix, options?.hashOptions);
12
12
  if (classes) {
13
13
  const newContent = stringifyXMLContent(root, options);
14
14
  if (newContent) return {
@@ -1,9 +1,9 @@
1
- import { CSSRules } from "../css/types.js";
1
+ import { CSSHashOptions, CSSRules } from "../css/types.js";
2
2
  import { ParsedXMLTagElement } from "../xml/types.js";
3
3
  /**
4
4
  * Convert SVG tags tree to SVG+CSS
5
5
  *
6
6
  * Returns used CSS class names with rules
7
7
  */
8
- declare function convertSVGRootToCSS(root: ParsedXMLTagElement[], classNamePrefix?: string): Record<string, CSSRules>;
8
+ declare function convertSVGRootToCSS(root: ParsedXMLTagElement[], classNamePrefix?: string, hashOptions?: CSSHashOptions): Record<string, CSSRules>;
9
9
  export { convertSVGRootToCSS };
@@ -8,13 +8,13 @@ import { extractSVGTagPropertiesForCSS } from "./props/props.js";
8
8
  *
9
9
  * Returns used CSS class names with rules
10
10
  */
11
- function convertSVGRootToCSS(root, classNamePrefix = "") {
11
+ function convertSVGRootToCSS(root, classNamePrefix = "", hashOptions) {
12
12
  const rules = Object.create(null);
13
13
  iterateXMLContent(root, (node) => {
14
14
  if (node.type === "tag") {
15
15
  const props = extractSVGTagPropertiesForCSS(node);
16
16
  if (props) {
17
- const className = createCSSClassName(props.rules, classNamePrefix);
17
+ const className = createCSSClassName(props.rules, classNamePrefix, hashOptions);
18
18
  toggleClassName(node.attribs, className, true);
19
19
  rules[className] = props.rules;
20
20
  }
@@ -1,10 +1,11 @@
1
- import { CSSKeyframes, CSSRules } from "../css/types.js";
1
+ import { CSSHashOptions, CSSKeyframes, CSSRules } from "../css/types.js";
2
2
  import { StringifyXMLOptions } from "../xml/types.js";
3
3
  /**
4
4
  * Options for converting SVG content to SVG+CSS
5
5
  */
6
6
  interface ConvertSVGContentOptions extends StringifyXMLOptions {
7
7
  classNamePrefix?: string;
8
+ hashOptions?: CSSHashOptions;
8
9
  }
9
10
  /**
10
11
  * Result of converting SVG content to SVG+CSS
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "type": "module",
4
4
  "description": "Common functions for working with SVG used by various packages.",
5
5
  "author": "Vjacheslav Trushkin",
6
- "version": "0.1.2",
6
+ "version": "0.1.4",
7
7
  "license": "MIT",
8
8
  "bugs": "https://github.com/cyberalien/svg-utils/issues",
9
9
  "homepage": "https://cyberalien.dev/",