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

cmd-helper

Package Overview
Dependencies
Maintainers
1
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

cmd-helper - npm Package Compare versions

Comparing version 0.2.6 to 0.2.7

2

docs/ast.md

@@ -10,3 +10,3 @@ # ast

```js
var ast = require('cmd-util').ast
var ast = require('cmd-helper').ast
```

@@ -13,0 +13,0 @@

@@ -10,3 +10,3 @@ # css

```js
var css = require('cmd-util').css
var css = require('cmd-helper').css
```

@@ -13,0 +13,0 @@

@@ -8,3 +8,3 @@ # iduri

```js
var iduri = require('cmd-util').iduri
var iduri = require('cmd-helper').iduri
```

@@ -11,0 +11,0 @@

@@ -13,4 +13,5 @@ /**

function getAst(ast, options){
if (isString(ast)) return UglifyJS.parse(ast, options || {});
return ast;
if (isString(ast)) return UglifyJS.parse(ast, options || {});
return ast;
}

@@ -31,20 +32,22 @@ exports.getAst = getAst;

function parse(ast){
var walker, meta = [];
var walker, meta = [];
ast = getAst(ast);
ast = getAst(ast);
walker = new UglifyJS.TreeWalker(function (node){
// don't collect dependencies in the define in define
if (node instanceof UglifyJS.AST_Call && node.expression.name === 'define') {
var define = getDefine(node);
walker = new UglifyJS.TreeWalker(function (node){
var define;
if (define) meta.push(define);
// don't collect dependencies in the define in define
if (node instanceof UglifyJS.AST_Call && node.expression.name === 'define' && node.args.length) {
define = getDefine(node);
return true;
}
});
if (define) meta.push(define);
ast.walk(walker);
return true;
}
});
return meta;
ast.walk(walker);
return meta;
}

@@ -57,3 +60,3 @@ exports.parse = parse;

exports.parseFirst = function (ast){
return parse(ast)[0];
return parse(ast)[0];
};

@@ -83,95 +86,95 @@

function modify(ast, options){
var idfn, depfn, requirefn,
asyncfn, alias, trans;
var idfn, depfn, requirefn,
asyncfn, alias, trans;
ast = getAst(ast);
options = options || {};
ast = getAst(ast);
options = options || {};
if (isFunction(options)) {
idfn = depfn = requirefn = asyncfn = options;
} else {
idfn = options.id;
depfn = options.dependencies;
requirefn = options.require;
asyncfn = options.async;
}
if (isFunction(options)) {
idfn = depfn = requirefn = asyncfn = options;
} else {
idfn = options.id;
depfn = options.dependencies;
requirefn = options.require;
asyncfn = options.async;
}
if (isObject(depfn)) {
alias = depfn;
depfn = function (value){
if (alias.hasOwnProperty(value)) {
return alias[value];
} else {
return value;
}
};
}
if (isObject(depfn)) {
alias = depfn;
depfn = function (value){
if (alias.hasOwnProperty(value)) {
return alias[value];
} else {
return value;
}
};
}
trans = new UglifyJS.TreeTransformer(function (node){
// modify define
if ((idfn || depfn) && node instanceof UglifyJS.AST_Call
&& node.expression.name === 'define' && node.args.length) {
var elements, value, args = [],
meta = getDefine(node);
trans = new UglifyJS.TreeTransformer(function (node){
// modify define
if ((idfn || depfn) && node instanceof UglifyJS.AST_Call
&& node.expression.name === 'define' && node.args.length) {
var elements, value, args = [],
meta = getDefine(node);
if (idfn && isFunction(idfn)) {
meta.id = idfn(meta.id);
} else if (idfn && isString(idfn)) {
meta.id = idfn;
}
if (idfn && isFunction(idfn)) {
meta.id = idfn(meta.id);
} else if (idfn && isString(idfn)) {
meta.id = idfn;
}
if (meta.id) {
args.push(new UglifyJS.AST_String({
value: meta.id
}));
}
if (meta.id) {
args.push(new UglifyJS.AST_String({
value: meta.id
}));
}
// modify dependencies
if (meta.dependencyNode && !depfn) {
args.push(meta.dependencyNode);
} else if (depfn) {
elements = [];
// modify dependencies
if (meta.dependencyNode && !depfn) {
args.push(meta.dependencyNode);
} else if (depfn) {
elements = [];
if (meta.dependencies.length && isFunction(depfn)) {
for (var i = 0, len = meta.dependencies.length; i < len; i++) {
value = depfn(meta.dependencies[i]);
if (meta.dependencies.length && isFunction(depfn)) {
for (var i = 0, len = meta.dependencies.length; i < len; i++) {
value = depfn(meta.dependencies[i]);
if (value) elements.push(new UglifyJS.AST_String({value: value}));
}
} else if (isString(depfn)) {
elements = [new UglifyJS.AST_String({value: depfn})];
} else if (Array.isArray(depfn)) {
elements = depfn.map(function (value){
return new UglifyJS.AST_String({
value: value
});
});
}
if (value) elements.push(new UglifyJS.AST_String({ value: value }));
}
} else if (isString(depfn)) {
elements = [new UglifyJS.AST_String({ value: depfn })];
} else if (Array.isArray(depfn)) {
elements = depfn.map(function (value){
return new UglifyJS.AST_String({
value: value
});
});
}
if (meta.dependencyNode) {
args.push(new UglifyJS.AST_Array({
start: meta.dependencyNode.start,
end: meta.dependencyNode.end,
elements: elements
}));
} else {
args.push(new UglifyJS.AST_Array({elements: elements}));
}
} else {
args.push(new UglifyJS.AST_Array({elements: []}));
}
if (meta.dependencyNode) {
args.push(new UglifyJS.AST_Array({
start: meta.dependencyNode.start,
end: meta.dependencyNode.end,
elements: elements
}));
} else {
args.push(new UglifyJS.AST_Array({ elements: elements }));
}
} else {
args.push(new UglifyJS.AST_Array({ elements: [] }));
}
if (meta.factory) args.push(meta.factory);
if (meta.factory) args.push(meta.factory);
node.args = args;
node.args = args;
return node;
}
});
return node;
}
});
ast = ast.transform(trans);
ast = ast.transform(trans);
if (requirefn || asyncfn) ast = replaceRequire(ast, requirefn, asyncfn);
if (requirefn || asyncfn) ast = replaceRequire(ast, requirefn, asyncfn);
return ast;
return ast;
}

@@ -181,55 +184,50 @@ exports.modify = modify;

function getDefine(node){
var id, child, factory, firstChild,
secondChild, dependencyNode, dependencies = [];
var id, child, factory, firstChild,
secondChild, dependencyNode, dependencies = [];
// don't collect dependencies in the define in define
if (node instanceof UglifyJS.AST_Call && node.expression.name === 'define') {
if (!node.args || !node.args.length) return null;
if (node.args.length === 1) {
factory = node.args[0];
if (node.args.length === 1) {
factory = node.args[0];
if (factory instanceof UglifyJS.AST_Function) dependencies = getRequires(factory);
} else if (node.args.length === 2) {
factory = node.args[1];
child = node.args[0];
if (factory instanceof UglifyJS.AST_Function) dependencies = getRequires(factory);
} else if (node.args.length === 2) {
factory = node.args[1];
child = node.args[0];
if (child instanceof UglifyJS.AST_Array) {
// define([], function(){});
dependencies = map(child.elements, function (el){
if (el instanceof UglifyJS.AST_String) return el.getValue();
});
dependencyNode = child;
}
if (child instanceof UglifyJS.AST_Array) {
// define([], function(){});
dependencies = map(child.elements, function (el){
if (el instanceof UglifyJS.AST_String) return el.getValue();
});
dependencyNode = child;
}
if (child instanceof UglifyJS.AST_String) {
// define('id', function() {});
id = child.getValue();
dependencies = getRequires(factory);
}
} else {
factory = node.args[2];
firstChild = node.args[0];
secondChild = node.args[1];
if (child instanceof UglifyJS.AST_String) {
// define('id', function() {});
id = child.getValue();
dependencies = getRequires(factory);
}
} else {
factory = node.args[2];
firstChild = node.args[0];
secondChild = node.args[1];
if (firstChild instanceof UglifyJS.AST_String) id = firstChild.getValue();
if (firstChild instanceof UglifyJS.AST_String) id = firstChild.getValue();
if (secondChild instanceof UglifyJS.AST_Array) {
dependencies = map(secondChild.elements, function (el){
if (el instanceof UglifyJS.AST_String) return el.getValue();
});
dependencyNode = secondChild;
} else if ((secondChild instanceof UglifyJS.AST_Null)
|| (secondChild instanceof UglifyJS.AST_Undefined)) {
if (factory instanceof UglifyJS.AST_Function) dependencies = getRequires(factory);
}
}
if (secondChild instanceof UglifyJS.AST_Array) {
dependencies = map(secondChild.elements, function (el){
if (el instanceof UglifyJS.AST_String) return el.getValue();
});
dependencyNode = secondChild;
} else if ((secondChild instanceof UglifyJS.AST_Null)
|| (secondChild instanceof UglifyJS.AST_Undefined)) {
if (factory instanceof UglifyJS.AST_Function) dependencies = getRequires(factory);
}
}
return {
id: id,
dependencies: dependencies,
factory: factory,
dependencyNode: dependencyNode
};
return {
id: id,
dependencies: dependencies,
factory: factory,
dependencyNode: dependencyNode
};
}

@@ -252,23 +250,23 @@

function getRequires(ast){
var walker, deps = [];
var walker, deps = [];
ast = getAst(ast);
ast = getAst(ast);
walker = new UglifyJS.TreeWalker(function (node){
if (node instanceof UglifyJS.AST_Call && node.expression.name === 'require') {
var child, args = node.expression.args || node.args;
walker = new UglifyJS.TreeWalker(function (node){
if (node instanceof UglifyJS.AST_Call && node.expression.name === 'require') {
var child, args = node.expression.args || node.args;
if (args && args.length === 1) {
child = args[0];
if (args && args.length === 1) {
child = args[0];
if (child instanceof UglifyJS.AST_String) deps.push(child.getValue());
}
if (child instanceof UglifyJS.AST_String) deps.push(child.getValue());
}
return true;
}
});
return true;
}
});
ast.walk(walker);
ast.walk(walker);
return deps;
return deps;
}

@@ -299,81 +297,81 @@

function replaceRequire(ast, requirefn, asyncfn){
var makeFunction, replaceChild, trans;
var makeFunction, replaceChild, trans;
ast = getAst(ast);
ast = getAst(ast);
makeFunction = function (fn){
if (isFunction(fn)) return fn;
makeFunction = function (fn){
if (isFunction(fn)) return fn;
if (isObject(fn)) {
var alias = fn;
if (isObject(fn)) {
var alias = fn;
return function (value){
if (alias.hasOwnProperty(value)) {
return alias[value];
} else {
return value;
}
};
return function (value){
if (alias.hasOwnProperty(value)) {
return alias[value];
} else {
return value;
}
};
}
return function (value){
return value;
};
return function (value){
return value;
};
};
replaceChild = function (node, fn){
var child, args = node.args[0],
children = args instanceof UglifyJS.AST_Array ? args.elements : [args];
replaceChild = function (node, fn){
var child, args = node.args[0],
children = args instanceof UglifyJS.AST_Array ? args.elements : [args];
for (var i = 0, len = children.length; i < len; i++) {
child = children[i];
for (var i = 0, len = children.length; i < len; i++) {
child = children[i];
if (child instanceof UglifyJS.AST_String) child.value = fn(child.getValue());
}
};
if (child instanceof UglifyJS.AST_String) child.value = fn(child.getValue());
}
};
requirefn = makeFunction(requirefn);
asyncfn = makeFunction(asyncfn);
requirefn = makeFunction(requirefn);
asyncfn = makeFunction(asyncfn);
trans = new UglifyJS.TreeTransformer(function (node){
// require('foo')
if (requirefn && node instanceof UglifyJS.AST_Call
&& node.expression.name === 'require' && node.args.length) {
return replaceChild(node, requirefn);
}
trans = new UglifyJS.TreeTransformer(function (node){
// require('foo')
if (requirefn && node instanceof UglifyJS.AST_Call
&& node.expression.name === 'require' && node.args.length) {
return replaceChild(node, requirefn);
}
// require.async('foo', function(foo){ //TODO callback })
if (asyncfn && node instanceof UglifyJS.AST_Call && node.start.value === 'require'
&& node.expression.property === 'async' && node.args.length) {
return replaceChild(node, asyncfn);
}
});
// require.async('foo', function(foo){ //TODO callback })
if (asyncfn && node instanceof UglifyJS.AST_Call && node.start.value === 'require'
&& node.expression.property === 'async' && node.args.length) {
return replaceChild(node, asyncfn);
}
});
return ast.transform(trans);
return ast.transform(trans);
}
function isString(str){
return typeof str === 'string';
return typeof str === 'string';
}
function isFunction(fn){
return typeof fn === 'function';
return typeof fn === 'function';
}
function isObject(obj){
return (typeof obj === 'object' && !Array.isArray(obj));
function isObject(object){
return typeof object === 'object' && !Array.isArray(object);
}
function map(obj, fn, context){
var results = [];
function map(object, fn, context){
var results = [];
if (obj === null) return results;
if (object === null) return results;
if (obj.map === Array.prototype.map) return obj.map(fn, context);
if (object.map === Array.prototype.map) return object.map(fn, context);
for (var i = 0; i < obj.length; i++) {
results[i] = fn.call(context, obj[i], i, obj);
}
for (var i = 0; i < object.length; i++) {
results[i] = fn.call(context, object[i], i, object);
}
return results;
return results;
}

@@ -9,9 +9,9 @@ /*

var trimRE = /^(\r\n|\r|\n)|(\r\n|\r|\n)$/g,
endblockRE = /^\/\*!\s*endblock(?:\s*|\s+(.+?)\s*)\*\/$/,
importRE = /^@import\s+url\s*\((['"]?)(.+?)\1\);?$|^@import\s+(['"])(.+?)\3;?$/,
tokensRE = /(\/\*[^*]*\*+([^/*][^*]*\*+)*\/)|(@import\s+url\s*\(.+?\);?|@import\s+(['"]).+?\4;?)|(([\S\s](?!@import\s|\/\*))+([\S\s](?=@import\s|\/\*))*)|(([\S\s](?!@import\s|\/\*))*([\S\s](?=@import\s|\/\*))+)/g;
endblockRE = /^\/\*!\s*endblock(?:\s*|\s+(.+?)\s*)\*\/$/,
importRE = /^@import\s+url\s*\((['"]?)(.+?)\1\);?$|^@import\s+(['"])(.+?)\3;?$/,
tokensRE = /(\/\*[^*]*\*+([^/*][^*]*\*+)*\/)|(@import\s+url\s*\(.+?\);?|@import\s+(['"]).+?\4;?)|(([\S\s](?!@import\s|\/\*))+([\S\s](?=@import\s|\/\*))*)|(([\S\s](?!@import\s|\/\*))*([\S\s](?=@import\s|\/\*))+)/g;
// is function
function isFuncrion(fn){
return typeof fn === 'function';
return typeof fn === 'function';
}

@@ -21,3 +21,3 @@

function isString(str){
return typeof str === 'string';
return typeof str === 'string';
}

@@ -28,20 +28,23 @@

*/
exports.parse = function (code){
var rules;
function parse(code){
var rules;
if (!isString(code)) return [];
if (!isString(code)) return [];
rules = code.match(tokensRE);
rules = code.match(tokensRE);
return parseBlock(rules);
};
if (rules === null) return [];
return parseBlock(rules);
}
exports.parse = parse;
function match(text, key){
// /*! key value */
var re = new RegExp('^\\/\\*!\\s*' + key + '\\s+(.+?)\\s*\\*\\/$'),
m = text.match(re);
// /*! key value */
var re = new RegExp('^\\/\\*!\\s*' + key + '\\s+(.+?)\\s*\\*\\/$'),
m = text.match(re);
if (!m) return;
if (!m) return;
return m[1];
return m[1];
}

