pax_global_header00006660000000000000000000000064123016664340014517gustar00rootroot0000000000000052 comment=e8edfd5ed76ff7f7e2cc5a0f6c9ae1371fe5b20f constantinople-2.0.0/000077500000000000000000000000001230166643400145565ustar00rootroot00000000000000constantinople-2.0.0/.gitattributes000066400000000000000000000007431230166643400174550ustar00rootroot00000000000000# 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 constantinople-2.0.0/.gitignore000066400000000000000000000001371230166643400165470ustar00rootroot00000000000000lib-cov *.seed *.log *.csv *.dat *.out *.pid *.gz pids logs results npm-debug.log node_modules constantinople-2.0.0/.travis.yml000066400000000000000000000000571230166643400166710ustar00rootroot00000000000000language: node_js node_js: - "0.8" - "0.10"constantinople-2.0.0/LICENSE000066400000000000000000000020421230166643400155610ustar00rootroot00000000000000Copyright (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.constantinople-2.0.0/README.md000066400000000000000000000034631230166643400160430ustar00rootroot00000000000000# 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 MITconstantinople-2.0.0/index.js000066400000000000000000000020431230166643400162220ustar00rootroot00000000000000'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 }constantinople-2.0.0/package.json000066400000000000000000000007331230166643400170470ustar00rootroot00000000000000{ "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" }constantinople-2.0.0/test/000077500000000000000000000000001230166643400155355ustar00rootroot00000000000000constantinople-2.0.0/test/index.js000066400000000000000000000036121230166643400172040ustar00rootroot00000000000000'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) }) })