@knowark/loggarkjs 0.3.6 → 0.3.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/index.d.ts +12 -0
- package/lib/index.js +1 -0
- package/lib/logger.test.js +7 -8
- package/lib/translator.js +22 -0
- package/lib/translator.test.js +35 -0
- package/package.json +1 -1
package/lib/index.d.ts
CHANGED
|
@@ -21,3 +21,15 @@ export declare class Logger {
|
|
|
21
21
|
|
|
22
22
|
debug(...data: any[]): void
|
|
23
23
|
}
|
|
24
|
+
|
|
25
|
+
export declare class Translator {
|
|
26
|
+
static translate(key: string, options?: object): string
|
|
27
|
+
|
|
28
|
+
constructor (options?: object)
|
|
29
|
+
|
|
30
|
+
translate(key: string, options?: object): string
|
|
31
|
+
|
|
32
|
+
t(key: string, options?: object): string
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export declare function t(key: string, options?: object): string
|
package/lib/index.js
CHANGED
package/lib/logger.test.js
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { it, expect } from '@jest/globals'
|
|
2
2
|
import { Logger } from './logger.js'
|
|
3
3
|
|
|
4
|
-
|
|
5
4
|
function setup () {
|
|
6
5
|
const mockGlobal = {
|
|
7
6
|
console: {
|
|
@@ -15,17 +14,17 @@ function setup () {
|
|
|
15
14
|
return {mockGlobal}
|
|
16
15
|
}
|
|
17
16
|
|
|
18
|
-
|
|
17
|
+
it('can be instantiated', () => {
|
|
19
18
|
const logger = new Logger()
|
|
20
19
|
expect(logger).toBeTruthy()
|
|
21
20
|
})
|
|
22
21
|
|
|
23
|
-
|
|
22
|
+
it('can be instantiated', () => {
|
|
24
23
|
const logger = new Logger()
|
|
25
24
|
expect(logger).toBeTruthy()
|
|
26
25
|
})
|
|
27
26
|
|
|
28
|
-
|
|
27
|
+
it('is disabled by default', () => {
|
|
29
28
|
const { mockGlobal } = setup()
|
|
30
29
|
const logger = new Logger({ global: mockGlobal })
|
|
31
30
|
|
|
@@ -45,7 +44,7 @@ test('is disabled by default', () => {
|
|
|
45
44
|
expect(mockGlobal.debugArgs).toBeFalsy()
|
|
46
45
|
})
|
|
47
46
|
|
|
48
|
-
|
|
47
|
+
it('adds a prefix to the logging methods according to loglevel', () => {
|
|
49
48
|
const { mockGlobal } = setup()
|
|
50
49
|
mockGlobal.LOGLEVEL = 'debug'
|
|
51
50
|
const logger = new Logger({ global: mockGlobal })
|
|
@@ -66,7 +65,7 @@ test('adds a prefix to the logging methods according to loglevel', () => {
|
|
|
66
65
|
expect(mockGlobal.debugArgs).toEqual(['[DEBUG]', 'Logging something...'])
|
|
67
66
|
})
|
|
68
67
|
|
|
69
|
-
|
|
68
|
+
it('adds the provided logging context labels to its final output', () => {
|
|
70
69
|
const { mockGlobal } = setup()
|
|
71
70
|
mockGlobal.LOGLEVEL = 'debug'
|
|
72
71
|
const context = { correlationId: 'ABCD1234', interactor: 'Informer' }
|
|
@@ -103,7 +102,7 @@ test('adds the provided logging context labels to its final output', () => {
|
|
|
103
102
|
'Logging something...'])
|
|
104
103
|
})
|
|
105
104
|
|
|
106
|
-
|
|
105
|
+
it('supports including static context labels', () => {
|
|
107
106
|
Logger.context = () => {
|
|
108
107
|
return { correlationId: 'ABCD1234' }
|
|
109
108
|
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
export class Translator {
|
|
2
|
+
static translate (key, _options) {
|
|
3
|
+
return key
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
#options = {}
|
|
7
|
+
|
|
8
|
+
constructor (options = {}) {
|
|
9
|
+
this.#options = options
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
translate (key, options) {
|
|
13
|
+
return this.constructor.translate(
|
|
14
|
+
key, { ...this.#options, ...options })
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
get t () {
|
|
18
|
+
return this.translate.bind(this)
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export const t = (key, options) => Translator.translate(key, options)
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { it, expect } from '@jest/globals'
|
|
2
|
+
import { Translator, t } from './translator.js'
|
|
3
|
+
|
|
4
|
+
it('can be instantiated', () => {
|
|
5
|
+
const translator = new Translator()
|
|
6
|
+
expect(translator).toBeTruthy()
|
|
7
|
+
})
|
|
8
|
+
|
|
9
|
+
it('defines a translate function with options', () => {
|
|
10
|
+
const translator = new Translator()
|
|
11
|
+
|
|
12
|
+
const result = translator.translate('any.key')
|
|
13
|
+
|
|
14
|
+
expect(result).toBe('any.key')
|
|
15
|
+
})
|
|
16
|
+
|
|
17
|
+
it('defines a t shortcut for function translate', () => {
|
|
18
|
+
const t = new Translator().t
|
|
19
|
+
|
|
20
|
+
const result = t('any.key')
|
|
21
|
+
|
|
22
|
+
expect(result).toBe('any.key')
|
|
23
|
+
})
|
|
24
|
+
|
|
25
|
+
it('defines a global t shortcut translate', () => {
|
|
26
|
+
const originalTranslate = Translator.translate
|
|
27
|
+
Translator.translate = (key, _options) => {
|
|
28
|
+
return `translated:${key}`
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const result = t('any.key')
|
|
32
|
+
|
|
33
|
+
expect(result).toBe('translated:any.key')
|
|
34
|
+
Translator.translate = originalTranslate
|
|
35
|
+
})
|