@cyberalien/svg-utils 0.0.10 → 0.0.12

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.
@@ -1,5 +1,5 @@
1
1
  /**
2
2
  * Convert hash to a string, usable in CSS for class names and keyframes
3
3
  */
4
- declare function hashToString(value: number[], hasPrefix?: boolean, limit?: number): string;
4
+ declare function hashToString(value: number[], hasPrefix?: boolean, limit?: number, caseSensitive?: boolean): string;
5
5
  export { hashToString };
@@ -1,11 +1,13 @@
1
- const firstChars = "abcdefghijklmnopqrstuvwxyz";
2
- const chars = firstChars + "0123456789_-";
3
- const firstLetterRadix = 26;
4
- const letterRadix = 38;
1
+ const baseChars = "abcdefghijklmnopqrstuvwxyz";
2
+ const extraChars = "0123456789_-";
5
3
  /**
6
4
  * Convert hash to a string, usable in CSS for class names and keyframes
7
5
  */
8
- function hashToString(value, hasPrefix = true, limit = 8) {
6
+ function hashToString(value, hasPrefix = true, limit = 8, caseSensitive = true) {
7
+ const firstChars = caseSensitive ? baseChars : `${baseChars}${baseChars.toUpperCase()}`;
8
+ const chars = firstChars + extraChars;
9
+ const firstLetterRadix = firstChars.length;
10
+ const letterRadix = chars.length;
9
11
  const result = [];
10
12
  let num = value.shift() ?? 0;
11
13
  if (!hasPrefix) {
@@ -1,6 +1,7 @@
1
1
  interface Options {
2
2
  prefix?: string;
3
3
  length: number;
4
+ caseSensitive?: boolean;
4
5
  lengths?: Record<string, number>;
5
6
  }
6
7
  /**
@@ -14,12 +14,12 @@ const uniqueWithPrefixHashes = Object.create(null);
14
14
  * 9 chars = 113t unique hashes
15
15
  */
16
16
  function getUniqueHash(data, options) {
17
- const { prefix, length, lengths } = options;
17
+ const { prefix, length, lengths, caseSensitive } = options;
18
18
  const str = typeof data === "string" ? data : JSON.stringify(sortObject(data));
19
19
  const hasPrefix = !!prefix;
20
20
  const values = hashString(str);
21
- let hash = hashToString(values, hasPrefix, length);
22
- if (lengths?.[hash]) hash = hashToString(values, hasPrefix, lengths[hash]);
21
+ let hash = hashToString(values, hasPrefix, length, caseSensitive);
22
+ if (lengths?.[hash]) hash = hashToString(values, hasPrefix, lengths[hash], caseSensitive);
23
23
  const cache = hasPrefix ? uniqueWithPrefixHashes : uniqueHashes;
24
24
  if (!cache[hash]) cache[hash] = str;
25
25
  else if (cache[hash] !== str) {
package/lib/svg/ids.js CHANGED
@@ -94,7 +94,32 @@ function changeSVGIDs(root, callback) {
94
94
  const node = idNodes.get(id);
95
95
  const newNode = cloneObject(node);
96
96
  delete newNode.attribs.id;
97
- for (const usageItem of usage) if (usageItem.node === node && usageItem.id === id) delete newNode.attribs[usageItem.attrib];
97
+ for (const usageItem of usage) if (usageItem.node === node && usageItem.id === id) {
98
+ const attr = usageItem.attrib;
99
+ const value = newNode.attribs[attr];
100
+ if (value === id) newNode.attribs[attr] = "";
101
+ else {
102
+ const parts = value.split(id);
103
+ const newValue = [];
104
+ const total = parts.length;
105
+ for (let i = 0; i < total; i++) {
106
+ if (i > 0) {
107
+ newValue.push(parts[i - 1]);
108
+ if (parts[i - i].slice(-1) !== ";") {
109
+ newValue.push(id);
110
+ continue;
111
+ }
112
+ }
113
+ if (i < total - 1) {
114
+ if (parts[i].slice(0, 1) !== ".") {
115
+ newValue.push(parts[i]);
116
+ continue;
117
+ }
118
+ }
119
+ }
120
+ newNode.attribs[attr] = newValue.join("");
121
+ }
122
+ }
98
123
  const content = stringifyXMLContent([newNode]);
99
124
  if (!content) throw new Error(`Failed to stringify node with ID: ${id}`);
100
125
  const newID = callback(id, content, node.tag);
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.0.10",
6
+ "version": "0.0.12",
7
7
  "license": "MIT",
8
8
  "bugs": "https://github.com/cyberalien/svg-utils/issues",
9
9
  "homepage": "https://cyberalien.dev/",