From e8b61a71b701e7fba8e5d7d68e045f87d3b4e46a Mon Sep 17 00:00:00 2001 From: Oscar Saharoy Date: Tue, 15 Oct 2024 12:11:58 +0100 Subject: [PATCH] improve group finding --- js/construct-rules.js | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/js/construct-rules.js b/js/construct-rules.js index 676c782..b4e9e5e 100644 --- a/js/construct-rules.js +++ b/js/construct-rules.js @@ -4,7 +4,7 @@ import { infoLog, successLog, errorLog } from "./log.js" import { getVerts, getTransforms } from "./parse-svg.js" -const containsPathsOnly = elm => [...elm.children].every( child => child.matches("path") ); +const containsPathsOnly = elm => elm.childElementCount > 0 && [...elm.children].every( child => child.matches("path") ); const containsOneGroup = elm => [...elm.children].filter( child => child.matches("g") ).length === 1; const containsOnePath = elm => [...elm.children].filter( child => child.matches("path") ).length === 1; const containsOneOf = candidates => elm => candidates.filter( candidate => elm.contains(candidate) ).length === 1; @@ -29,14 +29,15 @@ function getKeyAndSubdivisionRule( subdivisionOuterGroup ) { export function constructRules( svg, filename ) { - infoLog(`Finding inner subdivision groups...`); - + infoLog(`Finding subdivision groups...`); const groups = [ ...svg.querySelectorAll( "g" ) ]; - const subdivisionInnerGroups = groups.filter( containsPathsOnly ); + successLog(`Found ${groups.length} subdivision groups!`); + infoLog(`Finding inner subdivision groups...`); + const subdivisionInnerGroups = groups.filter( containsPathsOnly ); successLog(`Found ${subdivisionInnerGroups.length} inner subdivision groups!`); - infoLog(`Finding outer subdivision groups...`); + infoLog(`Finding outer subdivision groups...`); const subdivisionOuterGroups = groups .filter( containsOneGroup )