-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
43 lines (37 loc) · 1.06 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/* eslint-disable import/no-unused-modules */
import {getUrlParams} from '@rambler-tech/url'
import {removeItem, setItem, getItem} from '@rambler-tech/local-storage'
const RESET_DEBUG_LOCAL_STORAGE_VALUE = 'unset'
/**
* Init debug mode from URL
*
* ```ts
* initDebug('https://example.com?debug=value')
* ```
*
* Possible value:
* * `*` - Include all debug namespaces
* * `unset` - Reset debug value from local storage
* * `value` - Write debug value into local storage
*/
export function initDebug(href?: string) {
if (typeof window === 'undefined') {
return
}
const {debug: queryParam} = getUrlParams(href ?? window.location.href)
const storageParam = getItem('debug', {raw: true})
if (
!queryParam ||
typeof queryParam !== 'string' ||
storageParam === queryParam ||
(!storageParam && queryParam === RESET_DEBUG_LOCAL_STORAGE_VALUE)
) {
return
}
if (queryParam === RESET_DEBUG_LOCAL_STORAGE_VALUE) {
removeItem('debug')
} else {
setItem('debug', queryParam, {raw: true})
}
}
export {default as createDebug} from 'debug'