diff options
Diffstat (limited to 'node_modules/generate-function')
| -rw-r--r-- | node_modules/generate-function/.travis.yml | 3 | ||||
| -rw-r--r-- | node_modules/generate-function/LICENSE | 21 | ||||
| -rw-r--r-- | node_modules/generate-function/README.md | 89 | ||||
| -rw-r--r-- | node_modules/generate-function/example.js | 27 | ||||
| -rw-r--r-- | node_modules/generate-function/index.js | 181 | ||||
| -rw-r--r-- | node_modules/generate-function/package.json | 32 | ||||
| -rw-r--r-- | node_modules/generate-function/test.js | 49 | 
7 files changed, 402 insertions, 0 deletions
diff --git a/node_modules/generate-function/.travis.yml b/node_modules/generate-function/.travis.yml new file mode 100644 index 0000000..6e5919d --- /dev/null +++ b/node_modules/generate-function/.travis.yml @@ -0,0 +1,3 @@ +language: node_js +node_js: +  - "0.10" diff --git a/node_modules/generate-function/LICENSE b/node_modules/generate-function/LICENSE new file mode 100644 index 0000000..757562e --- /dev/null +++ b/node_modules/generate-function/LICENSE @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) 2014 Mathias Buus + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE.
\ No newline at end of file diff --git a/node_modules/generate-function/README.md b/node_modules/generate-function/README.md new file mode 100644 index 0000000..97419e9 --- /dev/null +++ b/node_modules/generate-function/README.md @@ -0,0 +1,89 @@ +# generate-function + +Module that helps you write generated functions in Node + +``` +npm install generate-function +``` + +[](http://travis-ci.org/mafintosh/generate-function) + +## Disclamer + +Writing code that generates code is hard. +You should only use this if you really, really, really need this for performance reasons (like schema validators / parsers etc). + +## Usage + +``` js +const genfun = require('generate-function') +const { d } = genfun.formats + +function addNumber (val) { +  const gen = genfun() + +  gen(` +    function add (n) {') +      return n + ${d(val)}) // supports format strings to insert values +    } +  `) + +  return gen.toFunction() // will compile the function +} + +const add2 = addNumber(2) + +console.log('1 + 2 =', add2(1)) +console.log(add2.toString()) // prints the generated function +``` + +If you need to close over variables in your generated function pass them to `toFunction(scope)` + +``` js +function multiply (a, b) { +  return a * b +} + +function addAndMultiplyNumber (val) { +  const gen = genfun() +   +  gen(` +    function (n) { +      if (typeof n !== 'number') { +        throw new Error('argument should be a number') +      } +      const result = multiply(${d(val)}, n + ${d(val)}) +      return result +    } +  `) + +  // use gen.toString() if you want to see the generated source + +  return gen.toFunction({multiply}) +} + +const addAndMultiply2 = addAndMultiplyNumber(2) + +console.log(addAndMultiply2.toString()) +console.log('(3 + 2) * 2 =', addAndMultiply2(3)) +``` + +You can call `gen(src)` as many times as you want to append more source code to the function. + +## Variables + +If you need a unique safe identifier for the scope of the generated function call `str = gen.sym('friendlyName')`. +These are safe to use for variable names etc. + +## Object properties + +If you need to access an object property use the `str = gen.property('objectName', 'propertyName')`. + +This returns `'objectName.propertyName'` if `propertyName` is safe to use as a variable. Otherwise +it returns `objectName[propertyNameAsString]`. + +If you only pass `gen.property('propertyName')` it will only return the `propertyName` part safely + +## License + +MIT diff --git a/node_modules/generate-function/example.js b/node_modules/generate-function/example.js new file mode 100644 index 0000000..7c36c76 --- /dev/null +++ b/node_modules/generate-function/example.js @@ -0,0 +1,27 @@ +const genfun = require('./') +const { d } = genfun.formats + +function multiply (a, b) { +  return a * b +} + +function addAndMultiplyNumber (val) { +  const fn = genfun(` +    function (n) { +      if (typeof n !== 'number') { +        throw new Error('argument should be a number') +      } +      const result = multiply(${d(val)}, n + ${d(val)}) +      return result +    } +  `) + +  // use fn.toString() if you want to see the generated source + +  return fn.toFunction({multiply}) +} + +const addAndMultiply2 = addAndMultiplyNumber(2) + +console.log(addAndMultiply2.toString()) +console.log('(3 + 2) * 2 =', addAndMultiply2(3)) diff --git a/node_modules/generate-function/index.js b/node_modules/generate-function/index.js new file mode 100644 index 0000000..8105dc0 --- /dev/null +++ b/node_modules/generate-function/index.js @@ -0,0 +1,181 @@ +var util = require('util') +var isProperty = require('is-property') + +var INDENT_START = /[\{\[]/ +var INDENT_END = /[\}\]]/ + +// from https://mathiasbynens.be/notes/reserved-keywords +var RESERVED = [ +  'do', +  'if', +  'in', +  'for', +  'let', +  'new', +  'try', +  'var', +  'case', +  'else', +  'enum', +  'eval', +  'null', +  'this', +  'true', +  'void', +  'with', +  'await', +  'break', +  'catch', +  'class', +  'const', +  'false', +  'super', +  'throw', +  'while', +  'yield', +  'delete', +  'export', +  'import', +  'public', +  'return', +  'static', +  'switch', +  'typeof', +  'default', +  'extends', +  'finally', +  'package', +  'private', +  'continue', +  'debugger', +  'function', +  'arguments', +  'interface', +  'protected', +  'implements', +  'instanceof', +  'NaN', +  'undefined' +] + +var RESERVED_MAP = {} + +for (var i = 0; i < RESERVED.length; i++) { +  RESERVED_MAP[RESERVED[i]] = true +} + +var isVariable = function (name) { +  return isProperty(name) && !RESERVED_MAP.hasOwnProperty(name) +} + +var formats = { +  s: function(s) { +    return '' + s +  }, +  d: function(d) { +    return '' + Number(d) +  }, +  o: function(o) { +    return JSON.stringify(o) +  } +} + +var genfun = function() { +  var lines = [] +  var indent = 0 +  var vars = {} + +  var push = function(str) { +    var spaces = '' +    while (spaces.length < indent*2) spaces += '  ' +    lines.push(spaces+str) +  } + +  var pushLine = function(line) { +    if (INDENT_END.test(line.trim()[0]) && INDENT_START.test(line[line.length-1])) { +      indent-- +      push(line) +      indent++ +      return +    } +    if (INDENT_START.test(line[line.length-1])) { +      push(line) +      indent++ +      return +    } +    if (INDENT_END.test(line.trim()[0])) { +      indent-- +      push(line) +      return +    } + +    push(line) +  } + +  var line = function(fmt) { +    if (!fmt) return line + +    if (arguments.length === 1 && fmt.indexOf('\n') > -1) { +      var lines = fmt.trim().split('\n') +      for (var i = 0; i < lines.length; i++) { +        pushLine(lines[i].trim()) +      } +    } else { +      pushLine(util.format.apply(util, arguments)) +    } + +    return line +  } + +  line.scope = {} +  line.formats = formats + +  line.sym = function(name) { +    if (!name || !isVariable(name)) name = 'tmp' +    if (!vars[name]) vars[name] = 0 +    return name + (vars[name]++ || '') +  } + +  line.property = function(obj, name) { +    if (arguments.length === 1) { +      name = obj +      obj = '' +    } + +    name = name + '' + +    if (isProperty(name)) return (obj ? obj + '.' + name : name) +    return obj ? obj + '[' + JSON.stringify(name) + ']' : JSON.stringify(name) +  } + +  line.toString = function() { +    return lines.join('\n') +  } + +  line.toFunction = function(scope) { +    if (!scope) scope = {} + +    var src = 'return ('+line.toString()+')' + +    Object.keys(line.scope).forEach(function (key) { +      if (!scope[key]) scope[key] = line.scope[key] +    }) + +    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 +} + +genfun.formats = formats +module.exports = genfun diff --git a/node_modules/generate-function/package.json b/node_modules/generate-function/package.json new file mode 100644 index 0000000..be2ac04 --- /dev/null +++ b/node_modules/generate-function/package.json @@ -0,0 +1,32 @@ +{ +  "name": "generate-function", +  "version": "2.3.1", +  "description": "Module that helps you write generated functions in Node", +  "main": "index.js", +  "scripts": { +    "test": "tape test.js" +  }, +  "repository": { +    "type": "git", +    "url": "https://github.com/mafintosh/generate-function" +  }, +  "keywords": [ +    "generate", +    "code", +    "generation", +    "function", +    "performance" +  ], +  "author": "Mathias Buus", +  "license": "MIT", +  "bugs": { +    "url": "https://github.com/mafintosh/generate-function/issues" +  }, +  "homepage": "https://github.com/mafintosh/generate-function", +  "devDependencies": { +    "tape": "^4.9.1" +  }, +  "dependencies": { +    "is-property": "^1.0.2" +  } +} diff --git a/node_modules/generate-function/test.js b/node_modules/generate-function/test.js new file mode 100644 index 0000000..9337b71 --- /dev/null +++ b/node_modules/generate-function/test.js @@ -0,0 +1,49 @@ +var tape = require('tape') +var genfun = require('./') + +tape('generate add function', function(t) { +  var fn = genfun() +    ('function add(n) {') +      ('return n + %d', 42) +    ('}') + +  t.same(fn.toString(), 'function add(n) {\n  return n + 42\n}', 'code is indented') +  t.same(fn.toFunction()(10), 52, 'function works') +  t.end() +}) + +tape('generate function + closed variables', function(t) { +  var fn = genfun() +    ('function add(n) {') +      ('return n + %d + number', 42) +    ('}') + +  var notGood = fn.toFunction() +  var good = fn.toFunction({number:10}) + +  try { +    notGood(10) +    t.ok(false, 'function should not work') +  } catch (err) { +    t.same(err.message, 'number is not defined', 'throws reference error') +  } + +  t.same(good(11), 63, 'function with closed var works') +  t.end() +}) + +tape('generate property', function(t) { +  var gen = genfun() + +  t.same(gen.property('a'), 'a') +  t.same(gen.property('42'), '"42"') +  t.same(gen.property('b', 'a'), 'b.a') +  t.same(gen.property('b', '42'), 'b["42"]') +  t.same(gen.sym(42), 'tmp') +  t.same(gen.sym('a'), 'a') +  t.same(gen.sym('a'), 'a1') +  t.same(gen.sym(42), 'tmp1') +  t.same(gen.sym('const'), 'tmp2') + +  t.end() +})  | 
