humps

概念

humps是一个驼峰化处理库,用于对JavaScript的字符串和对象键进行驼峰化处理。

基本用法:

Converting strings

humps.camelize('hello_world') // 'helloWorld'
humps.decamelize('fooBar') // 'foo_bar'
humps.decamelize('fooBarBaz', { separator: '-' }) // 'foo-bar-baz'

Converting object keys

var object = { attr_one: 'foo', attr_two: 'bar' }
humps.camelizeKeys(object); // { attrOne: 'foo', attrTwo: 'bar' }

var array = [{ attr_one: 'foo' }, { attr_one: 'bar' }]
humps.camelizeKeys(array); // [{ attrOne: 'foo' }, { attrOne: 'bar' }]

高级用法:

自定义convert行为,如下全是大写字母和数字的对象键不做转化。

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);
});

Converts camelCased object key to an underscore-separated key.

humps.decamelizeKeys(obj, {
    separator: '-',
    process: function (key, convert, options) {
      return /^[A-Z0-9_]+$/.test(key) ? key : convert(key, options);
    }
});

参考:

https://developer.aliyun.com/mirror/npm/package/humps

 

posted @ 2020-04-24 11:41  芝芝07  阅读(1140)  评论(0编辑  收藏  举报