UNPKG

module-id

Version:

Parses commonjs module id into name, version and path.

112 lines (92 loc) 3.24 kB
"use strict"; function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } // @const // 'a@1.2.3/abc' -> // ['a@1.2.3/abc', 'a', '1.2.3', '/abc'] // 0 1 2 3 4 var REGEX_PARSE_ID = /^(?:@([^/]+)\/)?((?:[^/])+?)(?:@([^/]+))?(\/.*)?$/; // On android 2.2, // `[^\/]+?` will fail to do the lazy match, but `(?:[^\/])+?` works. // Shit, go to hell! // Parses a module id into an object // @param {string} id path-resolved module identifier // 'a@1.0.0' -> 'a@1.0.0' // 'a' -> 'a@*' // 'a/inner' -> 'a@*/inner' function parse_module_id(id) { if (typeof id !== 'string') { var error = new TypeError("id must be a string, but got `".concat(id, "`")); error.code = 'INVALID_TYPE'; throw error; } var match = id.match(REGEX_PARSE_ID); if (!match) { var _error = new RangeError("\"".concat(id, "\" is not a valid module id")); _error.code = 'INVALID_MODULE_ID'; throw _error; } var scope = match[1]; var _name = match[2]; // 'a/inner' -> 'a@latest/inner' var version = match[3]; var path = match[4]; // There always be matches return { scope: scope, _name: _name, version: version, path: path }; } function format(obj) { var version = obj.version ? "@".concat(obj.version) : ''; var path = obj.path || ''; return obj.name + version + path; } var Pkg = /*#__PURE__*/ function () { function Pkg(_ref) { var scope = _ref.scope, _name = _ref._name, version = _ref.version, path = _ref.path; _classCallCheck(this, Pkg); this.scope = scope; this._name = _name; this.version = version; this.path = path; } _createClass(Pkg, [{ key: "name", get: function get() { var scope = this.scope ? "@".concat(this.scope, "/") : ''; return scope + this._name; }, set: function set(name) { var parsed = parse_module_id(name); this.scope = parsed.scope; this._name = parsed._name; } }, { key: "id", get: function get() { return format(this); } }, { key: "url", get: function get() { return [this.scope, this._name, this.version || '*'].filter(Boolean).join('/') + (this.path || "/".concat(this._name, ".js")); } }, { key: "pkg", get: function get() { return "".concat(this.name, "@").concat(this.version || '*'); } }]); return Pkg; }(); // @param {string} resolved path-resolved module identifier var parse = function parse(id) { return new Pkg(parse_module_id(id)); }; module.exports = parse; parse.format = format;