history-query-enhancer
Version:
Enhance session history with query property
96 lines (87 loc) • 4.28 kB
JavaScript
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
typeof define === 'function' && define.amd ? define(factory) :
(global.HistoryQueryEnhancer = factory());
}(this, (function () { 'use strict';
/*! *****************************************************************************
Copyright (c) Microsoft Corporation. All rights reserved.
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
THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
MERCHANTABLITY OR NON-INFRINGEMENT.
See the Apache Version 2.0 License for specific language governing permissions
and limitations under the License.
***************************************************************************** */
var __assign = Object.assign || function __assign(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
}
return t;
};
function __rest(s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) if (e.indexOf(p[i]) < 0)
t[p[i]] = s[p[i]];
return t;
}
var withQuery = function (_a) {
var parse = _a.parse, stringify = _a.stringify;
return function (history) {
var enhance = function (location) { return (__assign({}, location, { query: parse(location.search) })); };
var diminish = function (_a) {
var query = _a.query, location = __rest(_a, ["query"]);
return (__assign({}, location, { search: query && typeof query === 'object' ? stringify(query) : location.search }));
};
var enhancedHistory = __assign({}, history, { location: enhance(history.location), push: function (path, state) {
if (typeof path !== 'object') {
return history.push(path, state);
}
return history.push(diminish(path));
},
replace: function (path, state) {
if (typeof path !== 'object') {
return history.replace(path, state);
}
return history.replace(diminish(path));
},
block: function (prompt) {
if (typeof prompt !== 'function') {
return history.block(prompt);
}
return history.block(function (location, action) { return prompt(enhance(location), action); });
},
listen: function (listener) {
return history.listen(function (location, action) { return listener(enhance(location), action); });
},
createHref: function (location) {
return history.createHref(diminish(location));
} });
Object.defineProperties(enhancedHistory, {
length: {
get: function () {
return history.length;
},
},
action: {
get: function () {
return history.action;
},
},
location: {
get: function () {
return enhance(history.location);
},
},
});
return enhancedHistory;
};
};
return withQuery;
})));