Skip to content

Instantly share code, notes, and snippets.

@w3core
Last active October 17, 2016 10:40
Show Gist options
  • Save w3core/2ae339fb4cdcd2b64003 to your computer and use it in GitHub Desktop.
Save w3core/2ae339fb4cdcd2b64003 to your computer and use it in GitHub Desktop.
Tools
(new function(that){
var TRUE = !0, FALSE = !1, NULL = null, UNDEFINED = undefined, NAN = NaN, INFINITY = Infinity, PINFINITY = +Infinity, NINFINITY = -Infinity;
var _OBJECT_ = that._OBJECT_ = "object",
_STRING_ = that._STRING_ = "string",
_NUMBER_ = that._NUMBER_ = "number",
_BOOLEAN_ = that._BOOLEAN_ = "boolean",
_FUNCTION_ = that._FUNCTION_ = "function",
_UNDEFINED_ = that._UNDEFINED_ = "undefined",
_EMAIL_ = that._EMAIL_ = "email",
_IP_ = that._IP_ = "ip",
_URL_ = that._URL_ = "url",
_NAN_ = that._NAN_ = "nan",
_NULL_ = that._NULL_ = "null",
_ENUM_ = that._ENUM_ = "enum",
_TRUE_ = that._TRUE_ = "true",
_FALSE_ = that._FALSE_ = "false",
_EMPTY_ = that._EMPTY_ = "empty"
;
that.trim = trim;
that.pregQuote = pregQuote;
that.toArray = toArray;
that.toNormal = toNormal;
that.toShortString = toShortString;
that.empty = empty;
that.is = is;
that.not = not;
that.each = each;
that.extend = extend;
that.EventBase = EventBase;
that.getObjectPropertyByPath = getObjectPropertyByPath;
that.assignStringValues = assignStringValues;
function trim (s) {
return typeof "".trim == _FUNCTION_ ? (s+"").trim() : (s+"").replace(/^\s+|\s+$/mg, "");
}
function toShortString(string, max) {
var string = trim (
(string+"")
.replace(/(<(script|style)[^>]*>.*?<\/(script|style)[^>]*>)/ig, " ")
.replace(/(<([^>]+)>)/g," ")
.replace(/\s+/g, " ")
),
max = max*1 && max*1 != NAN ? max*1 : 80;
if (string.length > max) string = (trim(string.substr(0, max)) + "...").replace(/(\.\.\.)\.+$/g, "$1");
return string;
}
function pregQuote (s) {
return (s+"").replace(/([\\\.\+\*\?\[\^\]\$\(\)\{\}\=\!\<\>\|\:])/g, "\\$1");
}
function toArray (v) {
return is(_ENUM_, v) ? [].slice.call(v) : [];
}
function toNormal (v) {
if (!is(_STRING_, v)) return v;
try {return JSON.parse(v);}
catch(e) {
var o = {"true":TRUE, "false":FALSE, "null":NULL, "undefined":UNDEFINED, "NaN":NAN, "Infinity":INFINITY, "+Infinity":PINFINITY, "-Infinity":NINFINITY};
for(var i in o) if (i == v) return o[i];
if(/^[\-\.0-9e]+$/mg.test(v) && !is(_NAN_, v*1) ) return v * 1;
};
return v;
}
function empty (v) {
var _v = toNormal(v);
if (is(_ENUM_, _v)) return !_v.length;
if (is(_OBJECT_, _v)) {
var o = !0;
for (var i in _v) {o = !1; break;}
return o;
}
if (is(_STRING_, v)) return !trim(v).length;
return !_v;
}
function is (type, value) {
var WINDOW = typeof window == _OBJECT_ && window != NULL ? window : NULL,
type = trim(type).toLowerCase().replace(/\!\s+/g, "!"),
few = type.split(/\s+/g);
if (few.length > 1) {
for (var i=0; i<few.length; i++) if (!is(few[i], value)) return !1;
return !0;
}
if (type[0] == "!") return !is(type.substr(1), value);
if (type == _NULL_) return value === NULL;
if (type == _OBJECT_) return typeof value == _OBJECT_ && value != NULL;
if (type == _ENUM_) return (is(_OBJECT_, value) && is(_NUMBER_, value.length) && (!WINDOW || value != WINDOW));
if (type == _NAN_) return isNaN(value);
if (type == _EMPTY_) return empty(value);
if (type == _IP_) return is(_STRING_, value) && /^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]$/g.test(value);
if (type == _URL_) return is(_STRING_, value) && /^(https?:|ftp:)?\/{2}\w+/ig.test(value);
if (type == _EMAIL_) return is(_STRING_, value) && /^[^@]+@[^@]+?[a-z0-9]$/ig.test(value);
return typeof value == type;
}
function not (type, value) { return !is(type, value); }
function each (obj, fn) {
if (!is(_FUNCTION_, fn)) return;
if (is(_ENUM_, obj)) {
for (var i=0; i<obj.length; i++) {
if (fn(obj[i], i, obj) === FALSE) break;
}
}
else if (is(_OBJECT_, obj)) {
for (var i in obj) {
if (fn(obj[i], i, obj) === FALSE) break;
}
}
}
function extend (a, b) {
if (is(_OBJECT_, b) && !is(_OBJECT_, a)) {
a = (is(_ENUM_, b)) ? [] : {};
if (is(_ENUM_, b)) var b = toArray(b);
}
each(b, function (v, i){
try {
if(is(_OBJECT_, v)) a[i] = extend(a[i], v);
else a[i] = v;
}
catch(e) { a[i] = v; };
});
return a;
}
function getObjectPropertyByPath (path, object) {
if (is(_OBJECT_, object) && is(_STRING_, path) && /^[a-z0-9.-_$]+$/ig.test(path)) {
var path = '["' + path.replace(/\./g, '"]["') + '"]';
return (new Function('try{return this'+path+'}catch(e){return}')).call(object);
}
}
// {{vars.object.array.10.property.value | converters.toLowerCase | converters.object.myfn}}
function assignStringValues (string, vars, converters) {
if (is(_STRING_, string) && not(_EMPTY_, string) && vars && is(_OBJECT_, vars)) {
var string = string.replace(/(\{\{([^\}]+)\}\})/g, function($0, $1, $2){
var proc = $2.replace(/\s+/g, "").split("|");
var value = getObjectPropertyByPath(proc.shift(), vars);
for (var i=0; i<proc.length; i++) {
var fn = getObjectPropertyByPath(proc[i], converters);
if (is(_FUNCTION_, fn)) value = fn(value);
}
if (is(_FUNCTION_, value)) value = value();
if (is(_OBJECT_, value)) {
try {
value = JSON.stringify(value);
} catch(e){};
}
return "" + (is(_STRING_, value) || is(_NUMBER_, value) ? value : "");
});
}
return string;
}
function EventBase (that) {
var that = that || this,
id = (new Date).getTime() + "" + Math.random(),
node = document.createElement("i"),
recent = {},
_on = "on",
_off = "off";
that.on = on;
that.off = off;
that.emit = emit;
function on (event, handler) {
var event = unify(event);
if (is(_STRING_, event) && is(_FUNCTION_, handler)) {
var event = event.replace(/(([\w._\-~#&^]+)(:ready|:once)(:ready|:once)?)/ig, function ($0, $1, $e, $3, $4) {
var ready = /:ready/i.test($3 + $4),
once = /:once/i.test($3 + $4),
last = recent[$e],
done;
if (!once) listener(_on, $e, node, handler, FALSE);
if (ready && last) {
if (once) done = TRUE;
setTimeout(function(){handler(last)}, 0);
}
if (once && !done) {
var fn = function (e) {
listener(_off, $e, node, fn, FALSE);
handler(e);
};
listener(_on, $e, node, fn, FALSE);
}
return "";
});
listener(_on, cleanPseudo(event), node, handler, FALSE);
}
return that;
}
function off (event, handler) {
var event = unify(cleanPseudo(event));
if (!event) return that;
listener(_off, event, node, handler, FALSE);
return that;
}
function emit (event, data) {
var event = unify(cleanPseudo(event));
if (!event) return that;
recent[event] = dispatch(node, event, data);
return that;
}
function unify (event) {
if (event && is(_STRING_, event)) return event.toLowerCase();
}
function cleanPseudo (events) {
return is(_STRING_, events) ? events.replace(/:ready|:once/ig, '') : events;
}
function eventHandlerStatus (handler, event, status) {
if (!handler[id]) handler[id] = {};
if (is(_BOOLEAN_, status)) handler[id][event] = status;
return !!handler[id][event];
}
function listener (type, event, node, fn, sign) {
if (not(_STRING_, type) || not(_STRING_, event) || !event.length || !node || not(_FUNCTION_, fn)) return;
if (is(_ENUM_, node) && !node.nodeName) {
for (var i=0; i<node.length; i++) listener(type, event, node[i], fn, sign);
return;
}
var method = (type == _on) ? "addEventListener" : "removeEventListener";
var add = !!(type == _on);
var e = event.replace(/^\s+|\s+$/img, '').split(/[ ,]+/);
if (e.length == 1) {
if (e[0].length) {
var status = eventHandlerStatus(fn, e[0]);
if ((add && !status) || !add) {
node[method](e[0], fn, !!sign);
eventHandlerStatus(fn, e[0], add);
}
}
return;
}
for(var i=0; i<e.length; i++) listener(type, e[i], node, fn, sign);
}
function dispatch (node, event, data) {
if (document.createEventObject) {
var e = document.createEventObject();
e.data = data;
node.fireEvent("on"+event, e);
return e;
}
else {
var e = document.createEvent("HTMLEvents");
e.data = data;
e.stopPropagation = e.stopImmediatePropagation;
e.initEvent(event, TRUE, TRUE);
node.dispatchEvent(e);
return e;
}
}
}
if (typeof String.prototype.trim != _FUNCTION_) {
String.prototype.trim = function () {return trim(this);}
}
String.prototype.pregQoute = function () {return pregQoute(this);};
if (!Object.prototype.extends) {
Object.defineProperties(Object.prototype, {
"extends": {value:function (value) {return extend(value, this);}},
"toArray": {value:function () {return toArray(this);}},
});
}
}(window));
!new function(c){function l(a){return typeof"".trim==q?(a+"").trim():(a+"").replace(/^\s+|\s+$/mg,"")}function t(a){return d(r,a)?[].slice.call(a):[]}function x(a){if(!d(e,a))return a;try{return JSON.parse(a)}catch(c){var b={"true":!0,"false":!1,"null":null,undefined:void 0,NaN:NaN,Infinity:Infinity,"+Infinity":Infinity,"-Infinity":-Infinity},h;for(h in b)if(h==a)return b[h];if(/^[\-\.0-9e]+$/mg.test(a)&&!d(y,1*a))return 1*a}return a}function z(a){var b=x(a);if(d(r,b))return!b.length;if(d(f,b)){a=!0;for(var h in b){a=!1;break}return a}return d(e,a)?!l(a).length:!b}function d(a,b){var h=typeof window==f&&null!=window?window:null;a=l(a).toLowerCase().replace(/\!\s+/g,"!");var c=a.split(/\s+/g);if(1<c.length){for(h=0;h<c.length;h++)if(!d(c[h],b))return!1;return!0}return"!"==a[0]?!d(a.substr(1),b):a==D?null===b:a==f?typeof b==f&&null!=b:a==r?d(f,b)&&d(A,b.length)&&(!h||b!=h):a==y?isNaN(b):a==B?z(b):a==E?d(e,b)&&/^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]$/g.test(b):a==F?d(e,b)&&/^(https?:|ftp:)?\/{2}\w+/ig.test(b):a==G?d(e,b)&&/^[^@]+@[^@]+?[a-z0-9]$/ig.test(b):typeof b==a}function C(a,b){if(d(q,b))if(d(r,a))for(var c=0;c<a.length&&!1!==b(a[c],c,a);c++);else if(d(f,a))for(c in a)if(!1===b(a[c],c,a))break}function u(a,b){d(f,b)&&!d(f,a)&&(a=d(r,b)?[]:{},d(r,b)&&(b=t(b)));C(b,function(b,c){try{d(f,b)?a[c]=u(a[c],b):a[c]=b}catch(g){a[c]=b}});return a}function v(a,b){if(d(f,b)&&d(e,a)&&/^[a-z0-9.-_$]+$/ig.test(a))return a='["'+a.replace(/\./g,'"]["')+'"]',(new Function("try{return this"+a+"}catch(e){return}")).call(b)}var f=c._OBJECT_="object",e=c._STRING_="string",A=c._NUMBER_="number",H=c._BOOLEAN_="boolean",q=c._FUNCTION_="function";c._UNDEFINED_="undefined";var G=c._EMAIL_="email",E=c._IP_="ip",F=c._URL_="url",y=c._NAN_="nan",D=c._NULL_="null",r=c._ENUM_="enum";c._TRUE_="true";c._FALSE_="false";var B=c._EMPTY_="empty";c.trim=l;c.pregQuote=function(a){return(a+"").replace(/([\\\.\+\*\?\[\^\]\$\(\)\{\}\=\!\<\>\|\:])/g,"\\$1")};c.toArray=t;c.toNormal=x;c.toShortString=function(a,b){a=l((a+"").replace(/(<(script|style)[^>]*>.*?<\/(script|style)[^>]*>)/ig," ").replace(/(<([^>]+)>)/g," ").replace(/\s+/g," "));b=1*b?1*b:80;a.length>b&&(a=(l(a.substr(0,b))+"...").replace(/(\.\.\.)\.+$/g,"$1"));return a};c.empty=z;c.is=d;c.not=function(a,b){return!d(a,b)};c.each=C;c.extend=u;c.EventBase=function(a){function b(a){if(a&&d(e,a))return a.toLowerCase()}function c(a){return d(e,a)?a.replace(/:ready|:once/ig,""):a}function f(a,b,c){a[k]||(a[k]={});d(H,c)&&(a[k][b]=c);return!!a[k][b]}function g(a,b,c,h,w){if(d(e,a)&&d(e,b)&&b.length&&c&&d(q,h))if(d(r,c)&&!c.nodeName)for(var p=0;p<c.length;p++)g(a,b,c[p],h,w);else{var p="on"==a?"addEventListener":"removeEventListener",k="on"==a;b=b.replace(/^\s+|\s+$/img,"").split(/[ ,]+/);if(1==b.length)b[0].length&&(a=f(h,b[0]),k&&!a||!k)&&(c[p](b[0],h,!!w),f(h,b[0],k));else for(p=0;p<b.length;p++)g(a,b[p],c,h,w)}}a=a||this;var k=(new Date).getTime()+""+Math.random(),m=document.createElement("i"),l={};a.on=function(n,f){n=b(n);d(e,n)&&d(q,f)&&(n=n.replace(/(([\w._\-~#&^]+)(:ready|:once)(:ready|:once)?)/ig,function(a,b,c,d,h){a=/:ready/i.test(d+h);d=/:once/i.test(d+h);var n=l[c],k;d||g("on",c,m,f,!1);a&&n&&(d&&(k=!0),setTimeout(function(){f(n)},0));if(d&&!k){var e=function(a){g("off",c,m,e,!1);f(a)};g("on",c,m,e,!1)}return""}),g("on",c(n),m,f,!1));return a};a.off=function(d,f){d=b(c(d));if(!d)return a;g("off",d,m,f,!1);return a};a.emit=function(d,f){d=b(c(d));if(!d)return a;var g=d;var k=d;if(document.createEventObject){var e=document.createEventObject();e.data=f;m.fireEvent("on"+k,e)}else e=document.createEvent("HTMLEvents"),e.data=f,e.stopPropagation=e.stopImmediatePropagation,e.initEvent(k,!0,!0),m.dispatchEvent(e);l[g]=e;return a}};c.getObjectPropertyByPath=v;c.assignStringValues=function(a,b,c){d(e,a)&&!d(B,a)&&b&&d(f,b)&&(a=a.replace(/(\{\{([^\}]+)\}\})/g,function(a,g,k){a=k.replace(/\s+/g,"").split("|");g=v(a.shift(),b);for(k=0;k<a.length;k++){var m=v(a[k],c);d(q,m)&&(g=m(g))}d(q,g)&&(g=g());if(d(f,g))try{g=JSON.stringify(g)}catch(l){}return""+(d(e,g)||d(A,g)?g:"")}));return a};typeof String.prototype.trim!=q&&(String.prototype.trim=function(){return l(this)});String.prototype.pregQoute=function(){return pregQoute(this)};Object.prototype["extends"]||Object.defineProperties(Object.prototype,{"extends":{value:function(a){return u(a,this)}},toArray:{value:function(){return t(this)}}})}(window);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment