Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sometimes convert result will be \uXXXX. #1

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.idea
.iml
node_modules
*-converted.js
*.log
test/test-output
*.log
9 changes: 8 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,14 @@ function native2ascii(str) {


function ascii2native(str) {
return unescape(str.split('\\').join('%'));
if(str.indexOf('\\\\u')>-1){
// fix err js minify string
return ascii2native.call(null, str.replace(/\\(\\u(\w+){4})/g, function (all, key) {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There're some template engine will render "\u1234" -> "\u1234", so it seems to restore the string one by one.

return key;
}));
}else{
return unescape(str.split('\\').join('%'));
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or process string by default way.

}
}


Expand Down
11 changes: 5 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "gulp-native2ascii",
"description": "native2ascii plugin for gulp",
"version": "0.0.3",
"version": "0.0.4",
"homepage": "https://github.com/adamme/gulp-native2ascii",
"author": {
"name": "Adam Lau",
Expand Down Expand Up @@ -29,15 +29,14 @@
"gulp",
"native2ascii"
],
"scripts": {
"test": "node test/test.js"
},
"scripts": {
"test": "node test/test.js"
},
"dependencies": {
"gulp-util": "^2.2.14",
"through2": "^0.4.1"
},
"devDependencies": {
"gulp-mocha": "^0.4.1",
"gulp-rename": "^1.1.0"
"del": "^0.1.3"
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think use different dir for test-cases and test-output is more clearly,:D

}
}
1 change: 1 addition & 0 deletions test/cases/default.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log('你好,世界!');
1 change: 1 addition & 0 deletions test/cases/errMinify.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log('\\u7f16\\u7801\\u8f6c\\u6362');
File renamed without changes.
1 change: 0 additions & 1 deletion test/default.js

This file was deleted.

51 changes: 34 additions & 17 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,37 @@
var n2a = require('../');
var rename = require('gulp-rename');
var gulp = require('gulp');
var gulp = require('gulp'),
n2a = require('../'),
del = require('del');

const TEST_CASE = './test/cases/';
const OUTPUT = './test/test-output/';

gulp.src('./**/default.js', {buffer: false})
.pipe(n2a())
.pipe(rename(function (path) {
path.basename += "-converted";
}))
.pipe(gulp.dest('./'));
/** 1. clean up output files. **/
del([OUTPUT], function (err) {
if (err) {
console.error("clean up output files failed.");
} else {
testCase();
}
});

gulp.src('./**/reverse.js')
.pipe(n2a({
reverse: true
}))
.pipe(rename(function (path) {
path.basename += "-converted";
}))
.pipe(gulp.dest('./'));
function testCase() {
/** 2. convert unicode to ascii. **/
gulp.src(TEST_CASE + 'default.js', {buffer: false})
.pipe(n2a())
.pipe(gulp.dest(OUTPUT));

/** 3. convert ascii to unicode. **/
gulp.src(TEST_CASE + 'reverse.js')
.pipe(n2a({
reverse: true
}))
.pipe(gulp.dest(OUTPUT));

/** 4. fixs error uglify ascii string to unicode **/
gulp.src(TEST_CASE + 'errMinify.js')
.pipe(n2a({
reverse: true
}))
.pipe(gulp.dest(OUTPUT));

}