UNPKG

react-view-router

Version:
188 lines (183 loc) 6.56 kB
import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties"; import _defineProperty from "@babel/runtime/helpers/defineProperty"; var _excluded = ["configurable", "enumerable"]; function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; } function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; } import "core-js/modules/es6.array.filter.js"; import "core-js/modules/es6.object.to-string.js"; import "core-js/modules/es6.regexp.to-string.js"; import "core-js/modules/es6.array.slice.js"; import "core-js/modules/es6.regexp.search.js"; import "core-js/modules/es6.symbol.js"; import "core-js/modules/es7.object.get-own-property-descriptors.js"; var CAN_USE_DOM = !!(typeof window !== 'undefined' && window.document && window.document.createElement); // eslint-disable-next-line no-constant-condition var freeze = false /** __DEV__* */ ? obj => Object.freeze(obj) : obj => obj; function getPossibleHashType(_window = document.defaultView, hash = '') { if (!hash || !hash.startsWith('#')) hash = _window.location.hash; var locationHash = hash.substr(1, 1); return !locationHash || locationHash === '/' ? 'slash' : 'noslash'; } function clamp(n, lowerBound, upperBound) { return Math.min(Math.max(n, lowerBound), upperBound); } // function promptBeforeUnload(event: BeforeUnloadEvent) { // // Cancel the event. // event.preventDefault(); // // Chrome (and legacy IE) requires returnValue to be set. // event.returnValue = ''; // } function createEvents() { var handlers = []; return { get length() { return handlers.length; }, push(fn) { handlers.push(fn); return function () { handlers = handlers.filter(handler => handler !== fn); }; }, call(arg, payload) { handlers.forEach(fn => fn && fn(arg, payload)); } }; } function createKey() { return Math.random().toString(36).substr(2, 8); } function getBaseHref() { if (!CAN_USE_DOM) return ''; var base = globalThis.document.querySelector('base'); var href = ''; if (base && base.getAttribute('href')) { var url = globalThis.location.href; var hashIndex = url.indexOf('#'); href = hashIndex === -1 ? url : url.slice(0, hashIndex); } return href; } /** * Creates a string URL path from the given pathname, search, and hash components. * * @see https://github.com/ReactTraining/history/tree/master/docs/api-reference.md#createpath */ function createPath({ pathname = '/', search = '', hash = '' }) { return pathname + search + hash; } /** * Parses a string URL path into its separate pathname, search, and hash components. * * @see https://github.com/ReactTraining/history/tree/master/docs/api-reference.md#parsepath */ function parsePath(path) { var partialPath = {}; if (path) { var hashIndex = path.indexOf('#'); if (hashIndex >= 0) { partialPath.hash = path.substr(hashIndex); path = path.substr(0, hashIndex); if (path && !path.startsWith('/')) path = '/' + path; } var searchIndex = path.indexOf('?'); if (searchIndex >= 0) { partialPath.search = path.substr(searchIndex); path = path.substr(0, searchIndex); if (path && !path.startsWith('/')) path = '/' + path; } if (path) { partialPath.pathname = path; } } return partialPath; } function createHref(to, hashType, _window = globalThis) { var path = typeof to === 'string' ? to : createPath(to); if (!hashType) hashType = getPossibleHashType(_window, path); if (hashType != null) { var pathPrefix = ''; if (path.startsWith('#')) { pathPrefix = '#'; path = path.substr(1, path.length); } var slashChar = path.substr(0, 1); if (hashType === 'slash') { if (slashChar !== '/') path = '/' + path; } else if (hashType === 'noslash') { if (slashChar === '/') path = path.substr(1); } path = pathPrefix + path; } return path; } function allowTxWithParams(blockers, params) { var resultPayload; var count = blockers.length; var cb = params.callback; function callback(ok, payload) { if (!cb) return; if (!ok) { cb(ok); cb = null; return; } if (arguments.length > 1) resultPayload = payload; if (! --count) { cb(ok, resultPayload); cb = null; } } return !blockers.length || (blockers.call(_objectSpread(_objectSpread({}, params), {}, { callback })), false); } function allowTx(blockers, action, location, index, nextIndex, callback) { return allowTxWithParams(blockers, { action, location, index, nextIndex, callback }); } function readonly(obj, key, get, options) { var _ref = options || {}, _ref$configurable = _ref.configurable, configurable = _ref$configurable === void 0 ? true : _ref$configurable, _ref$enumerable = _ref.enumerable, enumerable = _ref$enumerable === void 0 ? true : _ref$enumerable, restOptions = _objectWithoutProperties(_ref, _excluded); Object.defineProperty(obj, key, _objectSpread({ get, configurable, enumerable }, restOptions)); return obj; } var _hasOwnProperty = Object.prototype.hasOwnProperty; function hasOwnProp(obj, key) { return Boolean(obj) && _hasOwnProperty.call(obj, key); } function copyOwnProperty(target, key, source) { if (!target || !source) return; var d = Object.getOwnPropertyDescriptor(source, key); d && Object.defineProperty(target, key, d); return d; } function copyOwnProperties(target, source, overwrite) { if (!target || !source) { return target; } Object.getOwnPropertyNames(source).forEach(key => { if (!overwrite && hasOwnProp(target, key)) return; copyOwnProperty(target, key, source); }); return target; } export { CAN_USE_DOM, freeze, getPossibleHashType, clamp, createEvents, createKey, getBaseHref, createPath, parsePath, createHref, allowTxWithParams, allowTx, readonly, hasOwnProp, copyOwnProperty, copyOwnProperties }; //# sourceMappingURL=utils.js.map