UNPKG

sk-js

Version:
270 lines (231 loc) 6.55 kB
import Proxy0 from './Proxy0'; /** * @param subId 'a' or 'b[2]' of 'a.b[2].c' * @returns {*} * @private */ function _skParseSubId(subId) { var bracketIdx = subId.indexOf('['); return bracketIdx > -1 ? { p: subId.substring(0, bracketIdx), i: subId.substring(bracketIdx + 1, subId.length - 1) } : { p: subId }; } if (!Array.prototype.skFilter) { Object.defineProperty(Array.prototype, 'skFilter', { writable: true, enumerable: false, configurable: true, value: function value(recursive, filterFunc) { var _this = this; var rtn = []; this.forEach(function (item, index) { if (Proxy0._.isFunction(filterFunc) && filterFunc(index, item, _this)) { rtn.push(recursive && (Proxy0._.isArray(item) || Proxy0._.isPlainObject(item)) ? item.skFilter(recursive, filterFunc) : item); } }); return rtn; } }); } /** * @example * [1,2,3].skRmv(2) -> [1,3] */ if (!Array.prototype.skRmv) { Object.defineProperty(Array.prototype, 'skRmv', { writable: true, enumerable: false, configurable: true, value: function value(item) { var idx = this.indexOf(item); if (idx > -1) { this.splice(idx, 1); } return this; } }); } /** * @example * [1,2,3].skToggle(2) -> [1,3] * [1,3].skToggle(2) -> [1,3,2] */ if (!Array.prototype.skToggle) { Object.defineProperty(Array.prototype, 'skToggle', { writable: true, enumerable: false, configurable: true, value: function value(item) { var idx = this.indexOf(item); if (idx > -1) { this.splice(idx, 1); } else { this.push(item); } return this; } }); } if (!Array.prototype.skTrans) { Object.defineProperty(Array.prototype, 'skTrans', { writable: true, enumerable: false, configurable: true, value: function value(recursive, transFunc) { var _this2 = this; var rtn = []; this.forEach(function (item, index) { if (Proxy0._.isFunction(transFunc)) { rtn.push(recursive && (Proxy0._.isArray(item) || Proxy0._.isPlainObject(item)) ? item.skTrans(recursive, transFunc) : transFunc(index, item, _this2)); } }); return rtn; } }); } /** * @example * (987654.321).skCurrencyFmt(2) -> 987,654.32 */ if (!Number.prototype.skCurrencyFmt) { Number.prototype.skCurrencyFmt = function (fraction) { return String(this).skCurrencyFmt(fraction); }; } if (!Object.prototype.skFilter) { Object.defineProperty(Object.prototype, 'skFilter', { writable: true, enumerable: false, configurable: true, value: function value(recursive, filterFunc) { var _this3 = this; var rtn = {}; Object.keys(this).forEach(function (key) { var tmpVal = _this3[key]; if (Proxy0._.isFunction(filterFunc) && filterFunc(key, tmpVal, _this3)) { rtn[key] = recursive && (Proxy0._.isArray(tmpVal) || Proxy0._.isPlainObject(tmpVal)) ? tmpVal.skFilter(recursive, filterFunc) : tmpVal; } }); return rtn; } }); } if (!Object.prototype.skTrans) { Object.defineProperty(Object.prototype, 'skTrans', { writable: true, enumerable: false, configurable: true, value: function value(recursive, transFunc) { var _this4 = this; var rtn = {}; Object.keys(this).forEach(function (key) { var tmpVal = _this4[key]; if (Proxy0._.isFunction(transFunc)) { rtn[key] = recursive && (Proxy0._.isArray(tmpVal) || Proxy0._.isPlainObject(tmpVal)) ? tmpVal.skTrans(recursive, transFunc) : transFunc(key, tmpVal, _this4); } }); return rtn; } }); } /** * @example * ({a:1}).skVal() -> undefined * ({a:1}).skVal('a') -> 1 * ({a:1}).skVal('a',2) -> ({a:2}) * ({a:1}).skVal('a',undefined) -> ({a:undefined}) * ({a:1}).skVal(undefined, 2) -> ({a:1}) */ if (!Object.prototype.skVal) { Object.defineProperty(Object.prototype, 'skVal', { writable: true, enumerable: false, configurable: true, value: function value(str, val) { var rtn = this; if (Proxy0._.isNil(str)) { if (arguments.length < 2) { rtn = str; } } else { var arr = str.split('.'); var idx = 0; if (arguments.length > 1) { for (; idx < arr.length - 1; idx += 1) { var pi = _skParseSubId(arr[idx]); if (rtn[pi.p] === undefined) { rtn[pi.p] = pi.i === undefined ? {} : []; } rtn = pi.i === undefined ? rtn[pi.p] : rtn[pi.p][pi.i]; } if (rtn) { var _pi = _skParseSubId(arr[idx]); if (_pi.i === undefined) { rtn[_pi.p] = val; } else { rtn[_pi.p][_pi.i] = val; } } } else { for (; idx < arr.length; idx += 1) { var _pi2 = _skParseSubId(arr[idx]); rtn = _pi2.i === undefined ? rtn[_pi2.p] : rtn[_pi2.p][_pi2.i]; if (rtn === undefined) { break; } } } } return rtn; } }); } if (!Object.prototype.skVals) { Object.defineProperty(Object.prototype, 'skVals', { writable: true, enumerable: false, configurable: true, value: function value() { var _this5 = this; return Object.keys(this).map(function (key) { return _this5[key]; }); } }); } 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 arr = "".concat(parseFloat(this.replace(/[^\d.-]/g, '')).toFixed(fraction)).split('.'); return arr[0].replace(/(\d)(?=(\d{3})+(?!\d))/g, '$1,') + (fraction === 0 ? '' : ".".concat(arr[1])); }; } if (!String.prototype.skEmpty) { String.prototype.skEmpty = function () { return this.length === 0; }; } if (!String.prototype.skFmt) { String.prototype.skFmt = function (o) { return this.replace(/(\$#\{\w+(\.\w+)*\})/g, function (replacement) { ///(\{\w+\.\})/g return o.skVal(replacement.replace('$#{', '').replace('}', '')); }); }; } if (!String.prototype.skFmtArr) { String.prototype.skFmtArr = function (array) { return this.replace(/\$#(\d+)/g, function (match, p1) { p1 -= 1; return array[p1]; }); }; }