mirror of
https://bitbucket.org/bellord13/kit.git
synced 2026-11-13 19:06:11 +03:00
125 lines
3.3 KiB
JavaScript
125 lines
3.3 KiB
JavaScript
/** @module support */
|
|
__PluginManager__.register('support', function(module, exports, require, global){
|
|
|
|
var node = {
|
|
isRelate: function (node, container = document) {
|
|
return node === container || container.contains(node);
|
|
}
|
|
};
|
|
var str = {
|
|
ucFirst: function (str) {
|
|
if (!str) return str;
|
|
return str[0].toUpperCase() + str.slice(1);
|
|
}
|
|
};
|
|
var arr = {
|
|
isKey: function (key) {
|
|
var n = Number(key);
|
|
return isInteger(n) && (''+n === ''+key);
|
|
}
|
|
};
|
|
function uniqueId (length = 32, firstLetter = true) {
|
|
var idStr = '';
|
|
if(length < 1) return idStr;
|
|
if(firstLetter) {
|
|
length && (idStr = (Math.floor((Math.random() * 25)) + 10).toString(36));
|
|
if (length === 1) return idStr;
|
|
}
|
|
do {
|
|
idStr += (Math.floor((Math.random() * 35))).toString(36);
|
|
} while (idStr.length < length);
|
|
return (idStr);
|
|
}
|
|
function valueEx(value) {
|
|
var args = [];
|
|
if(arguments.length > 1)
|
|
for(var i = 1; arguments.length - 1 >= i;i++) {
|
|
args.push(arguments[i]);
|
|
}
|
|
if(typeof value === 'function') {
|
|
return value.apply(this, args);
|
|
}
|
|
return value;
|
|
}
|
|
function empty(val){
|
|
return arguments.length === 0 ||
|
|
val === undefined ||
|
|
val === null ||
|
|
Number.isNaN(val) === true ||
|
|
val === '' ||
|
|
val === 0 ||
|
|
val === false;
|
|
}
|
|
function typeOf(value) {
|
|
return typeof value;
|
|
}
|
|
function isObject(value) {
|
|
return typeOf(value) === 'object' ? value !== null : typeof value === 'function';
|
|
}
|
|
function isNumber(value) {
|
|
return !Number.isNaN(value) && isFinite(value) && typeof value === 'number';
|
|
}
|
|
function isInteger(value) {
|
|
return isNumber(value) && (value % 1) === 0;
|
|
}
|
|
function isFloat(value) {
|
|
return isNumber(value);
|
|
}
|
|
function isString(value) {
|
|
return typeof value === 'string';
|
|
}
|
|
function isNode(value) {
|
|
return value instanceof Node;
|
|
}
|
|
function isElement(value) {
|
|
return value instanceof Element;
|
|
}
|
|
|
|
/**
|
|
* @param selector
|
|
* @return Element
|
|
*/
|
|
function getNode(selector) {
|
|
if(isString(selector)) {
|
|
return document.querySelector(selector);
|
|
}
|
|
if(isNode(selector) || isElement(selector)) {
|
|
return selector;
|
|
}
|
|
return undefined;
|
|
}
|
|
var nameFromToStringRegex = /^function\s?([^\s(]*)/;
|
|
function className(object, defaultName) {
|
|
var result = "";
|
|
if (typeof object === 'function') {
|
|
result = object.name || object.toString().match(nameFromToStringRegex)[1];
|
|
} else if (typeof object.constructor === 'function') {
|
|
result = className(object.constructor, defaultName);
|
|
}
|
|
return result || defaultName;
|
|
}
|
|
|
|
|
|
/**
|
|
* @memberOf kit
|
|
*/
|
|
var support = {
|
|
node: node,
|
|
str: str,
|
|
arr: arr,
|
|
uniqueId,
|
|
valueEx,
|
|
empty,
|
|
isNumber,
|
|
typeOf,
|
|
isObject,
|
|
isInteger,
|
|
isFloat,
|
|
isString,
|
|
isNode,
|
|
getNode,
|
|
};
|
|
|
|
return module.exports = support;
|
|
});
|