首页 > 其他 > 详细

humps

时间:2020-04-24 12:49:23      阅读:94      评论:0      收藏:0      [点我收藏+]

概念

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

 

humps

原文:https://www.cnblogs.com/zouyanzhi/p/12766373.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!