Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

fill-range

Package Overview
Dependencies
Maintainers
1
Versions
33
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

fill-range - npm Package Compare versions

Comparing version 1.1.0 to 1.2.0

109

index.js

@@ -20,4 +20,17 @@ /*!

/**
* Return a range of numbers or letters.
*
* @param {String} `a` Start of the range
* @param {String} `b` End of the range
* @param {String} `step` Increment or decrement to use.
* @param {Function} `fn` Custom function to modify each element in the range.
* @return {Array}
*/
function fillRange(a, b, step, fn) {
if (a == null || b == null) {
throw new Error('fill-range expects the first and second args to be strings.');
}
if (typeof step === 'function') {

@@ -28,20 +41,31 @@ fn = step;

// store a ref to unmodified args
var strA = a.toString();
var strB = b.toString();
var expand, sep = '';
var expand;
// store a ref to unmodified arg
var origA = a;
b = (b.toString() === '-0') ? 0 : b;
// handle special step characters
if (typeof step === 'string') {
if (/\?/.test(step)) {
var match = /\?|>|\|/g.exec(step);
var i = match && match.index;
if (match && match[0] === '?') {
return [randomize(a, b)];
}
if (/>/.test(step)) {
step = step.replace(/>/, '');
if (match && match[0] === '>') {
step = step.substr(0, i) + step.substr(i + 1);
expand = true;
}
if (match && match[0] === '|') {
step = step.substr(0, i) + step.substr(i + 1);
expand = true;
sep = '|';
}
}
// validate arguments
validateRange(a, b, step);

@@ -54,8 +78,8 @@

// is the range alphabetical? or numeric?
var isNumeric = isNumber(a);
var isNum = isNumber(a);
// 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);
a = isNum ? +a : a.charCodeAt(0);
b = isNum ? +b : b.charCodeAt(0);

@@ -66,21 +90,32 @@ // is the pattern positive or negative?

// detect padding
var padding = isPadded(strA, strB);
var padding = isPadded(origA);
var res, pad, arr = [];
var i = 0;
var ii = 0;
while (isNegative ? (a >= b) : (a <= b)) {
if (padding && isNumeric) {
if (padding && isNum) {
pad = padding(a);
}
// custom function
if (typeof fn === 'function') {
res = fn(a, isNumeric, pad, i++);
} else if (!isNumeric) {
res = fn(a, isNum, pad, ii++);
// letters
} else if (!isNum) {
res = String.fromCharCode(a);
// numbers
} else {
res = String(pad ? pad + a : a);
var result = pad ? pad + a : a;
if (pad && a.toString()[0] === '-') {
result = '-' + pad + a.toString().slice(1);
}
res = result.toString();
}
// add result to the array
arr.push(res);
// increment or decrement
if (isNegative) {

@@ -93,10 +128,30 @@ a -= num;

return expand ? [arr.join('')] : arr;
return expand ? join(arr, sep) : arr;
}
function isPadded(strA, strB) {
if ((!/[^.]\./.test(strA) || !/[^.]\./.test(strB)) && /^-*0+[1-9]/.test(strA)) {
/**
* Join `arr` with the given `sep`
*/
function join(arr, sep) {
var res = arr.join(sep);
if (sep === '|') {
res = '(' + res + ')';
}
return [res];
}
/**
* Test for padding. Returns the actual padding string
* or `false` if no padding.
*
* @param {*} `origA` String or number.
* @return {String|Boolean}
*/
function isPadded(origA) {
if (/[^.]\.|^-*0+[1-9]/.exec(origA)) {
return function (a) {
return repeat('0', strA.length - a.toString().length);
}
return repeat('0', origA.toString().length - a.toString().length);
};
}

@@ -106,2 +161,6 @@ return false;

/**
* Handle errors
*/
function validateRange(a, b, step) {

@@ -111,11 +170,9 @@ if (!/[\w\d]/.test(a) || !/[\w\d]/.test(b)) {

}
if (!isNumber(a) && isNumber(b)) {
var isNumA = isNumber(a), isNumB = isNumber(b);
if ((!isNumA && isNumB) || (isNumA && !isNumB)) {
throw new TypeError('fill-range: first range argument is incompatible with second.');
}
if (isNumber(a) && !isNumber(b)) {
throw new TypeError('fill-range: first range argument is incompatible with second.');
}
if (step && (!isNumber(step) && !/>/.test(step))) {
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": "1.1.0",
"version": "1.2.0",
"homepage": "https://github.com/jonschlinkert/fill-range",

@@ -6,0 +6,0 @@ "author": {

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

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

@@ -155,2 +155,2 @@ }

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

Product

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

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc