chicago
Version:
A front-end JavaScript library for user-interface developers.
238 lines (205 loc) • 5.75 kB
JavaScript
// @name: Chicago.utils
// @description: An object containing utility functions
// @since: 1.0.0-beta
_c.utils = _c.$.extend(_c.utils, {
is : {
undefined : function( obj ) {
return obj === undefined || typeof obj === 'undefined';
},
null : function( obj ) {
return Object.prototype.toString.call( obj ) === '[object Null]';
},
boolean : function( obj ) {
return typeof obj === 'boolean';
},
string : function( obj ) {
return typeof obj === 'string' || Object.prototype.toString.call( obj ) === '[object String]';
},
numeric : function( obj ) {
return ! isNaN( parseFloat( obj ) ) && isFinite( obj );
},
integer : function( obj ) {
return obj && Number( obj ) === obj && obj % 1 === 0;
},
float : function( obj ) {
return obj && Number( obj ) === obj && obj % 1 !== 0;
},
date : function( obj ) {
var d = new Date(obj);
return d !== 'Invalid Date' && d.toString() !== 'Invalid Date' && !isNaN(d);
},
jQueryObject : function( obj ) {
return obj instanceof win.jQuery;
},
element : function( obj ) {
var ele = this.jQueryObject( obj ) ? obj[0] : obj;
return ( typeof HTMLElement === 'object' ? ele instanceof HTMLElement : ( ele && typeof ele === 'object' && ele.nodeType === 1 && typeof ele.nodeName === 'string' ) );
},
function : function( obj ) {
return typeof obj === 'function';
},
array : function( obj ) {
return Object.prototype.toString.call( obj ) === '[object Array]';
},
object : function( obj ) {
return Object.prototype.toString.call( obj ) === '[object Object]';
},
},
now : Date.now || function() {
return new Date().getTime();
},
inArray : function(array, item) {
return array.indexOf( item ) > -1;
},
debounce : function(fn, wait, immediate) {
var timeout;
return function() {
var context = this,
args = arguments,
later = function() {
timeout = null;
if( ! immediate ) {
fn.apply( context, args );
}
},
callNow = immediate && ! timeout;
win.clearTimeout( timeout );
timeout = win.setTimeout(later, wait);
if( callNow ) {
fn.apply(context, args);
}
};
},
toCamelCase : function( string ) {
if(string === null || string === undefined) {
return '';
}
string = String( string ).trim();
return string.replace(/(\-[a-z])/g, function( letter ) {
return letter.toUpperCase().replace('-','');
});
},
uid : function( prefix ) {
var d = _c.utils.now();
prefix = prefix || '';
return prefix + 'xxxxxxxxxxxx4xxxyxxxxxxxxxxxxxxx'.replace(/[xy]/g, function( c ) {
var r = ( d + Math.random() * 16 ) % 16 | 0;
d = Math.floor( d / 16 );
return ( c == 'x' ? r : ( r&0x3 | 0x8) ).toString( 16 );
});
},
watch : function( object, property, callback ) {
var watch = Object.prototype.watch || Object.defineProperty( Object.prototype, "watch", {
enumerable : false,
configurable : true,
writable : false,
value : function( prop, handler ) {
var oldval = this[prop],
newval = oldval,
getter = function() {
return this.__clones[prop];
},
setter = function( val ) {
if( newval !== val ) {
oldval = newval;
newval = this.__clones[prop] = val;
return handler.call( this, prop, oldval, val );
}
};
if( delete this[prop] ) {
Object.defineProperty(this, prop, {
get : getter,
set : setter,
enumerable : true,
configurable : true
});
}
}
});
if( ! object.hasOwnProperty( '__clones' ) ) {
object.__clones = {};
}
object.__clones[property] = object[property];
object.watch( property, callback );
},
unwatch : function( object, property ) {
var unwatch = Object.prototype.unwatch || Object.defineProperty( Object.prototype, "unwatch", {
enumerable : false,
configurable : true,
writable : false,
value : function( prop ) {
var val = this[prop];
delete this[prop];
this[prop] = val;
}
});
function applyUnwatch( prop ) {
object.unwatch( prop );
if( object.__clones && object.__clones[prop] ) {
delete object.__clones[prop];
}
if( ! Object.keys( object.__clones ).length ) {
delete object.__clones;
}
}
if( property === undefined ) {
for( var key in object ) {
applyUnwatch( key );
}
} else {
applyUnwatch( property );
}
},
getCSSValue : function( ele, property ) {
var value = ele.css( property ).replace( /px/gi, '' );
if( _c.utils.is.numeric( value ) ) {
value = parseFloat( value );
if( _c.utils.is.float( value ) ) {
value = parseFloat( value.toFixed( 2 ) );
}
}
return value;
},
stringToMilliseconds : function( string ) {
if( ! _c.utils.is.string( string ) ) {
string = String( string );
}
return parseInt( parseFloat( string.replace( 's', '' ) ) * 1000, 10 );
},
stringToSlug : function( string ) {
string = String( string ) || '';
return string
.replace( /[^a-zA-Z0-9]/g, ' ' )
.replace( /\s+/g, '-' )
.toLowerCase();
},
str2json : function( str, notevil ) {
try {
if(notevil) {
return JSON.parse(str.replace(/([\$\w]+)\s* :/g, function(_, $1) {
return '"' + $1 + '" :';
}).replace(/'([^']+)'/g, function(_, $1) {
return '"' + $1 + '"';
}));
} else {
var newFN = Function;
return new newFN('', 'var json = ' + str + '; return JSON.parse(JSON.stringify(json));')();
}
} catch(e) {
return false;
}
},
options : function( string ) {
if( _c.utils.is.object(string) ) {
return string;
}
var start = string ? string.indexOf('{') : -1;
var options = {};
if(start !== -1) {
try {
options = _c.utils.str2json(string.substr(start));
} catch(_error) {}
}
return options;
},
});