filter-value
Version:
Data engine is small data management lib for some sort and filter.
17 lines (16 loc) • 3.98 kB
JavaScript
/** @license Data-Engine v3.1.1
* filter-value.production.min.js
*
* Copyright Jan Silhan
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
;(function(f,c){"object"===typeof exports&&"undefined"!==typeof module?module.exports=c():"function"===typeof define&&define.amd?define(c):f.FilterValue=c()})(this,function(){function f(b){return b.replace(k,"\\$\x26")}var c=function(b){return function(a){return b===a}},k=/[-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g,l=["number","string","boolean","regexp","date"],m={number:"number",string:"string",boolean:"boolean","function":"function"},n={boolean:c,string:c,number:c,"null":c,date:function(b){return function(a){var d=
a;a.getTime||(d=new Date(a));return b.getTime()===d.getTime()}},array:function(b){return function(a){return b.some(function(b){return b.compare(a)})}},regexp:function(b){return function(a){return b.test(""+a)}},"function":function(b){return function(a){return b(a)}},range:function(b){return"object"!==(b.from?typeof b.from:typeof b.to)?function(a){var d=b.from,e=b.to;return d&&e?d<=a&&e>=a:d?d<=a:e>=a}:function(a){var d=b.from,e=b.to,c=a;a.getTime||(c=new Date(a));a=d&&e?d.getTime()<=c.getTime()&&
e.getTime()>=c.getTime():d?d.getTime()<=c.getTime():e.getTime()>=c.getTime();return a}}},p={number:function(b){return parseFloat(b)},string:function(b){return""+b},boolean:function(b){return!!b},regexp:function(b){return new RegExp(f(""+b),"i")},date:function(b){try{var a=new Date(b);if(!isNaN(a.getTime()))return a;throw new TypeError(b+" does not have format to parse with native function!");}catch(d){throw d;}}},g=function(b){switch(typeof b){case "string":return"string";case "number":return"number";
case "object":return b instanceof Date?"date":null;default:return null}},q=function(b){return l.some(function(a){return a===b})},r=function(){function b(a,b){for(var d=0;d<b.length;d++){var c=b[d];c.enumerable=c.enumerable||!1;c.configurable=!0;"value"in c&&(c.writable=!0);Object.defineProperty(a,c.key,c)}}return function(a,d,c){d&&b(a.prototype,d);c&&b(a,c);return a}}();c=function(){function b(){var a=0<arguments.length&&void 0!==arguments[0]?arguments[0]:null,c=arguments[1],e=2<arguments.length&&
void 0!==arguments[2]?arguments[2]:null;if(!(this instanceof b))throw new TypeError("Cannot call a class as a function");t.call(this);this.originalItem=null;this.Name=a;null!==e&&(this.Type=e);void 0!==c&&(this.Value=c)}b.prototype.prepareItem=function(a){switch(this.type){case "array":if(Array.isArray(a[a.length-1]))throw new TypeError("Multi dimensional array is not supported!");return a.map(function(c){return new b(a.Name,c)});default:return a}};r(b,[{key:"Name",set:function(a){"string"===typeof a&&
(this.name=a)},get:function(){return this.name}},{key:"Type",set:function(a){if(q(a))this.staticType=a;else throw this.staticType=null,new TypeError(a+" is not supported. Cannot set static type");},get:function(){return this.staticType}},{key:"InternalType",get:function(){return this.type}},{key:"Value",set:function(a){var b=this.originalItem=a;if(this.staticType)try{b=p[this.staticType](a)}catch(e){throw new TypeError(a+" cannot by typed to "+this.staticType);}if(this.type=this.checkValidity(b))this.item=
this.prepareItem(b),this.compareFunc=n[this.type](this.item);else throw new TypeError(a+" is not supported. Use custom function!");},get:function(){return this.item}},{key:"original",get:function(){return this.originalItem}}]);return b}();c.regexpEscape=f;var t=function(){var b=this;this.staticType=null;this.removeType=function(){b.staticType=null};this.compare=function(a){return b.compareFunc(a)};this.checkValidity=function(a){var b=m[typeof a]||null;return b?b:null===a?"null":Array.isArray(a)?"array":
a instanceof RegExp?"regexp":a instanceof Date?"date":(a.from||a.to)&&(g(a.from)||g(a.to))?"range":null}},h=Object.freeze({default:c});c=h&&c||h;return c["default"]?c["default"]:c});