Path: blob/master/node_modules/@protobufjs/path/index.js
1126 views
"use strict";12/**3* A minimal path module to resolve Unix, Windows and URL paths alike.4* @memberof util5* @namespace6*/7var path = exports;89var isAbsolute =10/**11* Tests if the specified path is absolute.12* @param {string} path Path to test13* @returns {boolean} `true` if path is absolute14*/15path.isAbsolute = function isAbsolute(path) {16return /^(?:\/|\w+:)/.test(path);17};1819var normalize =20/**21* Normalizes the specified path.22* @param {string} path Path to normalize23* @returns {string} Normalized path24*/25path.normalize = function normalize(path) {26path = path.replace(/\\/g, "/")27.replace(/\/{2,}/g, "/");28var parts = path.split("/"),29absolute = isAbsolute(path),30prefix = "";31if (absolute)32prefix = parts.shift() + "/";33for (var i = 0; i < parts.length;) {34if (parts[i] === "..") {35if (i > 0 && parts[i - 1] !== "..")36parts.splice(--i, 2);37else if (absolute)38parts.splice(i, 1);39else40++i;41} else if (parts[i] === ".")42parts.splice(i, 1);43else44++i;45}46return prefix + parts.join("/");47};4849/**50* Resolves the specified include path against the specified origin path.51* @param {string} originPath Path to the origin file52* @param {string} includePath Include path relative to origin path53* @param {boolean} [alreadyNormalized=false] `true` if both paths are already known to be normalized54* @returns {string} Path to the include file55*/56path.resolve = function resolve(originPath, includePath, alreadyNormalized) {57if (!alreadyNormalized)58includePath = normalize(includePath);59if (isAbsolute(includePath))60return includePath;61if (!alreadyNormalized)62originPath = normalize(originPath);63return (originPath = originPath.replace(/(?:\/|^)[^/]+$/, "")).length ? normalize(originPath + "/" + includePath) : includePath;64};656667