Sha256: a320147a9d2314bf07acdab03a8f3e39b69a4369d06b71f1140640cf3addb5b9

Contents?: true

Size: 1.27 KB

Versions: 44

Compression:

Stored size: 1.27 KB

Contents

var util = require('util')

var INDENT_START = /[\{\[]/
var INDENT_END = /[\}\]]/

module.exports = function() {
  var lines = []
  var indent = 0

  var push = function(str) {
    var spaces = ''
    while (spaces.length < indent*2) spaces += '  '
    lines.push(spaces+str)
  }

  var line = function(fmt) {
    if (!fmt) return line

    if (INDENT_END.test(fmt.trim()[0]) && INDENT_START.test(fmt[fmt.length-1])) {
      indent--
      push(util.format.apply(util, arguments))
      indent++
      return line
    }
    if (INDENT_START.test(fmt[fmt.length-1])) {
      push(util.format.apply(util, arguments))
      indent++
      return line
    }
    if (INDENT_END.test(fmt.trim()[0])) {
      indent--
      push(util.format.apply(util, arguments))
      return line
    }

    push(util.format.apply(util, arguments))
    return line
  }

  line.toString = function() {
    return lines.join('\n')
  }

  line.toFunction = function(scope) {
    var src = 'return ('+line.toString()+')'

    var keys = Object.keys(scope || {}).map(function(key) {
      return key
    })

    var vals = keys.map(function(key) {
      return scope[key]
    })

    return Function.apply(null, keys.concat(src)).apply(null, vals)
  }

  if (arguments.length) line.apply(null, arguments)

  return line
}

Version data entries

44 entries across 39 versions & 12 rubygems

Version Path
optimacms-0.4.3 spec/dummy/node_modules/generate-function/index.js
optimacms-0.4.2 spec/dummy/node_modules/generate-function/index.js
locomotivecms-3.4.0 app/javascript/node_modules/generate-function/index.js
guard-sass-lint-0.1.2 node_modules/generate-function/index.js
guard-sass-lint-0.1.1 node_modules/generate-function/index.js
lanes-0.8.0 node_modules/fsevents/node_modules/generate-function/index.js
lanes-0.8.0 node_modules/generate-function/index.js
node-compiler-0.9.1 vendor/node/deps/npm/node_modules/request/node_modules/har-validator/node_modules/is-my-json-valid/node_modules/generate-function/index.js
node-compiler-0.9.0 vendor/node-v7.2.1/deps/npm/node_modules/request/node_modules/har-validator/node_modules/is-my-json-valid/node_modules/generate-function/index.js
select_all-rails-0.3.1 node_modules/generate-function/index.js
node-compiler-0.8.0 vendor/node-v7.2.0/deps/npm/node_modules/request/node_modules/har-validator/node_modules/is-my-json-valid/node_modules/generate-function/index.js
node-compiler-0.7.0 vendor/node-v6.9.1/deps/npm/node_modules/request/node_modules/har-validator/node_modules/is-my-json-valid/node_modules/generate-function/index.js
node-compiler-0.7.0 vendor/node-v7.1.0/deps/npm/node_modules/request/node_modules/har-validator/node_modules/is-my-json-valid/node_modules/generate-function/index.js
tck-lambdas-0.3.10 lib/tck/lambdas/chistacojs/source/node_modules/request/node_modules/har-validator/node_modules/is-my-json-valid/node_modules/generate-function/index.js
tck-lambdas-0.3.9 lib/tck/lambdas/chistacojs/source/node_modules/request/node_modules/har-validator/node_modules/is-my-json-valid/node_modules/generate-function/index.js
tck-lambdas-0.3.8 lib/tck/lambdas/chistacojs/source/node_modules/request/node_modules/har-validator/node_modules/is-my-json-valid/node_modules/generate-function/index.js
tck-lambdas-0.3.7 lib/tck/lambdas/chistacojs/source/node_modules/request/node_modules/har-validator/node_modules/is-my-json-valid/node_modules/generate-function/index.js
tck-lambdas-0.3.6 lib/tck/lambdas/chistacojs/source/node_modules/request/node_modules/har-validator/node_modules/is-my-json-valid/node_modules/generate-function/index.js
tck-lambdas-0.3.5 lib/tck/lambdas/chistacojs/source/node_modules/request/node_modules/har-validator/node_modules/is-my-json-valid/node_modules/generate-function/index.js
tck-lambdas-0.3.4 lib/tck/lambdas/chistacojs/source/node_modules/request/node_modules/har-validator/node_modules/is-my-json-valid/node_modules/generate-function/index.js