From 640daafd2b01dca1b37141bf3cf33d381689aafd Mon Sep 17 00:00:00 2001 From: TATSUNO Yasuhiro Date: Thu, 14 Mar 2019 17:17:45 +0900 Subject: [PATCH] :star: new(path): Keypath should parse if sub path contains spaces. (#533) by @exoego * feat(path): Keypath should parse if sub path contains spaces. * feat(path): Added test for whitespaces --- src/path.js | 1 - test/unit/basic.test.js | 7 +++++++ test/unit/fixture/index.js | 1 + test/unit/path.test.js | 17 +++++++++++++++++ 4 files changed, 25 insertions(+), 1 deletion(-) diff --git a/src/path.js b/src/path.js index 2db0bbcaa..6688c3da0 100644 --- a/src/path.js +++ b/src/path.js @@ -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 diff --git a/test/unit/basic.test.js b/test/unit/basic.test.js index 9fc548bc9..78a507418 100644 --- a/test/unit/basic.test.js +++ b/test/unit/basic.test.js @@ -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', () => { diff --git a/test/unit/fixture/index.js b/test/unit/fixture/index.js index 14a144f63..87514d0e2 100644 --- a/test/unit/fixture/index.js +++ b/test/unit/fixture/index.js @@ -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.', diff --git a/test/unit/path.test.js b/test/unit/path.test.js index a53ac7fcb..efb01f597 100644 --- a/test/unit/path.test.js +++ b/test/unit/path.test.js @@ -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', () => {