You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 7-8.RSVP
Socket
Socket
Sign inDemoInstall

constantinople

Package Overview
Dependencies
Maintainers
1
Versions
13
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 1.0.2 to 2.0.0

17

index.js

@@ -7,10 +7,13 @@ 'use strict'

var lastRes = true
var lastConstants = undefined;
module.exports = isConstant
function isConstant(src) {
function isConstant(src, constants) {
src = '(' + src + ')'
if (lastSRC === src) return lastRes
if (lastSRC === src && lastConstants === constants) return lastRes
lastSRC = src
try {
return lastRes = (detect(src).length === 0)
return lastRes = (detect(src).filter(function (key) {
return !constants || !(key in constants)
}).length === 0)
} catch (ex) {

@@ -23,5 +26,7 @@ return lastRes = false

isConstant.toConstant = toConstant
function toConstant(src) {
if (!isConstant(src)) throw new Error(JSON.stringify(src) + ' is not constant.')
return Function('return (' + src + ')')()
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];
}));
}

@@ -28,0 +33,0 @@

{
"name": "constantinople",
"version": "1.0.2",
"version": "2.0.0",
"description": "Determine whether a JavaScript expression evaluates to a constant (using UglifyJS)",

@@ -5,0 +5,0 @@ "keywords": [],

@@ -21,2 +21,5 @@ # constantinople

}
if (isConstant('Math.floor(10.5)', {Math: Math})) {
console.dir(isConstant.toConstant('Math.floor(10.5)', {Math: Math}))
}
```

@@ -26,12 +29,16 @@

### isConstant(src)
### 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.
### toConstant(src)
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

@@ -22,2 +22,5 @@ 'use strict'

})
it('handles "Math.floor(10.5)" with {Math: Math} as constants', function () {
assert(constaninople.isConstant('Math.floor(10.5)', {Math: Math}) === true)
})
})

@@ -52,2 +55,5 @@

})
it('handles "Math.floor(10.5)" with {Math: Math} as constants', function () {
assert(constaninople.toConstant('Math.floor(10.5)', {Math: Math}) === 10)
})
})
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc