commonform-flatten
Advanced tools
Comparing version 0.5.2 to 0.5.3
84
index.js
@@ -1,51 +0,43 @@ | ||
var clone = require('clone'); | ||
var resolve = require('commonform-resolve'); | ||
var clone = require('clone') | ||
var resolve = require('commonform-resolve') | ||
var flatten = function(form, list, depth) { | ||
return form.content.reduce(function(list, element) { | ||
var newContainer; | ||
if (element.hasOwnProperty('form')) { | ||
newContainer = { | ||
depth: depth + 1, | ||
content: [], | ||
numbering: element.numbering | ||
}; | ||
if (element.hasOwnProperty('heading')) { | ||
newContainer.heading = element.heading; | ||
} | ||
if (element.form.hasOwnProperty('conspicuous')) { | ||
newContainer.conspicuous = element.form.conspicuous; | ||
} | ||
list.push(newContainer); | ||
return flatten(element.form, list, depth + 1); | ||
} else { | ||
var listLength = list.length; | ||
var last = list[listLength - 1]; | ||
if ( | ||
!last || | ||
!last.hasOwnProperty('depth') || | ||
last.depth !== depth | ||
) { | ||
newContainer = { | ||
depth: depth, | ||
content: [] | ||
}; | ||
if (form.hasOwnProperty('conspicuous')) { | ||
newContainer.conspicuous = form.conspicuous; | ||
} | ||
newContainer.content.push(element); | ||
list.push(newContainer); | ||
} else { | ||
list[listLength - 1].content.push(element); | ||
} | ||
return list; | ||
} | ||
}, list); | ||
}; | ||
return form.content | ||
.reduce( | ||
function(list, element) { | ||
var newContainer | ||
if (element.hasOwnProperty('form')) { | ||
newContainer = { | ||
depth: ( depth + 1 ), | ||
content: [ ], | ||
numbering: element.numbering } | ||
if (element.hasOwnProperty('heading')) { | ||
newContainer.heading = element.heading } | ||
if (element.form.hasOwnProperty('conspicuous')) { | ||
newContainer.conspicuous = element.form.conspicuous } | ||
list.push(newContainer) | ||
return flatten(element.form, list, depth + 1) } | ||
else { | ||
var listLength = list.length | ||
var last = list[listLength - 1] | ||
var startNew = ( | ||
!last || | ||
!last.hasOwnProperty('depth') || | ||
last.depth !== depth ) | ||
if (startNew) { | ||
newContainer = { | ||
depth: depth, | ||
content: [ ] } | ||
if (form.hasOwnProperty('conspicuous')) { | ||
newContainer.conspicuous = form.conspicuous } | ||
newContainer.content.push(element) | ||
list.push(newContainer) } | ||
else { | ||
list[listLength - 1].content.push(element) } | ||
return list } }, | ||
list) } | ||
module.exports = function(form, values) { | ||
if (!values) { | ||
values = {}; | ||
} | ||
return flatten(resolve(clone(form), values), [], 1); | ||
}; | ||
values = { } } | ||
return flatten(resolve(clone(form), values), [ ], 1) } |
{ | ||
"name": "commonform-flatten", | ||
"description": "flatten Common Forms", | ||
"version": "0.5.2", | ||
"version": "0.5.3", | ||
"author": "Kyle E. Mitchell <kyle@kemitchell.com> (http://kemitchell.com)", | ||
@@ -6,0 +6,0 @@ "bugs": "https://github.com/commonform/commonform-flatten/issues", |
4485
41