Skip to content

Commit

Permalink
⭐ new(path): Keypath should parse if sub path contains spaces. (#533)…
Browse files Browse the repository at this point in the history
… by @exoego

* feat(path): Keypath should parse if sub path contains spaces.

* feat(path): Added test for whitespaces
  • Loading branch information
exoego authored and kazupon committed Mar 14, 2019
1 parent 70eedb1 commit 640daaf
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 1 deletion.
1 change: 0 additions & 1 deletion src/path.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,6 @@ function getPathCharType (ch: ?string): string {
case 0x2D: // -
return 'ident'

case 0x20: // Space
case 0x09: // Tab
case 0x0A: // Newline
case 0x0D: // Return
Expand Down
7 changes: 7 additions & 0 deletions test/unit/basic.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,13 @@ describe('basic', () => {
it('should be translated', () => {
assert.strictEqual(i18n.t('message.format'), messages.en.message.format)
})

it('should be translated if keypath contains spaces', () => {
assert.strictEqual(
i18n.t('message.Hello {0}', ['kazupon']),
'Hello kazupon'
)
})
})

describe('array keypath', () => {
Expand Down
1 change: 1 addition & 0 deletions test/unit/fixture/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export default {
circular2: 'Bar @:message.circular3',
circular3: 'Buz @:message.circular1',
linkTwice: '@:message.hello: @:message.hello',
'Hello {0}': 'Hello {0}',
'hyphen-locale': 'hello hyphen',
'1234': 'Number-based keys are found',
'1mixedKey': 'Mixed keys are not found.',
Expand Down
17 changes: 17 additions & 0 deletions test/unit/path.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,28 @@ describe('path', () => {
})
})

describe('whitespace', () => {
it('should get value if it contains space 0x20', () => {
const val = path.getPathValue({ 'a c': 1 }, 'a c')
assert.strictEqual(val, 1)
})

it('should return null if it contains whitespace chars except space 0x20', () => {
const val = path.getPathValue({ 'a\tc': 1 }, 'a\tc')
assert.strictEqual(val, null)
})
})

describe('object', () => {
it('should get path value', () => {
const val = path.getPathValue({ a: { b: 1 } }, 'a')
assert.strictEqual(val.b, 1)
})

it('should accept space 0x20 as keypath', () => {
const val = path.getPathValue({ a: { 'b c d': 1 } }, 'a.b c d')
assert.strictEqual(val, 1)
})
})

describe('number key in object', () => {
Expand Down

0 comments on commit 640daaf

Please sign in to comment.