history-query-enhancer
Version:
Enhance session history with query property
71 lines • 2.89 kB
JavaScript
var __assign = (this && this.__assign) || Object.assign || function(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;
};
var __rest = (this && this.__rest) || function (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;
};
};
export default withQuery;
//# sourceMappingURL=withQuery.js.map