-
Notifications
You must be signed in to change notification settings - Fork 1
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 #5 from rambler-digital-solutions/debug
feat(debug): add debug
- Loading branch information
Showing
9 changed files
with
164 additions
and
1 deletion.
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
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,15 @@ | ||
# Debug | ||
|
||
Debug management based on [debug](https://github.com/debug-js/debug). | ||
|
||
## Install | ||
|
||
``` | ||
npm install -D @rambler-tech/debug | ||
``` | ||
|
||
or | ||
|
||
``` | ||
yarn add -D @rambler-tech/debug | ||
``` |
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,56 @@ | ||
import {getItem, setItem} from '@rambler-tech/local-storage' | ||
import {initDebug} from '.' | ||
|
||
class LocalStorage implements LocalStorage { | ||
map: {[key: string]: any} = {} | ||
getItem(key: string): void { | ||
return this.map[key] | ||
} | ||
setItem(key: string, value: any): void { | ||
this.map[key] = value | ||
} | ||
removeItem(key: string): void { | ||
delete this.map[key] | ||
} | ||
} | ||
|
||
Object.defineProperty(window, 'localStorage', { | ||
value: new LocalStorage() | ||
}) | ||
|
||
test('reset local storage debug value', () => { | ||
setItem('debug', 'test', {raw: true}) | ||
|
||
expect(getItem('debug', {raw: true})).toBe('test') | ||
|
||
initDebug('https://foobar.ru?debug=unset') | ||
|
||
expect(getItem('debug', {raw: true})).toBeNull() | ||
}) | ||
|
||
test('use window.location.href value', () => { | ||
Object.defineProperty(window, 'location', { | ||
value: { | ||
href: 'https://test.ru?debug=bar' | ||
}, | ||
writable: true | ||
}) | ||
|
||
setItem('debug', 'foo', {raw: true}) | ||
|
||
expect(getItem('debug', {raw: true})).toBe('foo') | ||
|
||
initDebug() | ||
|
||
expect(getItem('debug', {raw: true})).toBe('bar') | ||
}) | ||
|
||
test('use debug value from initDebug arg', () => { | ||
setItem('debug', 'test', {raw: true}) | ||
|
||
expect(getItem('debug', {raw: true})).toBe('test') | ||
|
||
initDebug('https://foobar.ru?debug=foo') | ||
|
||
expect(getItem('debug', {raw: true})).toBe('foo') | ||
}) |
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,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' |
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,18 @@ | ||
{ | ||
"name": "@rambler-tech/debug", | ||
"version": "0.0.0", | ||
"main": "dist", | ||
"module": "dist", | ||
"types": "dist/index.d.ts", | ||
"license": "MIT", | ||
"sideEffects": false, | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"dependencies": { | ||
"@rambler-tech/local-storage": "*", | ||
"@rambler-tech/url": "*", | ||
"@types/debug": "^4.1.7", | ||
"debug": "^3" | ||
} | ||
} |
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,9 @@ | ||
{ | ||
"extends": "../../tsconfig.json", | ||
"compilerOptions": { | ||
"module": "commonjs", | ||
"baseUrl": ".", | ||
"outDir": "dist" | ||
}, | ||
"include": [".", "../../types"] | ||
} |
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,5 @@ | ||
{ | ||
"extends": "@rambler-tech/typedoc-config", | ||
"readme": "../../README.md", | ||
"entryPoints": ["./index.ts"] | ||
} |
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 |
---|---|---|
|
@@ -1685,6 +1685,13 @@ | |
resolved "https://registry.yarnpkg.com/@types/chalk/-/chalk-0.4.31.tgz#a31d74241a6b1edbb973cf36d97a2896834a51f9" | ||
integrity sha512-nF0fisEPYMIyfrFgabFimsz9Lnuu9MwkNrrlATm2E4E46afKDyeelT+8bXfw1VSc7sLBxMxRgT7PxTC2JcqN4Q== | ||
|
||
"@types/debug@^4.1.7": | ||
version "4.1.12" | ||
resolved "https://registry.yarnpkg.com/@types/debug/-/debug-4.1.12.tgz#a155f21690871953410df4b6b6f53187f0500917" | ||
integrity sha512-vIChWdVG3LG1SMxEvI/AK+FWJthlrqlTu7fbrlywTkkaONwk/UAGaULXRlf8vkzFBLVm0zkMdCquhL5aOjhXPQ== | ||
dependencies: | ||
"@types/ms" "*" | ||
|
||
"@types/graceful-fs@^4.1.3": | ||
version "4.1.7" | ||
resolved "https://registry.yarnpkg.com/@types/graceful-fs/-/graceful-fs-4.1.7.tgz#30443a2e64fd51113bc3e2ba0914d47109695e2a" | ||
|
@@ -1753,6 +1760,11 @@ | |
resolved "https://registry.yarnpkg.com/@types/minimist/-/minimist-1.2.2.tgz#ee771e2ba4b3dc5b372935d549fd9617bf345b8c" | ||
integrity sha512-jhuKLIRrhvCPLqwPcx6INqmKeiA5EWrsCOPhrlFSrbrmU4ZMPjj5Ul/oLCMDO98XRUIwVm78xICz4EPCektzeQ== | ||
|
||
"@types/ms@*": | ||
version "0.7.34" | ||
resolved "https://registry.yarnpkg.com/@types/ms/-/ms-0.7.34.tgz#10964ba0dee6ac4cd462e2795b6bebd407303433" | ||
integrity sha512-nG96G3Wp6acyAgJqGasjODb+acrI7KltPiRxzHPXnP3NgI28bpQDRv53olbqGXbfcgF5aiiHmO3xpwEpS5Ld9g== | ||
|
||
"@types/node@*": | ||
version "20.6.3" | ||
resolved "https://registry.yarnpkg.com/@types/node/-/node-20.6.3.tgz#5b763b321cd3b80f6b8dde7a37e1a77ff9358dd9" | ||
|
@@ -3162,7 +3174,7 @@ debug@4, [email protected], debug@^4.1.0, debug@^4.1.1, debug@^4.3.1, debug@^4.3.2, de | |
dependencies: | ||
ms "2.1.2" | ||
|
||
debug@^3.1.0, debug@^3.2.7: | ||
debug@^3, debug@^3.1.0, debug@^3.2.7: | ||
version "3.2.7" | ||
resolved "https://registry.yarnpkg.com/debug/-/debug-3.2.7.tgz#72580b7e9145fb39b6676f9c5e5fb100b934179a" | ||
integrity sha512-CFjzYYAi4ThfiQvizrFQevTTXHtnCqWfe7x1AhgEscTz6ZbLbfoLRLPugTQyBth6f8ZERVUSyWHFD/7Wu4t1XQ== | ||
|