forked from enviPath/enviPy
Current Dev State
This commit is contained in:
72
static/js/ketcher2/node_modules/generate-function/README.md
generated
vendored
Normal file
72
static/js/ketcher2/node_modules/generate-function/README.md
generated
vendored
Normal file
@ -0,0 +1,72 @@
|
||||
# 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
|
||||
var genfun = require('generate-function')
|
||||
|
||||
var addNumber = function(val) {
|
||||
var fn = genfun()
|
||||
('function add(n) {')
|
||||
('return n + %d', val) // supports format strings to insert values
|
||||
('}')
|
||||
|
||||
return fn.toFunction() // will compile the function
|
||||
}
|
||||
|
||||
var 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
|
||||
var multiply = function(a, b) {
|
||||
return a * b
|
||||
}
|
||||
|
||||
var addAndMultiplyNumber = function(val) {
|
||||
var fn = genfun()
|
||||
('function(n) {')
|
||||
('if (typeof n !== "number") {') // ending a line with { will indent the source
|
||||
('throw new Error("argument should be a number")')
|
||||
('}')
|
||||
('var result = multiply(%d, n+%d)', val, val)
|
||||
('return result')
|
||||
('}')
|
||||
|
||||
// use fn.toString() if you want to see the generated source
|
||||
|
||||
return fn.toFunction({
|
||||
multiply: multiply
|
||||
})
|
||||
}
|
||||
|
||||
var addAndMultiply2 = addAndMultiplyNumber(2)
|
||||
|
||||
console.log('(3 + 2) * 2 =', addAndMultiply2(3))
|
||||
```
|
||||
|
||||
## Related
|
||||
|
||||
See [generate-object-property](https://github.com/mafintosh/generate-object-property) if you need to safely generate code that
|
||||
can be used to reference an object property
|
||||
|
||||
## License
|
||||
|
||||
MIT
|
||||
27
static/js/ketcher2/node_modules/generate-function/example.js
generated
vendored
Normal file
27
static/js/ketcher2/node_modules/generate-function/example.js
generated
vendored
Normal file
@ -0,0 +1,27 @@
|
||||
var genfun = require('./')
|
||||
|
||||
var multiply = function(a, b) {
|
||||
return a * b
|
||||
}
|
||||
|
||||
var addAndMultiplyNumber = function(val) {
|
||||
var fn = genfun()
|
||||
('function(n) {')
|
||||
('if (typeof n !== "number") {') // ending a line with { will indent the source
|
||||
('throw new Error("argument should be a number")')
|
||||
('}')
|
||||
('var result = multiply(%d, n+%d)', val, val)
|
||||
('return result')
|
||||
('}')
|
||||
|
||||
// use fn.toString() if you want to see the generated source
|
||||
|
||||
return fn.toFunction({
|
||||
multiply: multiply
|
||||
})
|
||||
}
|
||||
|
||||
var addAndMultiply2 = addAndMultiplyNumber(2)
|
||||
|
||||
console.log(addAndMultiply2.toString())
|
||||
console.log('(3 + 2) * 2 =', addAndMultiply2(3))
|
||||
61
static/js/ketcher2/node_modules/generate-function/index.js
generated
vendored
Normal file
61
static/js/ketcher2/node_modules/generate-function/index.js
generated
vendored
Normal file
@ -0,0 +1,61 @@
|
||||
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
|
||||
}
|
||||
56
static/js/ketcher2/node_modules/generate-function/package.json
generated
vendored
Normal file
56
static/js/ketcher2/node_modules/generate-function/package.json
generated
vendored
Normal file
@ -0,0 +1,56 @@
|
||||
{
|
||||
"_from": "generate-function@^2.0.0",
|
||||
"_id": "generate-function@2.0.0",
|
||||
"_inBundle": false,
|
||||
"_integrity": "sha1-aFj+fAlpt9TpCTM3ZHrHn2DfvnQ=",
|
||||
"_location": "/generate-function",
|
||||
"_phantomChildren": {},
|
||||
"_requested": {
|
||||
"type": "range",
|
||||
"registry": true,
|
||||
"raw": "generate-function@^2.0.0",
|
||||
"name": "generate-function",
|
||||
"escapedName": "generate-function",
|
||||
"rawSpec": "^2.0.0",
|
||||
"saveSpec": null,
|
||||
"fetchSpec": "^2.0.0"
|
||||
},
|
||||
"_requiredBy": [
|
||||
"/is-my-json-valid"
|
||||
],
|
||||
"_resolved": "https://registry.npmjs.org/generate-function/-/generate-function-2.0.0.tgz",
|
||||
"_shasum": "6858fe7c0969b7d4e9093337647ac79f60dfbe74",
|
||||
"_spec": "generate-function@^2.0.0",
|
||||
"_where": "/home/manfred/enviPath/ketcher2/ketcher/node_modules/is-my-json-valid",
|
||||
"author": {
|
||||
"name": "Mathias Buus"
|
||||
},
|
||||
"bugs": {
|
||||
"url": "https://github.com/mafintosh/generate-function/issues"
|
||||
},
|
||||
"bundleDependencies": false,
|
||||
"deprecated": false,
|
||||
"description": "Module that helps you write generated functions in Node",
|
||||
"devDependencies": {
|
||||
"tape": "^2.13.4"
|
||||
},
|
||||
"homepage": "https://github.com/mafintosh/generate-function",
|
||||
"keywords": [
|
||||
"generate",
|
||||
"code",
|
||||
"generation",
|
||||
"function",
|
||||
"performance"
|
||||
],
|
||||
"license": "MIT",
|
||||
"main": "index.js",
|
||||
"name": "generate-function",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://github.com/mafintosh/generate-function.git"
|
||||
},
|
||||
"scripts": {
|
||||
"test": "tape test.js"
|
||||
},
|
||||
"version": "2.0.0"
|
||||
}
|
||||
33
static/js/ketcher2/node_modules/generate-function/test.js
generated
vendored
Normal file
33
static/js/ketcher2/node_modules/generate-function/test.js
generated
vendored
Normal file
@ -0,0 +1,33 @@
|
||||
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()
|
||||
})
|
||||
Reference in New Issue
Block a user