mirror of https://gitlab.com/octtspacc/sitoctt
9 lines
4.3 KiB
JavaScript
9 lines
4.3 KiB
JavaScript
|
/**
|
||
|
* Created by Erxin, Shang(Edwin) on 6/21/2016.
|
||
|
* JavaScript Configuration file(.ini) content parser, similar to python ConfigParser without I/O operations
|
||
|
* The license is under GPL-3.0
|
||
|
* Git repo:https://github.com/shangerxin/config-ini
|
||
|
* Author homepage: http://www.shangerxin.com
|
||
|
* Version, 1.6.1
|
||
|
*/
|
||
|
!function(t){var n=Object.assign(new Error,{name:"ConfigIniParser Error",message:"Parse config ini file error"}),r=Object.assign(new Error,{name:"ConfigIniParser Error",message:"The specify section not found"}),e=Object.assign(new Error,{name:"ConfigIniParser Error",message:"The specify option not found"}),i=Object.assign(new Error,{name:"ConfigIniParser Error",message:"Found duplicated section in the given ini file"}),o=Object.assign(new Error,{name:"ConfigIniParser Error",message:"Multiple call parse on the same parser instance"}),s=Object.assign(new TypeError,{name:"ConfigIniParser Error",message:"The argument type is not correct"}),a="__DEFAULT_SECTION__",u=/^\s*\[\s*([^\]]+?)\s*\]\s*$/,p=/\s*([^=:\s]+)\s*[=:]\s*(.*)\s*/,f=/^\s*[#;].*/,c=/^\s*$/,h=-1;function l(t,n){for(var r=t.sections,e=0;e<r.length;e++){var i=r[e];if(i.name==n)return i}}function m(t,n){for(var r=t.options,e=0;e<r.length;e++){var i=r[e];if(i.name==n)return i}}function g(t){return{name:t,options:[]}}function v(t,n){return{name:t,value:n}}var y=function(t){this.delimiter=t||"\n",this._calledParse=!1,this._ini={sections:[]},this._ini.sections.push(g(a))};y.prototype.addSection=function(t){if(l(this._ini,t))throw i;var n=g(t);return this._ini.sections.push(n),this},y.prototype.get=function(t,n,r){var i=l(this._ini,t||a);if(i){var o=m(i,n);if(o)return o.value}if(void 0===r)throw e;return r},y.prototype.getOptionFromDefaultSection=function(t,n){return this.get(null,t,n)},y.prototype.getBoolean=function(t,n){var r=this.get(t||a,n);return isNaN(r)?"true"==String(r).toLowerCase():0!=r},y.prototype.getBooleanFromDefaultSection=function(t){return this.getBoolean(null,t)},y.prototype.getNumber=function(t,n){return+this.get(t||a,n)},y.prototype.getNumberFromDefaultSection=function(t){return this.getNumber(null,t)},y.prototype.isHaveSection=function(t){if(!t)throw s;return!!l(this._ini,t)},y.prototype.isHaveOption=function(t,n){var r=l(this._ini,t||a);if(r&&m(r,n))return!0;return!1},y.prototype.isHaveOptionInDefaultSection=function(t){return this.isHaveOption(null,t)},y.prototype.items=function(t){for(var n=l(this._ini,t||a),r=[],e=0;e<n.options.length;e++){var i=n.options[e];r.push([i.name,i.value])}return r},y.prototype.options=function(t){var n=l(this._ini,t||a);if(n){for(var e,i=[],o=n.options,s=0;s<o.length;s++)e=o[s],i.push(e.name);return i}throw r},y.prototype.removeOption=function(t,n){var r=l(this._ini,t||a);if(r){var e=function(t,n){for(var r=t.options,e=0;e<r.length;e++)if(r[e].name==n)return e;return h}(r,n);if(e!=h)return r.options.splice(e,1),!0}return!1},y.prototype.removeOptionFromDefaultSection=function(t){return this.removeOption(null,t)},y.prototype.removeSection=function(t){var n=function(t,n){for(var r=t.sections,e=0;e<r.length;e++)if(r[e].name==n)return e;return h}(this._ini,t||a);return n!=h&&(this._ini.sections.splice(n,1),t===a&&this._ini.sections.push(g(a)),!0)},y.prototype.sections=function(){for(var t,n=[],r=this._ini.sections,e=0;e<r.length;e++)(t=r[e]).name!=a&&n.push(t.name);return n},y.prototype.set=function(t,n,e){var i,o=l(this._ini,t||a);if(o)return(i=m(o,n))?(i.value=e,this):(i=v(n,e),o.options.push(i),this);throw r},y.prototype.setOptionInDefaultSection=function(t,n){return this.set(null,t,n)},y.prototype.stringify=function(t){for(var n,r,e,i=[],o=this._ini.sections,s=0;s<o.length;s++){(n=o[s]).name!=a&&i.push("["+n.name+"]"),r=n.options;for(var u=0;u<r.length;u++)e=r[u],i.push(e.name+"="+e.value);i.length>0&&i.push("")}return i.join(t||this.delimiter)},y.prototype.parse=function(t){if(this._calledParse)throw o;this._calledParse=!0;for(var r=t.split(this.delimiter),e=l(this._ini,a),s=0;s<r.length;s++){var h=r[s];if(!h.match(f)&&!h.match(c)){var m=h.match(u);if(m){var y=m[1];if(l(this._ini,y))throw i;e=g(y),this._ini.sections.push(e)}else{var _=h.match(p);if(!_)throw n;var E=v(_[1],_[2]);e.options.push(E)}}}return this},y.Errors={Error:n,ErrorNoSection:r,ErrorNoOption:e,ErrorDuplicateSectionError:i,ErrorCallParseMultipleTimes:o,ErrorIncorrectArgumentType:s},t.ConfigIniParser=y}("undefined"!=typeof exports?exports:"undefined"!
|