From 2f1b72caa05ed35dbf836ddbffdf2ad32a4fc3ca Mon Sep 17 00:00:00 2001 From: corvofeng Date: Mon, 1 Nov 2021 10:27:49 +0800 Subject: [PATCH] Fix implicit conversion in string made by numbers. Signed-off-by: corvofeng --- src/json2yaml.js | 4 +++- test/basic.js | 3 ++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/src/json2yaml.js b/src/json2yaml.js index 9459928..4d390e8 100644 --- a/src/json2yaml.js +++ b/src/json2yaml.js @@ -82,7 +82,9 @@ } function normalizeString(str) { - if (str.match(/^[\w]+$/)) { + if (str.match(/^[\d]+$/)) { + return '"' + str + '"'; + } else if(str.match(/^[\w]+$/)) { return str; } else { return '"'+escape(str).replace(/%u/g,'\\u').replace(/%U/g,'\\U').replace(/%/g,'\\x')+'"'; diff --git a/test/basic.js b/test/basic.js index 85a47f4..a63d83e 100644 --- a/test/basic.js +++ b/test/basic.js @@ -21,4 +21,5 @@ test({ hello: '你好, 世界!'}); test({ hello: '#你好, 世界!'}); test({ hello: '\\你好, 世界!'}); test({ hello: '"你好, 世界!'}); -test({ hello: '%你好, 世界!'}); \ No newline at end of file +test({ hello: '%你好, 世界!'}); +test({ hello: '1' }); \ No newline at end of file