lodash.js是一款超火的js库,在npm上平均周下载量达到了惊人的12,374,096,github start36K!大量框架都用到了lodash,包括拥有123kstart的vue本文对比lodash英文文档,加上一些小栗子和个人的经验~~,希望能帮到你们lodash采用了immutable的思想,永远不会改变原数据,而是会返回一个新的结果String 字符串操作camelCase 转换驼峰命名_.camelCase([string=’’])console.log(.camelCase(‘Foo Bar’))// => ‘fooBar’console.log(.camelCase(’–foo-bar–’))// => ‘fooBar’console.log(.camelCase(’FOO_BAR’))// => ‘fooBar’console.log(.camelCase(’/_FOO_BAR_*\9’))// ‘fooBar9’console.log(.camelCase(‘fooBarbar_bar’))// fooBarbarBar字符串中非数字和字母都会被过滤掉,然后再转换为驼峰capitalize 转换大写.capitalize([string=’’])console.log(.capitalize(‘FRED’));// => ‘Fred’联想: 同string.prototype.toLocaleUpperCase();deburr 清理符号.capitalize([string=’’])deburr转换 Latin-1 Supplement和Latin Extended-A 为普通拉丁字母并且移除变音符号_.deburr(‘déjà vu’);// => ‘deja vu’一般用不到…endsWith 判断是否是某个字符串结尾_.endsWith([string=’’], [target], [position=string.length])console.log(.endsWith(‘abcdef3’, ‘c’, 3))// trueconsole.log(.endsWith(‘abcdef3’, ‘c’, 2))// false主要是第三个参数,不填表示检查整个字符串,有值代表从左截取几个字符,从截取的字符中进行判断ECMAScript 6中已经加入string.prototype.endsWith()方法escape 转义html实体字符_.escape([string=’’])会将&装换成&, < -> <, > -> > ’’ -> "。其他转义字符,如:×(乘号),÷(除号)等不会转义,请用he这样的专业处理转义的库console.log(.escape(a as <a> &'"" *
))// a as <a> &'"" *escapeRegExp 转义正则表达式特殊字符.escapeRegExp([string=’’])正则表达式中的特殊字符都会加’‘处理console.log(.escapeRegExp(’lodash’))// [lodash]()kebabCase 转换成kebabCase格式总结: 存在四种case格式CamelCase: TheQuickBrownFoxJumpsOverTheLazyDogSnakeCase: the_quick_brown_fox_jumps_over_the_lazy_dogKebabCase: the-quick-brown-fox-jumps-over-the-lazy-dogStudlycaps: tHeqUicKBrOWnFoXJUmpsoVeRThElAzydOG查看case的具体文档其他转换case语法通camelCaselowerCase 转换小写.lowerCase([string=’’]).lowerCase(’–Foo-Bar–’);// => ‘foo bar’ .lowerCase(‘fooBar’);// => ‘foo bar’ .lowerCase(’FOO_BAR’);// => ‘foo bar’通capitalize联想: string.prototype.toLocaleLowerCaselowerFirst 转换第一个字符为小写console.log(.lowerFirst(‘DS’))// dSconsole.log(.lowerFirst(’__DS’))// _DS无法过滤非字母字符pad 填充字符.pad([string=’’], [length=0], [chars=’ ‘])有三个参数: 原字符串,长度,填充字符如果原字符串长度短于给定的长度,则原字符串左右两边会填充指定字符(默认为空格),如果不能平均分配则会被截断。.pad(‘abc’, 8);// => ’ abc ’ .pad(‘abc’, 8, ‘-’);// => ‘-abc-_’ _.pad(‘abc’, 3);// => ‘abc’