-
-
Notifications
You must be signed in to change notification settings - Fork 467
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from ofostier/Hash-management
Hash management
- Loading branch information
Showing
4 changed files
with
72 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
const getHashSearchParams = (location) => { | ||
const hash = location.hash.slice(1); | ||
const [prefix, query] = hash.split('?'); | ||
|
||
return [prefix, new URLSearchParams(query)]; | ||
}; | ||
|
||
export const getHashParam = (location = window.location) => { | ||
const [, searchParams] = getHashSearchParams(location); | ||
return searchParams; | ||
}; | ||
|
||
export const setHashParam = (key, value, location = window.location) => { | ||
const [prefix, searchParams] = getHashSearchParams(location); | ||
|
||
if (typeof value === 'undefined') { | ||
searchParams.delete(key); | ||
} else { | ||
searchParams.set(key, value); | ||
} | ||
|
||
const search = searchParams.toString(); | ||
location.hash = search ? `${prefix}?${search}` : prefix; | ||
}; | ||
|
||
export const setHash = (settings) => { | ||
Object.entries(settings).forEach(([k, v]) => { | ||
setHashParam(k, v); | ||
}); | ||
}; | ||
|
||
export const stringToBoolean = (stringValue) => { | ||
switch (stringValue?.toLowerCase()?.trim()) { | ||
case 'true': | ||
case 'yes': | ||
case '1': | ||
return true; | ||
|
||
case 'false': | ||
case 'no': | ||
case '0': | ||
case null: | ||
case undefined: | ||
return false; | ||
|
||
default: | ||
return false; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters