Socket
Socket
Sign inDemoInstall

fill-range

Package Overview
Dependencies
5
Maintainers
1
Versions
32
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 0.2.0 to 1.0.0

benchmark/check.js

131

index.js
/*!
* fill-range <https://github.com/jonschlinkert/fill-range>
*
* Copyright (c) 2014 Jon Schlinkert, contributors.
* Copyright (c) 2014-2015, Jon Schlinkert.
* Licensed under the MIT license.

@@ -10,65 +10,102 @@ */

var randomize = require('randomatic');
var repeat = require('repeat-string');
var isNumber = require('is-number');
module.exports = function fillRange(a, b, c, fn) {
if (typeof c === 'function') {
fn = c;
c = undefined;
/**
* Expose `fillRange`
*/
module.exports = fillRange;
function fillRange(a, b, step, fn) {
if (typeof step === 'function') {
fn = step;
step = null;
}
var bad = /\./.test(a)
|| /\./.test(b)
|| c && !isNumber(c)
|| (!isNumber(Math.abs(a)) && isNumber(Math.abs(b))
|| isNumber(Math.abs(a)) && !isNumber(Math.abs(b)));
var expand;
if (typeof step === 'string') {
if (/\?/.test(step)) {
return [randomize(a, b)];
}
if (bad) {
return {bad: [ '{' + [a, b, c].filter(Boolean).join('..') + '}']};
if (/>/.test(step)) {
step = step.replace(/>/, '');
expand = true;
}
}
var inc = typeof c !== 'undefined'
? (+c < 0) ? (+c * -1) : +c
: 1;
validateRange(a, b, step);
var isLetter = !isNumber(+a);
a = isLetter ? a.charCodeAt(0) : +a;
b = isLetter ? b.charCodeAt(0) : +b;
var num = step && isNumber(step)
? Math.abs(step)
: 1;
if (b < a) {
return negativeRange(a, b, inc, fn, isLetter);
}
// store a ref to the unmodified first arg
var strA = a.toString();
return positiveRange(a, b, inc, fn, isLetter);
};
// is the range alphabetical? or numeric?
var isNumeric = isNumber(a);
function positiveRange(a, b, inc, fn, isLetter) {
var arr = [];
a -= inc;
// if numeric coerce to an integer, otherwise
// get the charCode to expand alpha ranges
a = isNumeric ? +a : a.charCodeAt(0);
b = isNumeric ? +b : b.charCodeAt(0);
for (var i = 0; a < b; i++) {
a += inc;
if (a <= b) {
var res = fn && fn(a, isLetter, i);
arr.push(res ? res : isLetter
? String.fromCharCode(a)
: String(a));
// is the pattern positive or negative?
var isNegative = b < a;
// detect padding
var padding = isPadded(strA, isNumeric);
var res, pad, arr = [];
var i = 0;
while (isNegative ? (a >= b) : (a <= b)) {
if (padding && isNumeric) {
pad = padding(a);
}
if (typeof fn === 'function') {
res = fn(a, isNumeric, pad, i++);
} else if (!isNumeric) {
res = String.fromCharCode(a);
} else {
res = String(pad ? pad + a : a);
}
arr.push(res);
if (isNegative) {
a -= num;
} else {
a += num;
}
}
return arr;
return expand ? [arr.join('')] : arr;
}
function negativeRange(a, b, inc, fn, isLetter) {
var arr = [];
a += inc;
function isPadded(strA, isNumeric) {
return isNumeric && /^-*0+[1-9]/.test(strA)
? function (a) {
var num = strA.length - a.toString().length;
return repeat('0', num);
} : false;
}
for (var i = 0; a > b; i--) {
a -= inc;
if (a >= b) {
var res = fn && fn(a, isLetter, i);
arr.push(res ? res : isLetter
? String.fromCharCode(a)
: String(a));
}
function validateRange(a, b, step) {
if (!/[\w\d]/.test(a) || !/[\w\d]/.test(b)) {
throw new Error('fill-range: invalid range arguments.');
}
return arr;
}
if (!isNumber(a) && isNumber(b)) {
throw new TypeError('fill-range: incompatible range arguments.');
}
if (isNumber(a) && !isNumber(b)) {
throw new TypeError('fill-range: incompatible range arguments.');
}
if (step && (!isNumber(step) && !/>/.test(step))) {
throw new TypeError('fill-range: invalid step.');
}
}
{
"name": "fill-range",
"description": "Fill in a range of numbers or letters, optionally passing an increment or multiplier to use.",
"version": "0.2.0",
"version": "1.0.0",
"homepage": "https://github.com/jonschlinkert/fill-range",

@@ -17,8 +17,6 @@ "author": {

},
"licenses": [
{
"type": "MIT",
"url": "https://github.com/jonschlinkert/fill-range/blob/master/LICENSE-MIT"
}
],
"license": {
"type": "MIT",
"url": "https://github.com/jonschlinkert/fill-range/blob/master/LICENSE-MIT"
},
"main": "index.js",

@@ -29,10 +27,14 @@ "engines": {

"scripts": {
"test": "mocha -R spec"
"test": "mocha -R spec",
"docs": "update && license && npmignore && deps && verb",
"all": "npm run test && npm run docs"
},
"dependencies": {
"alphabet": "^0.1.0",
"is-number": "^0.1.1"
"is-number": "^0.1.1",
"randomatic": "^1.0.1",
"repeat-string": "^1.2.0"
},
"devDependencies": {
"mocha": "*",
"benchmarked": "^0.1.3",
"chalk": "^0.5.1",
"should": "*"

@@ -57,2 +59,2 @@ },

]
}
}

@@ -5,4 +5,3 @@ # fill-range [![NPM version](https://badge.fury.io/js/fill-range.svg)](http://badge.fury.io/js/fill-range)

## Install
### Install with [npm](npmjs.org)
## Install with [npm](npmjs.org)

@@ -33,3 +32,3 @@ ```bash

- `end`: the number or letter to end with
- `increment`: optionally pass the increment to use. works for letters or numbers
- `step`: optionally pass the increment to use. works for letters or numbers

@@ -72,4 +71,4 @@ **Examples**

```js
range('a', 'e', function (val, isLetter, i) {
if (isLetter) {
range('a', 'e', function (val, isNumeric, i) {
if (!isNumeric) {
return String.fromCharCode(val) + i;

@@ -82,2 +81,62 @@ }

### Special characters
A special character may be passed as the third arg instead of a step increment. e.g:
```js
range('a', 'z', SPECIAL_CHARACTER_HERE);
```
Depending on how fill-range is implemented, the These characters can be pretty useful when brace expansion and similar use case.
#### `>`
Collapses all values in the returned array to a single value.
**Examples:**
```js
range('a', 'e', '>');
//=> ['abcde']
range('5', '8', '>');
//=> ['5678']
range('2', '20', '2>');
//=> ['2468101214161820']
```
#### `?`
Uses [randomatic] to generate randomized alpha, numeric, or alpha-numeric patterns based on the provided arguments.
**Examples:**
_(all results are obviously examples, since the actual results will be randomized)_
Generate a 5-character, uppercase, alphabetical string:
```js
range('A', 5, '?');
//=> ['NSHAK']
```
Generate a 5-digit random number:
```js
range('0', 5, '?');
//=> ['36583']
```
Generate a 10-character alpha-numeric string:
```js
range('A0', 10, '?');
//=> ['5YJD60VQNN']
```
See the [randomatic] repo for all available options and or to create issues or feature requests related to randomization.
## Contributing

@@ -94,3 +153,3 @@ Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](https://github.com/jonschlinkert/fill-range/issues)

## License
Copyright (c) 2014 Jon Schlinkert
Copyright (c) 2014-2015 Jon Schlinkert
Released under the MIT license

@@ -100,2 +159,2 @@

_This file was generated by [verb](https://github.com/assemble/verb) on November 25, 2014._
_This file was generated by [verb](https://github.com/assemble/verb) on January 04, 2015._
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc