@unmade/svg-helpers 0.1.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/README.md ADDED
@@ -0,0 +1,46 @@
1
+ # @unmade/svg-helpers
2
+
3
+ A set of standalone helper functions for working with SVG content, designed for modular imports. Each helper module can be imported individually for optimal bundle size and clarity.
4
+
5
+ ## Installation
6
+
7
+ ```sh
8
+ npm install @unmade/svg-helpers
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ You can import specific helpers or groups of helpers directly from the package using subpath imports:
14
+
15
+ ```js
16
+ // Import a specific helper from the colours module
17
+ import { getUniqueColoursFromSVG } from '@unmade/svg-helpers/colours';
18
+
19
+ // Import all helpers from the colours module
20
+ import * as colours from '@unmade/svg-helpers/colours';
21
+ ```
22
+
23
+ ## Exported Modules
24
+
25
+ ### colours
26
+ Helpers for extracting and working with colour information in SVGs.
27
+
28
+ #### Exports
29
+ - **getUniqueColoursFromSVG(svg: SVGElement): string[]**
30
+ - Returns a list of unique hex colours used in the SVG (expands short hex codes to full length).
31
+ - **getColourableElements(svg: SVGElement): SVGElement[]**
32
+ - Returns all SVG elements that can have a colour (fill, stroke, stop-color, etc).
33
+ - **expandHex(short: string): string**
34
+ - Converts a short hex colour (e.g. `#abc`) to full length (e.g. `#aabbcc`).
35
+
36
+ ## Adding More Helpers
37
+
38
+ To add a new helper module, create a new folder in `src/` with an `index.ts` file. It will be automatically exported as a subpath import:
39
+
40
+ ```js
41
+ import { yourHelper } from '@unmade/svg-helpers/your-module';
42
+ ```
43
+
44
+ ---
45
+
46
+ For more details, see the source code in each module.
@@ -0,0 +1 @@
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const a=o=>{let e=Array.from(o),t=!1;return e[0]==="#"&&(e.shift(),t=!0),e.length===3&&(e=e.map(r=>`${r}${r}`)),`${t?"#":""}${e.join("").toLowerCase()}`};exports.expandHex=a;
@@ -0,0 +1,7 @@
1
+ const a = (o) => {
2
+ let e = Array.from(o), t = !1;
3
+ return e[0] === "#" && (e.shift(), t = !0), e.length === 3 && (e = e.map((r) => `${r}${r}`)), `${t ? "#" : ""}${e.join("").toLowerCase()}`;
4
+ };
5
+ export {
6
+ a as expandHex
7
+ };
@@ -0,0 +1 @@
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const s=require("./colours/format.cjs"),e={svg:"svg",circle:"circle",desc:"desc",ellipse:"ellipse",rect:"rect",line:"line",path:"path",polygon:"polygon",polyline:"polyline",stop:"stop"},o={fill:"fill",stroke:"stroke"},E={tiling:[e.circle,e.ellipse,e.rect,e.line,e.path,e.polygon,e.polyline,e.stop,`[${o.fill}]`,`[${o.stroke}]`],nonTiling:[e.circle,e.desc,e.ellipse,e.rect,e.line,e.path,e.polygon,e.polyline,e.stop,`[${o.fill}]`,`[${o.stroke}]`]},a=l=>Array.from(l.querySelectorAll(E.tiling.map(t=>`*:not(clipPath) > ${t}`).join(", "))),u=l=>{const t=l.getAttribute(o.stroke);return t==="none"||!t?!1:t},d=l=>{const g=a(l).reduce((r,n)=>{const i=n.getAttribute("stop-color"),c=n.getAttribute(o.fill)||"#000",p=u(n);return!i&&c.startsWith("#")&&r.add(s.expandHex(c)),i&&r.add(s.expandHex(i)),p&&r.add(s.expandHex(p)),r},new Set);return Array.from(g)};exports.COLOURABLE_ELEMENT_SELECTORS=E;exports.ELEMENTS=e;exports.getColourableElements=a;exports.getUniqueColoursFromSVG=d;
@@ -0,0 +1,66 @@
1
+ import { expandHex as s } from "./colours/format.mjs";
2
+ const e = {
3
+ svg: "svg",
4
+ circle: "circle",
5
+ desc: "desc",
6
+ ellipse: "ellipse",
7
+ rect: "rect",
8
+ line: "line",
9
+ path: "path",
10
+ polygon: "polygon",
11
+ polyline: "polyline",
12
+ stop: "stop"
13
+ }, l = {
14
+ fill: "fill",
15
+ stroke: "stroke"
16
+ }, g = {
17
+ tiling: [
18
+ e.circle,
19
+ e.ellipse,
20
+ e.rect,
21
+ e.line,
22
+ e.path,
23
+ e.polygon,
24
+ e.polyline,
25
+ e.stop,
26
+ `[${l.fill}]`,
27
+ `[${l.stroke}]`
28
+ ],
29
+ nonTiling: [
30
+ e.circle,
31
+ e.desc,
32
+ e.ellipse,
33
+ e.rect,
34
+ e.line,
35
+ e.path,
36
+ e.polygon,
37
+ e.polyline,
38
+ e.stop,
39
+ `[${l.fill}]`,
40
+ `[${l.stroke}]`
41
+ ]
42
+ }, f = (o) => Array.from(
43
+ o.querySelectorAll(
44
+ g.tiling.map((t) => `*:not(clipPath) > ${t}`).join(", ")
45
+ )
46
+ ), u = (o) => {
47
+ const t = o.getAttribute(l.stroke);
48
+ return t === "none" || !t ? !1 : t;
49
+ }, d = (o) => {
50
+ const a = f(o).reduce(
51
+ (r, n) => {
52
+ const i = n.getAttribute("stop-color"), c = n.getAttribute(l.fill) || "#000", p = u(n);
53
+ return !i && c.startsWith("#") && r.add(s(c)), i && r.add(s(i)), p && r.add(s(p)), r;
54
+ },
55
+ /* @__PURE__ */ new Set()
56
+ );
57
+ return Array.from(
58
+ a
59
+ );
60
+ };
61
+ export {
62
+ g as COLOURABLE_ELEMENT_SELECTORS,
63
+ e as ELEMENTS,
64
+ f as getColourableElements,
65
+ d as getUniqueColoursFromSVG
66
+ };
@@ -0,0 +1,35 @@
1
+ export declare const COLOURABLE_ELEMENT_SELECTORS: {
2
+ tiling: string[];
3
+ nonTiling: string[];
4
+ };
5
+
6
+ export declare const ELEMENTS: {
7
+ svg: string;
8
+ circle: string;
9
+ desc: string;
10
+ ellipse: string;
11
+ rect: string;
12
+ line: string;
13
+ path: string;
14
+ polygon: string;
15
+ polyline: string;
16
+ stop: string;
17
+ };
18
+
19
+ /**
20
+ * Fetch all colourable elements from an SVG
21
+ *
22
+ * @param {SVGElement} svg - the SVG input
23
+ * @returns {SVGElement[]} the elements
24
+ */
25
+ export declare const getColourableElements: (svg: SVGElement) => SVGElement[];
26
+
27
+ /**
28
+ * Get a list of the unique colours used in an SVG document.
29
+ *
30
+ * @param {SVGElement} svg - the document
31
+ * @returns {Array} the colours (with shortened hex codes expanded to full)
32
+ */
33
+ export declare const getUniqueColoursFromSVG: (svg: SVGElement) => unknown[];
34
+
35
+ export { }
package/package.json ADDED
@@ -0,0 +1,35 @@
1
+ {
2
+ "name": "@unmade/svg-helpers",
3
+ "version": "0.1.0",
4
+ "description": "Set of helper functions for working with SVG content",
5
+ "type": "module",
6
+ "main": "./dist/index.cjs",
7
+ "module": "./dist/index.js",
8
+ "types": "./dist/index.d.ts",
9
+ "exports": {
10
+ ".": {
11
+ "import": "./dist/index.js",
12
+ "require": "./dist/index.cjs",
13
+ "types": "./dist/index.d.ts"
14
+ }
15
+ },
16
+ "files": [
17
+ "dist"
18
+ ],
19
+ "scripts": {
20
+ "build": "vite build",
21
+ "test": "vitest run",
22
+ "test:watch": "vitest",
23
+ "typecheck": "tsc --noEmit"
24
+ },
25
+ "devDependencies": {
26
+ "typescript": "^5.3.0",
27
+ "vite": "^5.0.0",
28
+ "vite-plugin-dts": "^3.7.0",
29
+ "vitest": "^1.2.0"
30
+ },
31
+ "keywords": [
32
+ "svg"
33
+ ],
34
+ "license": "MIT"
35
+ }