-
Notifications
You must be signed in to change notification settings - Fork 6
/
space.min.js
64 lines (64 loc) · 24.8 KB
/
space.min.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
function Space(a){this._load(a)}Space._setTokens=function(a,b){Space._lineChar=a||"\n";Space._lineRegex=new RegExp(Space._lineChar,"g");Space._assignmentChar=b||" ";Space._assignmentRegEx=new RegExp(Space._assignmentChar,"g")};Space._setTokens();Space.version="0.22.2";Space._isSpacePath=function(a){return-1<a.indexOf(Space._assignmentChar)};
Space.fromHeredoc=function(a,b,c){a=a.replace(/\r/g,"");a=a.split(Space._lineChar);var d=[],e=new RegExp("^"+b+("(?:"+Space._assignmentChar+"|$)")),f=a.length;b=b.length;c=new RegExp("^"+c);for(var g=null,h=0;h<f;h++)null===g?a[h].match(e)&&(g=h,a[h].length===b&&(a[h]+=Space._assignmentChar)):a[h].match(c)?(g=null,d.push(h)):a[h]=Space._assignmentChar+a[h];Space._removeItems(a,d);return new Space(a.join(Space._lineChar))};Space.fromCsv=function(a,b){return Space.fromDelimiter(a,",",b)};
Space.fromDelimiter=function(a,b,c,d,e){return Space._fromDelimiter(a,b,c,d,e||'"')};
Space._fromDelimiter=function(a,b,c,d,e){!1!==d&&-1<a.indexOf("\r")&&(a=a.replace(/\r/g,""));d=[[]];if(-1<a.indexOf(e))for(var f=a.length,g="",h=a.substr(0,1)===e,k=h?1:0,m,p,l=0,n,q;k<f;)n=a[k],p=k+1===f,m=a[k+1],q=m===e,h?n!==e?g+=n:q?(g+=m,k++):(h=!1,p&&d[l].push(g)):n===b?(d[l].push(g),g="",q&&(h=!0,k++)):"\n"===n?(d[l].push(g),g="",l++,m&&(d[l]=[]),q&&(h=!0,k++)):p?d[l].push(g+n):g+=n,k++;else for(a=a.split("\n"),e=a.length,f=0;f<e;f++)a[f]&&(d[f]=a[f].split(b));a=d[0].length;h=(c=!1===c?!1:
!0)?d[0]:[];if(c)for(e=0;e<a;e++)h[e]=h[e].replace(Space._assignmentRegEx,"");else for(e=0;e<a;e++)h.push(e);e=new Space;f=[];g=[];h={properties:h};k=d.length;m=0;for(c=c?1:0;c<k;c++){p=new Space;l=d[c];if(l.length>a)l[a-1]=l.slice(a-1).join(b),l=l.slice(0,a);else if(l.length<a)for(;l.length<a;)l.push("");p.setWithType(h,l);f.push(m);g.push(p);m++}e.setWithType({properties:f,index:f},g);return e};
Space.fromArrayWithHeader=function(a){if(!a.length)return new Space;for(var b=a.length,c=[],d=[],e={properties:a[0]},f=1;f<b;f++){var g=new Space;g.setWithType(e,a[f]);d.push(g);c.push(f-1)}a={properties:c,index:c};return(new Space).setWithType(a,d)};Space.fromSsv=function(a,b){return Space.fromDelimiter(a," ",b)};Space.fromTsv=function(a,b){return Space.fromDelimiter(a,"\t",b)};Space.makeIndex=function(a,b,c){var d=a.length;b=b||{};for(c=c||0||0;c<d;c++)b[a[c]]=c;return b};
Space._parseXml2=function(a){var b=document.createElement("div");b.innerHTML=a;return b};
Space._initializeXmlParser=function(){if(!Space._parseXml){var a=window;if("undefined"!==typeof a.DOMParser)Space._parseXml=function(b){return(new a.DOMParser).parseFromString(b,"text/xml")};else if("undefined"!==typeof a.ActiveXObject&&new a.ActiveXObject("Microsoft.XMLDOM"))Space._parseXml=function(b){var c=new a.ActiveXObject("Microsoft.XMLDOM");c.async="false";c.loadXML(b);return c};else throw Error("No XML parser found");}};
Space.fromXml=function(a){Space._initializeXmlParser();var b=Space._parseXml(a);try{return Space._fromXml(b).get("children")}catch(c){return Space._fromXml(Space._parseXml2(a)).get("children")}};
Space._fromXml=function(a){var b=new Space,c=new Space;if(a.attributes)for(var d=0;d<a.attributes.length;d++)b.set(a.attributes[d].name,a.attributes[d].value);a.data&&c.push(a.data);if(a.childNodes&&0<a.childNodes.length)for(d=0;d<a.childNodes.length;d++){var e=a.childNodes[d];if(e.tagName&&e.tagName.match(/parsererror/i))throw Error("Parse Error");0<e.childNodes.length&&e.tagName?c.append(e.tagName,Space._fromXml(e)):e.tagName?c.append(e.tagName,new Space):e.data&&(e=e.data.trim())&&c.push(e)}0<
c.length&&b.set("children",c);return b};Space._pairToString=function(a,b,c){var d=Space._assignmentChar,e=Space._lineChar;a=Space._strRepeat(d,c)+a;if(b instanceof Space)return a+e+b._toString(c+1);b=b.toString();return-1<b.indexOf(e)?a+d+b.replace(Space._lineRegex,e+Space._strRepeat(d,c+1))+e:a+d+b+e};Space._removeItems=function(a,b){var c=[];"number"===typeof b&&(b=[b]);for(var d=b.length-1;0<=d;d--)c.push(a.splice(b[d],1));return c};
Space._strRepeat=function(a,b){for(var c="",d=0;d<b;d++)c+=a;return c};Space.union=function(){for(var a=arguments.length,b=Space._unionSingle(arguments[0],arguments[1]),c=0;c<a&&(1===c||(b=Space._unionSingle(b,arguments[c]),b.length));c++);return b};
Space._unionSingle=function(a,b){var c=new Space;if(!(b instanceof Space))return c;a.each(function(a,e){var f=b._getValueByProperty(a);e instanceof Space&&f&&f instanceof Space&&c._setPair(a,Space._unionSingle(e,b._getValueByProperty(a)));e===f&&c._setPair(a,e)});return c};Space.prototype.append=function(a,b){this._setPair(a,b);return this};Space.prototype.at=function(a){return this._getValueAt(a)};
Space.prototype._clear=function(){delete this._properties;delete this._values;delete this._index;delete this._type;return this};Space.prototype.clear=function(a){if(this.isEmpty())return this;this._clear();a&&this._load(a);return this};Space.prototype.clone=function(){return new Space(this.toString())};Space.prototype.concat=function(a){"string"===typeof a&&(a=new Space(a));var b=this;a.each(function(a,d){b.append(a,d)});return this};
Space.prototype.deleteDuplicates=function(a){var b={},c=this;this.each(function(d,e,f){if(void 0!==b[d])return c._deleteByIndex(f),!0;b[d]=!0;a&&e instanceof Space&&e.deleteDuplicates(a)},null,!0);return this};Space.prototype._delete=function(a){return Space._isSpacePath(a)?this._deleteBySpacePath(a):this._deleteByProperty(a)};Space.prototype._deleteByIndex=function(a){var b=this._getValues();if(void 0===b[a])return 0;this._deleteProperty(a);b.splice(a,1);delete this._index;return 1};
Space.prototype._deleteByIndexes=function(a){for(var b=a.length,c=this._getValues(),b=b-1;0<=b;b--)this._deleteProperty(a[b]),c.splice(a[b],1);delete this._index;return this};Space.prototype._deleteByProperty=function(a){a=this.indexOf(a);return-1===a?0:this._deleteByIndex(a)};Space.prototype._deleteBySpacePath=function(a){var b=a.split(Space._assignmentChar);a=b.pop();b=this.get(b.join(Space._assignmentChar));return b instanceof Space?b._delete(a):0};
Space.prototype._deleteProperty=function(a){this._dropType();return this._getProperties().splice(a,1)};Space.prototype._dropType=function(){this._type&&(this._properties=this._type.properties.slice(),delete this._type)};Space.prototype._getProperties=function(){if(this._type)return this._type.properties;this._properties||(this._properties=[]);return this._properties};Space.prototype._insertProperty=function(a,b){this._dropType();this._getProperties().splice(a,0,b)};
Space.prototype._setProperty=function(a,b){this._dropType();this._getProperties()[a]=b;return this._getProperties()};Space.prototype._reverseProperties=function(){this._dropType();this._getProperties().reverse()};Space.prototype.decrement=function(a,b){return this.increment(a,b||-1)};Space.prototype.deepLength=function(){var a=0;this.every(function(){a++});return a};Space.prototype["delete"]=function(a){for(;this._delete(a););return this};
Space.prototype.deleteAt=function(a){"number"===typeof a?this._deleteByIndex(a):a&&a.length&&this._deleteByIndexes(a);return this};
Space.prototype.diff=function(a){var b=new Space;a instanceof Space||(a=new Space(a));var c=this;this.each(function(d,e){var f=a._getValueByProperty(d);if(void 0===f)return b._setPair(d,""),!0;var g=c._getValueByProperty(d),h=typeof f,k=typeof g;if(k!==h)return"object"===h?b._setPair(d,new Space(f)):g!=f&&b._setPair(d,f),!0;if("object"!==k)return g!==f&&b._setPair(d,f),!0;f=g.diff(f);f.length&&b._setPair(d,f)});a.each(function(d,e){if(c.has(d))return!0;if("object"!==typeof e)return b._setPair(d,e),
!0;e instanceof Space?b._setPair(d,new Space(e)):b._setPair(d,new Space(a))});return b};Space.prototype.each=function(a,b,c){var d=this.length,e=this._getProperties(),f=this._getValues();if(c)for(--d;0<=d&&(b&&f[d]instanceof Space&&f[d].each(a,b),!1!==a.call(this,e[d],f[d],d));d--);else for(c=0;c<d&&(b&&f[c]instanceof Space&&f[c].each(a,b),!1!==a.call(this,e[c],f[c],c));c++);return this};Space.prototype.every=function(a){this._every(a);return this};
Space.prototype._every=function(a,b){var c=this,d=!0;this.each(function(e,f,g){var h=f instanceof Space;h&&b||(d=a.call(c,e,f,g));if(!1===d)return!1;h&&(d=f._every(a,b));return d});return d};Space.prototype.extract=function(a){a=a.split(Space._assignmentChar);var b={},c=new Space;a.forEach(function(a){return b[a]=!0});this._extract(b,c);return c};Space.prototype._extract=function(a,b){this.each(function(c,d){a[c]?b.append(c,d):d instanceof Space&&d._extract(a,b)})};
Space.prototype.everyLeaf=function(a){this._every(a,!0);return this};Space.prototype.filter=function(a,b){if(b)return this._filterInPlace(a);for(var c=new Space,d=this.length,e=this._getProperties(),f=this._getValues(),g=0;g<d;g++)!0===a.call(this,e[g],f[g],g)&&c.append(e[g],f[g]);return c};
Space.prototype._filterInPlace=function(a){this._dropType();for(var b=this._getProperties(),c=this._getValues(),d=this.length-1;0<=d;d--)!0!==a.call(this,b[d],c[d],d)&&(b.splice(d,1),c.splice(d,1));delete this._index;return this};Space.prototype.find=function(a,b){var c=new Space;this.get(a)===b&&c.push(this);this.each(function(d,e){if(!(e instanceof Space))return!0;e.find(a,b).each(function(a,b){c.push(b)})});return c};
Space.prototype.flattenTypes=function(){var a=this.getUnionType();this.each(function(b,c){c instanceof Space&&c.setType(a)});return this};Space.prototype.format=function(a){var b=this;return a.replace(/{([^\}]+)}/g,function(a,d){var e=b.get(d);return void 0!==e?e:""})};Space.prototype.get=function(a){return void 0===a||null===a?void 0:this._getValueByString(a.toString())};Space.prototype.getAll=function(a){var b=new Space;this.each(function(c,d){if(c!==a)return!0;b.append(c,d)});return b};
Space.prototype.getArray=function(a,b){var c=[];this._getArray(a,void 0===b?!0:b,c);return c};Space.prototype._getArray=function(a,b,c){this.each(function(d,e){d===a&&c.push(e);b&&e instanceof Space&&e._getArray(a,!0,c)})};Space.prototype.getColumn=function(a){var b=[];this.each(function(c,d){d instanceof Space&&b.push(d.get(a))});return b};Space.prototype.getIndex=function(){var a=this.getParent(),b=this,c;if(!a)return-1;a.each(function(a,e,f){if(e===b)return c=f,!1});return c};
Space.prototype.getParent=function(){return this._parent||null};Space.prototype.getPath=function(){for(var a=this._parent,b=this,c="",d="";a;)a.each(function(a,f){if(f===b)return c=a+d+c,d=Space._assignmentChar,!1}),b=a,a=a._parent;return c};Space.prototype.getRoot=function(){var a=this._parent;if(!a)return this;for(;a._parent;)a=a._parent;return a||this};Space.prototype.getBySpace=function(a){return this._getValueBySpace(a)};Space.prototype._getCachedValue=function(a){return this._getValues()[this._getIndex()[a]]};
Space.prototype._getValueAt=function(a){0>a&&(a=this.length+a);return this._getValues()[a]};Space.prototype._getValueByProperty=function(a){return this._getCachedValue(a)};Space.prototype._getPropertyByIndex=function(a){var b=this.length;0>a&&(a=b+a);return a>=b?void 0:this._getProperties()[a]};Space.prototype.getProperties=function(){return this._getProperties().slice(0)};
Space.prototype.getType=function(){if(this._type)return this._type;this._type={properties:this._properties||[]};this._type.index=Space.makeIndex(this._properties);delete this._properties;delete this._index;return this._type};Space.prototype.getTypeIndex=function(){var a={};this.each(function(b,c,d){if(!(c instanceof Space))return!0;b=c.getType();void 0===b.key&&(b.key=b.properties.join(Space._assignmentChar));d=a[b.key];d?d&&d!==b&&(c._type=d):a[b.key]=b});return a};
Space.prototype.getUnionType=function(){if(!this.length)return{properties:[]};var a=this.getTypeIndex(),b=Object.keys(a);if(1===b.length)return a[b[0]];for(var a={},c=[],d={properties:c,index:a},b=b.join(Space._assignmentChar).split(Space._assignmentChar),e=b.length,f=0;f<e;f++)void 0===a[b[f]]&&(c.push(b[f]),a[b[f]]=c.length);return d};
Space.prototype._getValueByString=function(a){var b=this._getValueByProperty(a);if(b||""===b||0===b||!1===b)return b;if(Space._isSpacePath(a)&&(a=a.split(Space._assignmentChar),b=a.shift(),this.has(b)&&this._getValueByProperty(b)instanceof Space))return this._getValueByProperty(b).get(a.join(Space._assignmentChar))};
Space.prototype._getValueBySpace=function(a){var b=new Space,c=this;a.each(function(d,e){var f=c._getValueByProperty(d);if("undefined"===typeof f)return!0;if(!(a._getValueByProperty(d)instanceof Space&&a._getValueByProperty(d).length))return b._setPair(d,f),!0;if(!(f instanceof Space))return!0;b._setPair(d,f._getValueBySpace(a._getValueByProperty(d)))});return b};Space.prototype._getIndex=function(){return this._type&&this._type.index||this._index||this._makeIndex()};
Space.prototype._getValues=function(){this._values||(this._values=[]);return this._values};Space.prototype.getValues=function(){return this._getValues().slice(0)};Space.prototype.grab=function(a){var b=new Space,c=this;a.forEach(function(a){var e=c.get(a);e&&b.set(a,e)});return b};Space.prototype.group=function(a,b){"string"===typeof a&&(a=[a]);var c=new Space,d={};this.each(function(e,f,g){if(!(f instanceof Space))return!0;var h=f.grab(a);d[h]||(d[h]=new Space,c.push(d[h]));b&&b(d[h],f,e,g)});return c};
Space.prototype.has=function(a){return void 0!==this._getIndex()[a]};Space.prototype.increment=function(a,b){b=b||1;var c=this.get(a);void 0===c&&(c=0);this.set(a,parseFloat(c)+b);return this};Space.prototype.indexOf=function(a,b){if(!this.has(a))return-1;var c=this.length,d=this._getProperties();if(b)for(--c;0<=c;c--){if(d[c]===a)return c}else for(var e=0;e<c;e++)if(d[e]===a)return e;return-1};Space.prototype.insert=function(a,b,c){this._setPair(a,b,c);return this};
Space.prototype.isEmpty=function(){return 0===this.length};Space.prototype.isFlat=function(){for(var a=this.length,b=this._getValues(),c=0;c<a;c++)if(b[c]instanceof Space)return!1;return!0};Space.prototype.isStringMap=function(a){for(var b=this.length,c={},d=this._getProperties(),e=this._getValues(),f,g=0;g<b;g++){f=d[g];if(c[f])return!1;c[f]=!0;if(a&&(f=e[g],f instanceof Space&&!f.isStringMap()))return!1}return!0};Object.defineProperty(Space.prototype,"length",{get:function(){return this._getValues().length}});
Space.prototype._load=function(a,b){if(!a)return this;if("string"===typeof a)return this._loadFromString(this._sanitizeString(a));if(a instanceof Space){var c=this;a.each(function(a,b){b instanceof Space?c._setPair(a,b.clone()):c._setPair(a,b)});return this}if(a instanceof Date||"function"===typeof a)return this._load(a.toString());b||(b=[a]);return this._loadFromObject(a,b)};Space.prototype._loadFromObject=function(a,b){for(var c in a)a.hasOwnProperty(c)&&this._loadPair(c,a[c],b);return this};
Space.prototype._loadPair=function(a,b,c){var d=typeof b;null===b?this._setPair(a,"null"):void 0===b?this._setPair(a,""):"object"!==d?this._setPair(a,b):b instanceof Date?this._setPair(a,b.getTime().toString()):b instanceof Space?this._setPair(a,b.clone()):"function"===d?this._setPair(a,b.toString()):-1===c.indexOf(b)&&(c.push(b),this._setPair(a,(new Space)._load(b,c)))};
Space.prototype._sanitizeString=function(a){a=a.replace(new RegExp("^("+Space._assignmentChar+"|"+Space._lineChar+"|"+Space._assignmentChar+")*"),"");a=a.replace(/\r/g,"");return a=a.replace(new RegExp(""+Space._lineChar+Space._lineChar+"+","g"),Space._lineChar)};
Space.prototype._loadFromString=function(a){var b=Space._lineChar,c=Space._assignmentChar,d=new RegExp("^([^"+c+"]+)("+b+"|$)"),e=new RegExp("^([^"+c+"]+)"+c),f=new RegExp(""+b+c,"g"),g=new RegExp("^"+b+c+"+");a=a.split(new RegExp(b+"(?!"+c+")","g"));for(var c=a.length,h=0;h<c;h++){var k=a[h],m=void 0;(m=k.match(d))?this._setPair(m[1],(new Space)._loadFromString(k.substr(m[1].length).replace(f,b).replace(g,b))):(m=k.match(e))&&this._setPair(m[1],k.substr(m[1].length+1).replace(f,b))}return this};
Space.prototype._makeIndex=function(a){this._index&&a||(this._index={});return Space.makeIndex(this._getProperties(),this._index,a)};Space.prototype.map=function(a,b,c,d){if(!d)return(new Space(this)).map(a,b,c);for(var e=this.length,f=this._getValues(),g=this._getProperties(),h=0;h<e;h++){var k=g[h];a&&(g=this._setProperty(h,a(k)));c&&f[h]instanceof Space?f[h].map(a,b,c,d):b&&(f[h]=b(f[h],g[h],k))}delete this._index;return this};
Space.prototype.mergeDuplicates=function(){var a={},b=[],c=this;this.each(function(d,e,f){if(!(e instanceof Space))return!0;void 0===a[d]?a[d]=f:(e.each(function(b,e){c.at(a[d]).set(b,e)}),b.push(f))});return this._deleteByIndexes(b)};Space.prototype.nest=function(a){var b=new Space;b.set(a,this);return this.reload(b.toString())};Space.prototype.next=function(a){return this._getPropertyByIndex(this.indexOf(a)+1)};
Space.prototype.pairAt=function(a){return this.length?(new Space).set(this.propertyAt(a),this.at(a)):new Space};
Space.prototype._patch=function(a){a instanceof Space||(a=new Space(a));var b=this;a.each(function(a,d){if("string"===typeof d)return""===d?b._delete(a):b.set(a,d,null,!0),!0;if("number"===typeof d)return b.set(a,d,null,!0),!0;if(d instanceof Space&&!d.length)return b._delete(a),!0;if(b._getValueByProperty(a)instanceof Space)return b._getValueByProperty(a)._patch(d),!0;b.set(a,new Space(d),null,!0)});return this};Space.prototype.patch=function(a){return this._patch(a)};
Space.prototype.pop=function(){if(!this.length)return null;var a=new Space,b=this._getPropertyByIndex(-1),c=this._getValueByProperty(b);a.set(b,c);this._deleteByIndex(this.length-1);return a};Space.prototype.prepend=function(a,b){return this._setPair(a,b,0)};Space.prototype.prev=function(a){return this._getPropertyByIndex(this.indexOf(a)-1)};Space.prototype.propertyAt=function(a){if(this.length)return 0>a&&(a=this.length+a),this._getProperties()[a]};
Space.prototype.push=function(a){for(var b=this.length;this.get(b.toString());)b++;this._setPair(b.toString(),a);return this};Space.prototype.reload=function(a){delete this._properties;delete this._values;delete this._index;delete this._type;this._load(a);return this};Space.prototype._rename=function(a,b){var c=this.indexOf(a);if(-1===c)return this;this._setPair(b,this._getValueByProperty(a),c,!0);return this};
Space.prototype.rename=function(a,b,c,d){if(c)return this._renameAll(a,b,d);this._rename(a,b);return this};Space.prototype._renameAll=function(a,b,c){var d=this;this.each(function(e,f,g){e===a&&d._setPair(b,f,g,!0);c&&f instanceof Space&&f._renameAll(a,b,c)});return this};Space.prototype.renameObjects=function(a){var b=this;this.each(function(c,d,e){if(!(d instanceof Space))return!0;c=d.get(a);b._setProperty(e,c);d["delete"](a)});delete this._index;return this};
Space.prototype.replace=function(a,b){return this.reload(this.toString().replace(a,b))};Space.prototype.reverse=function(){this._reverseProperties();this._getValues().reverse();delete this._index;return this};Space.prototype.set=function(a,b,c){a=a.toString();Space._isSpacePath(a)?this._setBySpacePath(a,b):c?this._setPair(a,b,c):this.has(a)?this._setPair(a,b,this.indexOf(a),!0):this._setPair(a,b);return this};
Space.prototype.setType=function(a){if(this._type===a)return this;if(!this._values)return this.setWithType(a);var b=[],c=this;a.properties.forEach(function(a){b.push(c.get(a))});return this.setWithType(a,b)};
Space.prototype._setBySpacePath=function(a,b){for(var c=a.replace(Space._lineRegex,"").split(Space._assignmentChar).filter(function(a){return 0<a.length}),d=c.length,e=this,f,g,h,k=0;k<d;k++)f=c[k],!(g=k===c.length-1)&&e._getValueByProperty(f)instanceof Space||(h=g?b:new Space,e.has(f)?(g=e.indexOf(f),e._setPair(f,h,g,!0)):e._setPair(f,h)),e=e.get(f);return this};
Space.prototype._setPair=function(a,b,c,d){if(a=a.toString()){var e=this.length,f=typeof b,g=this._getValues(),h=b instanceof Space;!h&&"object"===f&&b&&(b=new Space(b),h=!0);"string"===f||"number"===f||h||(b=String(b));h&&(b._parent=this);void 0===c||c>=e?(this._setProperty(e,a),g.push(b),this._index&&(this._index[a]=e)):(d&&0<=c?(this._setProperty(c,a),g[c]=b):(this._insertProperty(c,a),g.splice(c,0,b)),this._index&&this._makeIndex(c))}};
Space.prototype.setWithType=function(a,b){this._values&&this._clear();a.index||(a.index=Space.makeIndex(a.properties));this._type=a;this._values=b;return this};Space.prototype.shift=function(){if(!this.length)return null;var a=this._getPropertyByIndex(0),b=new Space;b.set(a,this.get(a));this._deleteByIndex(0);return b};
Space.prototype.sort=function(a){for(var b=[],c=this._getProperties(),d=this.length,e=this._getValues(),f=0;f<d;f++)b.push({property:c[f],value:e[f],index:f});b.sort(a);this._clear();for(a=0;a<d;a++)this._setPair(b[a].property,b[a].value);return this};
Space.prototype.sortBy=function(a,b){a=a instanceof Array?a:[a];b=b instanceof Array?b:[b];var c=a.length;this.sort(function(d,e){var f=d.value instanceof Space,g=e.value instanceof Space;if(g||f){if(!f)return-1;if(!g)return 1}else return 0;for(f=0;f<c;f++){var h=a[f],g=b[f],k=d.value.get(h),h=e.value.get(h);g&&(k=g(k),h=g(h));if(k>h)return 1;if(k<h)return-1}return 0});return this};
Space.prototype.split=function(a,b){var c=b?new Space:[],d=null;this.each(function(e,f){e===a&&(d&&(b?c.append(b,d):c.push(d)),d=new Space);if(!d)return!0;d.append(e,f)});d&&(b?c.append(b,d):c.push(d));return c};Space.prototype.tableOfContents=function(){return this.getProperties().join(Space._assignmentChar)};Space.prototype.toCsv=function(){return this.toDelimited(",")};
Space.prototype.toDelimited=function(a,b){var c=new RegExp('(\\n|\\"|\\'+a+")");b=b||this.getUnionType().properties;return this._toDelimited(a,b,function(a,b,f){return a.match(c)?'"'+a.replace(/\"/g,'""')+'"':a})};Space.prototype._toDelimited=function(a,b,c){var d=b.map(function(a,b){return c(a,0,b)}).join(a)+"\n";this.each(function(e,f,g){if(!(f instanceof Space))return!0;e=b.map(function(a,b){return c((f.get(a)||"").toString(),g+1,b)}).join(a);d+=e+"\n"});return d};
Space.prototype.toArrayWithHeader=function(a){if(!this.length)return[];var b=this._getValues();if(!a&&(this.each(function(b,c){if(a=c instanceof Space&&c.getType())return!1}),!a))return[];for(var c=this.length,d=[a.properties],e=0;e<c;e++)b[e]._type===a&&d.push(b[e]._getValues());return d};
Space.prototype.toFixedWidth=function(a){a=a||100;var b=this.getUnionType().properties,c=b.map(function(b){return b.length>a?a:b.length});this.each(function(d,e){if(!(e instanceof Space))return!0;b.forEach(function(b,d){var h=e.get(b);if(!h)return!0;h=h.toString().length;h>c[d]&&(c[d]=h>a?a:h)})});return this._toDelimited(" ",b,function(a,b,f){b=c[f];a=a.toString().replace(/\n/g,"\\n");f=a.length;return f>b?a.substr(0,b):Space._strRepeat(" ",b-f)+a})};
Space.prototype.toggle=function(a,b,c){this.get(a)===b?void 0===c?this.delete(a):this.set(a,c):this.set(a,b);return this};Space.prototype.toJavascript=function(a){return a?"new Space(`"+this.toString().replace(/\`/g,"\\`")+"`)":'new Space("'+this.toString().replace(/\"/g,'\\"').replace(/\n/g,"\\n")+'")'};Space.prototype.toJSON=function(a,b){return JSON.stringify(this.toObject(a),null,b?" ":null)};
Space.prototype._toObjectWithTypes=function(){var a=this.getProperties(),b=0<a.length,c=0;a.forEach(function(a){a!==c.toString()&&(b=!1);c++});var d=b?[]:{};this.each(function(a,c){var g;g=c instanceof Space?c._toObjectWithTypes():"false"===c?!1:"true"===c?!0:"null"===c?null:"number"===typeof c||c.match(/^[\-\.]?[0-9]+[0-9\.]*$/)?parseFloat(c):c;b?d.push(g):d[a]=g});return d};
Space.prototype.toObject=function(a){if(a)return this._toObjectWithTypes();var b={};this.each(function(a,d){b[a]=d instanceof Space?d.toObject():d});return b};Space.prototype.toQueryString=function(){var a="",b="";this.each(function(c,d){a+=b+encodeURIComponent(c)+"="+encodeURIComponent(d);b="&"});return a};Space.prototype.toSsv=function(){return this.toDelimited(" ")};Space.prototype.toString=function(){return this._toString(0)};
Space.prototype._toString=function(a){for(var b=this._getProperties(),c=this.length,d=this._getValues(),e="",f=0;f<c;f++)e+=Space._pairToString(b[f],d[f],a);return e};Space.prototype.toTsv=function(){return this.toDelimited("\t")};Space.prototype.toURL=function(){return encodeURIComponent(this.toString())};Space.prototype.toXML=function(a){return this._toXML(a?0:-1)};
Space.prototype._toXML=function(a){var b=-1===a?"":Space._strRepeat(" ",a),c="";this.each(function(d,e){c+=b+"<"+d+">";e instanceof Space?0<e.length&&(c+=(-1===a?"":"\n")+e._toXML(-1<a?a+2:-1)+b):c+=e;c+="</"+d+">"+(-1===a?"":"\n")});return c};Space.prototype.toXMLWithAttributes=function(a){return this.at(0)._toXMLWithAttributes(this.propertyAt(0),a?0:-1)};
Space.prototype._toXMLWithAttributes=function(a,b){var c=-1===b?"":Space._strRepeat(" ",b),d=this.get("children"),e="",f="",g="";this.each(function(a,b){if("children"===a)return!0;f=b&&b.replace?f+(" "+a+'="'+b.replace('"','\\"')+'"'):f+(" "+a)});d&&d.each(function(a,d){g=d instanceof Space?g+((-1===b?"":"\n")+d._toXMLWithAttributes(a,-1<b?b+2:-1)+c):g+d;if("children"===a)return!0});return e+(c+"<"+a+f+">"+g+"</"+a+">"+(-1!==b&&g?"\n":""))};
Space.prototype.trim=function(a){var b=[];this.each(function(c,d,e){d instanceof Space?(a&&d.trim(a),0===d.length&&b.push(e)):d||b.push(e)});return this._deleteByIndexes(b)};Space.prototype.update=function(a,b,c){this._setPair(b,c,a,!0);return this};Space.prototype.valueAt=function(a){return this.at(a)};"undefined"!==typeof exports&&(module.exports=Space);