Files
element-web/src/vector/url_utils.ts
Michael Telatynski c044e1a00c fix typescript
Signed-off-by: Michael Telatynski <7t3chguy@gmail.com>
2020-04-12 01:54:48 +01:00

53 lines
1.7 KiB
TypeScript

/*
Copyright 2018, 2020 New Vector Ltd
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.
*/
interface IParamsObject {
[key: string]: string;
}
function searchParamsToObject(params: URLSearchParams) {
return <IParamsObject>Object.fromEntries([...params.entries()]);
}
// We want to support some name / value pairs in the fragment
// so we're re-using query string like format
//
// returns {location, params}
export function parseQsFromFragment(location: Location) {
// if we have a fragment, it will start with '#', which we need to drop.
// (if we don't, this will return '').
const fragment = location.hash.substring(1);
// our fragment may contain a query-param-like section. we need to fish
// this out *before* URI-decoding because the params may contain ? and &
// characters which are only URI-encoded once.
const hashparts = fragment.split('?');
const result = {
location: decodeURIComponent(hashparts[0]),
params: <IParamsObject>{},
};
if (hashparts.length > 1) {
result.params = searchParamsToObject(new URLSearchParams(hashparts[1]));
}
return result;
}
export function parseQs(location: Location) {
return searchParamsToObject(new URLSearchParams(location.search));
}