Comparing version 3.0.3 to 3.0.5
819
lib/ntils.js
@@ -1,2 +0,819 @@ | ||
!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define("ntils",[],e):"object"==typeof exports?exports.ntils=e():t.ntils=e()}(this,function(){return function(t){function e(r){if(n[r])return n[r].exports;var o=n[r]={i:r,l:!1,exports:{}};return t[r].call(o.exports,o,o.exports,e),o.l=!0,o.exports}var n={};return e.m=t,e.c=n,e.d=function(t,n,r){e.o(t,n)||Object.defineProperty(t,n,{configurable:!1,enumerable:!0,get:r})},e.n=function(t){var n=t&&t.__esModule?function(){return t.default}:function(){return t};return e.d(n,"a",n),n},e.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},e.p="",e(e.s=0)}([function(t,e,n){t.exports=n(1)},function(t,e,n){e.__esModule=!0;var r=n(2),o=function(t){if(t&&t.__esModule)return t;var e={};if(null!=t)for(var n in t)Object.prototype.hasOwnProperty.call(t,n)&&(e[n]=t[n]);return e.default=t,e}(r);e.default=o,t.exports=e.default},function(t,e){function n(){}function r(t){return Object.prototype.toString.call(t)}function o(t){return/^\[object (.+)\]$/i.exec(r(t))[1]}function i(t){var e=o(t);return"Undefined"===e||"Null"===e}function u(t){return i(t)?t:t.trim?t.trim():t.replace(/(^[\\s]*)|([\\s]*$)/g,"")}function c(t,e,n){return i(t)?t:t.replace(new RegExp(e,"g"),n)}function f(t,e){return!i(t)&&!i(e)&&0===t.indexOf(e)}function a(t,e){return!i(t)&&!i(e)&&t.indexOf(e)>-1}function s(t,e){return!i(t)&&!i(e)&&t.indexOf(e)===t.length-e.length}function p(t,e){return!i(t)&&!i(e)&&(e in t||t.hasOwnProperty(e))}function l(t){return!i(t)&&"Function"===o(t)}function g(t){return!i(t)&&"String"===o(t)}function y(t){return!i(t)&&"Number"===o(t)}function d(t){return!i(t)&&"Boolean"===o(t)}function h(t){return!i(t)&&(window.Element?t instanceof Element:t.tagName&&t.nodeType&&t.nodeName&&t.attributes&&t.ownerDocument)}function _(t){return!i(t)&&t instanceof Text}function b(t){if(i(t))return!1;var e=o(t);return"Object"===e||"Array"===e}function O(t){if(i(t))return!1;var e="Array"===o(t),n=t instanceof Array,r=!g(t)&&y(t.length)&&l(t.splice),u=!g(t)&&y(t.length)&&t[0];return e||n||r||u}function v(t){return!i(t)&&t instanceof Date}function m(t){return t instanceof RegExp}function x(t){return i(t)?[]:Array.prototype.slice.call(t)}function w(t){var e=this;return e.isNumber(t)?new Date(t):e.isString(t)?new Date(e.replace(e.replace(t,"-","/"),"T"," ")):e.isDate(t)?t:null}function j(t,e,n){if(!i(t)&&!i(e))if(O(t))for(var r=t.length,o=0;o<r;o++){var u=e.call(n||t[o],o,t[o]);if(!i(u))return u}else for(var c in t){var u=e.call(n||t[c],c,t[c]);if(!i(u))return u}}function P(t,e,n){if(i(e)||i(t))return t;t=w(t),n=n||{};var r={"M+":t.getMonth()+1,"d+":t.getDate(),"h+":t.getHours(),"m+":t.getMinutes(),"s+":t.getSeconds(),"w+":t.getDay(),"q+":Math.floor((t.getMonth()+3)/3),S:t.getMilliseconds()};/(y+)/.test(e)&&(e=e.replace(RegExp.$1,(t.getFullYear()+"").substr(4-RegExp.$1.length)));for(var o in r)if(new RegExp("("+o+")").test(e)){var u=r[o];u=n[u]||u,e=e.replace(RegExp.$1,1==RegExp.$1.length?u:("00"+u).substr((""+u).length))}return e}function E(t,e,n){return e=e||(O(t)?[]:{}),j(t,function(r){if(!(n&&n.indexOf(r)>-1))if(delete e[r],Object.getOwnPropertyDescriptor)try{Object.defineProperty(e,r,Object.getOwnPropertyDescriptor(t,r))}catch(n){e[r]=t[r]}else e[r]=t[r]}),e}function S(t,e){if(i(t)||g(t)||y(t)||d(t)||v(t))return t;var n=t;try{n=new t.constructor}catch(t){}return j(t,function(t,r){n[t]==r||a(e,t)||(b(r)?n[t]=S(r,e):n[t]=r)},this),["toString","valueOf"].forEach(function(r){a(e,r)||D(n,r,t[r])},this),n}function M(t,e,n,r,o){if(r)switch(r){case 1:return M(t.prototype,e.prototype,n,0);case 2:M(t.prototype,e.prototype,n,0);break;case 3:return M(t,e.prototype,n,0);case 4:return M(t.prototype,e,n,0)}return e=e||{},t=t||(O(e)?[]:{}),N(e).forEach(function(r){a(n,r)||o&&i(e[r])||(!b(e[r])||e[r].constructor!=Object&&e[r].constructor!=Array&&null!=e[r].constructor?t[r]=e[r]:t[r]=M(t[r],e[r],n,0,o))},this),t}function D(t,e,n){try{Object.defineProperty(t,e,{value:n,enumerable:!1,configurable:!0,writable:!1})}catch(r){t[e]=n}}function N(t){if(Object.keys)return Object.keys(t);var e=[];return j(t,function(t){e.push(t)}),e}function R(t,e){function n(){}if(Object.create)return Object.create(t,e);n.prototype=t;var r=new n;return e&&E(e,r),r}function A(t,e){if(Object.setPrototypeOf)return Object.setPrototypeOf(t,e||R(null));"__proto__"in Object||E(e,t),t.__proto__=e}function T(t){return t.__proto__?t.__proto__:Object.getPrototypeOf?Object.getPrototypeOf(t):t.constructor?t.constructor.prototype:void 0}function $(t,e){if(t===e)return!0;if(!b(t)||!b(e))return!1;var n=N(t),r=N(e);if(n.length!==r.length)return!1;var o=n.concat(r),i=R(null),u=!0;return j(o,function(n,r){i[r]||($(t[r],e[r])||(u=!1),i[r]=!0)},this),u}function L(t,e,n,r){if(r||(r=[n,n=r][0]),n=Math.abs(n||1),t<e)for(var o=t;o<=e;o+=n)r(o);else for(var o=t;o>=e;o-=n)r(o)}function F(){function t(){return(65536*(1+Math.random())|0).toString(16).substring(1)}return t()+t()+"-"+t()+"-"+t()+"-"+t()+"-"+t()+t()+t()}function I(t,e){var n=O(t)?[]:{};return j(t,function(t,r){n[t]=e(t,r)}),n}function k(t,e,n){i(t)||i(e)||""===e||(O(e)||(e=e.replace(/\[/,".").replace(/\]/,".").split(".")),j(e,function(r,o){i(o)||o.length<1||(r===e.length-1?t[o]=n:(t[o]=t[o]||{},t=t[o]))},this))}function B(t,e){return i(t)||i(e)||""===e?t:(O(e)||(e=e.replace(/\[/,".").replace(/\]/,".").split(".")),j(e,function(e,n){i(n)||n.length<1||i(t)||(t=t[n])},this),t)}function q(t){if(i(t))return t;var e=[];return j(t,function(t,n){e.indexOf(n)>-1||e.push(n)}),e}function H(t){if(!t)return[];var e=t.toString(),n=e.split(")")[0].split("=>")[0].split("(");return(n[1]||n[0]).split(",").map(function(t){return t.trim()}).filter(function(t){return"function"!=t})}function U(t,e){if(!t)return t;e=e||40;var n=t.length,r=e/2;return n>e?t.substr(0,r)+"..."+t.substr(n-r):t}function W(t){if(!i(t))return t.substring(0,1).toUpperCase()+t.substring(1)}function z(t){return t.replace(/[-\/\\^$*+?.()|[\]{}]/g,"\\$&")}function C(t){window._NTILS_PARSE_DOM_=window._NTILS_PARSE_DOM_||document.createElement("div"),window._NTILS_PARSE_DOM_.innerHTML=u(t);var e=window._NTILS_PARSE_DOM_.childNodes[0];return e&&(e=e.cloneNode(!0)),window._NTILS_PARSE_DOM_.innerHTML="",e}e.__esModule=!0,e.noop=n,e.toString=r,e.getType=o,e.isNull=i,e.trim=u,e.replace=c,e.startWith=f,e.contains=a,e.endWith=s,e.has=p,e.isFunction=l,e.isString=g,e.isNumber=y,e.isBoolean=d,e.isElement=h,e.isText=_,e.isObject=b,e.isArray=O,e.isDate=v,e.isRegexp=m,e.toArray=x,e.toDate=w,e.each=j,e.formatDate=P,e.copy=E,e.clone=S,e.mix=M,e.defineFreezeProp=D,e.keys=N,e.create=R,e.setPrototypeOf=A,e.getPrototypeOf=T,e.deepEqual=$,e.fromTo=L,e.newGuid=F,e.map=I,e.setByPath=k,e.getByPath=B,e.unique=q,e.getFunctionArgumentNames=H,e.short=U,e.firstUpper=W,e.escapeRegExp=z,e.parseDom=C;e.hasProperty=p}])}); | ||
(function webpackUniversalModuleDefinition(root, factory) { | ||
if(typeof exports === 'object' && typeof module === 'object') | ||
module.exports = factory(); | ||
else if(typeof define === 'function' && define.amd) | ||
define("ntils", [], factory); | ||
else if(typeof exports === 'object') | ||
exports["ntils"] = factory(); | ||
else | ||
root["ntils"] = factory(); | ||
})(this, function() { | ||
return /******/ (function(modules) { // webpackBootstrap | ||
/******/ // The module cache | ||
/******/ var installedModules = {}; | ||
/******/ | ||
/******/ // The require function | ||
/******/ function __webpack_require__(moduleId) { | ||
/******/ | ||
/******/ // Check if module is in cache | ||
/******/ if(installedModules[moduleId]) { | ||
/******/ return installedModules[moduleId].exports; | ||
/******/ } | ||
/******/ // Create a new module (and put it into the cache) | ||
/******/ var module = installedModules[moduleId] = { | ||
/******/ i: moduleId, | ||
/******/ l: false, | ||
/******/ exports: {} | ||
/******/ }; | ||
/******/ | ||
/******/ // Execute the module function | ||
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__); | ||
/******/ | ||
/******/ // Flag the module as loaded | ||
/******/ module.l = true; | ||
/******/ | ||
/******/ // Return the exports of the module | ||
/******/ return module.exports; | ||
/******/ } | ||
/******/ | ||
/******/ | ||
/******/ // expose the modules object (__webpack_modules__) | ||
/******/ __webpack_require__.m = modules; | ||
/******/ | ||
/******/ // expose the module cache | ||
/******/ __webpack_require__.c = installedModules; | ||
/******/ | ||
/******/ // define getter function for harmony exports | ||
/******/ __webpack_require__.d = function(exports, name, getter) { | ||
/******/ if(!__webpack_require__.o(exports, name)) { | ||
/******/ Object.defineProperty(exports, name, { | ||
/******/ configurable: false, | ||
/******/ enumerable: true, | ||
/******/ get: getter | ||
/******/ }); | ||
/******/ } | ||
/******/ }; | ||
/******/ | ||
/******/ // getDefaultExport function for compatibility with non-harmony modules | ||
/******/ __webpack_require__.n = function(module) { | ||
/******/ var getter = module && module.__esModule ? | ||
/******/ function getDefault() { return module['default']; } : | ||
/******/ function getModuleExports() { return module; }; | ||
/******/ __webpack_require__.d(getter, 'a', getter); | ||
/******/ return getter; | ||
/******/ }; | ||
/******/ | ||
/******/ // Object.prototype.hasOwnProperty.call | ||
/******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); }; | ||
/******/ | ||
/******/ // __webpack_public_path__ | ||
/******/ __webpack_require__.p = ""; | ||
/******/ | ||
/******/ // Load entry module and return exports | ||
/******/ return __webpack_require__(__webpack_require__.s = 0); | ||
/******/ }) | ||
/************************************************************************/ | ||
/******/ ([ | ||
/* 0 */ | ||
/***/ (function(module, exports, __webpack_require__) { | ||
module.exports = __webpack_require__(1); | ||
/***/ }), | ||
/* 1 */ | ||
/***/ (function(module, exports, __webpack_require__) { | ||
exports.__esModule = true; | ||
var _utils = __webpack_require__(2); | ||
Object.keys(_utils).forEach(function (key) { | ||
if (key === "default" || key === "__esModule") return; | ||
Object.defineProperty(exports, key, { | ||
enumerable: true, | ||
get: function get() { | ||
return _utils[key]; | ||
} | ||
}); | ||
}); | ||
var utils = _interopRequireWildcard(_utils); | ||
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } } | ||
exports.default = utils; | ||
module.exports = exports['default']; | ||
/***/ }), | ||
/* 2 */ | ||
/***/ (function(module, exports) { | ||
exports.__esModule = true; | ||
exports.noop = noop; | ||
exports.toString = toString; | ||
exports.getType = getType; | ||
exports.isNull = isNull; | ||
exports.trim = trim; | ||
exports.replace = replace; | ||
exports.startWith = startWith; | ||
exports.contains = contains; | ||
exports.endWith = endWith; | ||
exports.has = has; | ||
exports.isFunction = isFunction; | ||
exports.isString = isString; | ||
exports.isNumber = isNumber; | ||
exports.isBoolean = isBoolean; | ||
exports.isElement = isElement; | ||
exports.isText = isText; | ||
exports.isObject = isObject; | ||
exports.isArray = isArray; | ||
exports.isDate = isDate; | ||
exports.isRegexp = isRegexp; | ||
exports.toArray = toArray; | ||
exports.toDate = toDate; | ||
exports.each = each; | ||
exports.formatDate = formatDate; | ||
exports.copy = copy; | ||
exports.clone = clone; | ||
exports.mix = mix; | ||
exports.defineFreezeProp = defineFreezeProp; | ||
exports.keys = keys; | ||
exports.create = create; | ||
exports.setPrototypeOf = setPrototypeOf; | ||
exports.getPrototypeOf = getPrototypeOf; | ||
exports.deepEqual = deepEqual; | ||
exports.fromTo = fromTo; | ||
exports.newGuid = newGuid; | ||
exports.map = map; | ||
exports.setByPath = setByPath; | ||
exports.getByPath = getByPath; | ||
exports.unique = unique; | ||
exports.getFunctionArgumentNames = getFunctionArgumentNames; | ||
exports.short = short; | ||
exports.firstUpper = firstUpper; | ||
exports.escapeRegExp = escapeRegExp; | ||
exports.parseDom = parseDom; | ||
/** | ||
* 空函数 | ||
*/ | ||
function noop() {}; | ||
function toString(obj) { | ||
return Object.prototype.toString.call(obj); | ||
} | ||
function getType(obj) { | ||
var str = toString(obj); | ||
return (/^\[object (.+)\]$/i.exec(str)[1] | ||
); | ||
} | ||
/** | ||
* 验证一个对象是否为NULL | ||
* @method isNull | ||
* @param {Object} obj 要验证的对象 | ||
* @return {Boolean} 结果 | ||
* @static | ||
*/ | ||
function isNull(obj) { | ||
var type = getType(obj); | ||
return type === 'Undefined' || type === 'Null'; | ||
}; | ||
/** | ||
* 除去字符串两端的空格 | ||
* @method trim | ||
* @param {String} str 源字符串 | ||
* @return {String} 结果字符串 | ||
* @static | ||
*/ | ||
function trim(str) { | ||
if (isNull(str)) return str; | ||
if (str.trim) { | ||
return str.trim(); | ||
} else { | ||
return str.replace(/(^[\\s]*)|([\\s]*$)/g, ""); | ||
} | ||
}; | ||
/** | ||
* 替换所有 | ||
* @method replace | ||
* @param {String} str 源字符串 | ||
* @param {String} str1 要替换的字符串 | ||
* @param {String} str2 替换为的字符串 | ||
* @static | ||
*/ | ||
function replace(str, str1, str2) { | ||
if (isNull(str)) return str; | ||
return str.replace(new RegExp(str1, 'g'), str2); | ||
}; | ||
/** | ||
* 从字符串开头匹配 | ||
* @method startWith | ||
* @param {String} str1 源字符串 | ||
* @param {String} str2 要匹配的字符串 | ||
* @return {Boolean} 匹配结果 | ||
* @static | ||
*/ | ||
function startWith(str1, str2) { | ||
if (isNull(str1) || isNull(str2)) return false; | ||
return str1.indexOf(str2) === 0; | ||
}; | ||
/** | ||
* 是否包含 | ||
* @method contains | ||
* @param {String} str1 源字符串 | ||
* @param {String} str2 检查包括字符串 | ||
* @return {Boolean} 结果 | ||
* @static | ||
*/ | ||
function contains(str1, str2) { | ||
var self = this; | ||
if (isNull(str1) || isNull(str2)) return false; | ||
return str1.indexOf(str2) > -1; | ||
}; | ||
/** | ||
* 从字符串结束匹配 | ||
* @method endWidth | ||
* @param {String} str1 源字符串 | ||
* @param {String} str2 匹配字符串 | ||
* @return {Boolean} 匹配结果 | ||
* @static | ||
*/ | ||
function endWith(str1, str2) { | ||
if (isNull(str1) || isNull(str2)) return false; | ||
return str1.indexOf(str2) === str1.length - str2.length; | ||
}; | ||
/** | ||
* 是否包含属性 | ||
* @method hasProperty | ||
* @param {Object} obj 对象 | ||
* @param {String} name 属性名 | ||
* @return {Boolean} 结果 | ||
* @static | ||
*/ | ||
function has(obj, name) { | ||
if (isNull(obj) || isNull(name)) return false; | ||
return name in obj || obj.hasOwnProperty(name); | ||
}; | ||
var hasProperty = exports.hasProperty = has; | ||
/** | ||
* 验证一个对象是否为Function | ||
* @method isFunction | ||
* @param {Object} obj 要验证的对象 | ||
* @return {Boolean} 结果 | ||
* @static | ||
*/ | ||
function isFunction(obj) { | ||
if (isNull(obj)) return false; | ||
return getType(obj) === "Function"; | ||
}; | ||
/** | ||
* 验证一个对象是否为String | ||
* @method isString | ||
* @param {Object} obj 要验证的对象 | ||
* @return {Boolean} 结果 | ||
* @static | ||
*/ | ||
function isString(obj) { | ||
if (isNull(obj)) return false; | ||
return getType(obj) === 'String'; | ||
}; | ||
/** | ||
* 验证一个对象是否为Number | ||
* @method isNumber | ||
* @param {Object} obj 要验证的对象 | ||
* @return {Boolean} 结果 | ||
* @static | ||
*/ | ||
function isNumber(obj) { | ||
if (isNull(obj)) return false; | ||
return getType(obj) === 'Number'; | ||
}; | ||
/** | ||
* 验证一个对象是否为Boolean | ||
* @method isBoolean | ||
* @param {Object} obj 要验证的对象 | ||
* @return {Boolean} 结果 | ||
* @static | ||
*/ | ||
function isBoolean(obj) { | ||
if (isNull(obj)) return false; | ||
return getType(obj) === 'Boolean'; | ||
}; | ||
/** | ||
* 验证一个对象是否为HTML Element | ||
* @method isElement | ||
* @param {Object} obj 要验证的对象 | ||
* @return {Boolean} 结果 | ||
* @static | ||
*/ | ||
function isElement(obj) { | ||
if (isNull(obj)) return false; | ||
if (window.Element) { | ||
return obj instanceof Element; | ||
} else { | ||
return obj.tagName && obj.nodeType && obj.nodeName && obj.attributes && obj.ownerDocument; | ||
} | ||
}; | ||
/** | ||
* 验证一个对象是否为HTML Text Element | ||
* @method isText | ||
* @param {Object} obj 要验证的对象 | ||
* @return {Boolean} 结果 | ||
* @static | ||
*/ | ||
function isText(obj) { | ||
if (isNull(obj)) return false; | ||
return obj instanceof Text; | ||
}; | ||
/** | ||
* 验证一个对象是否为Object | ||
* @method isObject | ||
* @param {Object} obj 要验证的对象 | ||
* @return {Boolean} 结果 | ||
* @static | ||
*/ | ||
function isObject(obj) { | ||
if (isNull(obj)) return false; | ||
var type = getType(obj); | ||
return type === 'Object' || type === 'Array'; | ||
}; | ||
/** | ||
* 验证一个对象是否为Array或伪Array | ||
* @method isArray | ||
* @param {Object} obj 要验证的对象 | ||
* @return {Boolean} 结果 | ||
* @static | ||
*/ | ||
function isArray(obj) { | ||
if (isNull(obj)) return false; | ||
var v1 = getType(obj) === 'Array'; | ||
var v2 = obj instanceof Array; | ||
var v3 = !isString(obj) && isNumber(obj.length) && isFunction(obj.splice); | ||
var v4 = !isString(obj) && isNumber(obj.length) && obj[0]; | ||
return v1 || v2 || v3 || v4; | ||
}; | ||
/** | ||
* 验证是不是一个日期对象 | ||
* @method isDate | ||
* @param {Object} val 要检查的对象 | ||
* @return {Boolean} 结果 | ||
* @static | ||
*/ | ||
function isDate(val) { | ||
if (isNull(val)) return false; | ||
return val instanceof Date; | ||
}; | ||
/** | ||
* 验证是不是一个正则对象 | ||
* @method isDate | ||
* @param {Object} val 要检查的对象 | ||
* @return {Boolean} 结果 | ||
* @static | ||
*/ | ||
function isRegexp(val) { | ||
return val instanceof RegExp; | ||
}; | ||
/** | ||
* 转换为数组 | ||
* @method toArray | ||
* @param {Array|Object} array 伪数组 | ||
* @return {Array} 转换结果数组 | ||
* @static | ||
*/ | ||
function toArray(array) { | ||
if (isNull(array)) return []; | ||
return Array.prototype.slice.call(array); | ||
}; | ||
/** | ||
* 转为日期格式 | ||
* @method toDate | ||
* @param {Number|String} val 日期字符串或整型数值 | ||
* @return {Date} 日期对象 | ||
* @static | ||
*/ | ||
function toDate(val) { | ||
var self = this; | ||
if (self.isNumber(val)) return new Date(val);else if (self.isString(val)) return new Date(self.replace(self.replace(val, '-', '/'), 'T', ' '));else if (self.isDate(val)) return val;else return null; | ||
}; | ||
/** | ||
* 遍历一个对像或数组 | ||
* @method each | ||
* @param {Object or Array} obj 要遍历的数组或对象 | ||
* @param {Function} fn 处理函数 | ||
* @return {void} 无返回值 | ||
* @static | ||
*/ | ||
function each(list, handler, scope) { | ||
if (isNull(list) || isNull(handler)) return; | ||
if (isArray(list)) { | ||
var listLength = list.length; | ||
for (var i = 0; i < listLength; i++) { | ||
var rs = handler.call(scope || list[i], i, list[i]); | ||
if (!isNull(rs)) return rs; | ||
} | ||
} else { | ||
for (var key in list) { | ||
var rs = handler.call(scope || list[key], key, list[key]); | ||
if (!isNull(rs)) return rs; | ||
} | ||
} | ||
}; | ||
/** | ||
* 格式化日期 | ||
* @method formatDate | ||
* @param {Date|String|Number} date 日期 | ||
* @param {String} format 格式化字符串 | ||
* @param {object} dict 反译字典 | ||
* @return {String} 格式化结果 | ||
* @static | ||
*/ | ||
function formatDate(date, format, dict) { | ||
if (isNull(format) || isNull(date)) return date; | ||
date = toDate(date); | ||
dict = dict || {}; | ||
var placeholder = { | ||
"M+": date.getMonth() + 1, //month | ||
"d+": date.getDate(), //day | ||
"h+": date.getHours(), //hour | ||
"m+": date.getMinutes(), //minute | ||
"s+": date.getSeconds(), //second | ||
"w+": date.getDay(), //week | ||
"q+": Math.floor((date.getMonth() + 3) / 3), //quarter | ||
"S": date.getMilliseconds() //millisecond | ||
}; | ||
if (/(y+)/.test(format)) { | ||
format = format.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length)); | ||
} | ||
for (var key in placeholder) { | ||
if (new RegExp("(" + key + ")").test(format)) { | ||
var value = placeholder[key]; | ||
value = dict[value] || value; | ||
format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? value : ("00" + value).substr(("" + value).length)); | ||
} | ||
} | ||
return format; | ||
}; | ||
/** | ||
* 拷贝对象 | ||
* @method copy | ||
* @param {Object} src 源对象 | ||
* @param {Object} dst 目标对象 | ||
* @static | ||
*/ | ||
function copy(src, dst, igonres) { | ||
dst = dst || (isArray(src) ? [] : {}); | ||
each(src, function (key) { | ||
if (igonres && igonres.indexOf(key) > -1) return; | ||
delete dst[key]; | ||
if (Object.getOwnPropertyDescriptor) { | ||
try { | ||
Object.defineProperty(dst, key, Object.getOwnPropertyDescriptor(src, key)); | ||
} catch (ex) { | ||
dst[key] = src[key]; | ||
} | ||
} else { | ||
dst[key] = src[key]; | ||
} | ||
}); | ||
return dst; | ||
}; | ||
/** | ||
* 深度克隆对象 | ||
* @method clone | ||
* @param {Object} src 源对象 | ||
* @return {Object} 新对象 | ||
* @static | ||
*/ | ||
function clone(src, igonres) { | ||
if (isNull(src) || isString(src) || isNumber(src) || isBoolean(src) || isDate(src)) { | ||
return src; | ||
} | ||
var objClone = src; | ||
try { | ||
objClone = new src.constructor(); | ||
} catch (ex) {} | ||
each(src, function (key, value) { | ||
if (objClone[key] != value && !contains(igonres, key)) { | ||
if (isObject(value)) { | ||
objClone[key] = clone(value, igonres); | ||
} else { | ||
objClone[key] = value; | ||
} | ||
} | ||
}, this); | ||
['toString', 'valueOf'].forEach(function (key) { | ||
if (contains(igonres, key)) return; | ||
defineFreezeProp(objClone, key, src[key]); | ||
}, this); | ||
return objClone; | ||
}; | ||
/** | ||
* 合并对象 | ||
* @method mix | ||
* @return 合并后的对象 | ||
* @param {Object} dst 目标对象 | ||
* @param {Object} src 源对象 | ||
* @param {Array} igonres 忽略的属性名, | ||
* @param {Number} mode 模式 | ||
*/ | ||
function mix(dst, src, igonres, mode, igonreNull) { | ||
//根据模式来判断,默认是Obj to Obj的 | ||
if (mode) { | ||
switch (mode) { | ||
case 1: | ||
// proto to proto | ||
return mix(dst.prototype, src.prototype, igonres, 0); | ||
case 2: | ||
// object to object and proto to proto | ||
mix(dst.prototype, src.prototype, igonres, 0); | ||
break; // pass through | ||
case 3: | ||
// proto to static | ||
return mix(dst, src.prototype, igonres, 0); | ||
case 4: | ||
// static to proto | ||
return mix(dst.prototype, src, igonres, 0); | ||
default: // object to object is what happens below | ||
} | ||
} | ||
//--- | ||
src = src || {}; | ||
dst = dst || (isArray(src) ? [] : {}); | ||
keys(src).forEach(function (key) { | ||
if (contains(igonres, key)) return; | ||
if (igonreNull && isNull(src[key])) return; | ||
if (isObject(src[key]) && (src[key].constructor == Object || src[key].constructor == Array || src[key].constructor == null)) { | ||
dst[key] = mix(dst[key], src[key], igonres, 0, igonreNull); | ||
} else { | ||
dst[key] = src[key]; | ||
} | ||
}, this); | ||
return dst; | ||
}; | ||
/** | ||
* 定义不可遍历的属性 | ||
**/ | ||
function defineFreezeProp(obj, name, value) { | ||
try { | ||
Object.defineProperty(obj, name, { | ||
value: value, | ||
enumerable: false, | ||
configurable: true, //能不能重写定义 | ||
writable: false //能不能用「赋值」运算更改 | ||
}); | ||
} catch (err) { | ||
obj[name] = value; | ||
} | ||
}; | ||
/** | ||
* 获取所有 key | ||
*/ | ||
function keys(obj) { | ||
if (Object.keys) return Object.keys(obj); | ||
var keys = []; | ||
each(obj, function (key) { | ||
keys.push(key); | ||
}); | ||
return keys; | ||
}; | ||
/** | ||
* 创建一个对象 | ||
*/ | ||
function create(proto, props) { | ||
if (Object.create) return Object.create(proto, props); | ||
function Cotr() {}; | ||
Cotr.prototype = proto; | ||
var obj = new Cotr(); | ||
if (props) copy(props, obj); | ||
return obj; | ||
}; | ||
/** | ||
* 设置 proto | ||
* 在不支持 setPrototypeOf 也不支持 __proto__ 的浏览器 | ||
* 中,会采用 copy 方式 | ||
*/ | ||
function setPrototypeOf(obj, proto) { | ||
if (Object.setPrototypeOf) { | ||
return Object.setPrototypeOf(obj, proto || create(null)); | ||
} else { | ||
if (!('__proto__' in Object)) copy(proto, obj); | ||
obj.__proto__ = proto; | ||
} | ||
}; | ||
/** | ||
* 获取 proto | ||
*/ | ||
function getPrototypeOf(obj) { | ||
if (obj.__proto__) return obj.__proto__; | ||
if (Object.getPrototypeOf) return Object.getPrototypeOf(obj); | ||
if (obj.constructor) return obj.constructor.prototype; | ||
}; | ||
/** | ||
* 是否深度相等 | ||
*/ | ||
function deepEqual(a, b) { | ||
if (a === b) return true; | ||
if (!isObject(a) || !isObject(b)) return false; | ||
var aKeys = keys(a); | ||
var bKeys = keys(b); | ||
if (aKeys.length !== bKeys.length) return false; | ||
var allKeys = aKeys.concat(bKeys); | ||
var checkedMap = create(null); | ||
var result = true; | ||
each(allKeys, function (i, key) { | ||
if (checkedMap[key]) return; | ||
if (!deepEqual(a[key], b[key])) result = false; | ||
checkedMap[key] = true; | ||
}, this); | ||
return result; | ||
}; | ||
/** | ||
* 从一个数值循环到别一个数 | ||
* @param {number} fromNum 开始数值 | ||
* @param {Number} toNum 结束数值 | ||
* @param {Number} step 步长值 | ||
* @param {function} handler 执行函数 | ||
* @returns {void} 无返回 | ||
*/ | ||
function fromTo(fromNum, toNum, step, handler) { | ||
if (!handler) handler = [step, step = handler][0]; | ||
step = Math.abs(step || 1); | ||
if (fromNum < toNum) { | ||
for (var i = fromNum; i <= toNum; i += step) { | ||
handler(i); | ||
} | ||
} else { | ||
for (var i = fromNum; i >= toNum; i -= step) { | ||
handler(i); | ||
} | ||
} | ||
}; | ||
/** | ||
* 生成一个Guid | ||
* @method newGuid | ||
* @return {String} GUID字符串 | ||
* @static | ||
*/ | ||
function newGuid() { | ||
function s4() { | ||
return ((1 + Math.random()) * 0x10000 | 0).toString(16).substring(1); | ||
}; | ||
return s4() + s4() + "-" + s4() + "-" + s4() + "-" + s4() + "-" + s4() + s4() + s4(); | ||
}; | ||
/** | ||
* 对象变换 | ||
**/ | ||
function map(list, fn) { | ||
var buffer = isArray(list) ? [] : {}; | ||
each(list, function (name, value) { | ||
buffer[name] = fn(name, value); | ||
}); | ||
return buffer; | ||
}; | ||
/** | ||
* 通过路径设置属性值 | ||
*/ | ||
function setByPath(obj, path, value) { | ||
if (isNull(obj) || isNull(path) || path === '') { | ||
return; | ||
} | ||
if (!isArray(path)) { | ||
path = path.replace(/\[/, '.').replace(/\]/, '.').split('.'); | ||
} | ||
each(path, function (index, name) { | ||
if (isNull(name) || name.length < 1) return; | ||
if (index === path.length - 1) { | ||
obj[name] = value; | ||
} else { | ||
obj[name] = obj[name] || {}; | ||
obj = obj[name]; | ||
} | ||
}, this); | ||
}; | ||
/** | ||
* 通过路径获取属性值 | ||
*/ | ||
function getByPath(obj, path) { | ||
if (isNull(obj) || isNull(path) || path === '') { | ||
return obj; | ||
} | ||
if (!isArray(path)) { | ||
path = path.replace(/\[/, '.').replace(/\]/, '.').split('.'); | ||
} | ||
each(path, function (index, name) { | ||
if (isNull(name) || name.length < 1) return; | ||
if (!isNull(obj)) obj = obj[name]; | ||
}, this); | ||
return obj; | ||
}; | ||
/** | ||
* 数组去重 | ||
**/ | ||
function unique(array) { | ||
if (isNull(array)) return array; | ||
var newArray = []; | ||
each(array, function (i, value) { | ||
if (newArray.indexOf(value) > -1) return; | ||
newArray.push(value); | ||
}); | ||
return newArray; | ||
}; | ||
/** | ||
* 解析 function 的参数列表 | ||
**/ | ||
function getFunctionArgumentNames(fn) { | ||
if (!fn) return []; | ||
var src = fn.toString(); | ||
var parts = src.split(')')[0].split('=>')[0].split('('); | ||
return (parts[1] || parts[0]).split(',').map(function (name) { | ||
return name.trim(); | ||
}).filter(function (name) { | ||
return name != 'function'; | ||
}); | ||
}; | ||
/** | ||
* 缩短字符串 | ||
*/ | ||
function short(str, maxLength) { | ||
if (!str) return str; | ||
maxLength = maxLength || 40; | ||
var strLength = str.length; | ||
var trimLength = maxLength / 2; | ||
return strLength > maxLength ? str.substr(0, trimLength) + '...' + str.substr(strLength - trimLength) : str; | ||
}; | ||
/** | ||
* 首字母大写 | ||
*/ | ||
function firstUpper(str) { | ||
if (isNull(str)) return; | ||
return str.substring(0, 1).toUpperCase() + str.substring(1); | ||
}; | ||
/** | ||
* 编码正则字符串 | ||
*/ | ||
function escapeRegExp(str) { | ||
return str.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&'); | ||
}; | ||
/** | ||
* 解析字符串为 dom | ||
* @param {string} str 字符串 | ||
* @returns {HTMLNode} 解析后的 DOM | ||
*/ | ||
function parseDom(str) { | ||
window._NTILS_PARSE_DOM_ = window._NTILS_PARSE_DOM_ || document.createElement('div'); | ||
window._NTILS_PARSE_DOM_.innerHTML = trim(str); | ||
var firstNode = window._NTILS_PARSE_DOM_.childNodes[0]; | ||
//先 clone 一份再通过 innerHTML 清空 | ||
//否则 IE9 下,清空时会导不通过返回的 DOM 没有子结点 | ||
if (firstNode) firstNode = firstNode.cloneNode(true); | ||
window._NTILS_PARSE_DOM_.innerHTML = ''; | ||
return firstNode; | ||
}; | ||
/***/ }) | ||
/******/ ]); | ||
}); | ||
//# sourceMappingURL=ntils.js.map |
{ | ||
"name": "ntils", | ||
"version": "3.0.3", | ||
"version": "3.0.5", | ||
"description": "一个 Node & Browser 工具函数集", | ||
"main": "./lib/ntils.js", | ||
"jsnext:main": "./src/index.js", | ||
"scripts": { | ||
@@ -7,0 +8,0 @@ "test": "mocha" |
import * as utils from './utils'; | ||
export * from './utils'; | ||
export default utils; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
136412
13
1439