使用JavaScript實現(xiàn)node.js中的path.join方法
Node.JS中的 path.join 非常方便,能直接按相對或絕對合并路徑,使用: path.join([path1], [path2], [...]),有時侯前端也需要這種方法,如何實現(xiàn)呢?
其實直接從 node.js 的 path.js 拿到源碼加工一下就可以了:
1. 將 const 等 es6 屬性改為 var,以便前端瀏覽器兼容
2. 添加一個判斷路戲分隔符的變量 sep,即左斜杠還是右斜杠,以第一個路戲分隔符為準(zhǔn)
3. 將引用的變量和函數(shù)放到一個文件里就可以了:
Path 的源碼: https://github.com/nodejs/node/blob/master/lib/path.js
var CHAR_FORWARD_SLASH = 47
var CHAR_BACKWARD_SLASH = 92
var CHAR_DOT = 46
function isPathSeparator(code) {
return code === CHAR_FORWARD_SLASH || code === CHAR_BACKWARD_SLASH;
}
function isPosixPathSeparator(code) {
return code === CHAR_FORWARD_SLASH;
}
function normalize(path) {
if (path.length === 0)
return '.';
var isAbsolute = path.charCodeAt(0) === CHAR_FORWARD_SLASH;
var trailingSeparator =
path.charCodeAt(path.length - 1) === CHAR_FORWARD_SLASH;
// Normalize the path
path = normalizeString(path, !isAbsolute, '/', isPosixPathSeparator);
if (path.length === 0 && !isAbsolute)
path = '.';
if (path.length > 0 && trailingSeparator)
path += '/';
if (isAbsolute)
return '/' + path;
return path;
}
function normalizeString(path, allowAboveRoot, separator, isPathSeparator) {
var res = '';
var lastSegmentLength = 0;
var lastSlash = -1;
var dots = 0;
var code;
for (var i = 0; i <= path.length; ++i) {
if (i < path.length)
code = path.charCodeAt(i);
else if (isPathSeparator(code))
break;
else
code = CHAR_FORWARD_SLASH;
if (isPathSeparator(code)) {
if (lastSlash === i - 1 || dots === 1) {
// NOOP
} else if (lastSlash !== i - 1 && dots === 2) {
if (res.length < 2 || lastSegmentLength !== 2 ||
res.charCodeAt(res.length - 1) !== CHAR_DOT ||
res.charCodeAt(res.length - 2) !== CHAR_DOT) {
if (res.length > 2) {
const lastSlashIndex = res.lastIndexOf(separator);
if (lastSlashIndex !== res.length - 1) {
if (lastSlashIndex === -1) {
res = '';
lastSegmentLength = 0;
} else {
res = res.slice(0, lastSlashIndex);
lastSegmentLength = res.length - 1 - res.lastIndexOf(separator);
}
lastSlash = i;
dots = 0;
continue;
}
} else if (res.length === 2 || res.length === 1) {
res = '';
lastSegmentLength = 0;
lastSlash = i;
dots = 0;
continue;
}
}
if (allowAboveRoot) {
if (res.length > 0)
res += `${separator}..`;
else
res = '..';
lastSegmentLength = 2;
}
} else {
if (res.length > 0)
res += separator + path.slice(lastSlash + 1, i);
else
res = path.slice(lastSlash + 1, i);
lastSegmentLength = i - lastSlash - 1;
}
lastSlash = i;
dots = 0;
} else if (code === CHAR_DOT && dots !== -1) {
++dots;
} else {
dots = -1;
}
}
return res;
}
function join() {
if (arguments.length === 0)
return '.';
var sep = arguments[0].indexOf('/') > -1 ? '/' : '\\'
var joined;
var firstPart;
for (var i = 0; i < arguments.length; ++i) {
var arg = arguments[i];
if (arg.length > 0) {
if (joined === undefined)
joined = firstPart = arg;
else
joined += sep + arg;
}
}
if (joined === undefined)
return '.';
var needsReplace = true;
var slashCount = 0;
if (isPathSeparator(firstPart.charCodeAt(0))) {
++slashCount;
var firstLen = firstPart.length;
if (firstLen > 1) {
if (isPathSeparator(firstPart.charCodeAt(1))) {
++slashCount;
if (firstLen > 2) {
if (isPathSeparator(firstPart.charCodeAt(2)))
++slashCount;
else {
// We matched a UNC path in the first part
needsReplace = false;
}
}
}
}
}
if (needsReplace) {
// Find any more consecutive slashes we need to replace
for (; slashCount < joined.length; ++slashCount) {
if (!isPathSeparator(joined.charCodeAt(slashCount)))
break;
}
// Replace the slashes if needed
if (slashCount >= 2)
joined = sep + joined.slice(slashCount);
}
return normalize(joined);
}
使用:
join('../var/www', '../abc')
> "../var/abc"
join('../var/www', '\abc')
../var/www/abc
總結(jié)
以上所述是小編給大家介紹的使用JavaScript實現(xiàn)node.js中的path.join方法,希望對大家有所幫助,如果對大家有所幫助,如果大家有任何疑問歡迎給我留言,小編會及時回復(fù)大家的!
相關(guān)文章
jquery對單選框,多選框,文本框等常見操作小結(jié)
本篇文章主要是對jquery對單選框,多選框,文本框等常見操作進(jìn)行了介紹,需要的朋友可以過來參考下,希望對大家有所幫助2014-01-01
基于js與flash實現(xiàn)的網(wǎng)站flv視頻播放插件代碼
這篇文章主要介紹了基于js與flash實現(xiàn)的網(wǎng)站flv視頻播放插件代碼,該功能在很多網(wǎng)站上都有著廣泛的應(yīng)用,本文以實例形式對其進(jìn)行介紹,需要的朋友可以參考下2014-10-10
JS運動相關(guān)知識點小結(jié)(附彈性運動示例)
這篇文章主要介紹了JS運動相關(guān)知識點,總結(jié)分析了JavaScript運動所涉及的相關(guān)知識點與注意事項,并附帶了一個JavaScript彈性運動的實例供大家參考,需要的朋友可以參考下2016-01-01
JS+CSS實現(xiàn)分類動態(tài)選擇及移動功能效果代碼
這篇文章主要介紹了JS+CSS實現(xiàn)分類動態(tài)選擇及移動功能效果代碼,涉及JavaScript實現(xiàn)頁面元素動態(tài)變換效果實現(xiàn)技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-10-10
在實例中重學(xué)JavaScript事件循環(huán)
這篇文章主要介紹了在實例中重學(xué)JavaScript事件循環(huán),幫助大家更好的理解和學(xué)習(xí)JavaScript,感興趣的朋友可以了解下2020-12-12
JavaScript中函數(shù)表達(dá)式和函數(shù)聲明及函數(shù)聲明與函數(shù)表達(dá)式的不同
這篇文章主要介紹了JavaScript中函數(shù)表達(dá)式和函數(shù)聲明及函數(shù)聲明與函數(shù)表達(dá)式的不同的相關(guān)資料,需要的朋友可以參考下2015-11-11

