@@ -29,6 +29,16 @@ import { toPath, toPoints } from 'svg-points'
2929 * @typedef {Object } Node
3030 */
3131
32+ /**
33+ * The data from a Node that is useful in Frame Shape and Plain Shape Object creation.
34+ *
35+ * @typedef {Object } NodeData
36+ *
37+ * @property {Object } attributes - All HTML attributes of the Node.
38+ * @property {Object[] } childNodes
39+ * @property {string } type - The nodeName of the Node.
40+ */
41+
3242/**
3343 * Wilderness' accepted node types.
3444 */
@@ -104,7 +114,7 @@ const diff = (current, next) => {
104114}
105115
106116/**
107- * Creates a FrameShape from a Node.
117+ * Creates a Frame Shape from a Node.
108118 *
109119 * @param {Node } node
110120 *
@@ -114,30 +124,23 @@ const diff = (current, next) => {
114124 * frameShapeFromNode(node)
115125 */
116126const frameShape = el => {
117- const { attributes : attrs , childNodes, nodeName : type } = el
118- const attributes = { }
119-
120- if ( el . hasAttributes ( ) ) {
121- [ ...attrs ] . map ( ( { name, value } ) => {
122- attributes [ name ] = value
123- } )
124- }
127+ const { attributes, childNodes, type } = nodeData ( el )
125128
126129 if ( type === 'g' ) {
127130 return {
128131 attributes,
129- childFrameShapes : [ ... childNodes ] . filter ( validNode ) . map ( frameShape )
132+ childFrameShapes : childNodes . filter ( validNode ) . map ( frameShape )
130133 }
131134 }
132135
133136 return {
134137 attributes : removeCoreProps ( type , attributes ) ,
135- points : toPoints ( plainShapeObject ( type , attributes ) )
138+ points : toPoints ( plainShapeObjectFromAttrs ( type , attributes ) )
136139 }
137140}
138141
139142/**
140- * Creates a group Node from a FrameShape array.
143+ * Creates a group Node from a Frame Shape array.
141144 *
142145 * @param {FrameShape[] } childFrameShapes
143146 *
@@ -154,6 +157,51 @@ const groupNode = childFrameShapes => {
154157 return group
155158}
156159
160+ /**
161+ * Creates a Node from a Frame Shape.
162+ *
163+ * @param {FrameShape } frameShape
164+ *
165+ * @returns {Node }
166+ *
167+ * @example
168+ * node(frameShape)
169+ */
170+ const node = ( { attributes, childFrameShapes, points } ) => {
171+ const el = childFrameShapes
172+ ? groupNode ( childFrameShapes )
173+ : pathNode ( points )
174+
175+ Object . keys ( attributes ) . map ( attr => {
176+ el . setAttribute ( attr , attributes [ attr ] )
177+ } )
178+
179+ return el
180+ }
181+
182+ /**
183+ * Creates Node Data given a Node.
184+ *
185+ * @param {Node } el
186+ *
187+ * @returns {NodeData }
188+ *
189+ * @example
190+ * nodeData(el)
191+ */
192+ const nodeData = el => {
193+ const { attributes : attrs , childNodes, nodeName : type } = el
194+ const attributes = { }
195+
196+ if ( el . hasAttributes ( ) ) {
197+ [ ...attrs ] . map ( ( { name, value } ) => {
198+ attributes [ name ] = value
199+ } )
200+ }
201+
202+ return { attributes, childNodes : [ ...childNodes ] , type }
203+ }
204+
157205/**
158206 * Creates a path Node from Points.
159207 *
@@ -172,6 +220,33 @@ const pathNode = points => {
172220 return path
173221}
174222
223+ /**
224+ * Creates a Plain Shape Object from a Node.
225+ *
226+ * @param {Node } el
227+ *
228+ * @returns {PlainShapeObject }
229+ *
230+ * @example
231+ * plainShapeObject(el)
232+ */
233+ const plainShapeObject = el => {
234+ const { attributes, childNodes, type } = nodeData ( el )
235+
236+ if ( type === 'g' ) {
237+ return {
238+ ...attributes ,
239+ type,
240+ shapes : childNodes . filter ( validNode ) . map ( plainShapeObject )
241+ }
242+ }
243+
244+ return {
245+ ...attributes ,
246+ ...plainShapeObjectFromAttrs ( type , attributes )
247+ }
248+ }
249+
175250/**
176251 * Creates a Plain Shape Object from type and an attribute object.
177252 *
@@ -181,9 +256,9 @@ const pathNode = points => {
181256 * @returns {PlainShapeObject }
182257 *
183258 * @example
184- * plainShapeObject ('rect', attributes)
259+ * plainShapeObjectFromAttrs ('rect', attributes)
185260 */
186- const plainShapeObject = ( type , attributes ) => {
261+ const plainShapeObjectFromAttrs = ( type , attributes ) => {
187262 const props = coreProps ( type )
188263 const result = { type }
189264
@@ -198,28 +273,6 @@ const plainShapeObject = (type, attributes) => {
198273 return result
199274}
200275
201- /**
202- * Creates a Node from a FrameShape.
203- *
204- * @param {FrameShape } frameShape
205- *
206- * @returns {Node }
207- *
208- * @example
209- * node(frameShape)
210- */
211- const node = ( { attributes, childFrameShapes, points } ) => {
212- const el = childFrameShapes
213- ? groupNode ( childFrameShapes )
214- : pathNode ( points )
215-
216- Object . keys ( attributes ) . map ( attr => {
217- el . setAttribute ( attr , attributes [ attr ] )
218- } )
219-
220- return el
221- }
222-
223276/**
224277 * Removes type's core props from attributes object.
225278 *
@@ -245,7 +298,7 @@ const removeCoreProps = (type, attributes) => {
245298}
246299
247300/**
248- * Updates a Node from a FrameShape .
301+ * Updates a Node from a Frame Shape .
249302 *
250303 * @param {Node } el
251304 * @param {FrameShape } frameShape
0 commit comments