From 5b81c7b381c505a5a47166df0e2c41060106c4c7 Mon Sep 17 00:00:00 2001 From: Douglas Crockford Date: Fri, 1 May 2015 18:07:21 -0700 Subject: [PATCH] ternary --- json.js | 17 +++++++++-------- json2.js | 17 +++++++++-------- 2 files changed, 18 insertions(+), 16 deletions(-) diff --git a/json.js b/json.js index 8f5d91e..d188cb2 100644 --- a/json.js +++ b/json.js @@ -1,6 +1,6 @@ /* json.js - 2014-02-25 + 2015-05-01 Public Domain @@ -343,8 +343,8 @@ if (typeof JSON !== 'object') { v = partial.length === 0 ? '[]' : gap - ? '[\n' + gap + partial.join(',\n' + gap) + '\n' + mind + ']' - : '[' + partial.join(',') + ']'; + ? '[\n' + gap + partial.join(',\n' + gap) + '\n' + mind + ']' + : '[' + partial.join(',') + ']'; gap = mind; return v; } @@ -386,8 +386,8 @@ if (typeof JSON !== 'object') { v = partial.length === 0 ? '{}' : gap - ? '{\n' + gap + partial.join(',\n' + gap) + '\n' + mind + '}' - : '{' + partial.join(',') + '}'; + ? '{\n' + gap + partial.join(',\n' + gap) + '\n' + mind + '}' + : '{' + partial.join(',') + '}'; gap = mind; return v; } @@ -510,9 +510,10 @@ if (typeof JSON !== 'object') { // ',' or ':' or '{' or '}'. If that is so, then the text is safe for eval. if (/^[\],:{}\s]*$/.test( - text.replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g, '@') - .replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, ']') - .replace(/(?:^|:|,)(?:\s*\[)+/g, '') + text + .replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g, '@') + .replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, ']') + .replace(/(?:^|:|,)(?:\s*\[)+/g, '') )) { // In the third stage we use the eval function to compile the text into a diff --git a/json2.js b/json2.js index d403181..bc48164 100644 --- a/json2.js +++ b/json2.js @@ -1,6 +1,6 @@ /* json2.js - 2015-02-25 + 2015-05-01 Public Domain. @@ -311,8 +311,8 @@ if (typeof JSON !== 'object') { v = partial.length === 0 ? '[]' : gap - ? '[\n' + gap + partial.join(',\n' + gap) + '\n' + mind + ']' - : '[' + partial.join(',') + ']'; + ? '[\n' + gap + partial.join(',\n' + gap) + '\n' + mind + ']' + : '[' + partial.join(',') + ']'; gap = mind; return v; } @@ -358,8 +358,8 @@ if (typeof JSON !== 'object') { v = partial.length === 0 ? '{}' : gap - ? '{\n' + gap + partial.join(',\n' + gap) + '\n' + mind + '}' - : '{' + partial.join(',') + '}'; + ? '{\n' + gap + partial.join(',\n' + gap) + '\n' + mind + '}' + : '{' + partial.join(',') + '}'; gap = mind; return v; } @@ -483,9 +483,10 @@ if (typeof JSON !== 'object') { if ( /^[\],:{}\s]*$/.test( - text.replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g, '@') - .replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, ']') - .replace(/(?:^|:|,)(?:\s*\[)+/g, '') + text + .replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g, '@') + .replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, ']') + .replace(/(?:^|:|,)(?:\s*\[)+/g, '') ) ) {