indent ternary

This commit is contained in:
Douglas Crockford 2015-05-02 09:19:02 -07:00
parent 5b81c7b381
commit 1e3869cb39
4 changed files with 133 additions and 127 deletions

79
json.js
View file

@ -1,6 +1,6 @@
/*
json.js
2015-05-01
2015-05-02
Public Domain
@ -47,7 +47,9 @@
// convert the value to a date.
myData = text.parseJSON(function (key, value) {
return key.indexOf('date') >= 0 ? new Date(value) : value;
return key.indexOf('date') >= 0
? new Date(value)
: value;
});
This file will break programs with improper for..in loops. See
@ -85,7 +87,9 @@
Date.prototype.toJSON = function (key) {
function f(n) {
// Format integers to have at least two digits.
return n < 10 ? '0' + n : n;
return n < 10
? '0' + n
: n;
}
return this.getUTCFullYear() + '-' +
@ -131,8 +135,9 @@
// text is '[\n\t"e",\n\t{\n\t\t"pluribus": "unum"\n\t}\n]'
text = JSON.stringify([new Date()], function (key, value) {
return this[key] instanceof Date ?
'Date(' + this[key] + ')' : value;
return this[key] instanceof Date
? 'Date(' + this[key] + ')'
: value;
});
// text is '["Date(---current time---)"]'
@ -205,8 +210,8 @@ if (typeof JSON !== 'object') {
function f(n) {
// Format integers to have at least two digits.
return n < 10
? '0' + n
: n;
? '0' + n
: n;
}
function this_value() {
@ -218,13 +223,13 @@ if (typeof JSON !== 'object') {
Date.prototype.toJSON = function (ignore) {
return isFinite(this.valueOf())
? this.getUTCFullYear() + '-' +
f(this.getUTCMonth() + 1) + '-' +
f(this.getUTCDate()) + 'T' +
f(this.getUTCHours()) + ':' +
f(this.getUTCMinutes()) + ':' +
f(this.getUTCSeconds()) + 'Z'
: null;
? this.getUTCFullYear() + '-' +
f(this.getUTCMonth() + 1) + '-' +
f(this.getUTCDate()) + 'T' +
f(this.getUTCHours()) + ':' +
f(this.getUTCMinutes()) + ':' +
f(this.getUTCSeconds()) + 'Z'
: null;
};
Boolean.prototype.toJSON = this_value;
@ -249,13 +254,13 @@ if (typeof JSON !== 'object') {
escapable.lastIndex = 0;
return escapable.test(string)
? '"' + string.replace(escapable, function (a) {
var c = meta[a];
return typeof c === 'string'
? c
: '\\u' + ('0000' + a.charCodeAt(0).toString(16)).slice(-4);
}) + '"'
: '"' + string + '"';
? '"' + string.replace(escapable, function (a) {
var c = meta[a];
return typeof c === 'string'
? c
: '\\u' + ('0000' + a.charCodeAt(0).toString(16)).slice(-4);
}) + '"'
: '"' + string + '"';
}
@ -296,8 +301,8 @@ if (typeof JSON !== 'object') {
// JSON numbers must be finite. Encode non-finite numbers as null.
return isFinite(value)
? String(value)
: 'null';
? String(value)
: 'null';
case 'boolean':
case 'null':
@ -341,10 +346,10 @@ if (typeof JSON !== 'object') {
// brackets.
v = partial.length === 0
? '[]'
: gap
? '[\n' + gap + partial.join(',\n' + gap) + '\n' + mind + ']'
: '[' + partial.join(',') + ']';
? '[]'
: gap
? '[\n' + gap + partial.join(',\n' + gap) + '\n' + mind + ']'
: '[' + partial.join(',') + ']';
gap = mind;
return v;
}
@ -359,8 +364,8 @@ if (typeof JSON !== 'object') {
v = str(k, value);
if (v) {
partial.push(quote(k) + (gap
? ': '
: ':') + v);
? ': '
: ':') + v);
}
}
}
@ -373,8 +378,8 @@ if (typeof JSON !== 'object') {
v = str(k, value);
if (v) {
partial.push(quote(k) + (gap
? ': '
: ':') + v);
? ': '
: ':') + v);
}
}
}
@ -384,10 +389,10 @@ if (typeof JSON !== 'object') {
// and wrap them in braces.
v = partial.length === 0
? '{}'
: gap
? '{\n' + gap + partial.join(',\n' + gap) + '\n' + mind + '}'
: '{' + partial.join(',') + '}';
? '{}'
: gap
? '{\n' + gap + partial.join(',\n' + gap) + '\n' + mind + '}'
: '{' + partial.join(',') + '}';
gap = mind;
return v;
}
@ -527,8 +532,8 @@ if (typeof JSON !== 'object') {
// each name/value pair to a reviver function for possible transformation.
return typeof reviver === 'function'
? walk({'': j}, '')
: j;
? walk({'': j}, '')
: j;
}
// If the text is not JSON parseable, then a SyntaxError is thrown.

View file

@ -1,6 +1,6 @@
/*
json2.js
2015-05-01
2015-05-02
Public Domain.
@ -49,8 +49,8 @@
function f(n) {
// Format integers to have at least two digits.
return n < 10
? '0' + n
: n;
? '0' + n
: n;
}
return this.getUTCFullYear() + '-' +
@ -96,8 +96,9 @@
// text is '[\n\t"e",\n\t{\n\t\t"pluribus": "unum"\n\t}\n]'
text = JSON.stringify([new Date()], function (key, value) {
return this[key] instanceof Date ?
'Date(' + this[key] + ')' : value;
return this[key] instanceof Date
? 'Date(' + this[key] + ')'
: value;
});
// text is '["Date(---current time---)"]'
@ -173,8 +174,8 @@ if (typeof JSON !== 'object') {
function f(n) {
// Format integers to have at least two digits.
return n < 10
? '0' + n
: n;
? '0' + n
: n;
}
function this_value() {
@ -186,13 +187,13 @@ if (typeof JSON !== 'object') {
Date.prototype.toJSON = function () {
return isFinite(this.valueOf())
? this.getUTCFullYear() + '-' +
f(this.getUTCMonth() + 1) + '-' +
f(this.getUTCDate()) + 'T' +
f(this.getUTCHours()) + ':' +
f(this.getUTCMinutes()) + ':' +
f(this.getUTCSeconds()) + 'Z'
: null;
? this.getUTCFullYear() + '-' +
f(this.getUTCMonth() + 1) + '-' +
f(this.getUTCDate()) + 'T' +
f(this.getUTCHours()) + ':' +
f(this.getUTCMinutes()) + ':' +
f(this.getUTCSeconds()) + 'Z'
: null;
};
Boolean.prototype.toJSON = this_value;
@ -217,13 +218,13 @@ if (typeof JSON !== 'object') {
escapable.lastIndex = 0;
return escapable.test(string)
? '"' + string.replace(escapable, function (a) {
var c = meta[a];
return typeof c === 'string'
? c
: '\\u' + ('0000' + a.charCodeAt(0).toString(16)).slice(-4);
}) + '"'
: '"' + string + '"';
? '"' + string.replace(escapable, function (a) {
var c = meta[a];
return typeof c === 'string'
? c
: '\\u' + ('0000' + a.charCodeAt(0).toString(16)).slice(-4);
}) + '"'
: '"' + string + '"';
}
@ -264,8 +265,8 @@ if (typeof JSON !== 'object') {
// JSON numbers must be finite. Encode non-finite numbers as null.
return isFinite(value)
? String(value)
: 'null';
? String(value)
: 'null';
case 'boolean':
case 'null':
@ -309,10 +310,10 @@ if (typeof JSON !== 'object') {
// brackets.
v = partial.length === 0
? '[]'
: gap
? '[\n' + gap + partial.join(',\n' + gap) + '\n' + mind + ']'
: '[' + partial.join(',') + ']';
? '[]'
: gap
? '[\n' + gap + partial.join(',\n' + gap) + '\n' + mind + ']'
: '[' + partial.join(',') + ']';
gap = mind;
return v;
}
@ -328,8 +329,8 @@ if (typeof JSON !== 'object') {
if (v) {
partial.push(quote(k) + (
gap
? ': '
: ':'
? ': '
: ':'
) + v);
}
}
@ -344,8 +345,8 @@ if (typeof JSON !== 'object') {
if (v) {
partial.push(quote(k) + (
gap
? ': '
: ':'
? ': '
: ':'
) + v);
}
}
@ -356,10 +357,10 @@ if (typeof JSON !== 'object') {
// and wrap them in braces.
v = partial.length === 0
? '{}'
: gap
? '{\n' + gap + partial.join(',\n' + gap) + '\n' + mind + '}'
: '{' + partial.join(',') + '}';
? '{}'
: gap
? '{\n' + gap + partial.join(',\n' + gap) + '\n' + mind + '}'
: '{' + partial.join(',') + '}';
gap = mind;
return v;
}
@ -501,8 +502,8 @@ if (typeof JSON !== 'object') {
// each name/value pair to a reviver function for possible transformation.
return typeof reviver === 'function'
? walk({'': j}, '')
: j;
? walk({'': j}, '')
: j;
}
// If the text is not JSON parseable, then a SyntaxError is thrown.

View file

@ -1,6 +1,6 @@
/*
json_parse.js
2015-02-25
2015-05-02
Public Domain.
@ -306,8 +306,8 @@ var json_parse = (function () {
return number();
default:
return ch >= '0' && ch <= '9'
? number()
: word();
? number()
: word();
}
};
@ -333,22 +333,22 @@ var json_parse = (function () {
// result.
return typeof reviver === 'function'
? (function walk(holder, key) {
var k, v, value = holder[key];
if (value && typeof value === 'object') {
for (k in value) {
if (Object.prototype.hasOwnProperty.call(value, k)) {
v = walk(value, k);
if (v !== undefined) {
value[k] = v;
} else {
delete value[k];
? (function walk(holder, key) {
var k, v, value = holder[key];
if (value && typeof value === 'object') {
for (k in value) {
if (Object.prototype.hasOwnProperty.call(value, k)) {
v = walk(value, k);
if (v !== undefined) {
value[k] = v;
} else {
delete value[k];
}
}
}
}
}
return reviver.call(holder, key, value);
}({'': result}, ''))
: result;
return reviver.call(holder, key, value);
}({'': result}, ''))
: result;
};
}());

View file

@ -1,6 +1,6 @@
/*
json_parse_state.js
2015-02-25
2015-05-02
Public Domain.
@ -288,9 +288,9 @@ var json_parse = (function () {
// Remove and replace any backslash escapement.
return text.replace(/\\(?:u(.{4})|([^u]))/g, function (ignore, b, c) {
return b
? String.fromCharCode(parseInt(b, 16))
: escapes[c];
return b
? String.fromCharCode(parseInt(b, 16))
: escapes[c];
});
}
@ -299,7 +299,7 @@ var json_parse = (function () {
// A regular expression is used to extract tokens from the JSON text.
// The extraction process is cautious.
var r, // The result of the exec method.
var result,
tx = /^[\u0020\t\n\r]*(?:([,:\[\]{}]|true|false|null)|(-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?)|"((?:[^\r\n\t\\\"]|\\(?:["\\\/trnfb]|u[0-9a-fA-F]{4}))*)")/;
// Set the starting state.
@ -318,36 +318,36 @@ var json_parse = (function () {
// For each token...
while (true) {
r = tx.exec(source);
if (!r) {
result = tx.exec(source);
if (!result) {
break;
}
// r is the result array from matching the tokenizing regular expression.
// r[0] contains everything that matched, including any initial whitespace.
// r[1] contains any punctuation that was matched, or true, false, or null.
// r[2] contains a matched number, still in string form.
// r[3] contains a matched string, without quotes but with escapement.
// result is the result array from matching the tokenizing regular expression.
// result[0] contains everything that matched, including any initial whitespace.
// result[1] contains any punctuation that was matched, or true, false, or null.
// result[2] contains a matched number, still in string form.
// result[3] contains a matched string, without quotes but with escapement.
if (r[1]) {
if (result[1]) {
// Token: Execute the action for this state and token.
action[r[1]][state]();
action[result[1]][state]();
} else if (r[2]) {
} else if (result[2]) {
// Number token: Convert the number string into a number value and execute
// the action for this state and number.
value = +r[2];
value = +result[2];
number[state]();
} else {
// String token: Replace the escapement sequences and execute the action for
// this state and string.
value = debackslashify(r[3]);
value = debackslashify(result[3]);
string[state]();
}
@ -355,7 +355,7 @@ var json_parse = (function () {
// are tokens. This is a slow process, but it allows the use of ^ matching,
// which assures that no illegal tokens slip through.
source = source.slice(r[0].length);
source = source.slice(result[0].length);
}
// If we find a state/token combination that is illegal, then the action will
@ -370,9 +370,9 @@ var json_parse = (function () {
//a well-formed JSON text.
if (state !== 'ok' || (/[^\u0020\t\n\r]/.test(source))) {
throw state instanceof SyntaxError
? state
: new SyntaxError('JSON');
throw state instanceof SyntaxError
? state
: new SyntaxError('JSON');
}
// If there is a reviver function, we recursively walk the new structure,
@ -381,23 +381,23 @@ var json_parse = (function () {
// value in an empty key. If there is not a reviver function, we simply return
// that value.
return typeof reviver === 'function'
? (function walk(holder, key) {
var k, v, value = holder[key];
if (value && typeof value === 'object') {
for (k in value) {
if (Object.prototype.hasOwnProperty.call(value, k)) {
v = walk(value, k);
if (v !== undefined) {
value[k] = v;
} else {
delete value[k];
return typeof reviver === 'function'
? (function walk(holder, key) {
var k, v, value = holder[key];
if (value && typeof value === 'object') {
for (k in value) {
if (Object.prototype.hasOwnProperty.call(value, k)) {
v = walk(value, k);
if (v !== undefined) {
value[k] = v;
} else {
delete value[k];
}
}
}
}
}
return reviver.call(holder, key, value);
}({'': value}, ''))
: value;
return reviver.call(holder, key, value);
}({'': value}, ''))
: value;
};
}());