react / react-0.13.3 / examples / basic-commonjs / node_modules / browserify / node_modules / path-browserify / index.js
80713 views// Copyright Joyent, Inc. and other Node contributors.1//2// Permission is hereby granted, free of charge, to any person obtaining a3// copy of this software and associated documentation files (the4// "Software"), to deal in the Software without restriction, including5// without limitation the rights to use, copy, modify, merge, publish,6// distribute, sublicense, and/or sell copies of the Software, and to permit7// persons to whom the Software is furnished to do so, subject to the8// following conditions:9//10// The above copyright notice and this permission notice shall be included11// in all copies or substantial portions of the Software.12//13// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS14// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF15// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN16// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,17// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR18// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE19// USE OR OTHER DEALINGS IN THE SOFTWARE.2021// resolves . and .. elements in a path array with directory names there22// must be no slashes, empty elements, or device names (c:\) in the array23// (so also no leading and trailing slashes - it does not distinguish24// relative and absolute paths)25function normalizeArray(parts, allowAboveRoot) {26// if the path tries to go above the root, `up` ends up > 027var up = 0;28for (var i = parts.length - 1; i >= 0; i--) {29var last = parts[i];30if (last === '.') {31parts.splice(i, 1);32} else if (last === '..') {33parts.splice(i, 1);34up++;35} else if (up) {36parts.splice(i, 1);37up--;38}39}4041// if the path is allowed to go above the root, restore leading ..s42if (allowAboveRoot) {43for (; up--; up) {44parts.unshift('..');45}46}4748return parts;49}5051// Split a filename into [root, dir, basename, ext], unix version52// 'root' is just a slash, or nothing.53var splitPathRe =54/^(\/?|)([\s\S]*?)((?:\.{1,2}|[^\/]+?|)(\.[^.\/]*|))(?:[\/]*)$/;55var splitPath = function(filename) {56return splitPathRe.exec(filename).slice(1);57};5859// path.resolve([from ...], to)60// posix version61exports.resolve = function() {62var resolvedPath = '',63resolvedAbsolute = false;6465for (var i = arguments.length - 1; i >= -1 && !resolvedAbsolute; i--) {66var path = (i >= 0) ? arguments[i] : process.cwd();6768// Skip empty and invalid entries69if (typeof path !== 'string') {70throw new TypeError('Arguments to path.resolve must be strings');71} else if (!path) {72continue;73}7475resolvedPath = path + '/' + resolvedPath;76resolvedAbsolute = path.charAt(0) === '/';77}7879// At this point the path should be resolved to a full absolute path, but80// handle relative paths to be safe (might happen when process.cwd() fails)8182// Normalize the path83resolvedPath = normalizeArray(filter(resolvedPath.split('/'), function(p) {84return !!p;85}), !resolvedAbsolute).join('/');8687return ((resolvedAbsolute ? '/' : '') + resolvedPath) || '.';88};8990// path.normalize(path)91// posix version92exports.normalize = function(path) {93var isAbsolute = exports.isAbsolute(path),94trailingSlash = substr(path, -1) === '/';9596// Normalize the path97path = normalizeArray(filter(path.split('/'), function(p) {98return !!p;99}), !isAbsolute).join('/');100101if (!path && !isAbsolute) {102path = '.';103}104if (path && trailingSlash) {105path += '/';106}107108return (isAbsolute ? '/' : '') + path;109};110111// posix version112exports.isAbsolute = function(path) {113return path.charAt(0) === '/';114};115116// posix version117exports.join = function() {118var paths = Array.prototype.slice.call(arguments, 0);119return exports.normalize(filter(paths, function(p, index) {120if (typeof p !== 'string') {121throw new TypeError('Arguments to path.join must be strings');122}123return p;124}).join('/'));125};126127128// path.relative(from, to)129// posix version130exports.relative = function(from, to) {131from = exports.resolve(from).substr(1);132to = exports.resolve(to).substr(1);133134function trim(arr) {135var start = 0;136for (; start < arr.length; start++) {137if (arr[start] !== '') break;138}139140var end = arr.length - 1;141for (; end >= 0; end--) {142if (arr[end] !== '') break;143}144145if (start > end) return [];146return arr.slice(start, end - start + 1);147}148149var fromParts = trim(from.split('/'));150var toParts = trim(to.split('/'));151152var length = Math.min(fromParts.length, toParts.length);153var samePartsLength = length;154for (var i = 0; i < length; i++) {155if (fromParts[i] !== toParts[i]) {156samePartsLength = i;157break;158}159}160161var outputParts = [];162for (var i = samePartsLength; i < fromParts.length; i++) {163outputParts.push('..');164}165166outputParts = outputParts.concat(toParts.slice(samePartsLength));167168return outputParts.join('/');169};170171exports.sep = '/';172exports.delimiter = ':';173174exports.dirname = function(path) {175var result = splitPath(path),176root = result[0],177dir = result[1];178179if (!root && !dir) {180// No dirname whatsoever181return '.';182}183184if (dir) {185// It has a dirname, strip trailing slash186dir = dir.substr(0, dir.length - 1);187}188189return root + dir;190};191192193exports.basename = function(path, ext) {194var f = splitPath(path)[2];195// TODO: make this comparison case-insensitive on windows?196if (ext && f.substr(-1 * ext.length) === ext) {197f = f.substr(0, f.length - ext.length);198}199return f;200};201202203exports.extname = function(path) {204return splitPath(path)[3];205};206207function filter (xs, f) {208if (xs.filter) return xs.filter(f);209var res = [];210for (var i = 0; i < xs.length; i++) {211if (f(xs[i], i, xs)) res.push(xs[i]);212}213return res;214}215216// String.prototype.substr - negative index don't work in IE8217var substr = 'ab'.substr(-1) === 'b'218? function (str, start, len) { return str.substr(start, len) }219: function (str, start, len) {220if (start < 0) start = str.length + start;221return str.substr(start, len);222}223;224225226