Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

extern-constantify

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

extern-constantify - npm Package Compare versions

Comparing version 0.1.0 to 0.2.0

.travis.yml

63

index.js

@@ -1,52 +0,15 @@

'use strict';
var transformTools = require('browserify-transform-tools');
var through = require('through2');
var esprima = require('esprima');
module.exports = function(file, opts) {
var buffer = '';
return through(transform, flush);
function transform(chunk, enc, cb) {
buffer += chunk;
cb();
}
function flush(cb) {
var tokens, deltas, tok, name, replacement, lineIdx, line, delta;
tokens = esprima.tokenize(buffer, {
loc: true
});
buffer = buffer.split('\n');
deltas = new Array(buffer.length);
for (var i=0,l=deltas.length;i<l;i++){deltas[i]=0;}
for (var i = 0, l = tokens.length; i < l; i++) {
tok = tokens[i];
console.log('[constantify]', tok);
if (tok.type === 'Identifier') {
name = tok.value;
if (opts.hasOwnProperty(name) && typeof opts[name] !== 'undefined') {
if (!isNaN(parseFloat(opts[name])) && isFinite(opts[name])) {
replacement = '' + opts[name];
} else {
replacement = '"' + opts[name] + '"';
}
if (tok.loc.start.line === tok.loc.end.line) {
lineIdx = tok.loc.start.line-1;
line = buffer[lineIdx];
delta = deltas[lineIdx];
console.log(line);
buffer[lineIdx] = line.substring(0 + delta, tok.loc.start.column + delta) + replacement + line.substring(tok.loc.end.column + delta);
deltas[lineIdx] = replacement.length - tok.value.length + delta;
} else {
this.emit('error', new Error('An identifier should not be split across more than one line'));
}
}
module.exports = transformTools.makeFalafelTransform('extern-constantify', {jsFilesOnly: true},
function(node, opts, done) {
var name = node.source(), data = opts.configData.config, replacement;
if (node.type === 'Identifier' && data.hasOwnProperty(name) && typeof data[name] !== 'undefined') {
if (!isNaN(parseFloat(data[name])) && isFinite(data[name])) {
replacement = '' + data[name];
} else {
replacement = '"' + data[name] + '"';
}
}
this.push(buffer.join('\n'));
cb();
}
};
node.update(replacement);
}
done();
});
{
"name": "extern-constantify",
"version": "0.1.0",
"version": "0.2.0",
"description": "a transform for browserify, which allows the replacement of (undeclared/implicit) CONSTANTS with values based on an external configuration",
"main": "index.js",
"scripts": {
"test": "echo 'Error: no tests written yet' && exit 1"
"test": "node ./tests.js | tap-spec"
},

@@ -15,3 +15,7 @@ "repository": {

"browserify",
"browserify-transform"
"browserify-transform",
"transform",
"constants",
"source",
"inline"
],

@@ -25,5 +29,8 @@ "author": "Adriaan Callaerts <adriaan.callaerts@gmail.com>",

"dependencies": {
"esprima": "^1.2.2",
"through2": "^0.6.3"
"browserify-transform-tools": "^1.2.1"
},
"devDependencies": {
"tap-spec": "^1.0.1",
"tape": "^3.0.1"
}
}

@@ -1,4 +0,85 @@

constantify
===========
extern-constantify
==================
Transform that allows does in-place replacement of global constants, without having to declare them or import them at every occurence
[![Build Status](https://travis-ci.org/call-a3/extern-constantify.svg?branch=master)](https://travis-ci.org/call-a3/extern-constantify)
[![Dependency Status](https://david-dm.org/call-a3/extern-constantify.svg)](https://david-dm.org/call-a3/extern-constantify) [![devDependency Status](https://david-dm.org/call-a3/extern-constantify/dev-status.svg)](https://david-dm.org/call-a3/extern-constantify#info=devDependencies)
Browserify transform that allows does in-place replacement of global constants, without having to declare them or import them at every occurence.
## Installation
[![extern-constantify](https://nodei.co/npm/extern-constantify.png?mini=true)](https://nodei.co/npm/extern-constantify)
## Usage
``` bash
browserify -t extern-constantify entry.js > bundle.js
```
## Example
For example, suppose you have different classes/objects in your project communicating by events/messages:
``` javascript
var Sender = function () {
this.emit('begin');
//do some stuff, reporting progress
this.emit('busy', progress);
//finalize and report success
this.emit('done');
};
var Receiver = function (sender) {
sender.on('begin', function(data) {
node.innerHTML = 'Starting...';
});
sender.on('busy', function(data) {
node.innerHTML = 'Work is ' + data + '% complete';
});
sender.on('done', function(data) {
node.innerHTML = 'Work is done';
});
};
```
Now suppose you later decide to change the names of these events to `start`, `progress` and `end`. You would have to look for the various occurences of the original literal strings in your code and replace them accordingly. This introduces a lot of room for error. Instead you could write this:
``` javascript
var Sender = function () {
this.emit(BEGIN_EVENT);
//do some stuff, reporting progress
this.emit(BUSY_EVENT, progress);
//finalize and report success
this.emit(END_EVENT);
};
var Receiver = function (sender) {
sender.on(BEGIN_EVENT, function(data) {
node.innerHTML = 'Starting...';
});
sender.on(BUSY_EVENT, function(data) {
node.innerHTML = 'Work is ' + data + '% complete';
});
sender.on(END_EVENT, function(data) {
node.innerHTML = 'Work is done';
});
};
```
and provide the following configuration in your `package.json` file
``` javascript
{
"extern-constantify": {
"BEGIN_EVENT": "begin",
"BUSY_EVENT": "busy",
"END_EVENT": "done"
}
}
```
The aforementioned change would then only require one edit in your configuration instead of the multiple edits that were previously required.
The matching happens case-sensitively, so you can avoid naming conflicts by uppercasing all characters of a constant name. This is the recommended coding style, however it is not mandatory.
## License
[MIT](http://github.com/call-a3/extern-constantify/blob/master/LICENSE)
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