@@ -53,115 +56,114 @@

function parseBlock(rules){
var tree,
node = {
id: null,
type: 'block',
code: []
},
blockDepth = [];
var tree,
node = {
id: null,
type: 'block',
code: []
},
blockDepth = [];
/*
* recursive parse a block code string
*/
function parseString(rule, blockNode){
var childNode = blockNode.code[blockNode.code.length - 1];
/*
* recursive parse a block code string
*/
function parseString(rule, blockNode){
var childNode = blockNode.code[blockNode.code.length - 1];
if (childNode && childNode.type === 'string') {
childNode.code += rule;
} else {
blockNode.code.push({
type: 'string',
code: rule
});
}
if (childNode && childNode.type === 'string') {
childNode.code += rule;
} else {
blockNode.code.push({
type: 'string',
code: rule
});
}
}
// parse block
function parseInBlock(rule){
var id, blockNode, start,
end, imports, childNode;
// parse block
function parseInBlock(rule){
var id, blockNode, start,
end, imports, childNode;
blockNode = blockDepth[blockDepth.length - 1] || node;
blockNode = blockDepth[blockDepth.length - 1] || node;
if (rule.substr(0, 3) === '/*!') {
/*! start block id */
if (start = match(rule, 'block')) {
childNode = {
id: start,
type: 'block',
code: []
};
if (rule.substr(0, 3) === '/*!') {
/*! start block id */
if (start = match(rule, 'block')) {
childNode = {
id: start,
type: 'block',
code: []
};
blockDepth.push(childNode);
blockNode.code.push(childNode);
return;
}
blockDepth.push(childNode);
blockNode.code.push(childNode);
return;
}
/*! endblock id */
if (end = rule.match(endblockRE)) {
/*! endblock id */
if (end = rule.match(endblockRE)) {
if (!blockDepth.length) {
throw new SyntaxError('block indent error.');
}
if (!blockDepth.length) {
throw new SyntaxError('block indent error.');
}
id = end[1];
id = end[1];
// endblock tag closed error
if (id && (id !== blockNode.id)) {
blockDepth = [];
throw new SyntaxError('block indent error.');
}
// endblock tag closed error
if (id && (id !== blockNode.id)) {
blockDepth = [];
throw new SyntaxError('block indent error.');
}
blockDepth.pop();
return;
}
blockDepth.pop();
return;
}
if (imports = match(rule, 'import')) {
childNode = {
id: imports,
type: 'import'
};
if (imports = match(rule, 'import')) {
childNode = {
id: imports,
type: 'import'
};
blockNode.code.push(childNode);
return;
}
blockNode.code.push(childNode);
return;
}
if (!node.id && (id = match(rule, 'define'))) {
node.id = id;
return;
}
}
if (!node.id && (id = match(rule, 'define'))) {
node.id = id;
return;
}
}
if (rule.substr(0, 8) === '@import ') {
if (id = rule.match(importRE)) {
if (id = id[2] || id[4]) {
childNode = {
id: id,
type: 'import'
};
if (rule.substr(0, 8) === '@import ') {
if (id = rule.match(importRE)) {
if (id = id[2] || id[4]) {
childNode = {
id: id,
type: 'import'
};
node.code.push(childNode);
return;
}
}
node.code.push(childNode);
return;
}
parseString(rule, blockNode);
}
}
// parse syntax tree, notes: for loop faster than forEach
for (var i = 0, len = rules.length; i < len; i++) {
parseInBlock(rules[i]);
}
parseString(rule, blockNode);
}
// lost endblock tag
if (blockDepth.length) {
blockDepth = [];
throw new SyntaxError('block not finished.');
}
// parse syntax tree, notes: for loop faster than forEach
for (var i = 0, len = rules.length; i < len; i++) {
parseInBlock(rules[i]);
}
!node.id && delete node.id;
// lost endblock tag
if (blockDepth.length) {
blockDepth = [];
throw new SyntaxError('block not finished.');
}
tree = [node];
!node.id && delete node.id;
return tree;
tree = [node];
return tree;
}

@@ -173,23 +175,23 @@

exports.walk = function (code, fn){
if (!Array.isArray(code)) {
code = exports.parse(code);
}
if (!Array.isArray(code)) {
code = parse(code);
}
fn = isFuncrion(fn) ? fn : function (){};
fn = isFuncrion(fn) ? fn : function (){};
function walk(code){
var node;
function walk(code){
var node;
// if fn return false, it will stop the walk
if (Array.isArray(code)) {
for (var i = 0, len = code.length; i < len; i++) {
node = code[i];
// if fn return false, it will stop the walk
if (Array.isArray(code)) {
for (var i = 0, len = code.length; i < len; i++) {
node = code[i];
if (fn(node) !== false && node.type === 'block'
&& Array.isArray(node.code)) walk(node.code);
}
}
if (fn(node) !== false && node.type === 'block'
&& Array.isArray(node.code)) walk(node.code);
}
}
}
walk(code);
walk(code);
};

@@ -201,56 +203,56 @@

exports.stringify = function (code, filter){
var firstRun;
var firstRun;
if (!Array.isArray(code)) {
return isString(code) ? code : '';
}
if (!Array.isArray(code)) {
return isString(code) ? code : '';
}
firstRun = true;
filter = isFuncrion(filter) ? filter : false;
firstRun = true;
filter = isFuncrion(filter) ? filter : false;
function print(code, parent){
var cursor = '';
function print(code, parent){
var cursor = '';
function walk(node){
if (filter) {
var ret = filter(node, parent);
function walk(node){
if (filter) {
var ret = filter(node, parent);
if (ret === false) return;
if (ret === false) return;
if (ret && ret.type) {
node = ret;
}
}
switch (node.type) {
case 'string':
cursor += node.code;
break;
case 'import':
cursor += '/*! import ' + node.id + ' */';
break;
case 'block':
if (node.id) {
cursor += '/*! block ' + node.id + ' */'
+ (firstRun
? '\n' + print(node.code, node).replace(trimRE, '') + '\n'
: print(node.code, node))
+ '/*! endblock ' + node.id + ' */';
} else {
cursor = print(node.code, node);
}
break;
}
firstRun = false;
if (ret && ret.type) {
node = ret;
}
}
for (var i = 0, len = code.length; i < len; i++) {
walk(code[i]);
}
switch (node.type) {
case 'string':
cursor += node.code;
break;
case 'import':
cursor += '/*! import ' + node.id + ' */';
break;
case 'block':
if (node.id) {
cursor += '/*! block ' + node.id + ' */'
+ (firstRun
? '\n' + print(node.code, node).replace(trimRE, '') + '\n'
: print(node.code, node))
+ '/*! endblock ' + node.id + ' */';
} else {
cursor = print(node.code, node);
}
break;
}
return cursor;
firstRun = false;
}
return print(code);
for (var i = 0, len = code.length; i < len; i++) {
walk(code[i]);
}
return cursor;
}
return print(code);
};

@@ -9,41 +9,41 @@ /*

var template,
path = require('path'),
FILE_EXT = ['js', 'css'], // file extension
WIN_DIR_SEP_RE = /\\/g, // windows dir separators
INVALID_SLASH_RE = /^\/{3,}/, // invalid slash
DOT_RE = /\/\.\//g, // dot
DOUBLE_DOT_RE = /[^/]*[^./]+[^/]*\/\.\.\//, // double dot
DOUBLE_SLASH_RE = /([^:])\/{2,}/g, // double slash
TRIM_START_RELATIVE_RE = /^((?:\/{1,2})?(?:\.{1,2}\/)*)([^/].*|$)/, // trim relative path at start
VERSION_RE = /^(\d+\.){2}\d+$/, // version
RELATIVE_RE = /^\.{1,2}[\\/]/, // relative path
slice = Array.prototype.slice; // array slice
path = require('path'),
FILE_EXT = ['js', 'css'], // file extension
WIN_DIR_SEP_RE = /\\/g, // windows dir separators
INVALID_SLASH_RE = /^\/{3,}/, // invalid slash
DOT_RE = /\/\.\//g, // dot
DOUBLE_DOT_RE = /[^/]*[^./]+[^/]*\/\.\.\//, // double dot
DOUBLE_SLASH_RE = /([^:])\/{2,}/g, // double slash
TRIM_START_RELATIVE_RE = /^((?:\/{1,2})?(?:\.{1,2}\/)*)([^/].*|$)/, // trim relative path at start
VERSION_RE = /^(\d+\.){2}\d+$/, // version
RELATIVE_RE = /^\.{1,2}[\\/]/, // relative path
slice = Array.prototype.slice; // array slice
// micro template engine
template = (function (){
var TMPLENG_RE = /\{\{\s*(.*?)\s*\}\}/g;
var TMPLENG_RE = /\{\{\s*(.*?)\s*\}\}/g;
// get data from source
function getData(source, key){
var keys = key.split('.');
// get data from source
function getData(source, key){
var keys = key.split('.');
for (var i = 0, len = keys.length; i < len; i++) {
key = keys[i];
for (var i = 0, len = keys.length; i < len; i++) {
key = keys[i];
if (source && source.hasOwnProperty(key)) {
source = source[key];
break;
}
}
// return result
return source || '';
if (source && source.hasOwnProperty(key)) {
source = source[key];
break;
}
}
// template method
return function (format, data){
return format.replace(TMPLENG_RE, function (holder, key){
return getData(data, key);
});
}
// return result
return source || '';
}
// template method
return function (format, data){
return format.replace(TMPLENG_RE, function (holder, key){
return getData(data, key);
});
}
}());

@@ -53,6 +53,6 @@

exports.addFileExt = function (ext){
if (!ext || typeof ext !== 'string' || ext === 'js'
|| ext === 'css' || FILE_EXT.indexOf(ext) !== -1) return;
if (!ext || typeof ext !== 'string' || ext === 'js'
|| ext === 'css' || FILE_EXT.indexOf(ext) !== -1) return;
FILE_EXT.push(ext);
FILE_EXT.push(ext);
};

@@ -62,12 +62,12 @@

exports.removeFileExt = function (ext){
var index;
var index;
if (!ext || typeof ext !== 'string' || ext === 'js' || ext === 'css') return;
if (!ext || typeof ext !== 'string' || ext === 'js' || ext === 'css') return;
// get ext index from array
index = FILE_EXT.indexOf(ext);
// get ext index from array
index = FILE_EXT.indexOf(ext);
if (index === -1) return;
if (index === -1) return;
FILE_EXT.splice(index, 1);
FILE_EXT.splice(index, 1);
};

@@ -77,3 +77,3 @@

function unknownFile(uri){
return FILE_EXT.indexOf(path.extname(uri).substring(1)) === -1;
return FILE_EXT.indexOf(path.extname(uri).substring(1)) === -1;
}

@@ -86,35 +86,35 @@

function normalize(uri){
var query, queryIndex = uri.indexOf('?');
var query, queryIndex = uri.indexOf('?');
// remove query
if (queryIndex === -1) {
query = '';
} else {
query = uri.substring(queryIndex);
// a/b/./c/./d?debug=true ==> a/b/./c/./d
uri = uri.substring(0, queryIndex);
}
// remove query
if (queryIndex === -1) {
query = '';
} else {
query = uri.substring(queryIndex);
// a/b/./c/./d?debug=true ==> a/b/./c/./d
uri = uri.substring(0, queryIndex);
}
// a\b\.\c\.\d ==> a/b/./c/./d
uri = uri.replace(WIN_DIR_SEP_RE, '/');
// a\b\.\c\.\d ==> a/b/./c/./d
uri = uri.replace(WIN_DIR_SEP_RE, '/');
// ///a/b/./c/./d ==> /a/b/./c/./d
uri = uri.replace(INVALID_SLASH_RE, '/');
// ///a/b/./c/./d ==> /a/b/./c/./d
uri = uri.replace(INVALID_SLASH_RE, '/');
// a//b/c ==> a/b/c
uri = uri.replace(DOUBLE_SLASH_RE, '$1/');
// a//b/c ==> a/b/c
uri = uri.replace(DOUBLE_SLASH_RE, '$1/');
// a/b/./c/./d ==> a/b/c/d
uri = uri.replace(DOT_RE, '/');
// a/b/./c/./d ==> a/b/c/d
uri = uri.replace(DOT_RE, '/');
// a/b/c/../../d ==> a/b/../d ==> a/d
while (uri.match(DOUBLE_DOT_RE)) {
uri = uri.replace(DOUBLE_DOT_RE, '');
}
// a/b/c/../../d ==> a/b/../d ==> a/d
while (uri.match(DOUBLE_DOT_RE)) {
uri = uri.replace(DOUBLE_DOT_RE, '');
}
// ./../a ==> ../a
uri = uri.substring(0, 5) === './../' ? uri.substring(2) : uri;
// ./../a ==> ../a
uri = uri.substring(0, 5) === './../' ? uri.substring(2) : uri;
// retrun normalize uri
return uri + query;
// retrun normalize uri
return uri + query;
}

@@ -127,16 +127,16 @@ exports.normalize = normalize;

function realpath(uri){
var lastChar,
queryIndex;
var lastChar,
queryIndex;
uri = normalize(uri);
lastChar = uri.slice(-1);
uri = normalize(uri);
lastChar = uri.slice(-1);
// if it ends with #, we should return the uri without #
if (lastChar === '#') return uri.slice(0, -1);
// if it ends with #, we should return the uri without #
if (lastChar === '#') return uri.slice(0, -1);
// if it has with ?, we should return the uri without search params
if ((queryIndex = uri.indexOf('?')) !== -1) return uri.slice(0, queryIndex);
// if it has with ?, we should return the uri without search params
if ((queryIndex = uri.indexOf('?')) !== -1) return uri.slice(0, queryIndex);
// ext logical
return uri;
// ext logical
return uri;
}

@@ -147,4 +147,4 @@ exports.realpath = realpath;

function join(){
var paths = slice.call(arguments);
return normalize(paths.join(path.sep));
var paths = slice.call(arguments);
return normalize(paths.join(path.sep));
}

@@ -155,3 +155,3 @@ exports.join = join;

function dirname(uri){
return path.dirname(realpath(uri));
return path.dirname(realpath(uri));
}

@@ -162,3 +162,3 @@ exports.dirname = dirname;

exports.basename = function (uri, ext){
return path.basename(realpath(uri), ext);
return path.basename(realpath(uri), ext);
};

@@ -168,13 +168,13 @@

exports.extname = function (uri){
var ext;
var ext;
uri = realpath(uri);
uri = realpath(uri);
// if uri is a dir return ''
if (uri.slice(-1) === '/') return '';
// if uri is a dir return ''
if (uri.slice(-1) === '/') return '';
ext = path.extname(uri);
ext = path.extname(uri);
// default ext is js
return ext ? ext : '.js';
// default ext is js
return ext ? ext : '.js';
};

@@ -187,18 +187,18 @@

exports.relative = function (base, uri){
var root, dir, prefix, match,
firstChar = uri.charAt(0);
var root, dir, prefix, match,
firstChar = uri.charAt(0);
// if start of \/ or a relative path do nothing
if (firstChar === '/' || firstChar === '\\'
|| RELATIVE_RE.test(uri)) return normalize(uri);
// if start of \/ or a relative path do nothing
if (firstChar === '/' || firstChar === '\\'
|| RELATIVE_RE.test(uri)) return normalize(uri);
match = realpath(base).match(TRIM_START_RELATIVE_RE);
prefix = match[1];
base = match[2];
dir = dirname(uri);
prefix = prefix === '/' ? '../' : prefix;
root = path.relative(base.slice(-1) === '/' ? base : dirname(base), dir) || '.';
uri = dir === '.' ? uri : uri.substring(dir.length + 1);
match = realpath(base).match(TRIM_START_RELATIVE_RE);
prefix = match[1];
base = match[2];
dir = dirname(uri);
prefix = prefix === '/' ? '../' : prefix;
root = path.relative(base.slice(-1) === '/' ? base : dirname(base), dir) || '.';
uri = dir === '.' ? uri : uri.substring(dir.length + 1);
return normalize(prefix + join(root, uri));
return normalize(prefix + join(root, uri));
};

@@ -210,7 +210,7 @@

exports.absolute = function (base, uri){
if (!RELATIVE_RE.test(uri)) return normalize(uri);
if (!RELATIVE_RE.test(uri)) return normalize(uri);
base = realpath(base);
base = realpath(base);
return join(base.slice(-1) === '/' ? base : dirname(base), uri);
return join(base.slice(-1) === '/' ? base : dirname(base), uri);
};

@@ -220,9 +220,9 @@

exports.appendext = function (uri){
var lastChar = uri.slice(-1);
var lastChar = uri.slice(-1);
// Add the default `.js` extension except that the uri ends with `#` or has '?'
if (lastChar !== '#' && lastChar !== '/'
&& uri.indexOf('?') === -1 && unknownFile(uri)) uri += '.js';
// Add the default `.js` extension except that the uri ends with `#` or has '?'
if (lastChar !== '#' && lastChar !== '/'
&& uri.indexOf('?') === -1 && unknownFile(uri)) uri += '.js';
return normalize(uri);
return normalize(uri);
};

@@ -232,48 +232,48 @@

exports.resolve = function (uri){
// family/name@version
// family/name#version
// family.name@version
// family.name#version
var verSep, nameSep,
family, name, version;
// family/name@version
// family/name#version
// family.name@version
// family.name#version
var verSep, nameSep,
family, name, version;
// normalize uri
uri = normalize(uri);
// version separators
verSep = Math.max(uri.lastIndexOf('@'), uri.lastIndexOf('#'));
version = verSep === -1 ? '' : uri.substring(verSep + 1);
// normalize uri
uri = normalize(uri);
// version separators
verSep = Math.max(uri.lastIndexOf('@'), uri.lastIndexOf('#'));
version = verSep === -1 ? '' : uri.substring(verSep + 1);
// resolve version
if (version && VERSION_RE.test(version)) {
uri = uri.substring(0, verSep);
} else {
version = '';
verSep = uri.length;
}
// resolve version
if (version && VERSION_RE.test(version)) {
uri = uri.substring(0, verSep);
} else {
version = '';
verSep = uri.length;
}
// name separators
nameSep = uri.lastIndexOf('/');
nameSep = nameSep === -1 ? uri.lastIndexOf('.') : nameSep;
// name separators
nameSep = uri.lastIndexOf('/');
nameSep = nameSep === -1 ? uri.lastIndexOf('.') : nameSep;
// resolve family and name
if (nameSep === -1) {
family = '';
name = uri;
} else {
family = uri.substring(0, nameSep);
name = nameSep === -1 ? uri : uri.substring(nameSep + 1, verSep);
}
// resolve family and name
if (nameSep === -1) {
family = '';
name = uri;
} else {
family = uri.substring(0, nameSep);
name = nameSep === -1 ? uri : uri.substring(nameSep + 1, verSep);
}
// if family and name not exist return null
if (!family && !name) return null;
// if family and name not exist return null
if (!family && !name) return null;
// if family not exist set name as family
if (!family) family = name;
// if family not exist set name as family
if (!family) family = name;
// return result
return {
family: family,
name: name,
version: version
};
// return result
return {
family: family,
name: name,
version: version
};
};

@@ -283,4 +283,4 @@

function getAlias(pkg){
pkg = pkg || {};
return pkg.alias || {};
pkg = pkg || {};
return pkg.alias || {};
}

@@ -290,18 +290,18 @@

exports.parseAlias = function (pkg, name){
var alias;
var alias;
// relative name: ./class
if (RELATIVE_RE.test(name)) {
if (name.length > 3 && name.slice(-3) === '.js') name = name.slice(0, -3);
return normalize(name);
}
// relative name: ./class
if (RELATIVE_RE.test(name)) {
if (name.length > 3 && name.slice(-3) === '.js') name = name.slice(0, -3);
return normalize(name);
}
// get alias
alias = getAlias(pkg);
// get alias
alias = getAlias(pkg);
if (alias.hasOwnProperty(name)) {
name = alias[name];
}
if (alias.hasOwnProperty(name)) {
name = alias[name];
}
return normalize(name);
return normalize(name);
};

@@ -311,4 +311,4 @@

exports.isAlias = function (pkg, name){
var alias = getAlias(pkg);
return alias.hasOwnProperty(name);
var alias = getAlias(pkg);
return alias.hasOwnProperty(name);
};

@@ -318,3 +318,3 @@

function isString(str){
return typeof str === 'string';
return typeof str === 'string';
}

@@ -324,29 +324,29 @@

exports.idFromPackage = function (pkg, format){
var id, filename;
var id, filename;
// reset pkg
pkg = pkg || {};
// set default value
pkg.family = isString(pkg.family) ? pkg.family : '';
pkg.name = isString(pkg.name) ? pkg.name : '';
pkg.version = isString(pkg.version) ? pkg.version : '';
filename = pkg.filename = isString(pkg.filename) ? pkg.filename : '';
// reset pkg
pkg = pkg || {};
// set default value
pkg.family = isString(pkg.family) ? pkg.family : '';
pkg.name = isString(pkg.name) ? pkg.name : '';
pkg.version = isString(pkg.version) ? pkg.version : '';
filename = pkg.filename = isString(pkg.filename) ? pkg.filename : '';
// replace .js to whitespace characters
if (filename.length > 3 && filename.slice(-3) === '.js') filename = filename.slice(0, -3);
// replace .js to whitespace characters
if (filename.length > 3 && filename.slice(-3) === '.js') filename = filename.slice(0, -3);
// reset filename
pkg.filename = filename;
// reset filename
pkg.filename = filename;
// if filename is a relative path return
if (RELATIVE_RE.test(filename)) return normalize(filename);
// if filename is a relative path return
if (RELATIVE_RE.test(filename)) return normalize(filename);
// reset format
format = isString(format) ? format : '{{family}}/{{name}}/{{version}}/{{filename}}';
// reset format
format = isString(format) ? format : '{{family}}/{{name}}/{{version}}/{{filename}}';
// get id
id = normalize(template(format, pkg));
// get id
id = normalize(template(format, pkg));
//return result
return id.slice(-1) === '/' ? id.slice(0, -1) : id;
//return result
return id.slice(-1) === '/' ? id.slice(0, -1) : id;
};
{
"name": "cmd-helper",
"version": "0.2.6",
"main": "index.js",
"license": "MIT",
"keywords": [
"cmd",
"seajs",
"cmd-uitl",
"commonjs",
"common module"
],
"homepage": "https://nuintun.github.io/cmd-helper",
"description": "Utilities for common module definition modify from cmd-util",
"author": {
"name": "Nuintun",
"email": "<nuintun@qq.com>"
},
"bugs": {
"url": "https://github.com/nuintun/cmd-helper/issues",
"email": "nuintun@qq.com"
},
"engines": {
"node": ">=0.8.0"
},
"repository": {
"type": "git",
"url": "https://github.com/nuintun/cmd-helper.git"
},
"dependencies": {
"uglify-js": "~2.4.13"
},
"devDependencies": {
"jshint": "*",
"mocha": ">=1.18.2",
"should": ">=3.3.1",
"coveralls": "~2.10.0",
"jscoverage": "~0.3.8",
"mocha-lcov-reporter": "~0.0.1"
},
"readmeFilename": "README.md",
"scripts": {
"test": "make test"
}
"name": "cmd-helper",
"version": "0.2.7",
"main": "index.js",
"license": "MIT",
"keywords": [
"cmd",
"seajs",
"cmd-uitl",
"commonjs",
"common module"
],
"homepage": "https://nuintun.github.io/cmd-helper",
"description": "Utilities for common module definition modify from cmd-util",
"author": {
"name": "Nuintun",
"email": "<nuintun@qq.com>"
},
"bugs": {
"url": "https://github.com/nuintun/cmd-helper/issues",
"email": "nuintun@qq.com"
},
"engines": {
"node": ">=0.8.0"
},
"repository": {
"type": "git",
"url": "https://github.com/nuintun/cmd-helper.git"
},
"dependencies": {
"uglify-js": "^2.4.21"
},
"devDependencies": {
"jshint": "^2.7.0",
"mocha": "^2.2.5",
"should": "^6.0.1",
"coveralls": "^2.11.2",
"jscoverage": "^0.5.9",
"mocha-lcov-reporter": "^0.0.2"
},
"readmeFilename": "README.md",
"scripts": {
"test": "make test"
}
}
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