Comparing version 3.1.5 to 3.1.7
{ | ||
"name": "ntils", | ||
"version": "3.1.5", | ||
"version": "3.1.7", | ||
"description": "一个 Node & Browser 工具函数集", | ||
"main": "./lib/ntils.js", | ||
"jsnext:main": "./src/index.js", | ||
"main": "./lib/index.js", | ||
"scripts": { | ||
@@ -25,3 +24,6 @@ "test": "mocha" | ||
}, | ||
"homepage": "https://github.com/Houfeng/ntils#readme" | ||
} | ||
"homepage": "https://github.com/Houfeng/ntils#readme", | ||
"devDependencies": { | ||
"rollup": "^0.50.0" | ||
} | ||
} |
726
src/index.js
@@ -1,3 +0,723 @@ | ||
import * as utils from './utils'; | ||
export * from './utils'; | ||
export default utils; | ||
/** | ||
* 空函数 | ||
*/ | ||
export function noop() { }; | ||
export function toString(obj) { | ||
return Object.prototype.toString.call(obj); | ||
} | ||
export function getType(obj) { | ||
var str = toString(obj); | ||
return (/^\[object (.+)\]$/i.exec(str))[1]; | ||
} | ||
/** | ||
* 验证一个对象是否为NULL | ||
* @method isNull | ||
* @param {Object} obj 要验证的对象 | ||
* @return {Boolean} 结果 | ||
* @static | ||
*/ | ||
export function isNull(obj) { | ||
var type = getType(obj); | ||
return type === 'Undefined' || type === 'Null'; | ||
}; | ||
/** | ||
* 除去字符串两端的空格 | ||
* @method trim | ||
* @param {String} str 源字符串 | ||
* @return {String} 结果字符串 | ||
* @static | ||
*/ | ||
export 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 | ||
*/ | ||
export 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 | ||
*/ | ||
export 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 | ||
*/ | ||
export 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 | ||
*/ | ||
export 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 | ||
*/ | ||
export function has(obj, name) { | ||
if (isNull(obj) || isNull(name)) return false; | ||
return (name in obj) || (obj.hasOwnProperty(name)); | ||
}; | ||
export var hasProperty = has; | ||
/** | ||
* 验证一个对象是否为Function | ||
* @method isFunction | ||
* @param {Object} obj 要验证的对象 | ||
* @return {Boolean} 结果 | ||
* @static | ||
*/ | ||
export function isFunction(obj) { | ||
if (isNull(obj)) return false; | ||
return getType(obj) === "Function"; | ||
}; | ||
/** | ||
* 验证一个对象是否为String | ||
* @method isString | ||
* @param {Object} obj 要验证的对象 | ||
* @return {Boolean} 结果 | ||
* @static | ||
*/ | ||
export function isString(obj) { | ||
if (isNull(obj)) return false; | ||
return getType(obj) === 'String'; | ||
}; | ||
/** | ||
* 验证一个对象是否为Number | ||
* @method isNumber | ||
* @param {Object} obj 要验证的对象 | ||
* @return {Boolean} 结果 | ||
* @static | ||
*/ | ||
export function isNumber(obj) { | ||
if (isNull(obj)) return false; | ||
return getType(obj) === 'Number'; | ||
}; | ||
/** | ||
* 验证一个对象是否为Boolean | ||
* @method isBoolean | ||
* @param {Object} obj 要验证的对象 | ||
* @return {Boolean} 结果 | ||
* @static | ||
*/ | ||
export function isBoolean(obj) { | ||
if (isNull(obj)) return false; | ||
return getType(obj) === 'Boolean'; | ||
}; | ||
/** | ||
* 验证一个对象是否为HTML Element | ||
* @method isElement | ||
* @param {Object} obj 要验证的对象 | ||
* @return {Boolean} 结果 | ||
* @static | ||
*/ | ||
export function isElement(obj) { | ||
if (isNull(obj)) return false; | ||
if (win.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 | ||
*/ | ||
export function isText(obj) { | ||
if (isNull(obj)) return false; | ||
return obj instanceof Text; | ||
}; | ||
/** | ||
* 验证一个对象是否为Object | ||
* @method isObject | ||
* @param {Object} obj 要验证的对象 | ||
* @return {Boolean} 结果 | ||
* @static | ||
*/ | ||
export 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 | ||
*/ | ||
export 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 | ||
*/ | ||
export function isDate(val) { | ||
if (isNull(val)) return false; | ||
return val instanceof Date; | ||
}; | ||
/** | ||
* 验证是不是一个正则对象 | ||
* @method isDate | ||
* @param {Object} val 要检查的对象 | ||
* @return {Boolean} 结果 | ||
* @static | ||
*/ | ||
export function isRegexp(val) { | ||
return val instanceof RegExp; | ||
}; | ||
/** | ||
* 转换为数组 | ||
* @method toArray | ||
* @param {Array|Object} array 伪数组 | ||
* @return {Array} 转换结果数组 | ||
* @static | ||
*/ | ||
export function toArray(array) { | ||
if (isNull(array)) return []; | ||
return Array.prototype.slice.call(array); | ||
}; | ||
/** | ||
* 转为日期格式 | ||
* @method toDate | ||
* @param {Number|String} val 日期字符串或整型数值 | ||
* @return {Date} 日期对象 | ||
* @static | ||
*/ | ||
export 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 | ||
*/ | ||
export 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 | ||
*/ | ||
export 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 | ||
*/ | ||
export 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 | ||
*/ | ||
export 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; | ||
final(objClone, key, src[key]); | ||
}, this); | ||
return objClone; | ||
}; | ||
/** | ||
* 合并对象 | ||
* @method mix | ||
* @return 合并后的对象 | ||
* @param {Object} dst 目标对象 | ||
* @param {Object} src 源对象 | ||
* @param {Array} igonres 忽略的属性名, | ||
* @param {Number} mode 模式 | ||
*/ | ||
export 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; | ||
}; | ||
/** | ||
* 定义不可遍历的属性 | ||
**/ | ||
export function final(obj, name, value) { | ||
if (arguments.length < 1) throw new Error('Parameter missing'); | ||
if (arguments.length < 2) { | ||
return each(obj, function (name, value) { | ||
final(obj, name, value); | ||
}); | ||
} | ||
if (arguments.length < 3) return final(obj, name, obj[name]); | ||
try { | ||
Object.defineProperty(obj, name, { | ||
get: function () { | ||
return value; | ||
}, | ||
set: function () { | ||
throw new Error('Cannot assign to final property:' + name); | ||
}, | ||
enumerable: false, //不能枚举 | ||
configurable: false, //不能重写定义 | ||
}); | ||
} catch (err) { | ||
obj[name] = value; | ||
} | ||
}; | ||
/** | ||
* 获取所有 key | ||
*/ | ||
export function keys(obj) { | ||
if (Object.keys) return Object.keys(obj); | ||
var keys = []; | ||
each(obj, function (key) { | ||
keys.push(key); | ||
}); | ||
return keys; | ||
}; | ||
/** | ||
* 创建一个对象 | ||
*/ | ||
export 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 方式 | ||
*/ | ||
export 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 | ||
*/ | ||
export function getPrototypeOf(obj) { | ||
if (obj.__proto__) return obj.__proto__; | ||
if (Object.getPrototypeOf) return Object.getPrototypeOf(obj); | ||
if (obj.constructor) return obj.constructor.prototype; | ||
}; | ||
/** | ||
* 是否深度相等 | ||
*/ | ||
export 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} 无返回 | ||
*/ | ||
export 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 | ||
*/ | ||
export function newGuid() { | ||
function s4() { | ||
return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1); | ||
}; | ||
return (s4() + s4() + "-" + s4() + "-" + s4() + "-" + | ||
s4() + "-" + s4() + s4() + s4()); | ||
}; | ||
/** | ||
* 对象变换 | ||
**/ | ||
export function map(list, fn) { | ||
var buffer = isArray(list) ? [] : {}; | ||
each(list, function (name, value) { | ||
buffer[name] = fn(name, value); | ||
}); | ||
return buffer; | ||
}; | ||
/** | ||
* 通过路径设置属性值 | ||
*/ | ||
export 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); | ||
}; | ||
/** | ||
* 通过路径获取属性值 | ||
*/ | ||
export 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; | ||
}; | ||
/** | ||
* 数组去重 | ||
**/ | ||
export 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 的参数列表 | ||
**/ | ||
export 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 trim(name); | ||
}).filter(function (name) { | ||
return name != 'function'; | ||
}); | ||
}; | ||
/** | ||
* 缩短字符串 | ||
*/ | ||
export 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; | ||
}; | ||
/** | ||
* 首字母大写 | ||
*/ | ||
export function firstUpper(str) { | ||
if (isNull(str)) return; | ||
return str.substring(0, 1).toUpperCase() + str.substring(1); | ||
}; | ||
/** | ||
* 编码正则字符串 | ||
*/ | ||
export function escapeRegExp(str) { | ||
return str.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&'); | ||
}; | ||
/** | ||
* 将字符串转成「驼峰」式 | ||
* @param {string} str 原始字符串 | ||
* @param {number} mode 1 大驼峰,0 小驼峰 | ||
* @return {string} 转换后的字符串 | ||
*/ | ||
export function toCamelCase(str, mode) { | ||
if (str) { | ||
str = str.replace(/\-[a-z0-9]/g, function ($1) { | ||
return $1.slice(1).toUpperCase() | ||
}); | ||
str = str.replace(/^[a-z]/i, function ($1) { | ||
return mode ? $1.toUpperCase() : $1.toLowerCase(); | ||
}); | ||
} | ||
return str; | ||
} | ||
/** | ||
* 将字符串转成分隔形式 | ||
* @param {string} str 原始字符串 | ||
* @return {string} 转换后的字符串 | ||
*/ | ||
export function toSplitCase(str) { | ||
if (str) { | ||
str = str.replace(/([A-Z])/g, '-$1'); | ||
if (str[0] == '-') str = str.slice(1); | ||
} | ||
return str; | ||
} | ||
export function htmlPrefilter(html) { | ||
var rxhtmlTag = /<(?!area|br|col|embed|hr|img|input|link|meta|param)(([a-z][^\/\0>\x20\t\r\n\f]*)[^>]*)\/>/gi; | ||
return html.replace(rxhtmlTag, "<$1></$2>"); | ||
} | ||
/** | ||
* 解析字符串为 dom | ||
* @param {string} str 字符串 | ||
* @returns {HTMLNode} 解析后的 DOM | ||
*/ | ||
export function parseHTML(str) { | ||
str = str || ' '; | ||
var parent = document.createElement('div'); | ||
parent.innerHTML = htmlPrefilter(trim(str)); | ||
var childNodes = toArray(parent.childNodes); | ||
//先 clone 一份再通过 innerHTML 清空 | ||
//否则 IE9 下,清空时会导不通过返回的 DOM 没有子结点 | ||
// if (firstNode) firstNode = firstNode.cloneNode(true); | ||
// win._NPH_.innerHTML = ''; | ||
each(childNodes, function (index, childNode) { | ||
parent.removeChild(childNode); | ||
}); | ||
return childNodes; | ||
}; |
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Debug access
Supply chain riskUses debug, reflection and dynamic code execution features.
Found 1 instance in 1 package
2145
0
56022
1
7
1