zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    Python基础Day2
    HDU
    HDU
    BZOJ
    Gym
    UVA
    UVA
    UVA
    UVA
    BZOJ
  • 原文地址:https://www.cnblogs.com/zouyanzhi/p/12766373.html
Copyright © 2011-2022 走看看