@abcnews/alternating-case-to-object 中文文档教程

发布于 3年前 浏览 19 项目主页 更新于 3年前

Alternating Case to Object

将 ALTERNATINGcase 字符串转换为对象的函数。

Usage

npm i @abcnews/alternating-case-to-object
const alternatingCaseToObject = require('@abcnews/alternating-case-to-object');

alternatingCaseToObject('PROPvalueSECONDthingALLOWEDyesTHINGS100');

// >>>

{
  prop: 'value',
  second: 'thing',
  allowed: true,
  things: 100
}

Automatic type inference

  • Number values will be parsed as floats.
  • The values "true", "yes", "false" & "no" will be converted to the booleans true, true, false & false, respectively.
  • If a prop appears more than once, multiple values will be returned as an array:
alternatingCaseToObject('GROUPfirstGROUPsecondGROUPthird');

// >>>

{
  group: ['first', 'second', 'third'];
}

多次出现的 prop 中的每个值都必须属于同一类型,否则将抛出异常。 例如,这将抛出:

alternatingCaseToObject('AtrueAstr');

Options

您可以将对象作为第二个参数传递,定义一个或多个选项:

arrayProps:Array

此数组中的道具将始终作为数组返回,即使它们在配置字符串中出现零次或一次。

alternatingCaseToObject('AtrueAfalseBvalueAtrueDvalue', {
  arrayProps: ['a', 'b', 'c']
});

// >>>

{
  a: [true, false, true],
  b: ['value'],
  c: [],
  d: 'value'
}

propMap:Object

如果配置中的道具出现在这个对象中,它们将被重命名。

alternatingCaseToObject('CAMELCASEtrueKEBABCASE100', {
  propMap: {
    camelcase: 'camelCase',
    kebabcase: 'kebab-case'
  }
});

// >>>

{
  camelCase: true,
  'kebab-case': 100
}

Alternating Case to Object

A function to convert ALTERNATINGcase strings to objects.

Usage

npm i @abcnews/alternating-case-to-object
const alternatingCaseToObject = require('@abcnews/alternating-case-to-object');

alternatingCaseToObject('PROPvalueSECONDthingALLOWEDyesTHINGS100');

// >>>

{
  prop: 'value',
  second: 'thing',
  allowed: true,
  things: 100
}

Automatic type inference

  • Number values will be parsed as floats.
  • The values "true", "yes", "false" & "no" will be converted to the booleans true, true, false & false, respectively.
  • If a prop appears more than once, multiple values will be returned as an array:
alternatingCaseToObject('GROUPfirstGROUPsecondGROUPthird');

// >>>

{
  group: ['first', 'second', 'third'];
}

Every value in a prop which appears multiple times must be of the same type or an exception will be thrown. For example, this will throw:

alternatingCaseToObject('AtrueAstr');

Options

You can pass an object as a second argument, defining one or more options:

arrayProps:Array

Props in this array will always be returned as arrays, even if they occur in the config string zero or one time.

alternatingCaseToObject('AtrueAfalseBvalueAtrueDvalue', {
  arrayProps: ['a', 'b', 'c']
});

// >>>

{
  a: [true, false, true],
  b: ['value'],
  c: [],
  d: 'value'
}

propMap:Object

Props in the config will be renamed if they occur in this object.

alternatingCaseToObject('CAMELCASEtrueKEBABCASE100', {
  propMap: {
    camelcase: 'camelCase',
    kebabcase: 'kebab-case'
  }
});

// >>>

{
  camelCase: true,
  'kebab-case': 100
}
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文