UNPKG

filter-value

Version:

Data engine is small data management lib for some sort and filter.

18 lines (17 loc) 3.82 kB
/** @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. */ 'use strict';function f(b){return function(a){return b===a}}var g=/[-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g;function h(b){return b.replace(g,"\\$\x26")} var k=["number","string","boolean","regexp","date"],l={number:"number",string:"string",boolean:"boolean","function":"function"},m={boolean:f,string:f,number:f,"null":f,date:function(b){return function(a){var c=a;a.getTime||(c=new Date(a));return b.getTime()===c.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 c=b.from,d=b.to;return c&&d?c<=a&&d>=a:c?c<=a:d>=a}:function(a){var c=b.from,d=b.to,e=a;a.getTime||(e=new Date(a));a=c&&d?c.getTime()<=e.getTime()&&d.getTime()>=e.getTime():c?c.getTime()<=e.getTime():d.getTime()>=e.getTime();return a}}},n={number:function(b){return parseFloat(b)},string:function(b){return""+b},boolean:function(b){return!!b},regexp:function(b){return new RegExp(h(""+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(c){throw c;}}};function p(b){switch(typeof b){case "string":return"string";case "number":return"number";case "object":return b instanceof Date?"date":null;default:return null}}function q(b){return k.some(function(a){return a===b})} var r=function(){function b(a,b){for(var c=0;c<b.length;c++){var e=b[c];e.enumerable=e.enumerable||!1;e.configurable=!0;"value"in e&&(e.writable=!0);Object.defineProperty(a,e.key,e)}}return function(a,c,d){c&&b(a.prototype,c);d&&b(a,d);return a}}(),u=function(){function b(){var a=0<arguments.length&&void 0!==arguments[0]?arguments[0]:null,c=arguments[1],d=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!==d&&(this.Type=d);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=n[this.staticType](a)}catch(d){throw new TypeError(a+" cannot by typed to "+this.staticType);}if(this.type=this.checkValidity(b))this.item=this.prepareItem(b),this.compareFunc=m[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}();u.regexpEscape=h;function t(){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=l[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)&&(p(a.from)||p(a.to))?"range":null}} var v=Object.freeze({default:u}),w=v&&u||v;module.exports=w["default"]?w["default"]:w;