all files / commonform-docx/ index.js

100% Statements 20/20
100% Branches 12/12
100% Functions 3/3
100% Lines 20/20
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38     72× 288×   288× 228×     60×         13× 13× 13× 13× 13× 13×         13× 13×       12× 12× 12×    
var JSZip = require('jszip')
 
var doc = require('./templates/document')
 
var zipObject = function (zip, object) {
  Object.keys(object).forEach(function (path) {
    var content = object[path]
    // File
    if (typeof content === 'string') {
      zip.file(path, content.trim())
    // Folder
    } else {
      zipObject(zip.folder(path), content)
    }
  })
}
 
module.exports = function (form, values, options) {
  var title = options.title
  var centerTitle = options.centerTitle || false
  var numberStyle = options.numbering
  var indentMargins = options.indentMargins || false
  var after = options.after || ''
  var blanks = options.blanks === undefined
    ? {text: '[•]', highlight: 'yellow'}
    : typeof options.blanks === 'string'
      ? {text: options.blanks}
      : options.blanks
  var scaffold = require('./data/scaffold.json')
  scaffold.word['document.xml'] = doc(
    form, values, title,
    centerTitle, numberStyle, indentMargins, after, blanks
  )
  var zip = new JSZip()
  zipObject(zip, scaffold)
  return zip
}