Skip to content

Commit 887e719

Browse files
committed
fix(tokens): setting new tokens locally should not remove defaults
1 parent 8e38856 commit 887e719

File tree

2 files changed

+16
-7
lines changed

2 files changed

+16
-7
lines changed

__tests__/masker.test.js

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import masker, { setTokens } from '../src/masker'
22

3+
const tokens = {
4+
'F': { pattern: /[a-f0-9]/i },
5+
}
6+
37
test('no mask given', () => {
48
expect(masker('123')).toMatchObject({masked: '123', raw: '123'})
59
})
@@ -14,10 +18,12 @@ test('multiple mask given', () => {
1418
expect(masker('123', config)).toMatchObject({ masked: '12.3', raw: '123' })
1519
})
1620

17-
test('should override default tokens', () => {
18-
const tokens = {
19-
'F': { pattern: /[a-f0-9]/i },
20-
}
21+
test('should just append to the tokens when passing them in locally', () => {
22+
expect(masker('456DDS', { mask: '###-FFFF', tokens })).toMatchObject({ masked: '456-DD', raw: '456DD' })
23+
})
24+
25+
test('should override default tokens globally', () => {
2126
setTokens(tokens)
22-
expect(masker('1DFS', { mask: '#FFFF'})).toMatchObject({ masked: '#1DF', raw: '1DF' })
23-
})
27+
expect(masker('456DDS', { mask: '###-FFFF'})).toMatchObject({ masked: '###-456D', raw: '456D' })
28+
})
29+

src/masker.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,10 @@ export function dynamic(value, config = {}) {
2929
}
3030

3131
export function formatter(value = '', config = {}) {
32-
const { mask = '', tokens = tokenDefinitions, short = false } = config
32+
let { mask = '', tokens, short = false } = config
33+
34+
// append/override global tokens instead of complete override
35+
tokens = tokens ? Object.assign({}, tokenDefinitions, tokens) : tokenDefinitions
3336

3437
// ensure we have a string
3538
value = value.toString()

0 commit comments

Comments
 (0)