humps 下划线分割字符串转驼峰
- 安装
yarn add humps -D
// or
npm i humps -D
- 使用
// 引入
const humps = require('humps')
- 转换字符串
// 下划线转驼峰
humps.camelize('hello_world') // 'helloWorld'
// 驼峰转下划线
humps.decamelize('fooBar') // 'foo_bar'
// 特定分隔符分割
humps.decamelize('fooBarBaz', { separator: '-' }) // 'foo-bar-baz'
- 转换对象键
var object = { attr_one: 'foo', attr_two: 'bar' }
humps.camelizeKeys(object); // { attrOne: 'foo', attrTwo: 'bar' }
- 它还接受可以修改转换行为的回调。例如,要防止转换仅包含大写字母或数字的键:
humps.camelizeKeys(obj, function (key, convert) {
return /^[A-Z0-9_]+$/.test(key) ? key : convert(key);
});
humps.decamelizeKeys(obj, function (key, convert, options) {
return /^[A-Z0-9_]+$/.test(key) ? key : convert(key, options);
});
- 为了将回调与选项一起使用,请使用以下process选项:
humps.decamelizeKeys(obj, {
separator: '-',
process: function (key, convert, options) {
return /^[A-Z0-9_]+$/.test(key) ? key : convert(key, options);
}
});
这些基本够用了 其他的看文档
欢迎访问俺的博客BULINGBULING