-
Notifications
You must be signed in to change notification settings - Fork 0
/
sanityjs.min.js
2 lines (2 loc) · 8.8 KB
/
sanityjs.min.js
1
2
/*! sanityjs v1.7.1 2016-10-07 */
!function(){function a(a){return{}.toString.call(a).match(/\s([a-zA-Z]+)/)[1].toLowerCase()}function b(a){return{}.toString.call(a)}function c(a){return a===!0||a===!1||"[object Boolean]"===b(a)}function d(a){return"[object Number]"===b(a)}function e(a){return d(a)&&a%1===0}function f(a){return d(a)&&a%1!==0}function g(a){return"[object String]"===b(a)}function h(a){return"[object Date]"===b(a)}function i(a){return"[object RegExp]"===b(a)}function j(a){return"[object Error]"===b(a)}function k(a){return"[object Arguments]"===b(a)}function l(a){return"[object Function]"===b(a)}function m(a){return void 0===a||"[object Undefined]"===b(a)}function n(a){return!m(a)}function o(a){return Array.isArray(a)}function p(a){return"[object Object]"===b(a)}function q(a){return 0===Object.keys(a).length}function r(a,b,c,d){if(a===b)return 0!==a||1/a===1/b;if(null===a||null===b)return a===b;var e=toString.call(a);if(e!==toString.call(b))return!1;switch(e){case"[object RegExp]":case"[object String]":return""+a==""+b;case"[object Number]":return+a!==+a?+b!==+b:0===+a?1/+a===1/b:+a===+b;case"[object Date]":case"[object Boolean]":return+a===+b}var f="[object Array]"===e;if(!f){if("object"!=typeof a||"object"!=typeof b)return!1;var g=a.constructor,h=b.constructor;if(g!==h&&!(_.isFunction(g)&&g instanceof g&&_.isFunction(h)&&h instanceof h)&&"constructor"in a&&"constructor"in b)return!1}c=c||[],d=d||[];for(var i=c.length;i--;)if(c[i]===a)return d[i]===b;if(c.push(a),d.push(b),f){if(i=a.length,i!==b.length)return!1;for(;i--;)if(!r(a[i],b[i],c,d))return!1}else{var j,k=s(a);if(i=k.length,s(b).length!==i)return!1;for(;i--;)if(j=k[i],!t(b,j)||!r(a[j],b[j],c,d))return!1}return c.pop(),d.pop(),!0}function s(a){if(!p(a))return[];if(Object.keys)return Object.keys(a);var b=[];for(var c in a)t(a,c)&&b.push(c);return b}function t(a,b){return null!==a&&Object.prototype.hasOwnProperty.call(a,b)}function u(a,b){if(!m(b)&&p(b)||(b={}),b.throw_exception=n(b.throw_exception)?b.throw_exception:!0,b.verbose=n(b.verbose)?b.verbose:!1,b.throw_exception)throw new TypeError(a);return b.verbose&&console.error("[SanityJS Error]: "+a),!1}function v(a,b){!m(b)&&p(b)||(b={}),b.throw_exception=n(b.throw_exception)?b.throw_exception:!0,b.verbose=n(b.verbose)?b.verbose:!1,b.verbose&&console.warn("[SanityJS Warning]: "+a)}function w(a,b){!m(b)&&p(b)||(b={}),b.throw_exception=n(b.throw_exception)?b.throw_exception:!0,b.verbose=n(b.verbose)?b.verbose:!1,b.verbose&&console.log("[SanityJS Log]: "+a)}function x(a,b,k,s,t,y){if(!m(k)&&g(k)||(v("Bad third parameter 'name' in function object_check. It should be a string. Default empty name used instead.",s),k=""),m(b)||!p(b)&&!g(b)&&!o(b))return u("Bad second parameter 'type' for obj '"+k+"' in function object_check. It's a mandatory argument, must be a string, an object or an array",s);if(o(b)){var z,D=b.length,E=console.log,F=console.error,G="";for(console.log=function(a){G+=" "+a},console.error=function(a){G+=" "+a},z=0;D>z;z++)try{if(x(a,b[z],k,s,t,y))return console.log=E,console.error=F,!0}catch(H){G+=" "+H.message}return console.log=E,console.error=F,u(G,s)}if(g(b)&&(b={type:b}),!m(t)&&p(t)||(n(t)&&!p(t)&&v("Bad fifth parameter 'ctx' for obj '"+k+"' in function object_check. It should be an object. Recovered by overwriting.",s),t={}),!m(t.labels)&&p(t.labels)||(n(t.labels)&&!p(t.labels)&&v("Bad argument 'labels' of parameter 'ctx' for obj '"+k+"' in function object_check. It should be an object. Recovered by overwriting.",s),t.labels={}),!m(t.recursion_depth)&&e(t.recursion_depth)||(n(t.recursion_depth)&&!p(t.recursion_depth)&&v("Bad argument 'recursion_depth' of parameter 'ctx' for obj '"+k+"' in function object_check. It should be an integer. Recovered by overwriting.",s),t.recursion_depth=0),!m(t.recursion_count_warned)&&c(t.recursion_count_warned)||(n(t.recursion_count_warned)&&!c(t.recursion_count_warned)&&v("Bad argument 'recursion_count_warned' of parameter 'ctx' for obj '"+k+"' in function object_check. It should be a boolean. Recovered by overwriting.",s),t.recursion_count_warned=!1),!m(y)&&e(y)||(n(y)&&!e(y)&&v("Bad sixth parameter 'recursion_count' for obj '"+k+"' in function object_check. It should be an integer. Recovered by overwriting.",s),y=-1),y++,y>B&&!t.recursion_count_warned&&(v("Object depth reached "+B+". Behavior beyond this depth can't be guaranteed and checker might slow down.",s),t.recursion_count_warned=!0),y>t.recursion_depth&&(t.recursion_depth=y),b.not_empty=n(b.not_empty)?b.not_empty:!1,b.full_check=n(b.full_check)?b.full_check:!1,n(b.equal)&&!o(b.equal))return u("Bad equal parameter for object "+k+". It should be an array",s);if(n(b.not_equal)&&!o(b.not_equal))return u("Bad not_equal parameter for object "+k+". It should be an array",s);if(n(b.cb)&&!l(b.cb))return u("Bad cb parameter for object "+k+". It should be a function",s);if(n(b.cb_message)&&!g(b.cb_message))return u("Bad cb_message parameter for object "+k+". It should be a string",s);if(n(b.label)&&!g(b.label))return u("Bad label parameter for object "+k+". It should be a string",s);if(n(b.length)&&!e(b.length))return u("Bad length parameter for object "+k+". It should be an integer",s);if(n(b.structure)&&!o(b.structure))return u("Bad structure parameter for object "+k+". It should be an array",s);if(n(b.sub_type)&&!p(b.sub_type)&&!g(b.sub_type)&&!o(b.sub_type))return u("Bad sub_type parameter for object "+k+". It should be a string, an object or an array",s);var I,J;switch(b.type){case"defined":I=n(a);break;case"undefined":I=m(a);break;case"boolean":I=c(a);break;case"number":if(I=d(a),!I)break;if(isNaN(a))return u("Object "+k+" is NaN.",s);if(!isFinite(a))return u("Object "+k+" is of type "+b.type+" but is Infinite.",s);break;case"integer":I=e(a);break;case"float":I=f(a);break;case"string":if(I=g(a),!I)break;if(n(b.length)&&a.length!==b.length)return u("Object '"+k+"' of type "+b.type+" is of length "+a.length+" but was expected to be of length "+b.length+".",s);if(b.not_empty&&0===a.length)return u("Object '"+k+"' of type "+b.type+" is expected not to be empty.",s);break;case"stringnum":I=g(a)&&parseFloat(a)||d(a);break;case"array":if(I=o(a),!I)break;if(n(b.length)&&a.length!==b.length)return u("Object '"+k+"' of type "+b.type+" is of length "+a.length+" but was expected to be of length "+b.length+".",s);if(b.not_empty&&0===a.length)return u("Parameter '"+k+"' of type "+b.type+" is expected not to be empty.",s);if(a.length>0&&n(b.sub_type))if(b.full_check&&b.full_check){for(a.length>A&&v("Full check requested on the array "+k+" of length superior to "+A+". This can cause the checker to be slow.",s),J=0;J<a.length;J++)if(!x(a[J],b.sub_type,k+"["+J+"]",s,t,y))return!1}else if(!x(a[0],b.sub_type,k+"[0]",s,t,y))return!1;break;case"object":if(I=p(a),!I)break;if(n(b.structure))for(J=0;J<b.structure.length;J++)if(!x(a[b.structure[J].name],b.structure[J].type,k+"."+b.structure[J].name,s,t,y))return!1;if(b.not_empty&&q(a))return u("Parameter '"+k+"' of type "+b.type+" is expected not to be empty.",s);break;case"function":I=l(a);break;case"error":I=j(a);break;case"date":I=h(a);break;case"regexp":I=i(a);break;default:return u("Type "+b.type+" cannot be checked, or checker has not implemented this type.",s)}if(!I)return u("Parameter '"+k+"' is not of the type "+b.type+".",s);if(n(b.equal)&&0!==b.equal.length){var K=!1;for(J=0;J<b.equal.length;J++)r(a,b.equal[J])&&(K=!0);if(!K)return u("Parameter '"+k+"' of value "+a+" should be equal to one of these values: "+b.equal+".",s)}if(n(b.not_equal)&&0!==b.not_equal.length)for(J=0;J<b.not_equal.length;J++)if(r(a,b.not_equal[J]))return u("Parameter '"+k+"' is equal to "+b.not_equal[J]+", it should not.",s);return n(b.label)&&(t.labels[b.label]=a),n(b.cb)&&(I=b.cb(a,b,k,t.labels),!I)?n(b.cb_message)?u("Parameter '"+k+"' "+b.cb_message,s):u("Parameter '"+k+"' failed passing the callback.",s):(0===y&&t.recursion_depth>=C&&w("Object "+k+" has been successfuly checked with "+t.recursion_depth+" recursion depth.",s),!0)}function y(a,b){if(m(a)||!o(a))return u("Bad fist parameter argument_types. It's a mandatory argument, must be an array of types.",b);if(caller_arguments=Array.prototype.slice.call(arguments.callee.caller.arguments),caller_arguments_names=arguments.callee.caller.toString().replace(/((\/\/.*$)|(\/\*[\s\S]*?\*\/)|(\s))/gm,"").match(/^function\s*[^\(]*\(\s*([^\)]*)\)/m)[1].split(/,/),a.length>caller_arguments.length)return u("Function called with "+caller_arguments.length+" arguments, was expecting "+a.length+".",b);for(var c=0;c<a.length;c++)if(!x(caller_arguments[c],a[c],caller_arguments_names[c],b,{}))return!1;return!0}var z={},A=20,B=20,C=3;z.toType=a,z.isBoolean=c,z.isNumber=d,z.isInteger=e,z.isFloat=f,z.isString=g,z.isDate=h,z.isRegExp=i,z.isError=j,z.isArguments=k,z.isFunction=l,z.isUndefined=m,z.isDefined=n,z.isArray=o,z.isObject=p,z.isObjectEmpty=q,z.isEqual=r,z.object_check=x,z.arguments_check=y,"undefined"!=typeof module&&module.exports&&(module.exports=z),this.sanityjs=z}();