UNPKG

faker-api

Version:

A fully customizible rest api faking package that allows you to mock , clone and fake Rest API with fake yet realistic data

110 lines 3.87 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.PathUtil = void 0; /** * Performs an comparison of the current path to the path format * Basically it will extract dynamic parameters from the currentPath that has thesame position with a /:param/ in the pathFormat * * Example * * ```javascript * console.log(isMatch("/:id/info", "/5/info")) * ``` * * This will return * * >```json * >{ * >"id" : 5 * >} * >``` * * @param {string} pathFormat The path format to compare the currentPath to * * @param {string} currentPath The actual path which will be compared to the format and data will be extracted * * @param {boolean} stripEnd If ending trailing forward slashes would be omitted * * @returns {undefined | ParamsType} Undefined if currentPath does not match pathFormat, else an Object that contains all exracted params with key as the param name and the value as the extacted data * . */ function isMatch(pathFormat, currentPath, stripEnd) { if (stripEnd === void 0) { stripEnd = false; } /* This path is a static path return an empty parameter list, not regex comparison required to be performed */ if (pathFormat === currentPath) return {}; // Spliting both the current and path to an array with separator / var strippedPath = pathFormat.trim().replace(/^\//, ""); if (!strippedPath.endsWith("/")) { strippedPath = strippedPath + "/"; } var strippedCurrentPath = currentPath.trim().replace(/^\//, ""); if (!strippedCurrentPath.endsWith("/")) { strippedCurrentPath = strippedCurrentPath + "/"; } var _path = strippedPath.split("/"); var _currentPath = strippedCurrentPath.split("/"); // This will eliminate / at the end of paths // example /users/ will become /users if (stripEnd) { if (_path[_path.length - 1].length < 1) { _path.pop(); } if (_currentPath[_currentPath.length - 1].length < 1) { _currentPath.pop(); } } // Path segment length does not match if (_path.length !== _currentPath.length) return; var failed = false; var params = {}; for (var index = 0; index < _currentPath.length; index++) { // Remove trailing spaces to path and current path, to avoid mismatchs due to user error var _c = _currentPath[index].trim(); var _p = _path[index].trim(); if (_p === _c) { continue; } else { var paramPattern = /^\:.+$/; var endPattern = /\?$/; var optionalPattern = /^\**$/; if (_p.match(paramPattern)) { // Checks if the parameter is optional (has ? as the end) var startParamPatten = /^\:/; if (_p.match(endPattern)) { params[_p.replace(startParamPatten, "")] = _c; continue; } // The paramater is not optional , then check that the parameter is not empty if (_c.length > 0) { params[_p.replace(startParamPatten, "")] = _c; continue; } // The parameter is not optional and the parameter was not provided failed = true; break; } else if (_p.match(optionalPattern)) { // Ignore this path of the pattern and move to the next segment continue; } else { failed = true; break; } } } // Paths did not match, return undefined (nothing) if (failed) return; // Paths matches all return the parsed parameters return params; } var Path = { isMatch: isMatch, }; exports.PathUtil = Path; //# sourceMappingURL=path.js.map