Difference between revisions of "Core"
From CCIL
Line 40: | Line 40: | ||
==== Utils ==== | ==== Utils ==== | ||
− | ===== isFunction ===== | + | ===== General ===== |
+ | ====== isFunction ====== | ||
<pre> | <pre> | ||
Line 50: | Line 51: | ||
</pre> | </pre> | ||
− | ===== isDefined ===== | + | ====== isDefined ====== |
<pre> | <pre> | ||
Line 58: | Line 59: | ||
</pre> | </pre> | ||
− | ===== isUndefined ===== | + | ====== isUndefined ====== |
<pre> | <pre> | ||
Line 66: | Line 67: | ||
</pre> | </pre> | ||
− | ===== isNull ===== | + | ====== isNull ====== |
<pre> | <pre> | ||
Line 74: | Line 75: | ||
</pre> | </pre> | ||
− | ===== isVal ===== | + | ====== isVal ====== |
<pre> | <pre> | ||
CCIL.utils.isVal = function(val) { | CCIL.utils.isVal = function(val) { | ||
return CCIL.utils.isDefined(val) && (!CCIL.utils.isNull(val)); | return CCIL.utils.isDefined(val) && (!CCIL.utils.isNull(val)); | ||
+ | } | ||
+ | </pre> | ||
+ | |||
+ | |||
+ | ===== Color ===== | ||
+ | |||
+ | ====== randomColor ====== | ||
+ | |||
+ | <pre> | ||
+ | CCIL.utils.randomColor = function() { | ||
+ | var letters = '0123456789ABCDEF'.split(''); | ||
+ | var color = '#'; | ||
+ | for (var i = 0; i < 6; i++) { | ||
+ | color += letters[Math.floor(Math.random() * 16)]; | ||
+ | } | ||
+ | return color; | ||
+ | } | ||
+ | </pre> | ||
+ | |||
+ | ===== System ===== | ||
+ | |||
+ | ====== parentFolder ======= | ||
+ | |||
+ | <pre> | ||
+ | CCIL.utils.parentFolder = function(targetFile) { | ||
+ | var index = targetFile.lastIndexOf('/'); | ||
+ | if (index > 0) { | ||
+ | var parentFolder = targetFile.substring(0, index); // , | ||
+ | // targetFile.length | ||
+ | return parentFolder; | ||
+ | } else { | ||
+ | return ''; | ||
+ | } | ||
+ | } | ||
+ | </pre> | ||
+ | |||
+ | ====== getParam ====== | ||
+ | |||
+ | <pre> | ||
+ | CCIL.utils.getParam = function(val) { | ||
+ | var result = null, tmp = []; | ||
+ | location.search | ||
+ | // .replace ( "?", "" ) | ||
+ | // this is better, there might be a question mark inside | ||
+ | .substr(1).split("&").forEach(function(item) { | ||
+ | tmp = item.split("="); | ||
+ | if (tmp[0] === val) | ||
+ | result = decodeURIComponent(tmp[1]); | ||
+ | }); | ||
+ | return result; | ||
+ | } | ||
+ | </pre> | ||
+ | |||
+ | ===== JSON ===== | ||
+ | |||
+ | ====== toJSON ====== | ||
+ | |||
+ | <pre> | ||
+ | CCIL.utils.toJSON = function(val) { | ||
+ | // convert to JSON string | ||
+ | var json = JSON.stringify(val); | ||
+ | |||
+ | // patch | ||
+ | if (!(typeof json == 'undefined')) { | ||
+ | json = json.split('&').join('__AMP__'); | ||
+ | json = json.split('%').join('__PERCENT__'); | ||
+ | } else { | ||
+ | json = 'null'; | ||
+ | } | ||
+ | |||
+ | return json; | ||
+ | } | ||
+ | </pre> | ||
+ | |||
+ | ====== fromJSON ====== | ||
+ | |||
+ | <pre> | ||
+ | CCIL.utils.fromJSON = function(val) { | ||
+ | val = val.split('__AMP__').join('&'); | ||
+ | val = val.split('__PERCENT__').join('%'); | ||
+ | |||
+ | return JSON.parse(val); | ||
+ | } | ||
+ | </pre> | ||
+ | |||
+ | ===== General ===== | ||
+ | |||
+ | ====== humanFileSize ====== | ||
+ | |||
+ | <pre> | ||
+ | CCIL.utils.humanFileSize = function(bytes) { | ||
+ | return CCIL.utils.humanFileSize(bytes, false); | ||
+ | } | ||
+ | |||
+ | CCIL.utils.humanFileSize = function(bytes, si) { | ||
+ | var thresh = si ? 1000 : 1024; | ||
+ | if (Math.abs(bytes) < thresh) { | ||
+ | return bytes + ' B'; | ||
+ | } | ||
+ | var units = si ? [ 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB' ] : [ | ||
+ | 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB' ]; | ||
+ | var u = -1; | ||
+ | do { | ||
+ | bytes /= thresh; | ||
+ | ++u; | ||
+ | } while (Math.abs(bytes) >= thresh && u < units.length - 1); | ||
+ | |||
+ | return bytes.toFixed(1) + ' ' + units[u]; | ||
+ | } | ||
+ | </pre> | ||
+ | |||
+ | ===== Shurtcuts ===== | ||
+ | |||
+ | ====== shortcutChars ====== | ||
+ | |||
+ | <pre> | ||
+ | CCIL.utils.shortcutChars = '1234567890qwertyuiop[]asdfghjkl;zxcvbnm'; | ||
+ | </pre> | ||
+ | |||
+ | ====== shortuctIndex ====== | ||
+ | |||
+ | <pre> | ||
+ | CCIL.utils.shortuctIndex = function(_char) { | ||
+ | var res = String.fromCharCode(_char); | ||
+ | var index = CCIL.utils.shortcutChars.indexOf(res); | ||
+ | |||
+ | if (index == -1) { | ||
+ | CCIL.log('ERROR: unknown charecter (' + _char + ').'); | ||
+ | |||
+ | return null; | ||
+ | } | ||
+ | |||
+ | return index; | ||
+ | } | ||
+ | </pre> | ||
+ | |||
+ | ====== shortcutSymbol ======= | ||
+ | |||
+ | <pre> | ||
+ | CCIL.utils.shortcutSymbol = function(index) { | ||
+ | if (index >= CCIL.utils.shortcutChars.length) { | ||
+ | CCIL.log('ERROR: index is greater than the vailable charecters (' | ||
+ | + index + ').'); | ||
+ | return null; | ||
+ | } | ||
+ | |||
+ | return CCIL.utils.shortcutChars.charAt(index); | ||
} | } | ||
</pre> | </pre> |
Revision as of 08:40, 15 April 2017
Contents
Include
Maven
API
String Prototypes
contains
String.prototype.contains = function(str, ignoreCase) { return (ignoreCase ? this.toUpperCase() : this) .indexOf(ignoreCase ? str.toUpperCase() : str) >= 0; };
endsWith
String.prototype.endsWith = function(suffix) { return this.indexOf(suffix, this.length - suffix.length) !== -1; };
replaceAll
String.prototype.replaceAll = function(search, replacement) { var target = this; return target.replace(new RegExp(search, 'g'), replacement); };
Utils
General
isFunction
CCIL.utils.isFunction = function(functionToCheck) { var getType = {}; return functionToCheck && getType.toString.call(functionToCheck) === '[object Function]'; }
isDefined
CCIL.utils.isDefined = function(val) { return !CCIL.utils.isUndefined(val); }
isUndefined
CCIL.utils.isUndefined = function(val) { return (typeof val === 'undefined'); }
isNull
CCIL.utils.isNull = function(val) { return url === null; }
isVal
CCIL.utils.isVal = function(val) { return CCIL.utils.isDefined(val) && (!CCIL.utils.isNull(val)); }
Color
randomColor
CCIL.utils.randomColor = function() { var letters = '0123456789ABCDEF'.split(''); var color = '#'; for (var i = 0; i < 6; i++) { color += letters[Math.floor(Math.random() * 16)]; } return color; }
System
parentFolder =
CCIL.utils.parentFolder = function(targetFile) { var index = targetFile.lastIndexOf('/'); if (index > 0) { var parentFolder = targetFile.substring(0, index); // , // targetFile.length return parentFolder; } else { return ''; } }
getParam
CCIL.utils.getParam = function(val) { var result = null, tmp = []; location.search // .replace ( "?", "" ) // this is better, there might be a question mark inside .substr(1).split("&").forEach(function(item) { tmp = item.split("="); if (tmp[0] === val) result = decodeURIComponent(tmp[1]); }); return result; }
JSON
toJSON
CCIL.utils.toJSON = function(val) { // convert to JSON string var json = JSON.stringify(val); // patch if (!(typeof json == 'undefined')) { json = json.split('&').join('__AMP__'); json = json.split('%').join('__PERCENT__'); } else { json = 'null'; } return json; }
fromJSON
CCIL.utils.fromJSON = function(val) { val = val.split('__AMP__').join('&'); val = val.split('__PERCENT__').join('%'); return JSON.parse(val); }
General
humanFileSize
CCIL.utils.humanFileSize = function(bytes) { return CCIL.utils.humanFileSize(bytes, false); } CCIL.utils.humanFileSize = function(bytes, si) { var thresh = si ? 1000 : 1024; if (Math.abs(bytes) < thresh) { return bytes + ' B'; } var units = si ? [ 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB' ] : [ 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB' ]; var u = -1; do { bytes /= thresh; ++u; } while (Math.abs(bytes) >= thresh && u < units.length - 1); return bytes.toFixed(1) + ' ' + units[u]; }
Shurtcuts
shortcutChars
CCIL.utils.shortcutChars = '1234567890qwertyuiop[]asdfghjkl;zxcvbnm';
shortuctIndex
CCIL.utils.shortuctIndex = function(_char) { var res = String.fromCharCode(_char); var index = CCIL.utils.shortcutChars.indexOf(res); if (index == -1) { CCIL.log('ERROR: unknown charecter (' + _char + ').'); return null; } return index; }
shortcutSymbol =
CCIL.utils.shortcutSymbol = function(index) { if (index >= CCIL.utils.shortcutChars.length) { CCIL.log('ERROR: index is greater than the vailable charecters (' + index + ').'); return null; } return CCIL.utils.shortcutChars.charAt(index); }