refactor path module to lib/path.js
This commit is contained in:
parent
7ff53f4c6a
commit
e0061a511d
64
lib/path.js
Normal file
64
lib/path.js
Normal file
@ -0,0 +1,64 @@
|
|||||||
|
|
||||||
|
exports.join = function () {
|
||||||
|
return exports.normalize(Array.prototype.join.call(arguments, "/"));
|
||||||
|
};
|
||||||
|
|
||||||
|
exports.normalizeArray = function (parts, keepBlanks) {
|
||||||
|
var directories = [], prev;
|
||||||
|
for (var i = 0, l = parts.length - 1; i <= l; i++) {
|
||||||
|
var directory = parts[i];
|
||||||
|
|
||||||
|
// if it's blank, but it's not the first thing, and not the last thing, skip it.
|
||||||
|
if (directory === "" && i !== 0 && i !== l && !keepBlanks) continue;
|
||||||
|
|
||||||
|
// if it's a dot, and there was some previous dir already, then skip it.
|
||||||
|
if (directory === "." && prev !== undefined) continue;
|
||||||
|
|
||||||
|
if (
|
||||||
|
directory === ".."
|
||||||
|
&& directories.length
|
||||||
|
&& prev !== ".."
|
||||||
|
&& prev !== "."
|
||||||
|
&& prev !== undefined
|
||||||
|
&& (prev !== "" || keepBlanks)
|
||||||
|
) {
|
||||||
|
directories.pop();
|
||||||
|
prev = directories.slice(-1)[0]
|
||||||
|
} else {
|
||||||
|
if (prev === ".") directories.pop();
|
||||||
|
directories.push(directory);
|
||||||
|
prev = directory;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return directories;
|
||||||
|
};
|
||||||
|
|
||||||
|
exports.normalize = function (path, keepBlanks) {
|
||||||
|
return exports.normalizeArray(path.split("/"), keepBlanks).join("/");
|
||||||
|
};
|
||||||
|
|
||||||
|
exports.dirname = function (path) {
|
||||||
|
return path && path.substr(0, path.lastIndexOf("/")) || ".";
|
||||||
|
};
|
||||||
|
|
||||||
|
exports.filename = function () {
|
||||||
|
throw new Error("path.filename is deprecated. Please use path.basename instead.");
|
||||||
|
};
|
||||||
|
exports.basename = function (path, ext) {
|
||||||
|
var f = path.substr(path.lastIndexOf("/") + 1);
|
||||||
|
if (ext && f.substr(-1 * ext.length) === ext) {
|
||||||
|
f = f.substr(0, f.length - ext.length);
|
||||||
|
}
|
||||||
|
return f;
|
||||||
|
};
|
||||||
|
|
||||||
|
exports.extname = function (path) {
|
||||||
|
var index = path.lastIndexOf('.');
|
||||||
|
return index < 0 ? '' : path.substring(index);
|
||||||
|
};
|
||||||
|
|
||||||
|
exports.exists = function (path, callback) {
|
||||||
|
process.binding('fs').stat(path, function (err, stats) {
|
||||||
|
if (callback) callback(err ? false : true);
|
||||||
|
});
|
||||||
|
};
|
@ -1308,6 +1308,7 @@ static Handle<Value> Binding(const Arguments& args) {
|
|||||||
exports->Set(String::New("url"), String::New(native_url));
|
exports->Set(String::New("url"), String::New(native_url));
|
||||||
exports->Set(String::New("utils"), String::New(native_utils));
|
exports->Set(String::New("utils"), String::New(native_utils));
|
||||||
exports->Set(String::New("events"), String::New(native_events));
|
exports->Set(String::New("events"), String::New(native_events));
|
||||||
|
exports->Set(String::New("path"), String::New(native_path));
|
||||||
binding_cache->Set(module, exports);
|
binding_cache->Set(module, exports);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
73
src/node.js
73
src/node.js
@ -196,72 +196,13 @@ function debug (x) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var pathModule = createInternalModule
|
||||||
var pathModule = createInternalModule("path", function (exports) {
|
( 'path'
|
||||||
exports.join = function () {
|
, process.compile
|
||||||
return exports.normalize(Array.prototype.join.call(arguments, "/"));
|
( "(function (exports) {" + natives.path + "})"
|
||||||
};
|
, "path"
|
||||||
|
)
|
||||||
exports.normalizeArray = function (parts, keepBlanks) {
|
);
|
||||||
var directories = [], prev;
|
|
||||||
for (var i = 0, l = parts.length - 1; i <= l; i++) {
|
|
||||||
var directory = parts[i];
|
|
||||||
|
|
||||||
// if it's blank, but it's not the first thing, and not the last thing, skip it.
|
|
||||||
if (directory === "" && i !== 0 && i !== l && !keepBlanks) continue;
|
|
||||||
|
|
||||||
// if it's a dot, and there was some previous dir already, then skip it.
|
|
||||||
if (directory === "." && prev !== undefined) continue;
|
|
||||||
|
|
||||||
if (
|
|
||||||
directory === ".."
|
|
||||||
&& directories.length
|
|
||||||
&& prev !== ".."
|
|
||||||
&& prev !== "."
|
|
||||||
&& prev !== undefined
|
|
||||||
&& (prev !== "" || keepBlanks)
|
|
||||||
) {
|
|
||||||
directories.pop();
|
|
||||||
prev = directories.slice(-1)[0]
|
|
||||||
} else {
|
|
||||||
if (prev === ".") directories.pop();
|
|
||||||
directories.push(directory);
|
|
||||||
prev = directory;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return directories;
|
|
||||||
};
|
|
||||||
|
|
||||||
exports.normalize = function (path, keepBlanks) {
|
|
||||||
return exports.normalizeArray(path.split("/"), keepBlanks).join("/");
|
|
||||||
};
|
|
||||||
|
|
||||||
exports.dirname = function (path) {
|
|
||||||
return path.substr(0, path.lastIndexOf("/")) || ".";
|
|
||||||
};
|
|
||||||
|
|
||||||
exports.filename = function () {
|
|
||||||
throw new Error("path.filename is deprecated. Please use path.basename instead.");
|
|
||||||
};
|
|
||||||
exports.basename = function (path, ext) {
|
|
||||||
var f = path.substr(path.lastIndexOf("/") + 1);
|
|
||||||
if (ext && f.substr(-1 * ext.length) === ext) {
|
|
||||||
f = f.substr(0, f.length - ext.length);
|
|
||||||
}
|
|
||||||
return f;
|
|
||||||
};
|
|
||||||
|
|
||||||
exports.extname = function (path) {
|
|
||||||
var index = path.lastIndexOf('.');
|
|
||||||
return index < 0 ? '' : path.substring(index);
|
|
||||||
};
|
|
||||||
|
|
||||||
exports.exists = function (path, callback) {
|
|
||||||
requireNative('fs').stat(path, function (err, stats) {
|
|
||||||
if (callback) callback(err ? false : true);
|
|
||||||
});
|
|
||||||
};
|
|
||||||
});
|
|
||||||
|
|
||||||
var path = pathModule.exports;
|
var path = pathModule.exports;
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user