Comparing version 2.1.0 to 2.3.0
@@ -425,14 +425,20 @@ (function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.Hjson = f()}})(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){ | ||
function getComment(wat) { | ||
function getComment(cAt) { | ||
var i; | ||
wat--; | ||
cAt--; | ||
// remove trailing whitespace | ||
for (i = at - 2; i > wat && text[i] <= ' ' && text[i] !== '\n'; i--); | ||
// but only up to EOL | ||
for (i = at - 2; i > cAt && text[i] <= ' ' && text[i] !== '\n'; i--); | ||
if (text[i] === '\n') i--; | ||
if (text[i] === '\r') i--; | ||
var res = text.substr(wat, i-wat+1); | ||
for (i = 0; i < res.length; i++) | ||
if (res[i] > ' ') return res; | ||
return ""; | ||
var res = text.substr(cAt, i-cAt+1); | ||
// return if we find anything other than whitespace | ||
for (i = 0; i < res.length; i++) { | ||
if (res[i] > ' ') { | ||
var j = res.indexOf('\n'); | ||
if (j >= 0) return [res.substr(0, j), res.substr(j+1)]; | ||
else return [res]; | ||
} | ||
} | ||
return []; | ||
} | ||
@@ -479,15 +485,16 @@ | ||
var array = []; | ||
var kw, wat; | ||
var comments, cAt, nextComment; | ||
try { | ||
if (keepWsc) { | ||
if (Object.defineProperty) Object.defineProperty(array, "__WSC__", { enumerable: false, writable: true }); | ||
array.__WSC__ = kw = []; | ||
if (Object.defineProperty) Object.defineProperty(array, "__COMMENTS__", { enumerable: false, writable: true }); | ||
array.__COMMENTS__ = comments = []; | ||
} | ||
next(); | ||
wat = at; | ||
cAt = at; | ||
white(); | ||
if (kw) kw.push(getComment(wat)); | ||
if (comments) nextComment = getComment(cAt).join(''); | ||
if (ch === ']') { | ||
next(); | ||
if (comments) comments.push([nextComment]); | ||
return array; // empty array | ||
@@ -498,9 +505,15 @@ } | ||
array.push(value()); | ||
wat = at; | ||
cAt = at; | ||
white(); | ||
// in Hjson the comma is optional and trailing commas are allowed | ||
if (ch === ',') { next(); wat = at; white(); } | ||
if (kw) kw.push(getComment(wat)); | ||
// note that we do not keep comments before the , if there are any | ||
if (ch === ',') { next(); cAt = at; white(); } | ||
if (comments) { | ||
var c = getComment(cAt); | ||
comments.push([nextComment||"", c[0]||""]); | ||
nextComment = c[1]; | ||
} | ||
if (ch === ']') { | ||
next(); | ||
if (comments) comments[comments.length-1][1] += nextComment||""; | ||
return array; | ||
@@ -521,11 +534,9 @@ } | ||
var key, object = {}; | ||
var kw, wat; | ||
function pushWhite(key) { kw.c[key]=getComment(wat); if (key) kw.o.push(key); } | ||
var key = "", object = {}; | ||
var comments, cAt, nextComment; | ||
try { | ||
if (keepWsc) { | ||
if (Object.defineProperty) Object.defineProperty(object, "__WSC__", { enumerable: false, writable: true }); | ||
object.__WSC__ = kw = { c: {}, o: [] }; | ||
if (withoutBraces) kw.noRootBraces = true; | ||
if (Object.defineProperty) Object.defineProperty(object, "__COMMENTS__", { enumerable: false, writable: true }); | ||
object.__COMMENTS__ = comments = { c: {}, o: [] }; | ||
} | ||
@@ -536,8 +547,9 @@ | ||
next(); | ||
wat = at; | ||
} else wat = 1; | ||
cAt = at; | ||
} else cAt = 1; | ||
white(); | ||
if (kw) pushWhite(""); | ||
if (comments) nextComment = getComment(cAt).join(''); | ||
if (ch === '}' && !withoutBraces) { | ||
if (comments) comments.c[""] = [nextComment]; | ||
next(); | ||
@@ -553,9 +565,16 @@ return object; // empty object | ||
object[key] = value(); | ||
wat = at; | ||
cAt = at; | ||
white(); | ||
// in Hjson the comma is optional and trailing commas are allowed | ||
if (ch === ',') { next(); wat = at; white(); } | ||
if (kw) pushWhite(key); | ||
// note that we do not keep comments before the , if there are any | ||
if (ch === ',') { next(); cAt = at; white(); } | ||
if (comments) { | ||
var c = getComment(cAt); | ||
comments.c[key] = [nextComment||"", c[0]||""]; | ||
nextComment = c[1]; | ||
if (key) comments.o.push(key); | ||
} | ||
if (ch === '}' && !withoutBraces) { | ||
next(); | ||
if (comments) comments.c[key][1] += nextComment||""; | ||
return object; | ||
@@ -612,2 +631,3 @@ } | ||
function hjsonParse(source, opt) { | ||
if (typeof source!=="string") throw new Error("source is not a string"); | ||
var dsfDef = null; | ||
@@ -661,3 +681,3 @@ if (opt && typeof opt === 'object') { | ||
// options | ||
var eol, keepWsc, bracesSameLine, quoteAlways, emitRootBraces; | ||
var eol, keepWsc, bracesSameLine, quoteAlways; | ||
var token = { | ||
@@ -760,6 +780,6 @@ obj: [ '{', '}' ], | ||
function startsWithNL(str) { return str && str[str[0] === '\r' ? 1 : 0] === '\n'; } | ||
function testWsc(str) { return str && !startsWithNL(str); } | ||
function wsc(str) { | ||
function commentOnThisLine(str) { return str && !startsWithNL(str); } | ||
function makeComment(str, prefix, trim) { | ||
if (!str) return ""; | ||
var i, len = str.length; | ||
var i, s = -1, len = str.length; | ||
for (i = 0; i < len; i++) { | ||
@@ -769,4 +789,6 @@ var c = str[i]; | ||
else if (c > ' ') { str = '# ' + str; break; } | ||
else s = i; // space | ||
} | ||
if (i < len) return " " + wrap(token.rem, str); | ||
if (trim && s >= 0) str = str.substr(s + 1); | ||
if (i < len) return prefix + wrap(token.rem, str); | ||
else return str; | ||
@@ -801,11 +823,10 @@ } | ||
var kw, kwl; // whitespace & comments | ||
if (keepWsc) kw = value.__WSC__; | ||
var comments, lastComment; // whitespace & comments | ||
if (keepWsc) comments = value.__COMMENTS__; | ||
var isArray = Object.prototype.toString.apply(value) === '[object Array]'; | ||
var showBraces = isArray || !isRootObject || (kw ? !kw.noRootBraces : emitRootBraces); | ||
// Make an array to hold the partial results of stringifying this object value. | ||
var mind = gap; | ||
if (showBraces) gap += indent; | ||
gap += indent; | ||
var eolMind = eol + mind; | ||
@@ -818,2 +839,3 @@ var eolGap = eol + gap; | ||
var k, v; // key, value | ||
var c, ca; | ||
@@ -825,11 +847,24 @@ if (isArray) { | ||
for (i = 0, length = value.length; i < length; i++) { | ||
if (kw) partial.push(wsc(kw[i]) + eolGap); | ||
partial.push(str(value[i], kw ? testWsc(kw[i + 1]) : false, true) || wrap(token.lit, 'null')); | ||
if (comments) { | ||
c = comments[i]||[]; | ||
ca = commentOnThisLine(c[1]) ? c[1] : ""; | ||
partial.push(makeComment(c[0], "\n") + eolGap); | ||
} | ||
partial.push(str(value[i], comments ? ca : false, true) || wrap(token.lit, 'null')); | ||
if (ca) partial.push(makeComment(ca, " ", true)); | ||
} | ||
if (kw) partial.push(wsc(kw[i]) + eolMind); | ||
if (comments) { | ||
if (length === 0) { | ||
// when empty | ||
c = comments[0]; | ||
partial.push(makeComment(c[0], "\n") + eolMind); | ||
} | ||
else partial.push(eolMind); | ||
} | ||
// Join all of the elements together, separated with newline, and wrap them in | ||
// brackets. | ||
if (kw) v = prefix + wrap(token.arr, partial.join('')); | ||
if (comments) v = prefix + wrap(token.arr, partial.join('')); | ||
else if (partial.length === 0) v = wrap(token.arr, ''); | ||
@@ -840,5 +875,4 @@ else v = prefix + wrap(token.arr, eolGap + partial.join(eolGap) + eolMind); | ||
if (kw) { | ||
kwl = wsc(kw.c[""]); | ||
var keys=kw.o.slice(); | ||
if (comments) { | ||
var keys = comments.o.slice(); | ||
for (k in value) { | ||
@@ -851,8 +885,17 @@ if (Object.prototype.hasOwnProperty.call(value, k) && keys.indexOf(k) < 0) | ||
k = keys[i]; | ||
if (showBraces || i>0 || kwl) partial.push(kwl + eolGap); | ||
kwl = wsc(kw.c[k]); | ||
v = str(value[k], testWsc(kwl)); | ||
c = comments.c[k]||[]; | ||
ca = commentOnThisLine(c[1]) ? c[1] : ""; | ||
partial.push(makeComment(c[0], "\n") + eolGap); | ||
v = str(value[k], ca); | ||
if (v) partial.push(quoteKey(k) + token.col + (startsWithNL(v) ? '' : ' ') + v); | ||
if (ca) partial.push(makeComment(ca, " ", true)); | ||
} | ||
if (showBraces || kwl) partial.push(kwl + eolMind); | ||
if (length === 0) { | ||
// when empty | ||
c = comments.c[""]; | ||
partial.push(makeComment(c[0], "\n") + eolMind); | ||
} | ||
else partial.push(eolMind); | ||
} else { | ||
@@ -870,8 +913,6 @@ for (k in value) { | ||
v = wrap(token.obj, ''); | ||
} else if (showBraces) { | ||
} else { | ||
// and wrap them in braces | ||
if (kw) v = prefix + wrap(token.obj, partial.join('')); | ||
if (comments) v = prefix + wrap(token.obj, partial.join('')); | ||
else v = prefix + wrap(token.obj, eolGap + partial.join(eolGap) + eolMind); | ||
} else { | ||
v = partial.join(kw ? '' : eolGap); | ||
} | ||
@@ -893,3 +934,2 @@ } | ||
bracesSameLine = false; | ||
emitRootBraces = true; | ||
quoteAlways = false; | ||
@@ -902,3 +942,2 @@ | ||
bracesSameLine = opt.bracesSameLine; | ||
emitRootBraces = opt.emitRootBraces !== false; | ||
quoteAlways = opt.quotes === 'always'; | ||
@@ -947,7 +986,7 @@ dsfDef = opt.dsf; | ||
},{"./hjson-common":1,"./hjson-dsf":2}],5:[function(require,module,exports){ | ||
module.exports="2.1.0"; | ||
module.exports="2.3.0"; | ||
},{}],6:[function(require,module,exports){ | ||
/*! @preserve | ||
* Hjson v2.1.0 | ||
* Hjson v2.3.0 | ||
* http://hjson.org | ||
@@ -995,3 +1034,3 @@ * | ||
emitRootBraces | ||
boolean, show braces for the root object. Default true. | ||
obsolete: will always emit braces | ||
@@ -998,0 +1037,0 @@ quotes string, controls how strings are displayed. |
@@ -1,3 +0,3 @@ | ||
!function(r){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=r();else if("function"==typeof define&&define.amd)define([],r);else{var n;n="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:this,n.Hjson=r()}}(function(){return function r(n,t,e){function o(f,u){if(!t[f]){if(!n[f]){var s="function"==typeof require&&require;if(!u&&s)return s(f,!0);if(i)return i(f,!0);var a=new Error("Cannot find module '"+f+"'");throw a.code="MODULE_NOT_FOUND",a}var c=t[f]={exports:{}};n[f][0].call(c.exports,function(r){var t=n[f][1][r];return o(t?t:r)},c,c.exports,r,n,t,e)}return t[f].exports}for(var i="function"==typeof require&&require,f=0;f<e.length;f++)o(e[f]);return o}({1:[function(r,n,t){"use strict";function e(r,n){function t(){return o=r.charAt(s),s++,o}var e,o,i="",f=0,u=!0,s=0;for(t(),"-"===o&&(i="-",t());o>="0"&&o<="9";)u&&("0"==o?f++:u=!1),i+=o,t();if(u&&f--,"."===o)for(i+=".";t()&&o>="0"&&o<="9";)i+=o;if("e"===o||"E"===o)for(i+=o,t(),"-"!==o&&"+"!==o||(i+=o,t());o>="0"&&o<="9";)i+=o,t();for(;o&&o<=" ";)t();return n&&(","!==o&&"}"!==o&&"]"!==o&&"#"!==o&&("/"!==o||"/"!==r[s]&&"*"!==r[s])||(o=0)),e=+i,o||f||!isFinite(e)?void 0:e}var o=r("os");n.exports={EOL:o.EOL||"\n",tryParseNumber:e}},{os:7}],2:[function(r,n,t){"use strict";function e(r,n){function t(r){return"[object Function]"==={}.toString.call(r)}if("[object Array]"!==Object.prototype.toString.apply(r)){if(r)throw new Error("dsf option must contain an array!");return i}if(0===r.length)return i;var e=[];return r.forEach(function(r){if(!r.name||!t(r.parse)||!t(r.stringify))throw new Error("extension does not match the DSF interface");e.push(function(){try{if("parse"==n)return r.parse.apply(null,arguments);if("stringify"==n){var t=r.stringify.apply(null,arguments);if(void 0!==t&&("string"!=typeof t||0===t.length||'"'===t[0]||[].some.call(t,function(r){return f(r)})))throw new Error("value may not be empty, start with a quote or contain a punctuator character except colon: "+t);return t}throw new Error("Invalid type")}catch(n){throw new Error("DSF-"+r.name+" failed; "+n.message)}})}),o.bind(null,e)}function o(r,n){if(r)for(var t=0;t<r.length;t++){var e=r[t](n);if(void 0!==e)return e}}function i(r){}function f(r){return"{"===r||"}"===r||"["===r||"]"===r||","===r}function u(r){return{name:"math",parse:function(r){switch(r){case"+inf":case"inf":case"+Inf":case"Inf":return 1/0;case"-inf":case"-Inf":return-(1/0);case"nan":case"NaN":return NaN}},stringify:function(r){if("number"==typeof r)return 1/r===-(1/0)?"-0":r===1/0?"Inf":r===-(1/0)?"-Inf":isNaN(r)?"NaN":void 0}}}function s(r){var n=r&&r.out;return{name:"hex",parse:function(r){if(/^0x[0-9A-Fa-f]+$/.test(r))return parseInt(r,16)},stringify:function(r){if(n&&Number.isInteger(r))return"0x"+r.toString(16)}}}function a(r){return{name:"date",parse:function(r){if(/^\d{4}-\d{2}-\d{2}$/.test(r)||/^\d{4}-\d{2}-\d{2}T\d{2}\:\d{2}\:\d{2}(?:.\d+)(?:Z|[+-]\d{2}:\d{2})$/.test(r)){var n=Date.parse(r);if(!isNaN(n))return new Date(n)}},stringify:function(r){if("[object Date]"===Object.prototype.toString.call(r)){var n=r.toISOString();return n.indexOf("T00:00:00.000Z",n.length-14)!==-1?n.substr(0,10):n}}}}r("./hjson-common");u.description="support for Inf/inf, -Inf/-inf, Nan/naN and -0",s.description="parse hexadecimal numbers prefixed with 0x",a.description="support ISO dates",n.exports={loadDsf:e,std:{math:u,hex:s,date:a}}},{"./hjson-common":1}],3:[function(r,n,t){"use strict";n.exports=function(n,t){function e(){w=0,k=" "}function o(r){return"{"===r||"}"===r||"["===r||"]"===r||","===r||":"===r}function i(r){var n,t=0,e=1;for(n=w-1;n>0&&"\n"!==x[n];n--,t++);for(;n>0;n--)"\n"===x[n]&&e++;throw new Error(r+" at line "+e+","+t+" >>>"+x.substr(w-t,20)+" ...")}function f(){return k=x.charAt(w),w++,k}function u(r){return x.charAt(w+r)}function s(){var r="";if('"'===k)for(;f();){if('"'===k)return f(),r;if("\\"===k)if(f(),"u"===k){for(var n=0,t=0;t<4;t++){f();var e,o=k.charCodeAt(0);k>="0"&&k<="9"?e=o-48:k>="a"&&k<="f"?e=o-97+10:k>="A"&&k<="F"?e=o-65+10:i("Bad \\u char "+k),n=16*n+e}r+=String.fromCharCode(n)}else{if("string"!=typeof q[k])break;r+=q[k]}else r+=k}i("Bad string")}function a(){function r(){for(var r=e;k&&k<=" "&&"\n"!==k&&r-- >0;)f()}for(var n="",t=0,e=0;;){var o=u(-e-5);if(!o||"\n"===o)break;e++}for(;k&&k<=" "&&"\n"!==k;)f();for("\n"===k&&(f(),r());;){if(k){if("'"===k){if(t++,f(),3===t)return"\n"===n.slice(-1)&&(n=n.slice(0,-1)),n;continue}for(;t>0;)n+="'",t--}else i("Bad multiline string");"\n"===k?(n+="\n",f(),r()):("\r"!==k&&(n+=k),f())}}function c(){if('"'===k)return s();for(var r="",n=w,t=-1;;){if(":"===k)return r?t>=0&&t!==r.length&&(w=n+t,i("Found whitespace in your key name (use quotes to include)")):i("Found ':' but no key name (for an empty key name use quotes)"),r;k<=" "?k?t<0&&(t=r.length):i("Found EOF while looking for a key name (check your syntax)"):o(k)?i("Found '"+k+"' where a key name was expected (check your syntax or use quotes if the key name includes {}[],: or whitespace)"):r+=k,f()}}function l(){for(;k;){for(;k&&k<=" ";)f();if("#"===k||"/"===k&&"/"===u(0))for(;k&&"\n"!==k;)f();else{if("/"!==k||"*"!==u(0))break;for(f(),f();k&&("*"!==k||"/"!==u(0));)f();k&&(f(),f())}}}function p(){var r=k;for(o(k)&&i("Found a punctuator character '"+k+"' when expecting a quoteless string (check your syntax)");;){if(f(),3===r.length&&"'''"===r)return a();var n="\r"===k||"\n"===k||""===k;if(n||","===k||"}"===k||"]"===k||"#"===k||"/"===k&&("/"===u(0)||"*"===u(0))){var t=r[0];switch(t){case"f":if("false"===r.trim())return!1;break;case"n":if("null"===r.trim())return null;break;case"t":if("true"===r.trim())return!0;break;default:if("-"===t||t>="0"&&t<="9"){var e=_.tryParseNumber(r);if(void 0!==e)return e}}if(n){r=r.trim();var s=S(r);return void 0!==s?s:r}}r+=k}}function h(r){var n;for(r--,n=w-2;n>r&&x[n]<=" "&&"\n"!==x[n];n--);"\n"===x[n]&&n--,"\r"===x[n]&&n--;var t=x.substr(r,n-r+1);for(n=0;n<t.length;n++)if(t[n]>" ")return t;return""}function d(r){function n(r,t){var e,o,i,f;switch(typeof r){case"string":r.indexOf(t)>=0&&(f=r);break;case"object":if("[object Array]"===Object.prototype.toString.apply(r))for(e=0,i=r.length;e<i;e++)f=n(r[e],t)||f;else for(o in r)Object.prototype.hasOwnProperty.call(r,o)&&(f=n(r[o],t)||f)}return f}function t(t){var e=n(r,t);return e?"found '"+t+"' in a string value, your mistake could be with:\n > "+e+"\n (unquoted strings contain everything up to the next line!)":""}return t("}")||t("]")}function m(){var r,n,t=[];try{if(O&&(Object.defineProperty&&Object.defineProperty(t,"__WSC__",{enumerable:!1,writable:!0}),t.__WSC__=r=[]),f(),n=w,l(),r&&r.push(h(n)),"]"===k)return f(),t;for(;k;){if(t.push(b()),n=w,l(),","===k&&(f(),n=w,l()),r&&r.push(h(n)),"]"===k)return f(),t;l()}i("End of input while parsing an array (missing ']')")}catch(r){throw r.hint=r.hint||d(t),r}}function y(r){function n(r){e.c[r]=h(o),r&&e.o.push(r)}var t,e,o,u={};try{if(O&&(Object.defineProperty&&Object.defineProperty(u,"__WSC__",{enumerable:!1,writable:!0}),u.__WSC__=e={c:{},o:[]},r&&(e.noRootBraces=!0)),r?o=1:(f(),o=w),l(),e&&n(""),"}"===k&&!r)return f(),u;for(;k;){if(t=c(),l(),":"!==k&&i("Expected ':' instead of '"+k+"'"),f(),u[t]=b(),o=w,l(),","===k&&(f(),o=w,l()),e&&n(t),"}"===k&&!r)return f(),u;l()}if(r)return u;i("End of input while parsing an object (missing '}')")}catch(r){throw r.hint=r.hint||d(u),r}}function b(){switch(l(),k){case"{":return y();case"[":return m();case'"':return s();default:return p()}}function g(r){return l(),k&&i("Syntax error, found trailing characters"),r}function v(){switch(l(),k){case"{":return g(y());case"[":return g(m())}try{return g(y(!0))}catch(r){e();try{return g(b())}catch(n){throw r}}}function j(r,n){var t=null;return n&&"object"==typeof n&&(O=n.keepWsc,t=n.dsf),S=E.loadDsf(t,"parse"),x=r,e(),v()}var x,w,k,O,S,_=r("./hjson-common"),E=r("./hjson-dsf"),q={'"':'"',"\\":"\\","/":"/",b:"\b",f:"\f",n:"\n",r:"\r",t:"\t"};return j(n,t)}},{"./hjson-common":1,"./hjson-dsf":2}],4:[function(r,n,t){"use strict";n.exports=function(n,t){function e(r,n){return r[0]+n+r[1]}function o(r){return r.replace(g,function(r){var n=w[r];return"string"==typeof n?e(_.esc,n):e(_.uni,("0000"+r.charCodeAt(0).toString(16)).slice(-4))})}function i(r,n,t,i){return r?(v.lastIndex=0,x.lastIndex=0,d||t||v.test(r)||void 0!==y.tryParseNumber(r,!0)||x.test(r)?(g.lastIndex=0,j.lastIndex=0,g.test(r)?j.test(r)||i?e(_.qstr,o(r)):f(r,n):e(_.qstr,r)):e(_.str,r)):e(_.qstr,"")}function f(r,n){var t,o=r.replace(/\r/g,"").split("\n");if(n+=S,1===o.length)return e(_.mstr,o[0]);var i=l+n+_.mstr[0];for(t=0;t<o.length;t++)i+=l,o[t]&&(i+=n+o[t]);return i+l+n+_.mstr[1]}function u(r){return r?k.test(r)?(g.lastIndex=0,e(_.qkey,g.test(r)?o(r):r)):e(_.key,r):'""'}function s(r,n,t,o){function f(r){return r&&"\n"===r["\r"===r[0]?1:0]}function a(r){return r&&!f(r)}function d(r){if(!r)return"";var n,t=r.length;for(n=0;n<t;n++){var o=r[n];if("#"===o||"/"===o&&("/"===r[n+1]||"*"===r[n+1]))break;if(o>" "){r="# "+r;break}}return n<t?" "+e(_.rem,r):r}var y=c(r);if(void 0!==y)return e(_.dsf,y);switch(typeof r){case"string":return i(r,O,n,o);case"number":return isFinite(r)?e(_.num,String(r)):e(_.lit,"null");case"boolean":return e(_.lit,String(r));case"object":if(!r)return e(_.lit,"null");var b,g;p&&(b=r.__WSC__);var v="[object Array]"===Object.prototype.toString.apply(r),j=v||!o||(b?!b.noRootBraces:m),x=O;j&&(O+=S);var w,k,E,q,N=l+x,I=l+O,F=t||h?"":N,A=[];if(v){for(w=0,k=r.length;w<k;w++)b&&A.push(d(b[w])+I),A.push(s(r[w],!!b&&a(b[w+1]),!0)||e(_.lit,"null"));b&&A.push(d(b[w])+N),q=b?F+e(_.arr,A.join("")):0===A.length?e(_.arr,""):F+e(_.arr,I+A.join(I)+N)}else{if(b){g=d(b.c[""]);var C=b.o.slice();for(E in r)Object.prototype.hasOwnProperty.call(r,E)&&C.indexOf(E)<0&&C.push(E);for(w=0,k=C.length;w<k;w++)E=C[w],(j||w>0||g)&&A.push(g+I),g=d(b.c[E]),q=s(r[E],a(g)),q&&A.push(u(E)+_.col+(f(q)?"":" ")+q);(j||g)&&A.push(g+N)}else for(E in r)Object.prototype.hasOwnProperty.call(r,E)&&(q=s(r[E]),q&&A.push(u(E)+_.col+(f(q)?"":" ")+q));q=0===A.length?e(_.obj,""):j?b?F+e(_.obj,A.join("")):F+e(_.obj,I+A.join(I)+N):A.join(b?"":I)}return O=x,q}}function a(r,n){var t,e,o=null;if(l=y.EOL,S=" ",p=!1,h=!1,m=!0,d=!1,n&&"object"==typeof n&&("\n"!==n.eol&&"\r\n"!==n.eol||(l=n.eol),e=n.space,p=n.keepWsc,h=n.bracesSameLine,m=n.emitRootBraces!==!1,d="always"===n.quotes,o=n.dsf,n.colors===!0&&(_={obj:["[30;1m{[0m","[30;1m}[0m"],arr:["[30;1m[[0m","[30;1m][0m"],key:["[33m","[0m"],qkey:['[33m"','"[0m'],col:["[37m:[0m"],str:["[37;1m","[0m"],qstr:['[37;1m"','"[0m'],mstr:["[37;1m'''","'''[0m"],num:["[36;1m","[0m"],lit:["[36m","[0m"],dsf:["[37m","[0m"],esc:["[31m\\","[0m"],uni:["[31m\\u","[0m"],rem:["[30;1m","[0m"]})),c=b.loadDsf(o,"stringify"),"number"==typeof e)for(S="",t=0;t<e;t++)S+=" ";else"string"==typeof e&&(S=e);return s(r,null,!0,!0)}var c,l,p,h,d,m,y=r("./hjson-common"),b=r("./hjson-dsf"),g=/[\\\"\x00-\x1f\x7f-\x9f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,v=/^\s|^"|^'''|^#|^\/\*|^\/\/|^\{|^\}|^\[|^\]|^:|^,|\s$|[\x00-\x1f\x7f-\x9f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,j=/'''|[\x00-\x09\x0b\x0c\x0e-\x1f\x7f-\x9f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,x=/^(true|false|null)\s*((,|\]|\}|#|\/\/|\/\*).*)?$/,w={"\b":"b","\t":"t","\n":"n","\f":"f","\r":"r",'"':'"',"\\":"\\"},k=/[,\{\[\}\]\s:#"]|\/\/|\/\*|'''/,O="",S=" ",_={obj:["{","}"],arr:["[","]"],key:["",""],qkey:['"','"'],col:[":"],str:["",""],qstr:['"','"'],mstr:["'''","'''"],num:["",""],lit:["",""],dsf:["",""],esc:["\\",""],uni:["\\u",""],rem:["",""]};return a(n,t)}},{"./hjson-common":1,"./hjson-dsf":2}],5:[function(r,n,t){n.exports="2.1.0"},{}],6:[function(r,n,t){/*! @preserve | ||
* Hjson v2.1.0 | ||
!function(r){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=r();else if("function"==typeof define&&define.amd)define([],r);else{var n;n="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:this,n.Hjson=r()}}(function(){return function r(n,t,e){function o(f,u){if(!t[f]){if(!n[f]){var s="function"==typeof require&&require;if(!u&&s)return s(f,!0);if(i)return i(f,!0);var a=new Error("Cannot find module '"+f+"'");throw a.code="MODULE_NOT_FOUND",a}var c=t[f]={exports:{}};n[f][0].call(c.exports,function(r){var t=n[f][1][r];return o(t?t:r)},c,c.exports,r,n,t,e)}return t[f].exports}for(var i="function"==typeof require&&require,f=0;f<e.length;f++)o(e[f]);return o}({1:[function(r,n,t){"use strict";function e(r,n){function t(){return o=r.charAt(s),s++,o}var e,o,i="",f=0,u=!0,s=0;for(t(),"-"===o&&(i="-",t());o>="0"&&o<="9";)u&&("0"==o?f++:u=!1),i+=o,t();if(u&&f--,"."===o)for(i+=".";t()&&o>="0"&&o<="9";)i+=o;if("e"===o||"E"===o)for(i+=o,t(),"-"!==o&&"+"!==o||(i+=o,t());o>="0"&&o<="9";)i+=o,t();for(;o&&o<=" ";)t();return n&&(","!==o&&"}"!==o&&"]"!==o&&"#"!==o&&("/"!==o||"/"!==r[s]&&"*"!==r[s])||(o=0)),e=+i,o||f||!isFinite(e)?void 0:e}var o=r("os");n.exports={EOL:o.EOL||"\n",tryParseNumber:e}},{os:7}],2:[function(r,n,t){"use strict";function e(r,n){function t(r){return"[object Function]"==={}.toString.call(r)}if("[object Array]"!==Object.prototype.toString.apply(r)){if(r)throw new Error("dsf option must contain an array!");return i}if(0===r.length)return i;var e=[];return r.forEach(function(r){if(!r.name||!t(r.parse)||!t(r.stringify))throw new Error("extension does not match the DSF interface");e.push(function(){try{if("parse"==n)return r.parse.apply(null,arguments);if("stringify"==n){var t=r.stringify.apply(null,arguments);if(void 0!==t&&("string"!=typeof t||0===t.length||'"'===t[0]||[].some.call(t,function(r){return f(r)})))throw new Error("value may not be empty, start with a quote or contain a punctuator character except colon: "+t);return t}throw new Error("Invalid type")}catch(n){throw new Error("DSF-"+r.name+" failed; "+n.message)}})}),o.bind(null,e)}function o(r,n){if(r)for(var t=0;t<r.length;t++){var e=r[t](n);if(void 0!==e)return e}}function i(r){}function f(r){return"{"===r||"}"===r||"["===r||"]"===r||","===r}function u(r){return{name:"math",parse:function(r){switch(r){case"+inf":case"inf":case"+Inf":case"Inf":return 1/0;case"-inf":case"-Inf":return-(1/0);case"nan":case"NaN":return NaN}},stringify:function(r){if("number"==typeof r)return 1/r===-(1/0)?"-0":r===1/0?"Inf":r===-(1/0)?"-Inf":isNaN(r)?"NaN":void 0}}}function s(r){var n=r&&r.out;return{name:"hex",parse:function(r){if(/^0x[0-9A-Fa-f]+$/.test(r))return parseInt(r,16)},stringify:function(r){if(n&&Number.isInteger(r))return"0x"+r.toString(16)}}}function a(r){return{name:"date",parse:function(r){if(/^\d{4}-\d{2}-\d{2}$/.test(r)||/^\d{4}-\d{2}-\d{2}T\d{2}\:\d{2}\:\d{2}(?:.\d+)(?:Z|[+-]\d{2}:\d{2})$/.test(r)){var n=Date.parse(r);if(!isNaN(n))return new Date(n)}},stringify:function(r){if("[object Date]"===Object.prototype.toString.call(r)){var n=r.toISOString();return n.indexOf("T00:00:00.000Z",n.length-14)!==-1?n.substr(0,10):n}}}}r("./hjson-common");u.description="support for Inf/inf, -Inf/-inf, Nan/naN and -0",s.description="parse hexadecimal numbers prefixed with 0x",a.description="support ISO dates",n.exports={loadDsf:e,std:{math:u,hex:s,date:a}}},{"./hjson-common":1}],3:[function(r,n,t){"use strict";n.exports=function(n,t){function e(){w=0,O=" "}function o(r){return"{"===r||"}"===r||"["===r||"]"===r||","===r||":"===r}function i(r){var n,t=0,e=1;for(n=w-1;n>0&&"\n"!==x[n];n--,t++);for(;n>0;n--)"\n"===x[n]&&e++;throw new Error(r+" at line "+e+","+t+" >>>"+x.substr(w-t,20)+" ...")}function f(){return O=x.charAt(w),w++,O}function u(r){return x.charAt(w+r)}function s(){var r="";if('"'===O)for(;f();){if('"'===O)return f(),r;if("\\"===O)if(f(),"u"===O){for(var n=0,t=0;t<4;t++){f();var e,o=O.charCodeAt(0);O>="0"&&O<="9"?e=o-48:O>="a"&&O<="f"?e=o-97+10:O>="A"&&O<="F"?e=o-65+10:i("Bad \\u char "+O),n=16*n+e}r+=String.fromCharCode(n)}else{if("string"!=typeof _[O])break;r+=_[O]}else r+=O}i("Bad string")}function a(){function r(){for(var r=e;O&&O<=" "&&"\n"!==O&&r-- >0;)f()}for(var n="",t=0,e=0;;){var o=u(-e-5);if(!o||"\n"===o)break;e++}for(;O&&O<=" "&&"\n"!==O;)f();for("\n"===O&&(f(),r());;){if(O){if("'"===O){if(t++,f(),3===t)return"\n"===n.slice(-1)&&(n=n.slice(0,-1)),n;continue}for(;t>0;)n+="'",t--}else i("Bad multiline string");"\n"===O?(n+="\n",f(),r()):("\r"!==O&&(n+=O),f())}}function c(){if('"'===O)return s();for(var r="",n=w,t=-1;;){if(":"===O)return r?t>=0&&t!==r.length&&(w=n+t,i("Found whitespace in your key name (use quotes to include)")):i("Found ':' but no key name (for an empty key name use quotes)"),r;O<=" "?O?t<0&&(t=r.length):i("Found EOF while looking for a key name (check your syntax)"):o(O)?i("Found '"+O+"' where a key name was expected (check your syntax or use quotes if the key name includes {}[],: or whitespace)"):r+=O,f()}}function l(){for(;O;){for(;O&&O<=" ";)f();if("#"===O||"/"===O&&"/"===u(0))for(;O&&"\n"!==O;)f();else{if("/"!==O||"*"!==u(0))break;for(f(),f();O&&("*"!==O||"/"!==u(0));)f();O&&(f(),f())}}}function p(){var r=O;for(o(O)&&i("Found a punctuator character '"+O+"' when expecting a quoteless string (check your syntax)");;){if(f(),3===r.length&&"'''"===r)return a();var n="\r"===O||"\n"===O||""===O;if(n||","===O||"}"===O||"]"===O||"#"===O||"/"===O&&("/"===u(0)||"*"===u(0))){var t=r[0];switch(t){case"f":if("false"===r.trim())return!1;break;case"n":if("null"===r.trim())return null;break;case"t":if("true"===r.trim())return!0;break;default:if("-"===t||t>="0"&&t<="9"){var e=N.tryParseNumber(r);if(void 0!==e)return e}}if(n){r=r.trim();var s=E(r);return void 0!==s?s:r}}r+=O}}function h(r){var n;for(r--,n=w-2;n>r&&x[n]<=" "&&"\n"!==x[n];n--);"\n"===x[n]&&n--,"\r"===x[n]&&n--;var t=x.substr(r,n-r+1);for(n=0;n<t.length;n++)if(t[n]>" "){var e=t.indexOf("\n");return e>=0?[t.substr(0,e),t.substr(e+1)]:[t]}return[]}function d(r){function n(r,t){var e,o,i,f;switch(typeof r){case"string":r.indexOf(t)>=0&&(f=r);break;case"object":if("[object Array]"===Object.prototype.toString.apply(r))for(e=0,i=r.length;e<i;e++)f=n(r[e],t)||f;else for(o in r)Object.prototype.hasOwnProperty.call(r,o)&&(f=n(r[o],t)||f)}return f}function t(t){var e=n(r,t);return e?"found '"+t+"' in a string value, your mistake could be with:\n > "+e+"\n (unquoted strings contain everything up to the next line!)":""}return t("}")||t("]")}function m(){var r,n,t,e=[];try{if(k&&(Object.defineProperty&&Object.defineProperty(e,"__COMMENTS__",{enumerable:!1,writable:!0}),e.__COMMENTS__=r=[]),f(),n=w,l(),r&&(t=h(n).join("")),"]"===O)return f(),r&&r.push([t]),e;for(;O;){if(e.push(b()),n=w,l(),","===O&&(f(),n=w,l()),r){var o=h(n);r.push([t||"",o[0]||""]),t=o[1]}if("]"===O)return f(),r&&(r[r.length-1][1]+=t||""),e;l()}i("End of input while parsing an array (missing ']')")}catch(r){throw r.hint=r.hint||d(e),r}}function y(r){var n,t,e,o="",u={};try{if(k&&(Object.defineProperty&&Object.defineProperty(u,"__COMMENTS__",{enumerable:!1,writable:!0}),u.__COMMENTS__=n={c:{},o:[]}),r?t=1:(f(),t=w),l(),n&&(e=h(t).join("")),"}"===O&&!r)return n&&(n.c[""]=[e]),f(),u;for(;O;){if(o=c(),l(),":"!==O&&i("Expected ':' instead of '"+O+"'"),f(),u[o]=b(),t=w,l(),","===O&&(f(),t=w,l()),n){var s=h(t);n.c[o]=[e||"",s[0]||""],e=s[1],o&&n.o.push(o)}if("}"===O&&!r)return f(),n&&(n.c[o][1]+=e||""),u;l()}if(r)return u;i("End of input while parsing an object (missing '}')")}catch(r){throw r.hint=r.hint||d(u),r}}function b(){switch(l(),O){case"{":return y();case"[":return m();case'"':return s();default:return p()}}function g(r){return l(),O&&i("Syntax error, found trailing characters"),r}function v(){switch(l(),O){case"{":return g(y());case"[":return g(m())}try{return g(y(!0))}catch(r){e();try{return g(b())}catch(n){throw r}}}function j(r,n){if("string"!=typeof r)throw new Error("source is not a string");var t=null;return n&&"object"==typeof n&&(k=n.keepWsc,t=n.dsf),E=S.loadDsf(t,"parse"),x=r,e(),v()}var x,w,O,k,E,N=r("./hjson-common"),S=r("./hjson-dsf"),_={'"':'"',"\\":"\\","/":"/",b:"\b",f:"\f",n:"\n",r:"\r",t:"\t"};return j(n,t)}},{"./hjson-common":1,"./hjson-dsf":2}],4:[function(r,n,t){"use strict";n.exports=function(n,t){function e(r,n){return r[0]+n+r[1]}function o(r){return r.replace(b,function(r){var n=x[r];return"string"==typeof n?e(E.esc,n):e(E.uni,("0000"+r.charCodeAt(0).toString(16)).slice(-4))})}function i(r,n,t,i){return r?(g.lastIndex=0,j.lastIndex=0,d||t||g.test(r)||void 0!==m.tryParseNumber(r,!0)||j.test(r)?(b.lastIndex=0,v.lastIndex=0,b.test(r)?v.test(r)||i?e(E.qstr,o(r)):f(r,n):e(E.qstr,r)):e(E.str,r)):e(E.qstr,"")}function f(r,n){var t,o=r.replace(/\r/g,"").split("\n");if(n+=k,1===o.length)return e(E.mstr,o[0]);var i=l+n+E.mstr[0];for(t=0;t<o.length;t++)i+=l,o[t]&&(i+=n+o[t]);return i+l+n+E.mstr[1]}function u(r){return r?w.test(r)?(b.lastIndex=0,e(E.qkey,b.test(r)?o(r):r)):e(E.key,r):'""'}function s(r,n,t,o){function f(r){return r&&"\n"===r["\r"===r[0]?1:0]}function a(r){return r&&!f(r)}function d(r,n,t){if(!r)return"";var o,i=-1,f=r.length;for(o=0;o<f;o++){var u=r[o];if("#"===u||"/"===u&&("/"===r[o+1]||"*"===r[o+1]))break;if(u>" "){r="# "+r;break}i=o}return t&&i>=0&&(r=r.substr(i+1)),o<f?n+e(E.rem,r):r}var m=c(r);if(void 0!==m)return e(E.dsf,m);switch(typeof r){case"string":return i(r,O,n,o);case"number":return isFinite(r)?e(E.num,String(r)):e(E.lit,"null");case"boolean":return e(E.lit,String(r));case"object":if(!r)return e(E.lit,"null");var y;p&&(y=r.__COMMENTS__);var b="[object Array]"===Object.prototype.toString.apply(r),g=O;O+=k;var v,j,x,w,N,S,_=l+g,q=l+O,I=t||h?"":_,F=[];if(b){for(v=0,j=r.length;v<j;v++)y&&(N=y[v]||[],S=a(N[1])?N[1]:"",F.push(d(N[0],"\n")+q)),F.push(s(r[v],!!y&&S,!0)||e(E.lit,"null")),S&&F.push(d(S," ",!0));y&&(0===j?(N=y[0],F.push(d(N[0],"\n")+_)):F.push(_)),w=y?I+e(E.arr,F.join("")):0===F.length?e(E.arr,""):I+e(E.arr,q+F.join(q)+_)}else{if(y){var M=y.o.slice();for(x in r)Object.prototype.hasOwnProperty.call(r,x)&&M.indexOf(x)<0&&M.push(x);for(v=0,j=M.length;v<j;v++)x=M[v],N=y.c[x]||[],S=a(N[1])?N[1]:"",F.push(d(N[0],"\n")+q),w=s(r[x],S),w&&F.push(u(x)+E.col+(f(w)?"":" ")+w),S&&F.push(d(S," ",!0));0===j?(N=y.c[""],F.push(d(N[0],"\n")+_)):F.push(_)}else for(x in r)Object.prototype.hasOwnProperty.call(r,x)&&(w=s(r[x]),w&&F.push(u(x)+E.col+(f(w)?"":" ")+w));w=0===F.length?e(E.obj,""):y?I+e(E.obj,F.join("")):I+e(E.obj,q+F.join(q)+_)}return O=g,w}}function a(r,n){var t,e,o=null;if(l=m.EOL,k=" ",p=!1,h=!1,d=!1,n&&"object"==typeof n&&("\n"!==n.eol&&"\r\n"!==n.eol||(l=n.eol),e=n.space,p=n.keepWsc,h=n.bracesSameLine,d="always"===n.quotes,o=n.dsf,n.colors===!0&&(E={obj:["[30;1m{[0m","[30;1m}[0m"],arr:["[30;1m[[0m","[30;1m][0m"],key:["[33m","[0m"],qkey:['[33m"','"[0m'],col:["[37m:[0m"],str:["[37;1m","[0m"],qstr:['[37;1m"','"[0m'],mstr:["[37;1m'''","'''[0m"],num:["[36;1m","[0m"],lit:["[36m","[0m"],dsf:["[37m","[0m"],esc:["[31m\\","[0m"],uni:["[31m\\u","[0m"],rem:["[30;1m","[0m"]})),c=y.loadDsf(o,"stringify"),"number"==typeof e)for(k="",t=0;t<e;t++)k+=" ";else"string"==typeof e&&(k=e);return s(r,null,!0,!0)}var c,l,p,h,d,m=r("./hjson-common"),y=r("./hjson-dsf"),b=/[\\\"\x00-\x1f\x7f-\x9f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,g=/^\s|^"|^'''|^#|^\/\*|^\/\/|^\{|^\}|^\[|^\]|^:|^,|\s$|[\x00-\x1f\x7f-\x9f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,v=/'''|[\x00-\x09\x0b\x0c\x0e-\x1f\x7f-\x9f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,j=/^(true|false|null)\s*((,|\]|\}|#|\/\/|\/\*).*)?$/,x={"\b":"b","\t":"t","\n":"n","\f":"f","\r":"r",'"':'"',"\\":"\\"},w=/[,\{\[\}\]\s:#"]|\/\/|\/\*|'''/,O="",k=" ",E={obj:["{","}"],arr:["[","]"],key:["",""],qkey:['"','"'],col:[":"],str:["",""],qstr:['"','"'],mstr:["'''","'''"],num:["",""],lit:["",""],dsf:["",""],esc:["\\",""],uni:["\\u",""],rem:["",""]};return a(n,t)}},{"./hjson-common":1,"./hjson-dsf":2}],5:[function(r,n,t){n.exports="2.3.0"},{}],6:[function(r,n,t){/*! @preserve | ||
* Hjson v2.3.0 | ||
* http://hjson.org | ||
@@ -4,0 +4,0 @@ * |
# hjson-js History | ||
- v2.3.0 | ||
- improved comment round trip | ||
- v2.2.0 | ||
- stringify will always emit root braces | ||
- v2.1.0 | ||
@@ -4,0 +8,0 @@ - add DSF (domain specific formats), experimental |
@@ -221,14 +221,20 @@ /* Hjson http://hjson.org */ | ||
function getComment(wat) { | ||
function getComment(cAt) { | ||
var i; | ||
wat--; | ||
cAt--; | ||
// remove trailing whitespace | ||
for (i = at - 2; i > wat && text[i] <= ' ' && text[i] !== '\n'; i--); | ||
// but only up to EOL | ||
for (i = at - 2; i > cAt && text[i] <= ' ' && text[i] !== '\n'; i--); | ||
if (text[i] === '\n') i--; | ||
if (text[i] === '\r') i--; | ||
var res = text.substr(wat, i-wat+1); | ||
for (i = 0; i < res.length; i++) | ||
if (res[i] > ' ') return res; | ||
return ""; | ||
var res = text.substr(cAt, i-cAt+1); | ||
// return if we find anything other than whitespace | ||
for (i = 0; i < res.length; i++) { | ||
if (res[i] > ' ') { | ||
var j = res.indexOf('\n'); | ||
if (j >= 0) return [res.substr(0, j), res.substr(j+1)]; | ||
else return [res]; | ||
} | ||
} | ||
return []; | ||
} | ||
@@ -275,15 +281,16 @@ | ||
var array = []; | ||
var kw, wat; | ||
var comments, cAt, nextComment; | ||
try { | ||
if (keepWsc) { | ||
if (Object.defineProperty) Object.defineProperty(array, "__WSC__", { enumerable: false, writable: true }); | ||
array.__WSC__ = kw = []; | ||
if (Object.defineProperty) Object.defineProperty(array, "__COMMENTS__", { enumerable: false, writable: true }); | ||
array.__COMMENTS__ = comments = []; | ||
} | ||
next(); | ||
wat = at; | ||
cAt = at; | ||
white(); | ||
if (kw) kw.push(getComment(wat)); | ||
if (comments) nextComment = getComment(cAt).join(''); | ||
if (ch === ']') { | ||
next(); | ||
if (comments) comments.push([nextComment]); | ||
return array; // empty array | ||
@@ -294,9 +301,15 @@ } | ||
array.push(value()); | ||
wat = at; | ||
cAt = at; | ||
white(); | ||
// in Hjson the comma is optional and trailing commas are allowed | ||
if (ch === ',') { next(); wat = at; white(); } | ||
if (kw) kw.push(getComment(wat)); | ||
// note that we do not keep comments before the , if there are any | ||
if (ch === ',') { next(); cAt = at; white(); } | ||
if (comments) { | ||
var c = getComment(cAt); | ||
comments.push([nextComment||"", c[0]||""]); | ||
nextComment = c[1]; | ||
} | ||
if (ch === ']') { | ||
next(); | ||
if (comments) comments[comments.length-1][1] += nextComment||""; | ||
return array; | ||
@@ -317,11 +330,9 @@ } | ||
var key, object = {}; | ||
var kw, wat; | ||
function pushWhite(key) { kw.c[key]=getComment(wat); if (key) kw.o.push(key); } | ||
var key = "", object = {}; | ||
var comments, cAt, nextComment; | ||
try { | ||
if (keepWsc) { | ||
if (Object.defineProperty) Object.defineProperty(object, "__WSC__", { enumerable: false, writable: true }); | ||
object.__WSC__ = kw = { c: {}, o: [] }; | ||
if (withoutBraces) kw.noRootBraces = true; | ||
if (Object.defineProperty) Object.defineProperty(object, "__COMMENTS__", { enumerable: false, writable: true }); | ||
object.__COMMENTS__ = comments = { c: {}, o: [] }; | ||
} | ||
@@ -332,8 +343,9 @@ | ||
next(); | ||
wat = at; | ||
} else wat = 1; | ||
cAt = at; | ||
} else cAt = 1; | ||
white(); | ||
if (kw) pushWhite(""); | ||
if (comments) nextComment = getComment(cAt).join(''); | ||
if (ch === '}' && !withoutBraces) { | ||
if (comments) comments.c[""] = [nextComment]; | ||
next(); | ||
@@ -349,9 +361,16 @@ return object; // empty object | ||
object[key] = value(); | ||
wat = at; | ||
cAt = at; | ||
white(); | ||
// in Hjson the comma is optional and trailing commas are allowed | ||
if (ch === ',') { next(); wat = at; white(); } | ||
if (kw) pushWhite(key); | ||
// note that we do not keep comments before the , if there are any | ||
if (ch === ',') { next(); cAt = at; white(); } | ||
if (comments) { | ||
var c = getComment(cAt); | ||
comments.c[key] = [nextComment||"", c[0]||""]; | ||
nextComment = c[1]; | ||
if (key) comments.o.push(key); | ||
} | ||
if (ch === '}' && !withoutBraces) { | ||
next(); | ||
if (comments) comments.c[key][1] += nextComment||""; | ||
return object; | ||
@@ -408,2 +427,3 @@ } | ||
function hjsonParse(source, opt) { | ||
if (typeof source!=="string") throw new Error("source is not a string"); | ||
var dsfDef = null; | ||
@@ -410,0 +430,0 @@ if (opt && typeof opt === 'object') { |
@@ -34,3 +34,3 @@ /* Hjson http://hjson.org */ | ||
// options | ||
var eol, keepWsc, bracesSameLine, quoteAlways, emitRootBraces; | ||
var eol, keepWsc, bracesSameLine, quoteAlways; | ||
var token = { | ||
@@ -133,6 +133,6 @@ obj: [ '{', '}' ], | ||
function startsWithNL(str) { return str && str[str[0] === '\r' ? 1 : 0] === '\n'; } | ||
function testWsc(str) { return str && !startsWithNL(str); } | ||
function wsc(str) { | ||
function commentOnThisLine(str) { return str && !startsWithNL(str); } | ||
function makeComment(str, prefix, trim) { | ||
if (!str) return ""; | ||
var i, len = str.length; | ||
var i, s = -1, len = str.length; | ||
for (i = 0; i < len; i++) { | ||
@@ -142,4 +142,6 @@ var c = str[i]; | ||
else if (c > ' ') { str = '# ' + str; break; } | ||
else s = i; // space | ||
} | ||
if (i < len) return " " + wrap(token.rem, str); | ||
if (trim && s >= 0) str = str.substr(s + 1); | ||
if (i < len) return prefix + wrap(token.rem, str); | ||
else return str; | ||
@@ -174,11 +176,10 @@ } | ||
var kw, kwl; // whitespace & comments | ||
if (keepWsc) kw = value.__WSC__; | ||
var comments, lastComment; // whitespace & comments | ||
if (keepWsc) comments = value.__COMMENTS__; | ||
var isArray = Object.prototype.toString.apply(value) === '[object Array]'; | ||
var showBraces = isArray || !isRootObject || (kw ? !kw.noRootBraces : emitRootBraces); | ||
// Make an array to hold the partial results of stringifying this object value. | ||
var mind = gap; | ||
if (showBraces) gap += indent; | ||
gap += indent; | ||
var eolMind = eol + mind; | ||
@@ -191,2 +192,3 @@ var eolGap = eol + gap; | ||
var k, v; // key, value | ||
var c, ca; | ||
@@ -198,11 +200,24 @@ if (isArray) { | ||
for (i = 0, length = value.length; i < length; i++) { | ||
if (kw) partial.push(wsc(kw[i]) + eolGap); | ||
partial.push(str(value[i], kw ? testWsc(kw[i + 1]) : false, true) || wrap(token.lit, 'null')); | ||
if (comments) { | ||
c = comments[i]||[]; | ||
ca = commentOnThisLine(c[1]) ? c[1] : ""; | ||
partial.push(makeComment(c[0], "\n") + eolGap); | ||
} | ||
partial.push(str(value[i], comments ? ca : false, true) || wrap(token.lit, 'null')); | ||
if (ca) partial.push(makeComment(ca, " ", true)); | ||
} | ||
if (kw) partial.push(wsc(kw[i]) + eolMind); | ||
if (comments) { | ||
if (length === 0) { | ||
// when empty | ||
c = comments[0]; | ||
partial.push(makeComment(c[0], "\n") + eolMind); | ||
} | ||
else partial.push(eolMind); | ||
} | ||
// Join all of the elements together, separated with newline, and wrap them in | ||
// brackets. | ||
if (kw) v = prefix + wrap(token.arr, partial.join('')); | ||
if (comments) v = prefix + wrap(token.arr, partial.join('')); | ||
else if (partial.length === 0) v = wrap(token.arr, ''); | ||
@@ -213,5 +228,4 @@ else v = prefix + wrap(token.arr, eolGap + partial.join(eolGap) + eolMind); | ||
if (kw) { | ||
kwl = wsc(kw.c[""]); | ||
var keys=kw.o.slice(); | ||
if (comments) { | ||
var keys = comments.o.slice(); | ||
for (k in value) { | ||
@@ -224,8 +238,17 @@ if (Object.prototype.hasOwnProperty.call(value, k) && keys.indexOf(k) < 0) | ||
k = keys[i]; | ||
if (showBraces || i>0 || kwl) partial.push(kwl + eolGap); | ||
kwl = wsc(kw.c[k]); | ||
v = str(value[k], testWsc(kwl)); | ||
c = comments.c[k]||[]; | ||
ca = commentOnThisLine(c[1]) ? c[1] : ""; | ||
partial.push(makeComment(c[0], "\n") + eolGap); | ||
v = str(value[k], ca); | ||
if (v) partial.push(quoteKey(k) + token.col + (startsWithNL(v) ? '' : ' ') + v); | ||
if (ca) partial.push(makeComment(ca, " ", true)); | ||
} | ||
if (showBraces || kwl) partial.push(kwl + eolMind); | ||
if (length === 0) { | ||
// when empty | ||
c = comments.c[""]; | ||
partial.push(makeComment(c[0], "\n") + eolMind); | ||
} | ||
else partial.push(eolMind); | ||
} else { | ||
@@ -243,8 +266,6 @@ for (k in value) { | ||
v = wrap(token.obj, ''); | ||
} else if (showBraces) { | ||
} else { | ||
// and wrap them in braces | ||
if (kw) v = prefix + wrap(token.obj, partial.join('')); | ||
if (comments) v = prefix + wrap(token.obj, partial.join('')); | ||
else v = prefix + wrap(token.obj, eolGap + partial.join(eolGap) + eolMind); | ||
} else { | ||
v = partial.join(kw ? '' : eolGap); | ||
} | ||
@@ -266,3 +287,2 @@ } | ||
bracesSameLine = false; | ||
emitRootBraces = true; | ||
quoteAlways = false; | ||
@@ -275,3 +295,2 @@ | ||
bracesSameLine = opt.bracesSameLine; | ||
emitRootBraces = opt.emitRootBraces !== false; | ||
quoteAlways = opt.quotes === 'always'; | ||
@@ -278,0 +297,0 @@ dsfDef = opt.dsf; |
@@ -1,1 +0,1 @@ | ||
module.exports="2.1.0"; | ||
module.exports="2.3.0"; |
/*! @preserve | ||
* Hjson v2.1.0 | ||
* Hjson v2.3.0 | ||
* http://hjson.org | ||
@@ -45,3 +45,3 @@ * | ||
emitRootBraces | ||
boolean, show braces for the root object. Default true. | ||
obsolete: will always emit braces | ||
@@ -48,0 +48,0 @@ quotes string, controls how strings are displayed. |
{ | ||
"name": "hjson", | ||
"description": "JSON for Humans. A configuration file format with relaxed syntax, fewer mistakes and more comments.", | ||
"description": "A user interface for JSON.", | ||
"main": "./lib/hjson.js", | ||
"author": "Christian Zangl", | ||
"version": "2.1.0", | ||
"version": "2.3.0", | ||
"keywords": [ | ||
@@ -8,0 +8,0 @@ "json", |
@@ -7,6 +7,10 @@ # hjson-js | ||
[Hjson](http://hjson.org), the Human JSON. A configuration file format for humans. Relaxed syntax, fewer mistakes, more comments. | ||
[Hjson](http://hjson.org), a user interface for JSON | ||
![Hjson Intro](http://hjson.org/hjson1.gif) | ||
JSON is easy for humans to read and write... in theory. In practice JSON gives us plenty of opportunities to make mistakes without even realizing it. | ||
Hjson is a syntax extension to JSON. It's NOT a proposal to replace JSON or to incorporate it into the JSON spec itself. It's intended to be used like a user interface for humans, to read and edit before passing the JSON data to the machine. | ||
```Hjson | ||
@@ -142,3 +146,3 @@ { | ||
// parse, keep whitespace and comments | ||
// (they are stored in a non enumerable __WSC__ member) | ||
// (they are stored in a non enumerable __COMMENTS__ member) | ||
var data = Hjson.rt.parse(text); | ||
@@ -149,16 +153,2 @@ | ||
// you can also edit comments by accessing __WSC__ | ||
var wsc = data.__WSC__; | ||
// __WSC__ for objects contains { c: {}, o: [] } | ||
// - c with the actual comment and | ||
// - o (array) with the order of the members | ||
wsc.c.hugo = "just another test"; | ||
wsc.o.splice(2, 0, "hugo"); | ||
data.hugo = "value"; | ||
data.arrayWithComments = [ "a" ]; | ||
// __WSC__ for arrays is just an array with the | ||
// comments ([0] for the space before the elements) | ||
data.arrayWithComments.__WSC__ = [ "before a", "after a" ]; | ||
// convert back to Hjson | ||
@@ -165,0 +155,0 @@ console.log(Hjson.rt.stringify(data)); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
6
118259
123
2438
158