tomlify-j0.4
Advanced tools
Comparing version 1.0.0 to 1.0.1
@@ -1,12 +0,12 @@ | ||
;(function(){'use strict';function v(a,c,b){if(null==a)throw Error("Undefined or null cannot be stringified.");if(H(a))throw Error("Converting circular structure to TOML.");c="function"===typeof c?c:null;b=C(b);if(!r(a))return D(a,c,b);var e=[];x({path:[],table:{"":a},c:!1,replace:c,a:0,b:b,d:e},"",a,function(a,b,c){var f=null;if(r(c))""!==b&&(0<e.length&&e.push(""),f=a.c?"[["+y(a.path,!0)+"]]":"["+y(a.path,!0)+"]",e.push(t(f,a.a,a.b)));else if(q(c))if(h(b))w(c)||(f=A(a,b,c),h(f)&&e.push(t(f,a.a,a.b))); | ||
else return!0;else return h(b)&&(f=A(a,b,c),h(f)&&e.push(t(f,a.a,a.b))),!0});return e.join("\n")}function C(a){return h(a)?a:"number"===typeof a&&0<=a&&isFinite(a)&&Math.floor(a)===a?E(" ",a):""}function t(a,c,b){c=E(b,c);return a.replace(/^/mg,c)}function E(a,c){if(a.repeat)return a.repeat(c);for(var b="",e="";0<c;)e+=e||a,c&1&&(b+=e),c>>>=1;return b}function x(a,c,b,e){if(!e(a,c,b)){c=a.table;a.table=b;if(q(b)){var f=a.c;a.c=w(b);for(var d=0,l=b.length;d<l;d++)a.path.push(d),x(a,d,b[d],e),a.path.pop(); | ||
a.c=f}else if(r(b)){f=a.c;a.c=!1;var p=[],m=[],g;for(g in b)Object.prototype.hasOwnProperty.call(b,g)&&(d=b[g],l=0<a.path.length&&(q(d)?w(d):r(d)),q(d)&&w(d)?m.push(g,d,l):r(d)?p.push(g,d,l):(a.path.push(g),x(a,g,d,e),a.path.pop()));if(a.replace){d=0;for(l=p.length;d<l;d+=3)a.path.push(p[d]),b=B(a,p[d],p[d+1]),h(b)?(a.d.push(t(b,a.a,a.b)),p[d+1]=null):null==b&&(p[d+1]=null),a.path.pop();d=0;for(l=m.length;d<l;d+=3){a.path.push(m[d]);b=B(a,m[d],m[d+1]);if(h(b))b=u(m[d])+" = "+b,a.d.push(t(b,a.a,a.b)); | ||
else if(null==b){m[d+1]=null;a.path.pop();continue}var n=0;for(g=m[d+1].length;n<g;n++)if(a.path.push(n),b=B(a,d,m[d+1][n]),a.path.pop(),!1!==b){b=A(a,m[d],m[d+1]);h(b)&&a.d.push(t(b,a.a,a.b));m[d+1]=null;break}a.path.pop()}}g=p.concat(m);d=0;for(l=g.length;d<l;d+=3)null!=g[d+1]&&(a.path.push(g[d]),g[d+2]&&a.a++,x(a,g[d],g[d+1],e),g[d+2]&&a.a--,a.path.pop());a.c=f}a.table=c}}function B(a,c,b){b=a.replace.call(F(a),c,b);if(h(b))return u(c)+" = "+b;if(null==b)return null;if(!1!==b)throw Error(n(a.path, | ||
"Replacer must return a string, false, null or undefined."));return!1}function F(a){var c={},b;for(b in a.table)c[b]=a.table[b];return{path:a.path.slice(0),table:c}}function A(a,c,b){var e=u(c);if(!e)throw Error(n(a.path,"Key cannot be an empty string."));a=z(a,c,b);return h(a)?e+" = "+a:null}function u(a){return a?/^[a-zA-Z0-9\-_]+$/.test(a)?a:G(a):!1}function D(a,c,b){if(null==a)throw Error("Undefined or null cannot be stringified.");return z({path:[],table:{"":a},c:!1,replace:"function"===typeof c? | ||
c:null,a:0,b:C(b)},"",a)}function z(a,c,b){if(a.replace){c=a.replace.call(F(a),c,b);if(h(c))return c;if(null==c)return null;if(!1!==c)throw Error(n(a.path,"Replacer must return a string, false, null or undefined."));}switch(f(b)){case f.Boolean:return b?"true":"false";case f.String:return G(b);case f.Number:if(!isFinite(b))throw Error(n(a.path,"Number must be finite."));a=(Number.e?Number.e(b):Math.floor(b)===b)?b.toFixed(1):String(b);a=a.replace(/([eE])(\d)/,"$1+$2");return/[.eE]/.test(a)?a:a+".0"; | ||
case f.Array:var e;a:{if(!(2>b.length)){e=f(b[0]);c=1;for(var k=b.length;c<k;c++)if(null!=b[c]&&f(b[c])!==e){e=!0;break a}}e=!1}if(e)throw Error(n(a.path,"Array cannot contain values of different types."));c=a.table;a.table=b;e=[];for(var k=0,d=b.length;k<d;k++)if(null!=b[k]){a.path.push(k);var l=z(a,k,b[k]);h(l)&&e.push(l);a.path.pop()}a.table=c;if(c=0<e.length&&a.b){a:{if(!(1>b.length))for(c=0,k=b.length;c<k;c++)if(q(b[c])){c=!0;break a}c=!1}c=c||w(b)}return c?"[\n"+t(e.join(",\n"),1,a.b)+"\n]": | ||
"["+e.join(", ")+"]";case f.Date:if(!isFinite(b.getTime()))throw Error(n(a.path,"Invalid Date"));return b.toISOString();default:c=a.table;a.table=b;k=[];for(e in b)if(Object.prototype.hasOwnProperty.call(b,e)&&null!=b[e]){if(!e)throw Error(n(a.path,"Key cannot be an empty string."));a.path.push(e);d=z(a,e,b[e]);h(d)&&k.push(u(e)+" = "+d);a.path.pop()}a.table=c;return"{"+k.join(", ")+"}"}}function G(a){return"object"===typeof JSON&&JSON?JSON.stringify(a):'"'+String(a).replace(/[\x00-\x1F"\\]/g,function(a){switch(a){case '"':case "\\":return"\\"+ | ||
a;case "\t":return"\\t";case "\n":return"\\n";case "\r":return"\\r";case "\b":return"\\b";case "\f":return"\\f";default:return a=a.charCodeAt(0).toString(16),"\\u"+"0000".substr(a.length)+a}})+'"'}function H(a){function c(a,e){if(!q(a)&&!r(a))return!1;if(-1!==e.indexOf(a))return!0;e.push(a);for(var f in a)if(Object.prototype.hasOwnProperty.call(a,f)){var d=e.length;if(c(a[f],e))return!0;e.splice(d)}return!1}return c(a,[])}function w(a){if(1>a.length)return!1;for(var c=0,b=a.length;c<b;c++)if(r(a[c]))return!0; | ||
return!1}function r(a){return null!==a&&"object"===typeof a&&!(q(a)||f(a)===f.Date)}function h(a){return"string"===typeof a}function f(a){return Object.prototype.toString.call(a)}function n(a,c){return(y(a)||"<root>")+": "+c}function y(a,c){if(0===a.length)return"";for(var b="",e=0,f=a.length;e<f;e++)if(h(a[e])&&a[e])b+=(b?".":"")+u(a[e]);else if("number"===typeof a[e])c||(b+=(b?".":"")+"["+a[e]+"]");else return!1;return b?b:!1}f.Boolean=f(!1);f.String=f("");f.Number=f(0);f.Array=f([]);f.Date=f(new Date(0)); | ||
var q=Array.isArray||function(a){return f(a)===f.Array};v.toKey=function(a,c){if(h(a)){var b=u(a);if(!b)throw Error("Key cannot be an empty string.");return b}if(q(a)){b=y(a,c);if(!1===b)throw Error("Key path must consist of non-empty string(s).");return b}throw Error("Invalid Arguments for tomlify.toKey({String | Array})");};v.toValue=D;null!=("undefined"!==typeof module&&null!==module?module.exports:void 0)?module.exports=v:"function"===typeof define&&define.amd?define([],function(){return v}): | ||
this.tomlify=v}).call(this); | ||
;(function(){'use strict';function u(a,c,b){if(null==a)throw Error("Undefined or null cannot be stringified.");if(F(a))throw Error("Converting circular structure to TOML.");c="function"===typeof c?c:null;b=B(b);if(!n(a))return C(a,c,b);var d=[];w({path:[],table:{"":a},c:!1,replace:c,a:0,b:b,d:d},"",a,function(a,b,c){var g=null;if(n(c))""!==b&&(0<d.length&&d.push(""),g=a.c?"[["+x(a.path,!0)+"]]":"["+x(a.path,!0)+"]",d.push(r(g,a.a,a.b)));else if(p(c))if(k(b))v(c)||(g=A(a,b,c),k(g)&&d.push(r(g,a.a,a.b))); | ||
else return!0;else return k(b)&&(g=A(a,b,c),k(g)&&d.push(r(g,a.a,a.b))),!0});return d.join("\n")}function B(a){return k(a)?a:"number"===typeof a&&0<=a&&isFinite(a)&&Math.floor(a)===a?D(" ",a):""}function r(a,c,b){c=D(b,c);return a.replace(/^/mg,c)}function D(a,c){if(a.repeat)return a.repeat(c);for(var b="",d="";0<c;)d+=d||a,c&1&&(b+=d),c>>>=1;return b}function w(a,c,b,d){var e;if(a.replace&&0===a.path.length&&(e=y(a,c,b),k(e)&&a.d.push(e),!1!==e))return;if(!d(a,c,b)){c=a.table;a.table=b;if(p(b)){var g= | ||
a.c;a.c=v(b);for(var f=0,l=b.length;f<l;f++)a.path.push(f),w(a,f,b[f],d),a.path.pop();a.c=g}else if(n(b)){g=a.c;a.c=!1;var q=[],m=[],h;for(h in b)Object.prototype.hasOwnProperty.call(b,h)&&(e=b[h],f=0<a.path.length&&(p(e)?v(e):n(e)),p(e)&&v(e)?m.push(h,e,f):n(e)?q.push(h,e,f):(a.path.push(h),w(a,h,e,d),a.path.pop()));if(a.replace){f=0;for(l=q.length;f<l;f+=3)a.path.push(q[f]),e=y(a,q[f],q[f+1]),k(e)?(a.d.push(r(e,a.a,a.b)),q[f+1]=null):null==e&&(q[f+1]=null),a.path.pop();f=0;for(l=m.length;f<l;f+= | ||
3){a.path.push(m[f]);e=y(a,m[f],m[f+1]);if(k(e))e=t(m[f])+" = "+e,a.d.push(r(e,a.a,a.b));else if(null==e){m[f+1]=null;a.path.pop();continue}b=0;for(h=m[f+1].length;b<h;b++)if(a.path.push(b),e=y(a,f,m[f+1][b]),a.path.pop(),!1!==e){e=A(a,m[f],m[f+1]);k(e)&&a.d.push(r(e,a.a,a.b));m[f+1]=null;break}a.path.pop()}}h=q.concat(m);f=0;for(l=h.length;f<l;f+=3)null!=h[f+1]&&(a.path.push(h[f]),h[f+2]&&a.a++,w(a,h[f],h[f+1],d),h[f+2]&&a.a--,a.path.pop());a.c=g}a.table=c}}function y(a,c,b){if(a.replace){b=a.replace.call({path:a.path.slice(0), | ||
table:a.table},c,b);if(k(b))return t(c)+" = "+b;if(null==b)return null;if(!1!==b)throw Error(l(a.path,"Replacer must return a string, false, null or undefined."));}return!1}function A(a,c,b){var d=t(c);if(!d)throw Error(l(a.path,"Key cannot be an empty string."));a=z(a,c,b);return k(a)?d+" = "+a:null}function t(a){return a?/^[a-zA-Z0-9\-_]+$/.test(a)?a:E(a):!1}function C(a,c,b){if(null==a)throw Error("Undefined or null cannot be stringified.");return z({path:[],table:{"":a},c:!1,replace:"function"=== | ||
typeof c?c:null,a:0,b:B(b)},"",a)}function z(a,c,b){if(a.replace){c=a.replace.call({path:a.path.slice(0),table:a.table},c,b);if(k(c))return c;if(null==c)return null;if(!1!==c)throw Error(l(a.path,"Replacer must return a string, false, null or undefined."));}switch(g(b)){case g.Boolean:return b?"true":"false";case g.String:return E(b);case g.Number:if(!isFinite(b))throw Error(l(a.path,"Number must be finite."));a=(Number.e?Number.e(b):Math.floor(b)===b)?b.toFixed(1):String(b);a=a.replace(/([eE])(\d)/, | ||
"$1+$2");return/[.eE]/.test(a)?a:a+".0";case g.Array:var d;a:{if(!(2>b.length)){d=g(b[0]);c=1;for(var e=b.length;c<e;c++)if(null!=b[c]&&g(b[c])!==d){d=!0;break a}}d=!1}if(d)throw Error(l(a.path,"Array cannot contain values of different types."));c=a.table;a.table=b;d=[];for(var e=0,n=b.length;e<n;e++){a.path.push(e);var f=z(a,e,b[e]);k(f)&&d.push(f);a.path.pop()}a.table=c;if(c=0<d.length&&a.b){a:{if(!(1>b.length))for(c=0,e=b.length;c<e;c++)if(p(b[c])){c=!0;break a}c=!1}c=c||v(b)}return c?"[\n"+r(d.join(",\n"), | ||
1,a.b)+"\n]":"["+d.join(", ")+"]";case g.Date:if(!isFinite(b.getTime()))throw Error(l(a.path,"Invalid Date"));return b.toISOString();default:if(null==b)a=null;else{c=a.table;a.table=b;e=[];for(d in b)if(Object.prototype.hasOwnProperty.call(b,d)&&null!=b[d]){if(!d)throw Error(l(a.path,"Key cannot be an empty string."));a.path.push(d);n=z(a,d,b[d]);k(n)&&e.push(t(d)+" = "+n);a.path.pop()}a.table=c;a="{"+e.join(", ")+"}"}return a}}function E(a){return"object"===typeof JSON&&JSON?JSON.stringify(a):'"'+ | ||
String(a).replace(/[\x00-\x1F"\\]/g,function(a){switch(a){case '"':case "\\":return"\\"+a;case "\t":return"\\t";case "\n":return"\\n";case "\r":return"\\r";case "\b":return"\\b";case "\f":return"\\f";default:return a=a.charCodeAt(0).toString(16),"\\u"+"0000".substr(a.length)+a}})+'"'}function F(a){function c(a,d){if(!p(a)&&!n(a))return!1;if(-1!==d.indexOf(a))return!0;d.push(a);for(var e in a)if(Object.prototype.hasOwnProperty.call(a,e)){var g=d.length;if(c(a[e],d))return!0;d.splice(g)}return!1}return c(a, | ||
[])}function v(a){if(1>a.length)return!1;for(var c=0,b=a.length;c<b;c++)if(n(a[c]))return!0;return!1}function n(a){return null!==a&&"object"===typeof a&&!(p(a)||g(a)===g.Date)}function k(a){return"string"===typeof a}function g(a){return Object.prototype.toString.call(a)}function l(a,c){return(x(a)||"<root>")+": "+c}function x(a,c){if(0===a.length)return"";for(var b="",d=0,e=a.length;d<e;d++)if(k(a[d])&&a[d])b+=(b?".":"")+t(a[d]);else if("number"===typeof a[d])c||(b+=(b?".":"")+"["+a[d]+"]");else return!1; | ||
return b?b:!1}g.Boolean=g(!1);g.String=g("");g.Number=g(0);g.Array=g([]);g.Date=g(new Date(0));var p=Array.isArray||function(a){return g(a)===g.Array};u.toKey=function(a,c){if(k(a)){var b=t(a);if(!b)throw Error("Key cannot be an empty string.");return b}if(p(a)){b=x(a,c);if(!1===b)throw Error("Key path must consist of non-empty string(s).");return b}throw Error("Invalid Arguments for tomlify.toKey({String | Array})");};u.toValue=C;null!=("undefined"!==typeof module&&null!==module?module.exports:void 0)? | ||
module.exports=u:"function"===typeof define&&define.amd?define([],function(){return u}):this.tomlify=u}).call(this); |
{ | ||
"name": "tomlify-j0.4", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"description": "A Object->TOML encoder/converter for TOML v0.4.0", | ||
@@ -5,0 +5,0 @@ "main": "tomlify.js", |
@@ -19,3 +19,7 @@ # TOMLify-j0.4 | ||
The parser used in the demo is [toml-j0.4] | ||
[toml-j0.4]: https://github.com/jakwings/toml-j0.4 | ||
### Usage | ||
@@ -48,3 +52,3 @@ | ||
year: new Date('2015-01-01T00:08:00+08:00'), | ||
months: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], | ||
months: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12] | ||
} | ||
@@ -99,2 +103,10 @@ }; | ||
*/ | ||
var text = tomlify({ | ||
null: null, | ||
undefined: undefined, | ||
numbers: [1, 2, null, , 3, 4] | ||
}); | ||
/* OUTPUT: | ||
* numbers = [1.0, 2.0, 3.0, 4.0] | ||
*/ | ||
} catch (err) { | ||
@@ -116,5 +128,8 @@ // do something | ||
default, all numbers are transformed into floats and arrays of numbers will | ||
become arrays of floats. You can change this behavior through `replacer`. | ||
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`. | ||
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)`. | ||
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)`. | ||
@@ -145,3 +160,3 @@ * replacer - `{function(this: Context, key: String|Number, value:Mixed): Mixed}`: | ||
```javascript | ||
tomlify.toValue({one: 1, two: 2}); //=> { one = 1.0, two = 2.0 } | ||
tomlify.toValue({one: 1, two: 2}); //=> {one = 1.0, two = 2.0} | ||
``` | ||
@@ -157,5 +172,7 @@ | ||
```javascript | ||
tomlify.toKey(['sir', 'Mr. Smith']); //=> sir."Mr.Smith" | ||
tomlify.toKey(['sir', 'Mr. Smith']); //=> sir."Mr. Smith" | ||
tomlify.toKey(['food', 0, 'price']); //=> food.[0].price | ||
tomlify.toKey(['food', 0, 'price'], true); //=> food.price | ||
``` | ||
@@ -166,3 +183,3 @@ | ||
* JavaScript does not have integer type. | ||
* JavaScript does not have any integer type. | ||
@@ -172,3 +189,3 @@ All numbers are floats in JavaScript. Any integer bigger than | ||
Number.MIN_SAFE_INTEGER (-9007199254740991 > -(2^63 - 1)) is not safe when | ||
being converted or used as pure integer! You should store big integers in | ||
being converted or used as a pure integer! You should store big integers in | ||
strings. | ||
@@ -175,0 +192,0 @@ |
@@ -167,10 +167,8 @@ (function () { | ||
for (var i = 0, l = obj.length; i < l; i++) { | ||
if (obj[i] != null) { | ||
context.path.push(i); | ||
var valueText = escapeValue_(context, i, obj[i]); | ||
if (isString(valueText)) { | ||
lines.push(valueText); | ||
} | ||
context.path.pop(); | ||
context.path.push(i); | ||
var valueText = escapeValue_(context, i, obj[i]); | ||
if (isString(valueText)) { | ||
lines.push(valueText); | ||
} | ||
context.path.pop(); | ||
} | ||
@@ -236,2 +234,5 @@ context.table = table; | ||
default: | ||
if (obj == null) { | ||
return null; | ||
} | ||
return escapeInlineTable(context, key, obj); | ||
@@ -274,9 +275,5 @@ } | ||
var wrapContext = function (context) { | ||
var table = {}; | ||
for (var k in context.table) { | ||
table[k] = context.table[k]; | ||
} | ||
return { | ||
path: context.path.slice(0), | ||
table: table, | ||
table: context.table | ||
}; | ||
@@ -286,10 +283,12 @@ }; | ||
var getReplacement = function (context, key, obj) { | ||
var valueText = context.replace.call(wrapContext(context), key, obj); | ||
if (isString(valueText)) { | ||
return escapeKey(key) + ' = ' + valueText; | ||
} else if (valueText == null) { | ||
return null; | ||
} else if (valueText !== false) { | ||
throw new Error(genErrMsg(context.path, | ||
'Replacer must return a string, false, null or undefined.')); | ||
if (context.replace) { | ||
var valueText = context.replace.call(wrapContext(context), key, obj); | ||
if (isString(valueText)) { | ||
return escapeKey(key) + ' = ' + valueText; | ||
} else if (valueText == null) { | ||
return null; | ||
} else if (valueText !== false) { | ||
throw new Error(genErrMsg(context.path, | ||
'Replacer must return a string, false, null or undefined.')); | ||
} | ||
} | ||
@@ -300,2 +299,12 @@ return false; | ||
var traverse = function (context, key, obj, callback) { | ||
var line; | ||
if (context.replace && context.path.length === 0) { | ||
line = getReplacement(context, key, obj); | ||
if (isString(line)) { | ||
context.lines.push(line); | ||
} | ||
if (line !== false) { | ||
return; | ||
} | ||
} | ||
if (callback(context, key, obj)) { | ||
@@ -339,3 +348,3 @@ return; | ||
context.path.push(tables[i]); | ||
var line = getReplacement(context, tables[i], tables[i+1]); | ||
line = getReplacement(context, tables[i], tables[i+1]); | ||
if (isString(line)) { | ||
@@ -351,3 +360,3 @@ context.lines.push(indent(line, context.level, context.space)); | ||
context.path.push(tableArrays[i]); | ||
var line = getReplacement(context, tableArrays[i], tableArrays[i+1]); | ||
line = getReplacement(context, tableArrays[i], tableArrays[i+1]); | ||
if (isString(line)) { | ||
@@ -364,3 +373,3 @@ line = escapeKey(tableArrays[i]) + ' = ' + line; | ||
var subTable = tableArrays[i+1][j]; | ||
var line = getReplacement(context, i, subTable); | ||
line = getReplacement(context, i, subTable); | ||
context.path.pop(); | ||
@@ -367,0 +376,0 @@ if (line !== false) { |
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
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
28745
528
187