1. constantinople
Determine whether a JavaScript expression evaluates to a constant
constantinople
Package: constantinople
Created by: ForbesLindesay
Last modified: Mon, 13 Jun 2022 22:13:13 GMT
Version: 4.0.1
License: MIT
Downloads: 7,460,285
Repository: https://github.com/ForbesLindesay/constantinople

Install

npm install constantinople
yarn add constantinople

constantinople

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
Dependency Status
NPM version

Installation

npm install constantinople

Usage

 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, [options]])

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.

Options are directly passed-through to Babylon.

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.

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.

Options are directly passed-through to Babylon.

License

MIT

RELATED POST

10 Must-Know Windows Shortcuts That Will Save You Time

10 Must-Know Windows Shortcuts That Will Save You Time

Arrays vs Linked Lists: Which is Better for Memory Management in Data Structures?

Arrays vs Linked Lists: Which is Better for Memory Management in Data Structures?

Navigating AWS Networking: Essential Hacks for Smooth Operation

Navigating AWS Networking: Essential Hacks for Smooth Operation

Achieving Stunning Visuals with Unity's Global Illumination

Achieving Stunning Visuals with Unity's Global Illumination

Nim's Hidden Gems: Lesser-known Features for Writing Efficient Code

Nim's Hidden Gems: Lesser-known Features for Writing Efficient Code