Socket
Socket
Sign inDemoInstall

constantinople

Package Overview
Dependencies
Maintainers
2
Versions
13
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

constantinople - npm Package Compare versions

Comparing version 3.1.0 to 3.1.1

.prettierrc

17

package.json
{
"name": "constantinople",
"version": "3.1.0",
"version": "3.1.1",
"main": "lib/index.js",
"types": "lib/index.d.ts",
"description": "Determine whether a JavaScript expression evaluates to a constant (using acorn)",

@@ -11,9 +13,16 @@ "keywords": [

"dependencies": {
"acorn": "^3.1.0",
"is-expression": "^2.0.1"
"@types/babylon": "^6.16.2",
"babel-types": "^6.16.0",
"babylon": "^6.18.0",
"babylon-walk": "^1.0.2"
},
"devDependencies": {
"mocha": "*"
"@types/node": "^9.4.4",
"mocha": "*",
"typescript": "^2.7.1"
},
"scripts": {
"prepublish": "npm run build",
"build": "tsc",
"pretest": "npm run build",
"test": "mocha -R spec"

@@ -20,0 +29,0 @@ },

# constantinople
Determine whether a JavaScript expression evaluates to a constant (using acorn). Here it is assumed to be safe to underestimate how constant something is.
Determine whether a JavaScript expression evaluates to a constant (using Babylon). Here it is assumed to be safe to underestimate how constant something is.
[![Build Status](https://img.shields.io/travis/ForbesLindesay/constantinople/master.svg)](https://travis-ci.org/ForbesLindesay/constantinople)
[![Dependency Status](https://img.shields.io/david/ForbesLindesay/constantinople.svg)](https://david-dm.org/ForbesLindesay/constantinople)
[![Build Status](https://img.shields.io/travis/pugjs/constantinople/master.svg)](https://travis-ci.org/pugjs/constantinople)
[![Dependency Status](https://img.shields.io/david/pugjs/constantinople.svg)](https://david-dm.org/pugjs/constantinople)
[![NPM version](https://img.shields.io/npm/v/constantinople.svg)](https://www.npmjs.org/package/constantinople)

@@ -28,3 +28,3 @@

### isConstant(src, [constants])
### isConstant(src, [constants, [options]])

@@ -35,4 +35,6 @@ 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])
Options are directly passed-through to [Babylon](https://github.com/babel/babylon#options).
### toConstant(src, [constants, [options]])
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.

@@ -42,4 +44,6 @@

Options are directly passed-through to [Babylon](https://github.com/babel/babylon#options).
## License
MIT

@@ -1,71 +0,86 @@

'use strict'
'use strict';
var assert = require('assert')
var constaninople = require('../')
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)
})
it('handles "this.myVar"', function () {
assert(constaninople.isConstant('this.myVar') === false)
})
it('handles "(function () { while (true); return 10; }())"', function () {
assert(constaninople.isConstant('(function () { while (true); return 10; }())') === false)
})
})
describe('isConstant(src)', function() {
it('handles "[5 + 3 + 10]"', function() {
assert(constaninople.isConstant('[5 + 3 + 10]') === true);
});
it('handles "/[a-z]/i.test(\'a\')"', function() {
assert(constaninople.isConstant("/[a-z]/i.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);
});
it('handles "this.myVar"', function() {
assert(constaninople.isConstant('this.myVar') === false);
});
it('handles "(function () { while (true); return 10; }())"', function() {
assert(
constaninople.isConstant(
'(function () { while (true); return 10; }())',
) === false,
);
});
it('handles "({}).toString.constructor("console.log(1)")()"', function() {
assert(
constaninople.isConstant(
'({}).toString.constructor("console.log(1)")()',
) === false,
);
});
});
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 () {
describe('toConstant(src)', function() {
it('handles "[5 + 3 + 10]"', function() {
assert.deepEqual(constaninople.toConstant('[5 + 3 + 10]'), [5 + 3 + 10]);
});
it('handles "/[a-z]/i.test(\'a\')"', function() {
assert(constaninople.toConstant("/[a-z]/i.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()')
constaninople.toConstant('Math.random()');
} catch (ex) {
return
return;
}
assert(false, 'Math.random() should result in an error')
})
it('handles "Math.random("', function () {
assert(false, 'Math.random() should result in an error');
});
it('handles "Math.random("', function() {
try {
constaninople.toConstant('Math.random(')
constaninople.toConstant('Math.random(');
} catch (ex) {
return
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)
})
it('handles "(function () { while (true); return 10; }())"', function () {
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);
});
it('handles "(function () { while (true); return 10; }())"', function() {
try {
constaninople.toConstant('(function () { while (true); return 10; }())')
constaninople.toConstant('(function () { while (true); return 10; }())');
} catch (ex) {
return
return;
}
assert(false, '(function () { while (true); return 10; }()) should result in an error')
})
})
assert(
false,
'(function () { while (true); return 10; }()) should result in an error',
);
});
});
SocketSocket SOC 2 Logo

Product

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

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc