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

ranma

Package Overview
Dependencies
Maintainers
2
Versions
21
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ranma - npm Package Compare versions

Comparing version 0.0.3 to 0.0.4

24

cjsify.js

@@ -138,3 +138,5 @@ var type = require('./type');

//全局变量,包括全局函数
var gVars = context.getVars();
var gVars = context.getVars().map(function(v) {
return v.leaves()[0].token().content();
});
var gChildren = context.getChildren();

@@ -146,17 +148,27 @@ gChildren.forEach(function(child) {

});
//全局使用的未声明的变量
var reqs = context.getVids().map(function(v) {
return v.token().content();
}).filter(function(v) {
return !context.hasVar(v) && !context.hasChild(v);
});
var requires = '';
reqs.forEach(function(req) {
requires += 'var ' + req + ' = require("' + req + '");';
});
//没有全局变量赋值null,只有一个则直接赋给module.exports;否则将变量名作为key加上值组成hash赋给exports
if(gVars.length == 0) {
return code + ';module.exports = null;'
return requires + code;
}
else if(gVars.length == 1) {
return code + ';module.exports = ' + gVars[0].leaves()[0].token().content() + ';';
return requires + code + ';module.exports = ' + gVars[0] + ';';
}
else {
var res = code + ';';
var res = requires + code + ';';
gVars.forEach(function(v) {
v = v.leaves()[0].token().content();
res += 'exports["' + v + '"] = ' + 'v;';
res += 'exports["' + v + '"] = ' + v + ';';
});
return res;
}
}
};
{
"name": "ranma",
"version": "0.0.3",
"version": "0.0.4",
"description": "A converter between CommonJS/AMD/CMD/other",

@@ -5,0 +5,0 @@ "maintainers": [

@@ -142,2 +142,18 @@ var ranma = require('../ranma');

});
it('normal js with var', function() {
var res = ranma.cjsify('var a = 1;');
expect(res).to.eql('var a = 1;;module.exports = a;')
});
it('normal js with function', function() {
var res = ranma.cjsify('function a(){}');
expect(res).to.eql('function a(){};module.exports = a;')
});
it('normal js with deps', function() {
var res = ranma.cjsify('var a = b;');
expect(res).to.eql('var b = require("b");var a = b;;module.exports = a;');
});
it('normal js with multi global vars', function() {
var res = ranma.cjsify('var a = b, c = d;');
expect(res).to.eql('var b = require("b");var d = require("d");var a = b, c = d;;exports["a"] = a;exports["c"] = c;');
});
});

@@ -144,0 +160,0 @@ describe('cmdify', function() {

Sorry, the diff of this file is not supported yet

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