diff --git a/lib/__snapshots__/vocabulary-builder.test.ts.snap b/lib/__snapshots__/vocabulary-builder.test.ts.snap new file mode 100644 index 0000000..3ccddd8 --- /dev/null +++ b/lib/__snapshots__/vocabulary-builder.test.ts.snap @@ -0,0 +1,33 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`convertSvgMarkupToSymbolData() works with SVGs that just have a path and no specs 1`] = ` +Object { + "bbox": Object { + "x": Object { + "max": 692.249, + "min": 27.751, + }, + "y": Object { + "max": 692.249, + "min": 27.751, + }, + }, + "layers": Array [ + Object { + "children": Array [], + "props": Object { + "d": "M 360.000 29.751 C 542.791 29.751 690.249 177.209 690.249 360.000 C 690.249 542.791 542.791 690.249 360.000 690.249 C 177.209 690.249 29.751 542.791 29.751 360.000 C 29.751 177.209 177.209 29.751 360.000 29.751 Z", + "fill": "#ffffff", + "fillRule": "evenodd", + "stroke": "#000000", + "strokeLinecap": "round", + "strokeLinejoin": "round", + "strokeWidth": "4", + }, + "tagName": "path", + }, + ], + "name": "blarg", + "specs": undefined, +} +`; diff --git a/lib/vocabulary-builder.test.ts b/lib/vocabulary-builder.test.ts new file mode 100644 index 0000000..f1d36e4 --- /dev/null +++ b/lib/vocabulary-builder.test.ts @@ -0,0 +1,11 @@ +import { convertSvgMarkupToSymbolData } from "./vocabulary-builder"; + +const CIRCLE = ``; + +describe("convertSvgMarkupToSymbolData()", () => { + it("works with SVGs that just have a path and no specs", () => { + expect( + convertSvgMarkupToSymbolData("blarg.svg", `${CIRCLE}`) + ).toMatchSnapshot(); + }); +}); diff --git a/lib/vocabulary-builder.ts b/lib/vocabulary-builder.ts index bb2f753..8ae5e23 100644 --- a/lib/vocabulary-builder.ts +++ b/lib/vocabulary-builder.ts @@ -82,6 +82,28 @@ function serializeSvgSymbolElement( throw new Error(`Unsupported SVG element: <${tagName}>`); } +export function convertSvgMarkupToSymbolData( + filename: string, + svgMarkup: string +): SvgSymbolData { + const name = path.basename(filename, SVG_EXT).toLowerCase(); + const $ = cheerio.load(svgMarkup); + const svgEl = $("svg"); + const rawLayers = onlyTags(svgEl.children()).map((ch) => + serializeSvgSymbolElement($, ch) + ); + const [specs, layers] = extractSpecs(rawLayers); + const bbox = getSvgBoundingBox(layers); + + const symbol: SvgSymbolData = { + name, + bbox, + layers, + specs, + }; + return symbol; +} + export function build() { const filenames = fs.readdirSync(SVG_DIR); const vocab: SvgSymbolData[] = []; @@ -91,21 +113,7 @@ export function build() { const svgMarkup = fs.readFileSync(path.join(SVG_DIR, filename), { encoding: "utf-8", }); - const $ = cheerio.load(svgMarkup); - const svgEl = $("svg"); - const name = path.basename(filename, SVG_EXT).toLowerCase(); - const rawLayers = onlyTags(svgEl.children()).map((ch) => - serializeSvgSymbolElement($, ch) - ); - const [specs, layers] = extractSpecs(rawLayers); - const bbox = getSvgBoundingBox(layers); - - const symbol: SvgSymbolData = { - name, - bbox, - layers, - specs, - }; + const symbol = convertSvgMarkupToSymbolData(filename, svgMarkup); vocab.push(symbol); } }