sk-polyfill
Version:
ShaneKing for Polyfill
261 lines (258 loc) • 8.37 kB
JavaScript
;(function () {
'use strict';
if (!Boolean.isBoolean) {
Boolean.isBoolean = function (b) {
return b === true || b === false;
}
}
if (!Function.isFunction) {
Function.isFunction = function (f) {
return !!f && !f.nodeName && f.constructor != String && f.constructor != RegExp && f.constructor != Array && /function/i.test(f + '');
}
}
if (!Object.isObject) {
Object.isObject = function (o) {
return (typeof o === 'object') && o.constructor === Object;
}
}
// [2,{skIdx0:3,skIdx1:[4,{skIdx0:5,skIdx1:[]}]}] -> [2,[3,[4,[5,[]]]]]
if (!Array.prototype.skArr) {
Object.defineProperty(Array.prototype, 'skArr', {
writable: true,
enumerable: false,
configurable: true,
value: function (recursive, keyFunc) {
var rtn = [];
this.forEach(function (item) {
rtn.push((recursive && (Array.isArray(item) || Object.isObject(item))) ? item.skArr(recursive, keyFunc) : item);
});
return rtn;
}
});
}
if (!Array.prototype.skFilter) {
Object.defineProperty(Array.prototype, 'skFilter', {
writable: true,
enumerable: false,
configurable: true,
value: function (recursive, filterFunc) {
var rtn = [];
this.forEach(function (item, index) {
if (Function.isFunction(filterFunc) && filterFunc(index, item, this)) {
rtn.push((recursive && (Array.isArray(item) || Object.isObject(item))) ? item.skFilter(recursive, filterFunc) : item)
}
}.bind(this));
return rtn;
}
});
}
// [1,{a:2,b:[3,{c:4,d:[5,{}]}]}] -> {skIdx0:1,skIdx1:{a:2,b:{skIdx0:3,skIdx1:{c:4,d:{skIdx0:5,skIdx1:{}}}}}}
if (!Array.prototype.skObj) {
Object.defineProperty(Array.prototype, 'skObj', {
writable: true,
enumerable: false,
configurable: true,
value: function (recursive, keyFunc) {
var rtn = {};
this.forEach(function (item, index) {
rtn[Function.isFunction(keyFunc) ? keyFunc(index, item, this) : index] = (recursive && (Array.isArray(item) || Object.isObject(item))) ? item.skObj(recursive, keyFunc) : item;
}.bind(this));
return rtn;
}
});
}
if (!Array.prototype.skSomeIn) {
Object.defineProperty(Array.prototype, 'skSomeIn', {
writable: true,
enumerable: false,
configurable: true,
value: function (a, compareFunc) {
var rtn = [];
this.forEach(function (item) {
a.forEach(function (item2) {
if (Function.isFunction(compareFunc) ? compareFunc(item, item2) : item === item2) {
rtn.push(item);
}
});
});
return rtn;
}
});
}
if (!Array.prototype.skUnique) {
Object.defineProperty(Array.prototype, 'skUnique', {
writable: true,
enumerable: false,
configurable: true,
value: function () {
var rtn = [];
this.forEach(function (item) {
if (rtn.indexOf(item) === -1) {
rtn.push(item);
}
});
return rtn;
}
});
}
if (!Number.prototype.skCurrencyFmt) {
Number.prototype.skCurrencyFmt = function (fraction) {
return String(this).skCurrencyFmt(fraction);
};
}
if (!Number.prototype.skIn) {
Number.prototype.skIn = function (a) {
return a.indexOf(this) !== -1;
};
}
//{skIdx0:1,skIdx1:[2,{skIdx0:3,skIdx1:[4,{skIdx0:5,skIdx1:[]}]}]} -> [1,[2,[3,[4,[5,[]]]]]]
if (!Object.prototype.skArr) {
Object.defineProperty(Object.prototype, 'skArr', {
writable: true,
enumerable: false,
configurable: true,
value: function (recursive, keyFunc) {
var rtnArr = [];
var rtnObj = {};
Object.keys(this).forEach(function (key) {
var value = this[key];
var rtn = (recursive && (Object.isObject(value) || Array.isArray(value))) ? value.skArr(recursive, keyFunc) : value;
rtnObj[key] = rtn;
if (Function.isFunction(keyFunc) && keyFunc(key, value, this)) {
rtnArr.push(rtn);
}
}.bind(this));
return Object.keys(rtnObj).length === rtnArr.length ? rtnArr : rtnObj;
}
});
}
if (!Object.prototype.skFilter) {
Object.defineProperty(Object.prototype, 'skFilter', {
writable: true,
enumerable: false,
configurable: true,
value: function (recursive, filterFunc) {
var rtn = {};
Object.keys(this).forEach(function (key) {
var value = this[key];
if (Function.isFunction(filterFunc) && filterFunc(key, value, this)) {
rtn[key] = (recursive && (Array.isArray(value) || Object.isObject(value))) ? value.skFilter(recursive, filterFunc) : value;
}
}.bind(this));
return rtn;
}
});
}
//{a:2,b:[3,{c:4,d:[5,{}]}]} -> {a:2,b:{skIdx0:3,skIdx1:{c:4,d:{skIdx0:5,skIdx1:{}}}}}
if (!Object.prototype.skObj) {
Object.defineProperty(Object.prototype, 'skObj', {
writable: true,
enumerable: false,
configurable: true,
value: function (recursive, keyFunc) {
var rtn = {};
Object.keys(this).forEach(function (key) {
var value = this[key];
rtn[key] = (recursive && (Array.isArray(value) || Object.isObject(value))) ? value.skObj(recursive, keyFunc) : value;
}.bind(this));
return rtn;
}
});
}
if (!Object.prototype.skVal) {
Object.defineProperty(Object.prototype, 'skVal', {
writable: true,
enumerable: false,
configurable: true,
value: function (str, val) {
var rtn = this;
var array = str.split('.');
var idx = 0;
if (arguments.length > 1) {
for (; idx < array.length - 1; idx++) {
if (rtn[array[idx]] === undefined) {
rtn[array[idx]] = {};
}
rtn = rtn[array[idx]];
}
if (rtn) {
rtn[array[idx]] = val;
}
return this;
} else {
for (; idx < array.length; idx++) {
rtn = rtn[array[idx]];
if (rtn === undefined) {
break;
}
}
return rtn;
}
}
});
}
if (!Object.prototype.skVals) {
Object.defineProperty(Object.prototype, 'skVals', {
writable: true,
enumerable: false,
configurable: true,
value: function () {
return Object.keys(this).map(function (key) {
return this[key];
}.bind(this));
}
});
}
if (!String.prototype.skBlank) {
String.prototype.skBlank = function () {
return this.trim().length === 0;
};
}
if (!String.prototype.skCurrencyFmt) {
String.prototype.skCurrencyFmt = function (fraction) {
fraction = fraction > 0 && fraction <= 20 ? fraction : 2;
var array = (parseFloat(this.replace(/[^\d\.-]/g, '')).toFixed(fraction) + '').split('.');
return array[0].replace(/(\d)(?=(\d{3})+(?!\d))/g, '$1,') + '.' + array[1];
};
}
if (!String.prototype.skEmpty) {
String.prototype.skEmpty = function () {
return this.length === 0;
};
}
if (!String.prototype.skEndWith) {
String.prototype.skEndWith = function (searchString, position) {
var subjectString = this.toString();
if (position === undefined || position > subjectString.length) {
position = subjectString.length;
}
position -= searchString.length;
var lastIndex = subjectString.indexOf(searchString, position);
return lastIndex !== -1 && lastIndex === position;
};
}
if (!String.prototype.skFmt) {
String.prototype.skFmt = function (o) {
return this.replace(/(\$\{\w+(\.\w+)*\})/g, function (matched) {///(\{\w+\.\})/g
return o.skVal(matched.replace('${', '').replace('}', ''));
});
};
}
if (!String.prototype.skFmtArr) {
String.prototype.skFmtArr = function (a) {
return this.replace(/\$(\d+)/g, function (_, m) {
return a[--m];
});
};
}
if (!String.prototype.skIn) {
String.prototype.skIn = function (a) {
return a.indexOf(this) !== -1;
};
}
if (!String.prototype.skStartWith) {
String.prototype.skStartWith = function (prefix) {
return this.slice(0, prefix.length) === prefix;
};
}
})();