Ven*_*ama 11
按照@ muistooshort的建议,我创建了我的mixin并希望分享它:
_.mixin({
isBlank: function(string) {
return (_.isUndefined(string) || _.isNull(string) || string.trim().length === 0)
}
});
> _("\t").isBlank()
< true
> _("qwerty").isBlank()
< false
Run Code Online (Sandbox Code Playgroud)
_.isEmpty(_.trim(string));
Run Code Online (Sandbox Code Playgroud)
这将运行null或空字符串检查.
编辑来自ehftwelve的更新
function isBlank(str) {
return !!(str||'').match(/^\s*$/);
}
isBlank(null); // => true
isBlank(''); // => true
isBlank(' \t '); // => true
isBlank(' foo '); // => false
Run Code Online (Sandbox Code Playgroud)