Skip to content

Commit 5d51ad0

Browse files
csleekazupon
authored andcommitted
⚡ improvement: To support double quote, tab, newline characters in value (#8) by @cslee
* To support double quote, tab, newline characters in value * To support double quote, tab, newline characters in value - add test cases * Add cross-env
1 parent ce76b58 commit 5d51ad0

File tree

4 files changed

+37
-5
lines changed

4 files changed

+37
-5
lines changed

lib/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ function generateCode(content) {
2424
var code = '';
2525

2626
var value = typeof content === 'string' ? JSON.parse(content) : content;
27-
value = JSON.stringify(value).replace(/\u2028/g, '\\u2028').replace(/\u2029/g, '\\u2029');
27+
value = JSON.stringify(value).replace(/\u2028/g, '\\u2028').replace(/\u2029/g, '\\u2029').replace(/\\/g, '\\\\');
2828

2929
code += 'function (Component) {\n Component.options.__i18n = Component.options.__i18n || []\n Component.options.__i18n.push(\'' + value.replace(/\u0027/g, '\\u0027') + '\')\n}\n';
3030
return code;

package.json

+5-4
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
"babel-preset-es2015": "^6.22.0",
2222
"conventional-changelog-cli": "^1.2.0",
2323
"conventional-github-releaser": "^1.1.3",
24+
"cross-env": "^5.0.5",
2425
"eslint": "^3.18.0",
2526
"eslint-config-vue": "^2.0.2",
2627
"eslint-plugin-vue": "^2.0.1",
@@ -48,15 +49,15 @@
4849
"url": "git+https://github.com/kazupon/vue-i18n-loader.git"
4950
},
5051
"scripts": {
51-
"build": "BABEL_ENV=production babel ./src --out-dir ./lib",
52+
"build": "cross-env BABEL_ENV=production babel ./src --out-dir ./lib",
5253
"changelog": "conventional-changelog -i CHANGELOG.md -s -n ./node_modules/git-commit-message-convention/convention.js",
5354
"clean": "rm -rf ./coverage && rm -rf ./lib/*.js*",
5455
"coverage": "./node_modules/.bin/nyc report --reporter=text-lcov > coverage.lcov",
5556
"lint": "eslint ./src ./test",
5657
"release": "conventional-github-releaser -n ./node_modules/git-commit-message-convention/convention.js",
5758
"test": "npm run lint && npm run test:cover",
58-
"test:cover": "BABEL_ENV=test ./node_modules/.bin/nyc report --reporter=html ava",
59-
"test:unit": "BABEL_ENV=test ava",
60-
"watch": "BABEL_ENV=development babel ./src --out-dir ./lib --watch"
59+
"test:cover": "cross-env BABEL_ENV=test ./node_modules/.bin/nyc report --reporter=html ava",
60+
"test:unit": "cross-env BABEL_ENV=test ava",
61+
"watch": "cross-env BABEL_ENV=development babel ./src --out-dir ./lib --watch"
6162
}
6263
}

src/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ function generateCode (content) {
2323
value = JSON.stringify(value)
2424
.replace(/\u2028/g, '\\u2028')
2525
.replace(/\u2029/g, '\\u2029')
26+
.replace(/\\/g, '\\\\')
2627

2728
code += `function (Component) {
2829
Component.options.__i18n = Component.options.__i18n || []

test/index.test.js

+30
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,16 @@ function assert (t, content) {
2525
)
2626
}
2727

28+
function assertSpecial (t, content) {
29+
const loader = new Loader(content, 2)
30+
t.deepEqual(
31+
loader._callback.content, `module.exports = function (Component) {
32+
Component.options.__i18n = Component.options.__i18n || []
33+
Component.options.__i18n.push('{\"en\":{\"hello\":\"hello\\\\ngreat\\\\t\\\\\"world\\\\\"\"}}')
34+
}\n`
35+
)
36+
}
37+
2838
test('string', t => {
2939
const json = JSON.stringify({
3040
en: {
@@ -45,6 +55,26 @@ test('object', t => {
4555
assert(t, json)
4656
})
4757

58+
test('string with special characters', t => {
59+
const json = JSON.stringify({
60+
en: {
61+
'hello': 'hello\ngreat\t"world"'
62+
}
63+
})
64+
65+
assertSpecial(t, json)
66+
})
67+
68+
test('object with special characters', t => {
69+
const json = {
70+
en: {
71+
'hello': 'hello\ngreat\t"world"'
72+
}
73+
}
74+
75+
assertSpecial(t, json)
76+
})
77+
4878
test('version 2 less', t => {
4979
const loader = new Loader({})
5080
t.deepEqual(loader._emitError, 'support webpack 2 later')

0 commit comments

Comments
 (0)