Skip to content

Commit

Permalink
⚡ improvement: To support double quote, tab, newline characters in va…
Browse files Browse the repository at this point in the history
…lue (#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
  • Loading branch information
cslee authored and kazupon committed Aug 25, 2017
1 parent ce76b58 commit 5d51ad0
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 5 deletions.
2 changes: 1 addition & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ function generateCode(content) {
var code = '';

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

code += 'function (Component) {\n Component.options.__i18n = Component.options.__i18n || []\n Component.options.__i18n.push(\'' + value.replace(/\u0027/g, '\\u0027') + '\')\n}\n';
return code;
Expand Down
9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"babel-preset-es2015": "^6.22.0",
"conventional-changelog-cli": "^1.2.0",
"conventional-github-releaser": "^1.1.3",
"cross-env": "^5.0.5",
"eslint": "^3.18.0",
"eslint-config-vue": "^2.0.2",
"eslint-plugin-vue": "^2.0.1",
Expand Down Expand Up @@ -48,15 +49,15 @@
"url": "git+https://github.com/kazupon/vue-i18n-loader.git"
},
"scripts": {
"build": "BABEL_ENV=production babel ./src --out-dir ./lib",
"build": "cross-env BABEL_ENV=production babel ./src --out-dir ./lib",
"changelog": "conventional-changelog -i CHANGELOG.md -s -n ./node_modules/git-commit-message-convention/convention.js",
"clean": "rm -rf ./coverage && rm -rf ./lib/*.js*",
"coverage": "./node_modules/.bin/nyc report --reporter=text-lcov > coverage.lcov",
"lint": "eslint ./src ./test",
"release": "conventional-github-releaser -n ./node_modules/git-commit-message-convention/convention.js",
"test": "npm run lint && npm run test:cover",
"test:cover": "BABEL_ENV=test ./node_modules/.bin/nyc report --reporter=html ava",
"test:unit": "BABEL_ENV=test ava",
"watch": "BABEL_ENV=development babel ./src --out-dir ./lib --watch"
"test:cover": "cross-env BABEL_ENV=test ./node_modules/.bin/nyc report --reporter=html ava",
"test:unit": "cross-env BABEL_ENV=test ava",
"watch": "cross-env BABEL_ENV=development babel ./src --out-dir ./lib --watch"
}
}
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ function generateCode (content) {
value = JSON.stringify(value)
.replace(/\u2028/g, '\\u2028')
.replace(/\u2029/g, '\\u2029')
.replace(/\\/g, '\\\\')

code += `function (Component) {
Component.options.__i18n = Component.options.__i18n || []
Expand Down
30 changes: 30 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,16 @@ function assert (t, content) {
)
}

function assertSpecial (t, content) {
const loader = new Loader(content, 2)
t.deepEqual(
loader._callback.content, `module.exports = function (Component) {
Component.options.__i18n = Component.options.__i18n || []
Component.options.__i18n.push('{\"en\":{\"hello\":\"hello\\\\ngreat\\\\t\\\\\"world\\\\\"\"}}')
}\n`
)
}

test('string', t => {
const json = JSON.stringify({
en: {
Expand All @@ -45,6 +55,26 @@ test('object', t => {
assert(t, json)
})

test('string with special characters', t => {
const json = JSON.stringify({
en: {
'hello': 'hello\ngreat\t"world"'
}
})

assertSpecial(t, json)
})

test('object with special characters', t => {
const json = {
en: {
'hello': 'hello\ngreat\t"world"'
}
}

assertSpecial(t, json)
})

test('version 2 less', t => {
const loader = new Loader({})
t.deepEqual(loader._emitError, 'support webpack 2 later')
Expand Down

0 comments on commit 5d51ad0

Please sign in to comment.