[Pkg-javascript-commits] [node-constantinople] 01/02: Imported Upstream version 2.0.0

Leo Iannacone l3on-guest at moszumanska.debian.org
Tue May 27 09:24:46 UTC 2014


This is an automated email from the git hooks/post-receive script.

l3on-guest pushed a commit to branch master
in repository node-constantinople.

commit 3a77acb3fa9b98f1ef6505b6089f2860b05560a3
Author: Leo Iannacone <l3on at ubuntu.com>
Date:   Thu May 8 00:23:36 2014 +0200

    Imported Upstream version 2.0.0
---
 .gitattributes | 22 ++++++++++++++++++++++
 .travis.yml    |  4 ++++
 LICENSE        | 19 +++++++++++++++++++
 README.md      | 42 ++++++++++++++++++++++++++++++++++++++++++
 index.js       | 40 ++++++++++++++++++++++++++++++++++++++++
 package.json   | 21 +++++++++++++++++++++
 test/index.js  | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 7 files changed, 205 insertions(+)

diff --git a/.gitattributes b/.gitattributes
new file mode 100644
index 0000000..412eeda
--- /dev/null
+++ b/.gitattributes
@@ -0,0 +1,22 @@
+# Auto detect text files and perform LF normalization
+* text=auto
+
+# Custom for Visual Studio
+*.cs     diff=csharp
+*.sln    merge=union
+*.csproj merge=union
+*.vbproj merge=union
+*.fsproj merge=union
+*.dbproj merge=union
+
+# Standard to msysgit
+*.doc	 diff=astextplain
+*.DOC	 diff=astextplain
+*.docx diff=astextplain
+*.DOCX diff=astextplain
+*.dot  diff=astextplain
+*.DOT  diff=astextplain
+*.pdf  diff=astextplain
+*.PDF	 diff=astextplain
+*.rtf	 diff=astextplain
+*.RTF	 diff=astextplain
diff --git a/.travis.yml b/.travis.yml
new file mode 100644
index 0000000..a12e3f0
--- /dev/null
+++ b/.travis.yml
@@ -0,0 +1,4 @@
+language: node_js
+node_js:
+  - "0.8"
+  - "0.10"
\ No newline at end of file
diff --git a/LICENSE b/LICENSE
new file mode 100644
index 0000000..35cc606
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,19 @@
+Copyright (c) 2013 Forbes Lindesay
+
+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/README.md b/README.md
new file mode 100644
index 0000000..7585bc8
--- /dev/null
+++ b/README.md
@@ -0,0 +1,42 @@
+# constantinople
+
+Determine whether a JavaScript expression evaluates to a constant (using UglifyJS).  Here it is assumed to be safe to underestimate how constant something is.
+
+[![Build Status](https://travis-ci.org/ForbesLindesay/constantinople.png?branch=master)](https://travis-ci.org/ForbesLindesay/constantinople)
+[![Dependency Status](https://gemnasium.com/ForbesLindesay/constantinople.png)](https://gemnasium.com/ForbesLindesay/constantinople)
+[![NPM version](https://badge.fury.io/js/constantinople.png)](http://badge.fury.io/js/constantinople)
+
+## Installation
+
+    npm install constantinople
+
+## Usage
+
+```js
+var isConstant = require('constantinople')
+
+if (isConstant('"foo" + 5')) {
+  console.dir(isConstant.toConstant('"foo" + 5'))
+}
+if (isConstant('Math.floor(10.5)', {Math: Math})) {
+  console.dir(isConstant.toConstant('Math.floor(10.5)', {Math: Math}))
+}
+```
+
+## API
+
+### isConstant(src, [constants])
+
+Returns `true` if `src` evaluates to a constant, `false` otherwise.  It will also return `false` if there is a syntax error, which makes it safe to use on potentially ES6 code.
+
+Constants is an object mapping strings to values, where those values should be treated as constants.  Note that this makes it a pretty bad idea to have `Math` in there if the user might make use of `Math.random` and a pretty bad idea to have `Date` in there.
+
+### toConstant(src, [constants])
+
+Returns the value resulting from evaluating `src`.  This method throws an error if the expression is not constant.  e.g. `toConstant("Math.random()")` would throw an error.
+
+Constants is an object mapping strings to values, where those values should be treated as constants.  Note that this makes it a pretty bad idea to have `Math` in there if the user might make use of `Math.random` and a pretty bad idea to have `Date` in there.
+
+## License
+
+  MIT
\ No newline at end of file
diff --git a/index.js b/index.js
new file mode 100644
index 0000000..9185bfe
--- /dev/null
+++ b/index.js
@@ -0,0 +1,40 @@
+'use strict'
+
+var uglify = require('uglify-js')
+
+var lastSRC = '(null)'
+var lastRes = true
+var lastConstants = undefined;
+
+module.exports = isConstant
+function isConstant(src, constants) {
+  src = '(' + src + ')'
+  if (lastSRC === src && lastConstants === constants) return lastRes
+  lastSRC = src
+  try {
+    return lastRes = (detect(src).filter(function (key) {
+      return !constants || !(key in constants)
+    }).length === 0)
+  } catch (ex) {
+    return lastRes = false
+  }
+}
+isConstant.isConstant = isConstant
+
+isConstant.toConstant = toConstant
+function toConstant(src, constants) {
+  if (!isConstant(src, constants)) throw new Error(JSON.stringify(src) + ' is not constant.')
+  return Function(Object.keys(constants || {}).join(','), 'return (' + src + ')').apply(null, Object.keys(constants || {}).map(function (key) {
+    return constants[key];
+  }));
+}
+
+function detect(src) {
+  var ast = uglify.parse(src.toString())
+  ast.figure_out_scope()
+  var globals = ast.globals
+    .map(function (node, name) {
+      return name
+    })
+  return globals
+}
\ No newline at end of file
diff --git a/package.json b/package.json
new file mode 100644
index 0000000..ca75417
--- /dev/null
+++ b/package.json
@@ -0,0 +1,21 @@
+{
+  "name": "constantinople",
+  "version": "2.0.0",
+  "description": "Determine whether a JavaScript expression evaluates to a constant (using UglifyJS)",
+  "keywords": [],
+  "dependencies": {
+    "uglify-js": "~2.4.0"
+  },
+  "devDependencies": {
+    "mocha": "*"
+  },
+  "scripts": {
+    "test": "mocha -R spec"
+  },
+  "repository": {
+    "type": "git",
+    "url": "https://github.com/ForbesLindesay/constantinople.git"
+  },
+  "author": "ForbesLindesay",
+  "license": "MIT"
+}
\ No newline at end of file
diff --git a/test/index.js b/test/index.js
new file mode 100644
index 0000000..dfeb123
--- /dev/null
+++ b/test/index.js
@@ -0,0 +1,57 @@
+'use strict'
+
+var assert = require('assert')
+var constaninople = require('../')
+
+describe('isConstant(src)', function () {
+  it('handles "[5 + 3 + 10]"', function () {
+    assert(constaninople.isConstant('[5 + 3 + 10]') === true)
+  })
+  it('handles "/[a-z]/.test(\'a\')"', function () {
+    assert(constaninople.isConstant('/[a-z]/.test(\'a\')') === true)
+  })
+  it('handles "{\'class\': [(\'data\')]}"', function () {
+    assert(constaninople.isConstant('{\'class\': [(\'data\')]}') === true)
+  })
+  it('handles "Math.random()"', function () {
+    assert(constaninople.isConstant('Math.random()') === false)
+  })
+  it('handles "Math.random("', function () {
+    assert(constaninople.isConstant('Math.random(') === false)
+  })
+  it('handles "Math.floor(10.5)" with {Math: Math} as constants', function () {
+    assert(constaninople.isConstant('Math.floor(10.5)', {Math: Math}) === true)
+  })
+})
+
+
+describe('toConstant(src)', function () {
+  it('handles "[5 + 3 + 10]"', function () {
+    assert.deepEqual(constaninople.toConstant('[5 + 3 + 10]'), [5 + 3 + 10])
+  })
+  it('handles "/[a-z]/.test(\'a\')"', function () {
+    assert(constaninople.toConstant('/[a-z]/.test(\'a\')') === true)
+  })
+  it('handles "{\'class\': [(\'data\')]}"', function () {
+    assert.deepEqual(constaninople.toConstant('{\'class\': [(\'data\')]}'), {'class': ['data']})
+  })
+  it('handles "Math.random()"', function () {
+    try {
+      constaninople.toConstant('Math.random()')
+    } catch (ex) {
+      return
+    }
+    assert(false, 'Math.random() should result in an error')
+  })
+  it('handles "Math.random("', function () {
+    try {
+      constaninople.toConstant('Math.random(')
+    } catch (ex) {
+      return
+    }
+    assert(false, 'Math.random( should result in an error')
+  })
+  it('handles "Math.floor(10.5)" with {Math: Math} as constants', function () {
+    assert(constaninople.toConstant('Math.floor(10.5)', {Math: Math}) === 10)
+  })
+})
\ No newline at end of file

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-constantinople.git



More information about the Pkg-javascript-commits mailing list