UNPKG

ilib-common

Version:

Common utility functions for ilib. iLib is a cross-engine library of internationalization (i18n) classes written in pure JS

48 lines (47 loc) 4.69 kB
"use strict";Object.defineProperty(exports,"__esModule",{value:true});exports["default"]=void 0;function _typeof(o){"@babel/helpers - typeof";return _typeof="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(o){return typeof o}:function(o){return o&&"function"==typeof Symbol&&o.constructor===Symbol&&o!==Symbol.prototype?"symbol":typeof o},_typeof(o)}function _classCallCheck(a,n){if(!(a instanceof n))throw new TypeError("Cannot call a class as a function")}function _defineProperties(e,r){for(var t=0;t<r.length;t++){var o=r[t];o.enumerable=o.enumerable||!1,o.configurable=!0,"value"in o&&(o.writable=!0),Object.defineProperty(e,_toPropertyKey(o.key),o)}}function _createClass(e,r,t){return r&&_defineProperties(e.prototype,r),t&&_defineProperties(e,t),Object.defineProperty(e,"prototype",{writable:!1}),e}function _toPropertyKey(t){var i=_toPrimitive(t,"string");return"symbol"==_typeof(i)?i:i+""}function _toPrimitive(t,r){if("object"!=_typeof(t)||!t)return t;var e=t[Symbol.toPrimitive];if(void 0!==e){var i=e.call(t,r||"default");if("object"!=_typeof(i))return i;throw new TypeError("@@toPrimitive must return a primitive value.")}return("string"===r?String:Number)(t)}/* * Path.js - minimal pure js implementation of the nodejs path module * which can be used in web browsers as well * * Copyright © 2015, 2021-2022 JEDLSoft * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * * See the License for the specific language governing permissions and * limitations under the License. *//** * @module Path */var Path=exports["default"]=/*#__PURE__*/function(){function Path(){_classCallCheck(this,Path)}return _createClass(Path,null,[{key:"fileUriToPath",value:function fileUriToPath(uri){if(typeof uri!=="string"||uri.length<=6||!uri.startsWith("file:")){throw new TypeError("must pass in a file:// URI to convert to a file path")}var re=/^(([^:\/?#]+):)?(\/\/([^\/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?/;var match=re.exec(uri);return match?match[5]:""}}])}();/** * Return the parent directory of the given pathname * similar to the dirname shell function. * @static * @param {string} pathname path to check * @return {string} the parent dir of the given pathname */Path.dirname=function(pathname){if(!pathname)return pathname;pathname=Path.normalize(pathname);return pathname==="."?"..":Path.normalize(pathname+"/..")};/** * Return the normalized version of the given pathname. This * cleans up things like double directory separators and such. * @static * @param {string} pathname path to check * @return {string} the normalized version of the given pathname */Path.normalize=function(pathname){if(pathname){pathname=pathname.replace(/\\/g,"/");pathname=pathname.replace(/\/\//g,"/");pathname=pathname.replace(/\/\//g,"/");pathname=pathname.replace(/\/[^/]*[^\./]\/\.\./g,"/.");pathname=pathname.replace(/^[^/]*[^\./]\/\.\./g,".");pathname=pathname.replace(/\/\.\//g,"/");pathname=pathname.replace(/^\.\//,"");pathname=pathname.replace(/\/\//g,"/");pathname=pathname.replace(/\/\.$/,"");pathname=pathname.replace(/\/\//g,"/");if(pathname.length>1)pathname=pathname.replace(/\/$/,"");if(pathname.length===0)pathname="."}return pathname};/** * Return a path that is the concatenation of all the of the arguments * which each name a path segment. * @static * @param {...string} var_args * @return {string} the concatenated pathname */Path.join=function(var_args){var arr=[];for(var i=0;i<arguments.length;i++){arr.push(arguments[i]&&arguments[i].length>0?arguments[i]:".")}return Path.normalize(arr.join("/"))};/** * Return the base file name of the path. If the extension is given, * with or without the leading dot, then the extension is removed from * the base name. * @param {string} pathname the path to take the base name of * @param {string|undefined} extension the optional extension to remove * @return {string} the base name of the file without the extension */Path.basename=function(pathname,extension){var base=pathname;var slash=pathname.lastIndexOf("/");if(slash!==-1){base=pathname.substring(slash+1)}if(extension){var ext=extension[0]==="."?extension:"."+extension;var index=base.lastIndexOf(ext);if(index>-1){base=base.substring(0,index)}}return base};module.exports=exports.default; //# sourceMappingURL=Path.js.map