Skip to content

Commit

Permalink
Merge pull request #5 from rambler-digital-solutions/debug
Browse files Browse the repository at this point in the history
feat(debug): add debug
  • Loading branch information
andrepolischuk authored Aug 14, 2024
2 parents 4403ec3 + 93c27f6 commit 13a9108
Show file tree
Hide file tree
Showing 9 changed files with 164 additions and 1 deletion.
4 changes: 4 additions & 0 deletions .size-limit.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
"path": "packages/cookie-storage/dist/index.js",
"limit": "990 B"
},
{
"path": "packages/debug/dist/index.js",
"limit": "3.4 KB"
},
{
"path": "packages/local-storage/dist/index.js",
"limit": "290 B"
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Common utils used by Rambler team
## Packages

- [@rambler-tech/cookie-storage](packages/cookie-storage)
- [@rambler-tech/debug](packages/debug)
- [@rambler-tech/lhci-report](packages/lhci-report)
- [@rambler-tech/local-storage](packages/local-storage)
- [@rambler-tech/session-storage](packages/session-storage)
Expand Down
15 changes: 15 additions & 0 deletions packages/debug/README.md
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
```
56 changes: 56 additions & 0 deletions packages/debug/index.test.ts
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')
})
43 changes: 43 additions & 0 deletions packages/debug/index.ts
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'
18 changes: 18 additions & 0 deletions packages/debug/package.json
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"
}
}
9 changes: 9 additions & 0 deletions packages/debug/tsconfig.json
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"]
}
5 changes: 5 additions & 0 deletions packages/debug/typedoc.json
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"]
}
14 changes: 13 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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==
Expand Down

0 comments on commit 13a9108

Please sign in to comment.