eyeglass
Version:
Sass modules for npm.
176 lines • 6.03 kB
JavaScript
;
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
result["default"] = mod;
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
const path = __importStar(require("path"));
const stringUtils = __importStar(require("./strings"));
const stdSep = "/";
const rAllPathSep = /[/\\]+/g;
const rIsRelative = /^\.{1,2}/;
const rUriFragments = /^([^?#]+)(\?[^#]*)?(#.*)?/;
const rSearchDelim = /^[?&]*/;
function shouldNormalizePathSep() {
// normalize if the path separator is a backslash or we haven't explicitly disabled normalization
return path.sep === "\\" || process.env.EYEGLASS_NORMALIZE_PATHS !== "false";
}
function convertSeparator(uri, sep) {
return shouldNormalizePathSep() ? uri.replace(rAllPathSep, sep) : uri;
}
/**
* Provides an interface for working with URIs
*
* @constructor
* @param {String} uri - the original URI
* @param {String} sep - the target separator to use when representing the pathname
*/
class URI {
constructor(uri, sep = null) {
this.sep = sep || stdSep;
this.path = "";
this.search = "";
this.hash = "";
let uriFragments = rUriFragments.exec(uri);
if (!uriFragments) {
throw new Error(`Malformed URI: ${uri}`);
}
this.setPath(uriFragments[1]);
this.setQuery(uriFragments[2]);
this.setHash(uriFragments[3]);
}
/**
* sets the new pathname for the URI
* @param {String} pathname - the new pathname to set
*/
setPath(pathname) {
// convert the path separator to standard system paths
pathname = convertSeparator(pathname, path.sep);
// then normalize the path
pathname = path.normalize(pathname);
// then set it using the specified path
this.path = convertSeparator(pathname, this.sep);
}
/**
* gets the pathname of the URI
* @param sep - the separator to use to represent the pathname
* @param relativeTo - if set, returns the pathname relative to this base path
*/
getPath(sep, relativeTo) {
let pathname = this.path;
if (relativeTo) {
pathname = convertSeparator(pathname, path.sep);
relativeTo = convertSeparator(relativeTo, path.sep);
if (pathname.indexOf(relativeTo) === 0) {
pathname = path.relative(relativeTo, pathname);
}
}
return convertSeparator(pathname, sep || this.sep);
}
/**
* adds a query string to the URI
* @param {String} search - the query string to append
*/
addQuery(search) {
if (!search) {
return;
}
// append the new search string
// ensuring the leading character is the appropriate delimiter
this.search += search.replace(rSearchDelim, this.search ? "&" : "?");
}
/**
* replaces the query string on the URI
* @param {String} search - the query string to set
*/
setQuery(search) {
// reset the search
this.search = "";
// then add the new one
this.addQuery(search);
}
/**
* replaces the hash string on the URI
* @param {String} hash - the hash string to set
*/
setHash(hash) {
this.hash = hash === undefined ? "" : hash;
}
/**
* returns the URI as a string
* @returns {String} the full URI
*/
toString() {
return this.path + this.search + this.hash;
}
/**
* given any number of path fragments, joins the non-empty fragments
* @returns {String} the joined fragments
*/
static join(...fragments) {
// join all the non-empty paths
let uri = new URI(fragments.filter((fragment) => {
return !!fragment;
}).join(stdSep));
return uri.getPath();
}
/**
* whether or not a given URI is relative
* @param {String} uri - the URI to check
* @returns {Boolean} whether or not the URI is relative like
*/
static isRelative(uri) {
return rIsRelative.test(uri);
}
/**
* normalizes the URI for use as a web URI
* @param {String} uri - the URI to normalize
* @returns {String} the normalized URI
*/
static web(uri) {
return (new URI(uri)).toString();
}
/**
* normalizes the URI for use as a system path
* @param {String} uri - the URI to normalize
* @returns {String} the normalized URI
*/
static system(uri) {
// convert the path separator to standard system paths
let pathname = convertSeparator(uri, path.sep);
// then normalize the path
return path.normalize(pathname);
}
/**
* ensures that the URI is able to be cleanly exported to a SassString
* @param {String} uri - the URI to normalize
* @returns {String} the normalized URI
*/
static sass(sassImpl, uri) {
// escape all backslashes for Sass string and quote it
// "C:\foo\bar.png" -> "C:\\foo\\bar.png"
// actual backslash, for real this time http://www.xkcd.com/1638/
return stringUtils.quoteJS(sassImpl, uri.replace(/\\/g, "\\\\"));
}
/**
* decorates a URI to preserve special characters
* @param {String} uri - the URI to decorate
* @returns {String} the decorated URI
*/
static preserve(uri) {
return uri.replace(/\\/g, "<BACKSLASH>");
}
/**
* restores a URI to restore special characters (opposite of URI.preserve)
* @param {String} uri - the URI to restore
* @returns {String} the restored URI
*/
static restore(uri) {
return uri.replace(/<BACKSLASH>/g, "\\");
}
}
exports.URI = URI;
//# sourceMappingURL=URI.js.map