-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[知识沉淀] 一些常见的判断类型的函数 utils #97
Comments
判断值是否是一个对象function isObject(v) {
return typeof v === "object" && Array.isArray(v) && v !== null;
} |
判断一个值是否是纯的对象const _toString = Object.prototype.toString function isPureObject(v) {
return _toString.call(v) === '[object Object]'
}
console.log(isPureObject(Object.create({}))); // true |
判断值是否是一个RegExpfunction isRegExp(v) {
return _toString.call(v) === '[object RegExp]'
}
console.log(isRegExp(new RegExp()), isRegExp(Object.create(null))); // true false |
将某个值转换成 Number ,失败则返回原值function toNumber(v) {
const n = parseFloat(v)
return isNaN(n) ? v : n;
}
console.log(toNumber('889'), toNumber('88x9')); // 889 88 |
将任意值转成一个字符串function toString(v) {
return v == null
? ''
: Array.isArray(v) || (isPureObject(v) && val.toString === _toString)
? JSON.stringify(v, null , 2)
: String(v)
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
判断值是否为undefined或null
The text was updated successfully, but these errors were encountered: