fill-range

填充数字或字母的范围,可选择传递一个增量或
step来使用或创建与regex兼容的范围options.toRegex
请考虑跟随这个项目的作者, Jon Schlinkert,并考虑主演该项目,以展示您的: 心: 和支持。
安装
安装与 npm:
$ npm install --save fill-range
用法
展开数字和字母,可选地使用 step 作为最后一个论点。 (数字可以定义为JavaScript数字或字符串)。
const fill = require('fill-range');
// fill(from, to[, step, options]);
console.log(fill('1', '10')); //=> ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']
console.log(fill('1', '10', { toRegex: true })); //=> [1-9]|10
参数个数
from: {字符串|数字} 开头的数字或字母to: {字符串|数字} 结尾的数字或字母step: {字符串| | |对象数量函数} 可选地传递 步骤 使用。options: {对象|函数}:查看所有可用的 选项
例子
默认情况下,返回一个值数组。
字母的范围
console.log(fill('a', 'e')); //=> ['a', 'b', 'c', 'd', 'e']
console.log(fill('A', 'E')); //=> [ 'A', 'B', 'C', 'D', 'E' ]
数值范围
数字可以定义为实际数字或字符串。
console.log(fill(1, 5)); //=> [ 1, 2, 3, 4, 5 ]
console.log(fill('1', '5')); //=> [ 1, 2, 3, 4, 5 ]
消极的范围
数字可以定义为实际数字或字符串。
console.log(fill('-5', '-1')); //=> [ '-5', '-4', '-3', '-2', '-1' ]
console.log(fill('-5', '5')); //=> [ '-5', '-4', '-3', '-2', '-1', '0', '1', '2', '3', '4', '5' ]
步骤(增量)
// numerical ranges with increments
console.log(fill('0', '25', 4)); //=> [ '0', '4', '8', '12', '16', '20', '24' ]
console.log(fill('0', '25', 5)); //=> [ '0', '5', '10', '15', '20', '25' ]
console.log(fill('0', '25', 6)); //=> [ '0', '6', '12', '18', '24' ]
// alphabetical ranges with increments
console.log(fill('a', 'z', 4)); //=> [ 'a', 'e', 'i', 'm', 'q', 'u', 'y' ]
console.log(fill('a', 'z', 5)); //=> [ 'a', 'f', 'k', 'p', 'u', 'z' ]
console.log(fill('a', 'z', 6)); //=> [ 'a', 'g', 'm', 's', 'y' ]
选项
options.step
类型: number (格式化为字符串或数字)
默认的: undefined
描述:范围的增量。可与字母或数字连用。
示例(年代)
// numbers
console.log(fill('1', '10', 2)); //=> [ '1', '3', '5', '7', '9' ]
console.log(fill('1', '10', 3)); //=> [ '1', '4', '7', '10' ]
console.log(fill('1', '10', 4)); //=> [ '1', '5', '9' ]
// letters
console.log(fill('a', 'z', 5)); //=> [ 'a', 'f', 'k', 'p', 'u', 'z' ]
console.log(fill('a', 'z', 7)); //=> [ 'a', 'h', 'o', 'v' ]
console.log(fill('a', 'z', 9)); //=> [ 'a', 'j', 's' ]
options.strictRanges
类型: boolean
默认的: false
描述:默认情况下, null 在传递无效范围时返回。启用此选项以抛出 RangeError 在无效范围上。
示例(年代)
以下选项均无效:
fill('1.1', '2'); // decimals not supported in ranges
fill('a', '2'); // incompatible range values
fill(1, 10, 'foo'); // invalid "step" argument
options.stringify
类型: boolean
默认的: undefined
描述:将所有返回值强制转换为字符串。默认情况下,整数作为数字返回。
示例(年代)
console.log(fill(1, 5)); //=> [ 1, 2, 3, 4, 5 ]
console.log(fill(1, 5, { stringify: true })); //=> [ '1', '2', '3', '4', '5' ]
options.toRegex
类型: boolean
默认的: undefined
描述:创建一个与regex兼容的源字符串,而不是将值扩展到数组。
示例(年代)
// alphabetical range
console.log(fill('a', 'e', { toRegex: true })); //=> '[a-e]'
// alphabetical with step
console.log(fill('a', 'z', 3, { toRegex: true })); //=> 'a|d|g|j|m|p|s|v|y'
// numerical range
console.log(fill('1', '100', { toRegex: true })); //=> '[1-9]|[1-9][0-9]|100'
// numerical range with zero padding
console.log(fill('000001', '100000', { toRegex: true }));
//=> '0{5}[1-9]|0{4}[1-9][0-9]|0{3}[1-9][0-9]{2}|0{2}[1-9][0-9]{3}|0[1-9][0-9]{4}|100000'
options.transform
类型: function
默认的: undefined
描述:自定义返回数组(或数组)中的每个值 字符串). (也可以将此函数作为最后一个参数传递给 fill())。
示例(年代)
// add zero padding
console.log(fill(1, 5, value => String(value).padStart(4, '0')));
//=> ['0001', '0002', '0003', '0004', '0005']
关于
贡献
拉请求和明星总是受欢迎的。对于bug和特性请求, 请创建一个问题。
运行测试
运行和审查单元测试是熟悉库及其API的好方法。可以使用以下命令安装依赖项并运行测试:
$ npm install && npm test
建立文档
(这个项目的自述。Md由 动词请不要直接编辑自述文件。对自述文件的任何更改都必须在 .verb.md 自述文件模板)。
使用实例生成自述文件。
$ npm install -g verbose/verb#dev verb-generate-readme && verb
贡献者
| 提交 | 贡献者 |
| --- | --- |
| 116 | jonschlinkert |
b| 4 b| 鲍尔米尔 |
| 2 | realityking |
| 2 | bluelovers |
| 1 | edorivai |
| 1 | wtgtybhertgeghgtwtg |
作者
Jon Schlinkert
请考虑在Patreon上支持我,或者 创建自己的Patreon页面!
许可证
版权所有©2019 Jon Schlinkert。 根据 麻省理工学院的许可。
该文件由 动词-生成-自述文件, v0.8.0,于2019年4月8日发布。