tomlify-j0.4
Advanced tools
Comparing version 2.2.1 to 3.0.0-alpha.0
@@ -0,1 +1,7 @@ | ||
### v3.0.0 | ||
* New API interface: `tomlify.toToml(obj, opts)`, `tomlify.toValue(obj, opts)`. | ||
`tomlify` is no longer a function. (incompatible changes) | ||
* New option `sort` to specify a compare function for sorting table keys. | ||
### v2.1.1 | ||
@@ -2,0 +8,0 @@ |
@@ -1,1 +0,1 @@ | ||
(function(){"use strict";var r=function(r,e){if(0===r.length)return"";for(var n="",i=0,u=r.length;i<u;i++)if(t(r[i])&&r[i])n+=(n?".":"")+m(r[i]);else{if(!a(r[i]))return!1;e||(n+=(n?".":"")+"["+r[i]+"]")}return n||!1},e=function(e,n){return(r(e)||"<root>")+": "+n},n=function(r){return Object.prototype.toString.call(r)};n.Boolean=n(!1),n.String=n(""),n.Number=n(0),n.Array=n([]),n.Date=n(new Date(0));var t=function(r){return"string"==typeof r},a=function(r){return"number"==typeof r},i=Array.isArray||function(r){return n(r)===n.Array},u=function(r){return n(r)===n.Date},l=function(r){return null!==r&&"object"==typeof r&&!(i(r)||u(r))},o=function(r){if(r.length<2)return!1;for(var e=n(r[0]),t=1,a=r.length;t<a;t++)if(null!=r[t]&&n(r[t])!==e)return!0;return!1},f=function(r){if(r.length<1)return!1;for(var e=0,n=r.length;e<n;e++)if(l(r[e]))return!0;return!1},p=function(r,e){return Object.prototype.hasOwnProperty.call(r,e)},c=function(r){var e=function(r,n){if(!i(r)&&!l(r))return!1;if(-1!==n.indexOf(r))return!0;n.push(r);for(var t in r)if(p(r,t)){var a=n.length;if(e(r[t],n))return!0;n.splice(a)}return!1};return e(r,[])},s=function(r,e,n){return n?"true":"false"},h=function(r,e,n){return"object"==typeof JSON&&JSON?JSON.stringify(n):'"'+String(n).replace(/[\x00-\x1F"\\]/g,function(r){switch(r){case'"':case"\\":return"\\"+r;case"\t":return"\\t";case"\n":return"\\n";case"\r":return"\\r";case"\b":return"\\b";case"\f":return"\\f";default:var e=r.charCodeAt(0).toString(16);return"\\u"+"0000".substr(e.length)+e}})+'"'},v=function(r,n,t){if(!isFinite(t))throw new Error(e(r.path,"Number must be finite."));var a;return a=(Number.isInteger?Number.isInteger(t):Math.floor(t)===t)?t.toFixed(1):String(t),a=a.replace(/([eE])(\d)/,"$1+$2"),/[.eE]/.test(a)?a:a+".0"},b=function(r,n,a){if(o(a))throw new Error(e(r.path,"Array cannot contain values of different types."));var i=r.table;r.table=a;var u=[],l=r.inPair;f(a)&&(r.inPair=!0);for(var p=0,c=a.length;p<c;p++){r.path.push(p);var s=w(r,p,a[p]);t(s)&&u.push(s),r.path.pop()}return r.inPair=l,r.table=i,!r.inPair&&u.length>0&&r.space?"[\n"+O(u.join(",\n"),1,r.space)+"\n]":"["+u.join(", ")+"]"},g=function(r,n,t){if(!isFinite(t.getTime()))throw new Error(e(r.path,"Invalid Date"));return t.toISOString()},y=function(r,n,a){var i=r.table;r.table=a;var u=r.inPair;r.inPair=!0;var l=[];for(var o in a)if(p(a,o)&&null!=a[o]){if(!o)throw new Error(e(r.path,"Key cannot be an empty string."));r.path.push(o);var f=w(r,o,a[o]);t(f)&&l.push(m(o)+" = "+f),r.path.pop()}return r.inPair=u,r.table=i,"{"+l.join(", ")+"}"},w=function(r,a,i){if(r.replace){var u=r.replace.call(E(r),a,i);if(t(u))return u;if(null==u)return null;if(!1!==u)throw new Error(e(r.path,"Replacer must return a string, false, null or undefined."))}switch(n(i)){case n.Boolean:return s(0,0,i);case n.String:return h(0,0,i);case n.Number:return v(r,0,i);case n.Array:return b(r,0,i);case n.Date:return g(r,0,i);default:return null==i?null:y(r,0,i)}},d=function(r,e,n){return x(r),w({path:[],table:{"":r},inTableArray:!1,replace:N(e),level:0,space:P(n)},"",r)},m=function(r){return!!r&&(/^[a-zA-Z0-9\-_]+$/.test(r)?r:h(0,0,r))},A=function(r,n,a){var i=m(n);if(!i)throw new Error(e(r.path,"Key cannot be an empty string."));var u=w(r,n,a);return t(u)?i+" = "+u:null},E=function(r){return{path:r.path.slice(0),table:r.table}},S=function(r,n,a){if(r.replace){var i=r.replace.call(E(r),n,a);if(t(i))return m(n)+" = "+i;if(null==i)return null;if(!1!==i)throw new Error(e(r.path,"Replacer must return a string, false, null or undefined."))}return!1},T=function(r,e,n,a){var u;if(!(r.replace&&0===r.path.length&&(u=S(r,e,n),t(u)&&r.lines.push(u),!1!==u)||a(r,e,n))){var o=r.table;if(r.table=n,i(n)){h=r.inTableArray;r.inTableArray=f(n);for(var c=0,s=n.length;c<s;c++)r.path.push(c),T(r,c,n[c],a),r.path.pop();r.inTableArray=h}else if(l(n)){var h=r.inTableArray;r.inTableArray=!1;var v=[],b=[];for(var g in n)if(p(n,g)){var y=n[g],w=r.path.length>0&&(i(y)?f(y):l(y));i(y)&&f(y)?b.push(g,y,w):l(y)?v.push(g,y,w):(r.path.push(g),T(r,g,y,a),r.path.pop())}if(r.replace){for(var c=0,s=v.length;c<s;c+=3)r.path.push(v[c]),!1!==(u=S(r,v[c],v[c+1]))&&(t(u)&&r.lines.push(O(u,r.level,r.space)),v[c+1]=null),r.path.pop();for(var c=0,s=b.length;c<s;c+=3)if(r.path.push(b[c]),!1===(u=S(r,b[c],b[c+1]))){for(var d=0,g=b[c+1].length;d<g;d++){r.path.push(d);var m=b[c+1][d];if(u=S(r,d,m),r.path.pop(),!1!==u){if(null==u){b[c+1][d]=null;continue}u=A(r,b[c],b[c+1]),t(u)&&r.lines.push(O(u,r.level,r.space)),b[c+1]=null;break}}r.path.pop()}else t(u)&&r.lines.push(O(u,r.level,r.space)),b[c+1]=null,r.path.pop()}for(var E=v.concat(b),c=0,s=E.length;c<s;c+=3)null!=E[c+1]&&(r.path.push(E[c]),E[c+2]&&r.level++,T(r,E[c],E[c+1],a),E[c+2]&&r.level--,r.path.pop());r.inTableArray=h}r.table=o}},j=function(r,e){if(r.repeat)return r.repeat(e);for(var n="",t="";e>0;)t+=t||r,1&e&&(n+=t),e>>>=1;return n},O=function(r,e,n){var t=j(n,e);return r.replace(/^(?!$)/gm,t)},P=function(r){return t(r)?r:a(r)&&r>=0&&isFinite(r)&&Math.floor(r)===r?j(" ",r):""},N=function(r){return"function"==typeof r?r:null},x=function(r){if(null==r)throw new Error("Undefined or null cannot be stringified.");if(c(r))throw new Error("Converting circular structure to TOML.")},D=function(e,n,a){x(e),n=N(n),a=P(a);var u=[];return T({path:[],table:{"":e},inPair:!1,inTableArray:!1,replace:n,level:0,space:a,lines:u},"",e,function(e,n,a){var o=null;if(l(a))""!==n&&(u.length>0&&u.push(""),o=e.inTableArray?"[["+r(e.path,!0)+"]]":"["+r(e.path,!0)+"]",u.push(O(o,e.level,e.space)));else{if(!i(a))return t(n)&&(o=A(e,n,a),t(o)&&u.push(O(o,e.level,e.space))),!0;if(!t(n))return!0;f(a)||(o=A(e,n,a),t(o)&&u.push(O(o,e.level,e.space)))}}),u.join("\n")},K=function(r,e,n){return l(r)?D(r,e,n):d(r,e,n)};K.toToml=function(r,e,n){if(!l(r))throw new Error("An object other than Array or Date is expected.");return D(r,e,n)},K.toKey=function(e,n){if(t(e)){if(!(a=m(e)))throw new Error("Key cannot be an empty string.");return a}if(i(e)){var a=r(e,n);if(!1===a)throw new Error("Key path must consist of non-empty string(s).");return a}throw new Error("Invalid Arguments for tomlify.toKey({String | Array})")},K.toValue=d,null!=("undefined"!=typeof module&&null!==module?module.exports:void 0)?module.exports=K:"function"==typeof define&&define.amd?define([],function(){return K}):this.tomlify=K}).call(this); | ||
(function(){"use strict";var r=function(r,e){if(0===r.length)return"";for(var n="",i=0,u=r.length;i<u;i++)if(t(r[i])&&r[i])n+=(n?".":"")+d(r[i]);else{if(!a(r[i]))return!1;e||(n+=(n?".":"")+"["+r[i]+"]")}return n||!1},e=function(e,n){return(r(e)||"<root>")+": "+n},n=function(r){return Object.prototype.toString.call(r)};n.Boolean=n(!1),n.String=n(""),n.Number=n(0),n.Array=n([]),n.Date=n(new Date(0));var t=function(r){return"string"==typeof r},a=function(r){return"number"==typeof r},i=Array.isArray||function(r){return n(r)===n.Array},u=function(r){return n(r)===n.Date},o=function(r){return null!==r&&"object"==typeof r&&!(i(r)||u(r))},l=function(r){if(r.length<2)return!1;for(var e=n(r[0]),t=1,a=r.length;t<a;t++)if(null!=r[t]&&n(r[t])!==e)return!0;return!1},f=function(r){if(r.length<1)return!1;for(var e=0,n=r.length;e<n;e++)if(o(r[e]))return!0;return!1},p=function(r,e){return Object.prototype.hasOwnProperty.call(r,e)},c=function(r){var e=function(r,n){if(!i(r)&&!o(r))return!1;if(-1!==n.indexOf(r))return!0;n.push(r);for(var t in r)if(p(r,t)){var a=n.length;if(e(r[t],n))return!0;n.splice(a)}return!1};return e(r,[])},s=function(r,e,n){return n?"true":"false"},h=function(r,e,n){return"object"==typeof JSON&&JSON?JSON.stringify(n):'"'+String(n).replace(/[\x00-\x1F"\\]/g,function(r){switch(r){case'"':case"\\":return"\\"+r;case"\t":return"\\t";case"\n":return"\\n";case"\r":return"\\r";case"\b":return"\\b";case"\f":return"\\f";default:var e=r.charCodeAt(0).toString(16);return"\\u"+"0000".substr(e.length)+e}})+'"'},v=function(r,n,t){if(!isFinite(t))throw new Error(e(r.path,"Number must be finite."));var a;return a=(Number.isInteger?Number.isInteger(t):Math.floor(t)===t)?t.toFixed(1):String(t),a=a.replace(/([eE])(\d)/,"$1+$2"),/[.eE]/.test(a)?a:a+".0"},b=function(r,n,a){if(l(a))throw new Error(e(r.path,"Array cannot contain values of different types."));var i=r.table;r.table=a;var u=[],o=r.inPair;f(a)&&(r.inPair=!0);for(var p=0,c=a.length;p<c;p++){r.path.push(p);var s=w(r,p,a[p]);t(s)&&u.push(s),r.path.pop()}return r.inPair=o,r.table=i,!r.inPair&&u.length>0&&r.space?"[\n"+T(u.join(",\n"),1,r.space)+"\n]":"["+u.join(", ")+"]"},y=function(r,n,t){if(!isFinite(t.getTime()))throw new Error(e(r.path,"Invalid Date"));return t.toISOString()},g=function(r,n,a){var i=r.table;r.table=a;var u=r.inPair;r.inPair=!0;var o=[],l=Object.keys(a);r.sort&&l.sort(r.sort);for(var f=0,c=l.length;f<c;f++){var s=l[f];if(p(a,s)&&null!=a[s]){if(!s)throw new Error(e(r.path,"Key cannot be an empty string."));r.path.push(s);var h=w(r,s,a[s]);t(h)&&o.push(d(s)+" = "+h),r.path.pop()}}return r.inPair=u,r.table=i,"{"+o.join(", ")+"}"},w=function(r,a,i){if(r.replace){var u=r.replace.call(E(r),a,i);if(t(u))return u;if(null==u)return null;if(!1!==u)throw new Error(e(r.path,"Replacer must return a string, false, null or undefined."))}switch(n(i)){case n.Boolean:return s(0,0,i);case n.String:return h(0,0,i);case n.Number:return v(r,0,i);case n.Array:return b(r,0,i);case n.Date:return y(r,0,i);default:return null==i?null:g(r,0,i)}},m=function(r,e){return w({path:[],table:{"":r},inPair:!1,inTableArray:!1,sort:e.sort,replace:e.replace,level:0,space:P(e.space)},"",r)},d=function(r){return!!r&&(/^[a-zA-Z0-9\-_]+$/.test(r)?r:h(0,0,r))},A=function(r,n,a){var i=d(n);if(!i)throw new Error(e(r.path,"Key cannot be an empty string."));var u=w(r,n,a);return t(u)?i+" = "+u:null},E=function(r){return{path:r.path.slice(0),table:r.table}},j=function(r,n,a){if(r.replace){var i=r.replace.call(E(r),n,a);if(t(i))return d(n)+" = "+i;if(null==i)return null;if(!1!==i)throw new Error(e(r.path,"Replacer must return a string, false, null or undefined."))}return!1},O=function(r,e,n,a){var u;if(!(r.replace&&0===r.path.length&&(u=j(r,e,n),t(u)&&r.lines.push(u),!1!==u)||a(r,e,n))){var l=r.table;if(r.table=n,i(n)){h=r.inTableArray;r.inTableArray=f(n);for(var c=0,s=n.length;c<s;c++)r.path.push(c),O(r,c,n[c],a),r.path.pop();r.inTableArray=h}else if(o(n)){var h=r.inTableArray;r.inTableArray=!1;var v=[],b=Object.keys(n);r.sort&&b.sort(r.sort);for(var c=0,s=b.length;c<s;c++){d=b[c];if(p(n,d)){var y=n[d],g=r.path.length>0&&(i(y)?f(y):o(y));i(y)&&f(y)?v.push([!0,d,y,g]):o(y)?v.push([!1,d,y,g]):(r.path.push(d),O(r,d,y,a),r.path.pop())}}if(b=null,r.replace)for(var c=0,s=v.length;c<s;c++)if((l=v[c])[0]){if(r.path.push(l[1]),!1!==(u=j(r,l[1],l[2]))){t(u)&&r.lines.push(T(u,r.level,r.space)),l[2]=null,r.path.pop();continue}for(var w=l[2],m=0,d=w.length;m<d;m++){var E=w[m];if(r.path.push(m),u=j(r,m,E),r.path.pop(),!1!==u){if(null==u){w[m]=null;continue}u=A(r,l[1],l[2]),t(u)&&r.lines.push(T(u,r.level,r.space)),l[2]=null;break}}r.path.pop()}else r.path.push(l[1]),!1!==(u=j(r,l[1],l[2]))&&(t(u)&&r.lines.push(T(u,r.level,r.space)),l[2]=null),r.path.pop();for(var c=0,s=v.length;c<s;c++)null!=(l=v[c])[2]&&(r.path.push(l[1]),l[3]&&r.level++,O(r,l[1],l[2],a),l[3]&&r.level--,r.path.pop());r.inTableArray=h}r.table=l}},S=function(r,e){if(r.repeat)return r.repeat(e);for(var n="",t="";e>0;)t+=t||r,1&e&&(n+=t),e>>>=1;return n},T=function(r,e,n){var t=S(n,e);return r.replace(/^(?!$)/gm,t)},P=function(r){return t(r)?r:a(r)&&r>=0&&isFinite(r)&&Math.floor(r)===r?S(" ",r):""},N=function(r,e){if(null==r)throw new Error("Undefined or null cannot be stringified.");if(c(r))throw new Error("Converting circular structure to TOML.");if(null!=e&&"object"!=typeof e)throw new Error("Options must be an object.");if(null!=e.replace&&"function"!=typeof e.replace)throw new Error("Replacer must be a function.");if(null!=e.sort&&"function"!=typeof e.sort)throw new Error("Compare function for sorting must be a function.")},x=function(e,n){var a=[];return O({path:[],table:{"":e},inPair:!1,inTableArray:!1,replace:n.replace,sort:n.sort,level:0,space:P(n.space),lines:a},"",e,function(e,n,u){var l=null;if(o(u))""!==n&&(a.length>0&&a.push(""),l=e.inTableArray?"[["+r(e.path,!0)+"]]":"["+r(e.path,!0)+"]",a.push(T(l,e.level,e.space)));else{if(!i(u))return t(n)&&(l=A(e,n,u),t(l)&&a.push(T(l,e.level,e.space))),!0;if(!t(n))return!0;f(u)||(l=A(e,n,u),t(l)&&a.push(T(l,e.level,e.space)))}}),a.join("\n")},D={};D.toToml=function(r,e){if(!o(r))throw new Error("An object other than Array or Date is expected.");return e=null!=e?e:{},N(r,e),x(r,e)},D.toKey=function(e,n){if(t(e)){if(!(a=d(e)))throw new Error("Key cannot be an empty string.");return a}if(i(e)){var a=r(e,n);if(!1===a)throw new Error("Key path must consist of non-empty string(s).");return a}throw new Error("Invalid Arguments for tomlify.toKey({String | Array})")},D.toValue=function(r,e){return e=null!=e?e:{},N(r,e),m(r,e)},null!=("undefined"!=typeof module&&null!==module?module.exports:void 0)?module.exports=D:"function"==typeof define&&define.amd?define([],function(){return D}):this.tomlify=D}).call(this); |
{ | ||
"name": "tomlify-j0.4", | ||
"version": "2.2.1", | ||
"version": "3.0.0-alpha.0", | ||
"description": "A Object->TOML encoder/converter only for TOML v0.4.0", | ||
"main": "tomlify.js", | ||
"types": "tomlify.d.ts", | ||
"scripts": { | ||
@@ -17,3 +16,2 @@ "dist": "uglifyjs --verbose --compress --mangle -o dist/tomlify.min.js tomlify.js", | ||
"dist/tomlify.min.js", | ||
"tomlify.d.ts", | ||
"tomlify.js" | ||
@@ -20,0 +18,0 @@ ], |
@@ -56,3 +56,3 @@ # TOMLify-j0.4 | ||
try { | ||
var text = tomlify(table, null, 2); // indent with 2 spaces | ||
var text = tomlify.toToml(table, {space: 2}); // indent with 2 spaces | ||
/* OUTPUT: | ||
@@ -83,15 +83,18 @@ * [about] | ||
*/ | ||
var text = tomlify(table, function (key, value) { | ||
var context = this; | ||
var path = tomlify.toKey(context.path); | ||
if (/^more\.version\.\[\d+\]$/.test(path)) { | ||
return value.toFixed(0); // Change the text transformed from the value. | ||
var text = tomlify.toToml(table, { | ||
space: ' ', | ||
replace: function (key, value) { | ||
var context = this; | ||
var path = tomlify.toKey(context.path); | ||
if (/^more\.version\.\[\d+\]$/.test(path)) { | ||
return value.toFixed(0); // Change the text transformed from the value. | ||
} | ||
if (context.path[0] === 'about' && | ||
context.path[1] === 'todos' && | ||
context.path[2] === 1) { | ||
return null; // Drop one table from the todo array. | ||
} | ||
return false; // Let tomlify decide for you. | ||
} | ||
if (context.path[0] === 'about' && | ||
context.path[1] === 'todos' && | ||
context.path[2] === 1) { | ||
return null; // Drop one table from the todo array. | ||
} | ||
return false; // Let tomlify decide for you. | ||
}, ' '); | ||
}); | ||
/* OUTPUT: | ||
@@ -117,3 +120,3 @@ * [about] | ||
*/ | ||
var text = tomlify({ | ||
var text = tomlify.toToml({ | ||
null: null, | ||
@@ -134,18 +137,16 @@ undefined: undefined, | ||
#### tomlify(table, replacer, space) | ||
#### tomlify.toToml(table, options) | ||
Use it to transform a table object into TOML text. | ||
* table - `{Object}`: The object to be transformed. | ||
* `table`: must be an object other than an instance of Array or Date. | ||
It can be any JavaScript object, except for `null` and `undefined`. By | ||
default, all numbers are transformed into floats and arrays of numbers will | ||
become arrays of floats. And `null` or `undefined` in an array or object | ||
property whose value is `null` or `undefined` will be ignored. You can | ||
change this behavior through `replacer`. | ||
By default, all numbers are transformed into floats and arrays of numbers | ||
will become arrays of floats. And `null` or `undefined` in an array or | ||
object property whose value is `null` or `undefined` will be ignored. You | ||
can change this behavior through `options.replace`. | ||
If `table` is a boolean value, a number, a string, a date or an array, the | ||
result will be the same as `tomlify.toValue(table, replacer, space)`. | ||
* options.replace - `{function(this: Context, key: String|Number, value:Mixed): Mixed}`: | ||
* replacer - `{function(this: Context, key: String|Number, value:Mixed): Mixed}`: | ||
The function used to change the default text output. | ||
@@ -160,4 +161,6 @@ * `@this {Context}`: | ||
* space - `{String|Number}`: Specify the padding string for indentation. | ||
* options.space - `{String|Number}`: | ||
Specify the padding string for indentation. | ||
If it is a non-negative integer N, then use N space " " for indentation. If | ||
@@ -167,12 +170,13 @@ it is a string, then use this string for indentation. Otherwise, no | ||
#### tomlify.toToml(value, replacer, space) | ||
* options.sort - `{function(a: String, b: String): Number}`: | ||
The same as `tomlify(table, replacer, space)`, except that `value` must be an | ||
object other than an instance of Array or Date. | ||
The compare function for sorting table keys. | ||
#### tomlify.toValue(value, replacer, space) | ||
It is used for `Array.prototype.sort()`. | ||
Just like `tomlify(table, replacer, space)`, it is used to transform a value | ||
into TOML value for a key-value pair. `value` cannot be null or undefined. | ||
#### tomlify.toValue(value, options) | ||
Just like `tomlify.toToml(table, options)`, it is used to transform a value into TOML | ||
value for a key-value pair. `value` cannot be null or undefined. | ||
However, an inline-table always fits into one line, no matter what it contains. | ||
@@ -186,3 +190,3 @@ | ||
tomlify.toValue(["apple", "banana"], null, 2); | ||
tomlify.toValue(["apple", "banana"], {space: 2}); | ||
//=> | ||
@@ -197,3 +201,3 @@ //[ | ||
{fruits: ["apple", "banana"]} | ||
], null, 2); | ||
], {space: 2}); | ||
//=> | ||
@@ -200,0 +204,0 @@ //[ |
163
tomlify.js
@@ -197,3 +197,8 @@ (function () { | ||
var lines = []; | ||
for (var k in obj) { | ||
var keys = Object.keys(obj); | ||
if (context.sort) { | ||
keys.sort(context.sort); | ||
} | ||
for (var i = 0, l = keys.length; i < l; i++) { | ||
var k = keys[i]; | ||
if (hasOwnProperty(obj, k) && obj[k] != null) { | ||
@@ -247,11 +252,12 @@ if (!k) { | ||
}; | ||
var escapeValue = function (obj, replace, space) { | ||
check(obj); | ||
var escapeValue = function (obj, opts) { | ||
return escapeValue_({ | ||
path: [], | ||
table: {'': obj}, | ||
inPair: false, | ||
inTableArray: false, | ||
replace: toReplacer(replace), | ||
sort: opts.sort, | ||
replace: opts.replace, | ||
level: 0, | ||
space: toSpace(space) | ||
space: toSpace(opts.space) | ||
}, '', obj); | ||
@@ -330,4 +336,8 @@ }; | ||
var tables = []; | ||
var tableArrays = []; | ||
for (var k in obj) { | ||
var keys = Object.keys(obj); | ||
if (context.sort) { | ||
keys.sort(context.sort); | ||
} | ||
for (var i = 0, l = keys.length; i < l; i++) { | ||
var k = keys[i]; | ||
if (hasOwnProperty(obj, k)) { | ||
@@ -338,5 +348,5 @@ var v = obj[k]; | ||
if (isArray(v) && containTables(v)) { | ||
tableArrays.push(k, v, toIndent); | ||
tables.push([true, k, v, toIndent]); | ||
} else if (isTable(v)) { | ||
tables.push(k, v, toIndent); | ||
tables.push([false, k, v, toIndent]); | ||
} else { | ||
@@ -349,57 +359,61 @@ context.path.push(k); | ||
} | ||
keys = null; | ||
if (context.replace) { | ||
for (var i = 0, l = tables.length; i < l; i += 3) { | ||
context.path.push(tables[i]); | ||
line = getReplacement(context, tables[i], tables[i+1]); | ||
if (line !== false) { | ||
if (isString(line)) { | ||
context.lines.push(indent(line, context.level, context.space)); | ||
for (var i = 0, l = tables.length; i < l; i ++) { | ||
var table = tables[i]; | ||
if (!table[0]) { | ||
context.path.push(table[1]); | ||
line = getReplacement(context, table[1], table[2]); | ||
if (line !== false) { | ||
if (isString(line)) { | ||
context.lines.push(indent(line, context.level, context.space)); | ||
} | ||
table[2] = null; | ||
} | ||
tables[i+1] = null; | ||
} | ||
context.path.pop(); | ||
} | ||
for (var i = 0, l = tableArrays.length; i < l; i += 3) { | ||
context.path.push(tableArrays[i]); | ||
line = getReplacement(context, tableArrays[i], tableArrays[i+1]); | ||
if (line !== false) { | ||
if (isString(line)) { | ||
context.lines.push(indent(line, context.level, context.space)); | ||
} | ||
tableArrays[i+1] = null; | ||
context.path.pop(); | ||
continue; | ||
} | ||
for (var j = 0, k = tableArrays[i+1].length; j < k; j++) { | ||
context.path.push(j); | ||
var subTable = tableArrays[i+1][j]; | ||
line = getReplacement(context, j, subTable); | ||
context.path.pop(); | ||
} else { | ||
context.path.push(table[1]); | ||
line = getReplacement(context, table[1], table[2]); | ||
if (line !== false) { | ||
if (line == null) { | ||
tableArrays[i+1][j] = null; | ||
continue; | ||
} | ||
line = escapeKeyValue(context, tableArrays[i], tableArrays[i+1]); | ||
if (isString(line)) { | ||
context.lines.push(indent(line, context.level, context.space)); | ||
} | ||
tableArrays[i+1] = null; | ||
break; | ||
table[2] = null; | ||
context.path.pop(); | ||
continue; | ||
} | ||
var subTables = table[2]; | ||
for (var j = 0, k = subTables.length; j < k; j++) { | ||
var subTable = subTables[j]; | ||
context.path.push(j); | ||
line = getReplacement(context, j, subTable); | ||
context.path.pop(); | ||
if (line !== false) { | ||
if (line == null) { | ||
subTables[j] = null; | ||
continue; | ||
} | ||
line = escapeKeyValue(context, table[1], table[2]); | ||
if (isString(line)) { | ||
context.lines.push(indent(line, context.level, context.space)); | ||
} | ||
table[2] = null; | ||
break; | ||
} | ||
} | ||
context.path.pop(); | ||
} | ||
context.path.pop(); | ||
} | ||
} | ||
var objects = tables.concat(tableArrays); | ||
for (var i = 0, l = objects.length; i < l; i += 3) { | ||
if (objects[i+1] == null) { | ||
for (var i = 0, l = tables.length; i < l; i++) { | ||
var table = tables[i]; | ||
if (table[2] == null) { | ||
continue; | ||
} | ||
context.path.push(objects[i]); | ||
if (objects[i+2]) { | ||
context.path.push(table[1]); | ||
if (table[3]) { | ||
context.level++; | ||
} | ||
traverse(context, objects[i], objects[i+1], callback); | ||
if (objects[i+2]) { | ||
traverse(context, table[1], table[2], callback); | ||
if (table[3]) { | ||
context.level--; | ||
@@ -445,11 +459,3 @@ } | ||
var toReplacer = function (replace) { | ||
if (typeof replace === 'function') { | ||
// @type {function(this: Context, key: String|Number, value: Mixed): Mixed} | ||
return replace; | ||
} | ||
return null; | ||
}; | ||
var check = function (obj) { | ||
var check = function (obj, opts) { | ||
if (obj == null) { | ||
@@ -461,8 +467,16 @@ throw new Error('Undefined or null cannot be stringified.'); | ||
} | ||
if (opts != null && typeof opts !== 'object') { | ||
throw new Error('Options must be an object.'); | ||
} | ||
// @type {function(this: Context, key: String|Number, value: Mixed): Mixed} | ||
if (opts.replace != null && typeof opts.replace !== 'function') { | ||
throw new Error('Replacer must be a function.'); | ||
} | ||
// @type {function(a: String, b: String): Number} | ||
if (opts.sort != null && typeof opts.sort !== 'function') { | ||
throw new Error('Compare function for sorting must be a function.'); | ||
} | ||
}; | ||
var escapeTable = function (table, replace, space) { | ||
check(table); | ||
replace = toReplacer(replace); | ||
space = toSpace(space); | ||
var escapeTable = function (table, opts) { | ||
var lines = []; | ||
@@ -509,5 +523,6 @@ var callback = function (context, key, obj) { | ||
inTableArray: false, | ||
replace: replace, | ||
replace: opts.replace, | ||
sort: opts.sort, | ||
level: 0, | ||
space: space, | ||
space: toSpace(opts.space), | ||
lines: lines // So special... | ||
@@ -518,14 +533,11 @@ }, '', table, callback); | ||
var tomlify = function (table, replace, space) { | ||
if (!isTable(table)) { | ||
return escapeValue(table, replace, space); | ||
} | ||
return escapeTable(table, replace, space); | ||
}; | ||
var tomlify = {}; | ||
tomlify.toToml = function (table, replace, space) { | ||
tomlify.toToml = function (table, opts) { | ||
if (!isTable(table)) { | ||
throw new Error('An object other than Array or Date is expected.'); | ||
} | ||
return escapeTable(table, replace, space); | ||
opts = opts != null ? opts : {}; | ||
check(table, opts); | ||
return escapeTable(table, opts); | ||
}; | ||
@@ -550,5 +562,8 @@ | ||
tomlify.toValue = escapeValue; | ||
tomlify.toValue = function (obj, opts) { | ||
opts = opts != null ? opts : {}; | ||
check(obj, opts); | ||
return escapeValue(obj, opts); | ||
}; | ||
if ((typeof module !== 'undefined' && module !== null ? module.exports : void 0) != null) { | ||
@@ -555,0 +570,0 @@ module.exports = tomlify; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
231
32021
6
543
2