cmd-helper
Advanced tools
Comparing version 0.0.1 to 0.0.2
@@ -6,2 +6,3 @@ /* | ||
* Hsiaoming Yang <me@lepture.com> | ||
* Nuintun <nuintun@qq.com> | ||
*/ | ||
@@ -8,0 +9,0 @@ var trimRE = /^(\r\n|\r|\n)|(\r\n|\r|\n)$/g, |
125
lib/iduri.js
/* | ||
* iduri | ||
* https://spmjs.org | ||
* | ||
* Hsiaoming Yang <me@lepture.com> | ||
* Nuintun <nuintun@qq.com> | ||
*/ | ||
// path | ||
var path = require('path'), | ||
PROTOCOL_RE = /(:\/)/g, | ||
CURDIR_RE = /^\.[/\\]+/, | ||
FILE_EXT = ['js', 'css'], | ||
RELPATH_RE = /^\.{1,2}[/\\]+/, | ||
TMPLENG_RE = /\{\{\s*(.*?)\s*\}\}/g; | ||
var template, | ||
path = require('path'), | ||
FILE_EXT = ['js', 'css'], // file extension | ||
RELPATH_RE = /^\.{1,2}[\\/]/, // relative path | ||
RESOLVE_RE = /^([a-z][a-z0-9\-]*(?:\/|\.))?([a-zA-Z][a-zA-Z0-9\-]*)((?:@|#).*)?$/; // resolve | ||
// micro template engine | ||
template = (function (){ | ||
var TMPLENG_RE = /\{\{\s*(.*?)\s*\}\}/g; | ||
function getData(source, key){ | ||
var keys = key.split('.'); | ||
for (var i = 0, len = keys.length; i < len; i++) { | ||
key = keys[i]; | ||
if (source && source.hasOwnProperty(key)) { | ||
source = source[key]; | ||
break; | ||
} | ||
} | ||
return source || ''; | ||
} | ||
return function (format, data){ | ||
return format.replace(TMPLENG_RE, function (holder, key){ | ||
return getData(data, key); | ||
}); | ||
} | ||
}()); | ||
// add support file extension | ||
@@ -46,2 +73,8 @@ exports.addFileExt = function (ext){ | ||
function unixPath(uri){ | ||
var isCurDir = /^\.[\\/]/.test(uri); | ||
uri = path.normalize(uri); | ||
uri = isCurDir ? './' + uri : uri; | ||
uri = uri.replace(/(:[\\/])/g, '$1/'); | ||
return uri.replace(/\\/g, '/'); | ||
@@ -57,6 +90,5 @@ } | ||
// family.name#version | ||
var m, family, name, version, | ||
regex = /^([a-z][a-z0-9\-]*(?:\/|\.))?([a-zA-Z][a-zA-Z0-9\-]*)((?:@|#).*)?$/; | ||
var m, family, name, version; | ||
m = uri.match(regex); | ||
m = uri.match(RESOLVE_RE); | ||
@@ -92,7 +124,5 @@ if (!m) return null; | ||
function normalize(uri){ | ||
var lastChar, isCurDir = CURDIR_RE.test(uri); | ||
var lastChar, searchIndex; | ||
uri = unixPath(path.normalize(uri)); | ||
uri = isCurDir ? './' + uri : uri; | ||
uri = uri.replace(PROTOCOL_RE, '$1/'); | ||
uri = unixPath(uri); | ||
@@ -106,2 +136,5 @@ lastChar = uri.slice(-1); | ||
// if it has with ?, we should return the uri without search params | ||
if ((searchIndex = uri.indexOf('?')) !== -1) return uri.slice(0, searchIndex); | ||
// TODO ext logical | ||
@@ -112,2 +145,17 @@ return uri; | ||
// join path | ||
exports.join = function (){ | ||
return unixPath(path.join.apply(path, arguments)); | ||
}; | ||
// dirname | ||
exports.dirname = function (uri){ | ||
return unixPath(path.dirname(uri)); | ||
}; | ||
// basename | ||
exports.basename = function (uri){ | ||
return unixPath(path.basename(uri)); | ||
}; | ||
// this is very different from node's path.relative. | ||
@@ -120,3 +168,3 @@ // if uri starts with /, it's absolute uri, we don't relative it. | ||
uri = normalize(uri); | ||
uri = unixPath(uri); | ||
@@ -144,19 +192,7 @@ if (uri.charAt(0) === '/') return uri; | ||
exports.absolute = function (base, uri){ | ||
if (!RELPATH_RE.test(uri)) return normalize(uri); | ||
if (!RELPATH_RE.test(uri)) return unixPath(uri); | ||
uri = path.join(path.dirname(base), uri); | ||
return normalize(uri); | ||
return unixPath(uri); | ||
}; | ||
exports.join = function (){ | ||
return unixPath(path.join.apply(path, arguments)); | ||
}; | ||
exports.dirname = function (uri){ | ||
return unixPath(path.dirname(uri)); | ||
}; | ||
exports.basename = function (uri){ | ||
return unixPath(path.basename(uri)); | ||
}; | ||
// this is very different from node's path.extname. | ||
@@ -189,2 +225,7 @@ exports.extname = function (uri){ | ||
// parse alias | ||
function getAlias(pkg){ | ||
return pkg.alias || {}; | ||
} | ||
exports.parseAlias = function (pkg, name){ | ||
@@ -212,2 +253,3 @@ var alias; | ||
// parse id | ||
exports.idFromPackage = function (pkg, filename, format){ | ||
@@ -231,27 +273,2 @@ if (filename && !format && filename.indexOf('{{') !== -1) { | ||
return template(format, pkg); | ||
}; | ||
function getAlias(pkg){ | ||
return pkg.alias || {}; | ||
} | ||
function getData(source, key){ | ||
var keys = key.split('.'); | ||
for (var i = 0, len = keys.length; i < len; i++) { | ||
key = keys[i]; | ||
if (source && source.hasOwnProperty(key)) { | ||
source = source[key]; | ||
break; | ||
} | ||
} | ||
return source || ''; | ||
} | ||
function template(format, data){ | ||
return format.replace(TMPLENG_RE, function (holder, key){ | ||
return getData(data, key); | ||
}); | ||
} | ||
}; |
{ | ||
"name": "cmd-helper", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"main": "index.js", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
Deprecated
MaintenanceThe maintainer of the package marked it as deprecated. This could indicate that a single version should not be used, or that the package is no longer maintained and any new vulnerabilities will not be fixed.
Found 1 instance in 1 package
Deprecated
MaintenanceThe maintainer of the package marked it as deprecated. This could indicate that a single version should not be used, or that the package is no longer maintained and any new vulnerabilities will not be fixed.
Found 1 instance in 1 package
31424
730