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

rangegen

Package Overview
Dependencies
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

rangegen - npm Package Compare versions

Comparing version 0.1.1 to 0.2.0

example2.js

133

index.js

@@ -8,34 +8,38 @@ /*

var RangeGen = function (from, to, step, error) {
var range = [];
step = ~~(isNaN(step)||1>step?1:step);
if (!(/^([a-z]+|[0-9]+)$/i.test(from)) || !(/^([a-z]+|[0-9]+)$/i.test(to))) {
return RangeGen.handleError({name:"InvalidInput",message:"\"from\" and \"to\" must be letters or numbers only!"},error);
};
if (isNaN(from) || isNaN(to)) {
if (isNaN(from)) {
var lcase = (from==from.toUpperCase());
from = RangeGen.dec(from);
} else {
return RangeGen.handleError({name:"InvalidFrom",message:"\"from\" must be a letter!"},error);
};
if (isNaN(to)) {
to = RangeGen.dec(to);
} else {
return RangeGen.handleError({name:"InvalidTo",message:"\"to\" must be a letter!"},error);
};
};
var from = Number(from),
to = Number(to),
direction = !(from>to),
incr = (direction?step:-step),
start = (direction?from:to),
end = (direction?to:from),
loops = ~~((end-start)/step+2);
while (--loops) {
range.push((lcase==undefined?from:RangeGen.enc(from,lcase)));
from += incr;
};
return range;
var range = [];
var invalid = RangeGen.validate(to,from);
if (invalid) {
return RangeGen.handleError(invalid,error);
};
var calc = RangeGen.calculate(from,to,RangeGen.getStep(step),2);
while (--calc["loops"]) {
range.push(RangeGen.enc(calc["from"],calc["lcase"]));
calc["from"] += calc["incr"];
};
return range;
};
RangeGen.validate = function (to, from) {
if ((/^([a-z]+|-?[0-9]+)$/i.test(from) && /^([a-z]+|-?[0-9]+)$/i.test(to))) {
if (isNaN(from) || isNaN(to)) {
if (!isNaN(from)) {
return RangeGen.Errors("InvalidFrom");
};
if (!isNaN(to)) {
return RangeGen.Errors("InvalidTo");
};
};
return false;
}
return RangeGen.Errors("InvalidInput");
};
RangeGen.getNum = function (input) {
if (!isNaN(input)) {
return Number(input);
};
return Number(RangeGen.dec(input));
};
RangeGen.enc = function (num,lcase) {
if (lcase==null) {
return num;
};
var str = "";

@@ -51,9 +55,60 @@ while (num >= 0) {

for (var i = 0, l = str.length; i != l; ++i) {
num = num*26+(str.charCodeAt(i)|0x20)-96;
num = num*26+(str.charCodeAt(i)|0x20)-96;
};
return num-1;
};
RangeGen.handleError = function (obj,error) {
RangeGen.getStep = function (step) {
return ~~(isNaN(step)||1>step?1:step);
};
RangeGen.calculate = function (from, to, step, ext) {
var lcase = (isNaN(from)?(from==from.toUpperCase()):null),
from = RangeGen.getNum(from),
to = RangeGen.getNum(to),
direction = !(from>to),
start = (direction?from:to),
end = (direction?to:from);
return {
lcase: lcase,
from: from,
incr: (direction?step:-step),
loops: ~~((end-start)/step+ext),
};
};
RangeGen.iterator = function (from, to, step, error) {
var invalid = RangeGen.validate(to,from);
if (invalid) {
return RangeGen.handleError(invalid,error,true);
};
var proto = {
__init: function (from, to, step) {
var calc = RangeGen.calculate(from,to,step,1),
self = this;
Object.keys(calc).map(function(key){
self[key] = calc[key];
});
this.length = this.loops;
return this;
},
hasNext: function () {
return (this.loops>0?true:false);
},
next: function () {
if (this.hasNext()) {
var str = RangeGen.enc(this.from,this.lcase);
this.from += this.incr;
--this.loops;
return str;
} else {
return RangeGen.handleError(RangeGen.Errors("NoSuchElement"),error,true);
}
},
};
return (function(){
return Object.create(proto);
})().__init(from, to, RangeGen.getStep(step));
}
RangeGen.iter = RangeGen.iterator;
RangeGen.handleError = function (obj,error,boolean) {
if (!error) {
return [];
return (!boolean?[]:false);
} else {

@@ -63,2 +118,12 @@ throw new RangeGen.Error(obj);

};
RangeGen.Errors = function (name) {
var Errors = {
"InvalidInput": {name:"InvalidInput",message:"\"from\" and \"to\" must be letters or numbers only!"},
"InvalidFrom": {name:"InvalidFrom",message:"\"from\" must be a letter!"},
"InvalidTo": {name:"InvalidTo",message:"\"to\" must be a letter!"},
"NoSuchElement": {name:"NoSuchElement",message:"No more elements left in the iterator!"},
"Unknown": {name:"UnknownError",message:"An unknown error has occurred!"}
};
return (Errors[name]?Errors[name]:Errors["Unknown"]);
};
RangeGen.Error = function (obj) {

@@ -68,3 +133,3 @@ obj = obj||{};

this.message = obj.message||"An unknown error has occurred!";
}
};
RangeGen.Error.prototype = new Error();

@@ -71,0 +136,0 @@ RangeGen.Error.prototype.constructor = RangeGen.Error;

{
"name": "rangegen",
"version": "0.1.1",
"version": "0.2.0",
"homepage": "https://github.com/LouisT/RangeGen",

@@ -25,3 +25,4 @@ "author": {

"increment",
"decrement"
"decrement",
"iterator"
],

@@ -28,0 +29,0 @@ "description": "Generate a range between two numbers or letters. Examples: 1-100, a-z, A-Z, a-zz or even A-ZZZZZ.",

@@ -1,2 +0,2 @@

RangeGen (v0.1.1)
RangeGen (v0.2.0)
======

@@ -20,11 +20,21 @@

RangeGen(<from>,<to>,[<step>[,<exceptions>]]);
From - The letter or number to start the range at. (numbers/letters)
To - The letter or number to end on/near. (numbers/letters)
Step* - The amount to increment or decrement by. Default, 1. (true/false, number)
exceptions* - Throw error messages. Default, return an empty array. (true/false);
-- Array generator (See "examples.js" for usage) --
var array = RangeGen(<from>,<to>,[<step>[,<exceptions>]]);
* Optional, see "Examples" for usage.
From - The letter or number to start the range at. (numbers/letters)
To - The letter or number to end on/near. (numbers/letters)
Step* - The amount to increment or decrement by. Default, 1. (true/false, number)
exceptions* - Throw error messages. Default, return an empty array. (true/false);
* Optional.
-- Iterators (See "examples2.js" for usage) --
var iterator = RangeGen.iterator(<from>,<to>[,<step>[,<exceptions>]]);
RangeGen.iter(<from>,<to>[,<step>[,<exceptions>]])
hasNext() - Returns true if the iteration has more elements.
next() - Returns the next element in the iteration, or false if `exceptions` is not set.
Otherwise throws a `NoSuchElement` exception.
length - The total number of iterations.
Examples:

@@ -77,3 +87,48 @@ -------

```
```javascript
var RangeGen = require('rangegen');
try {
var iterator = RangeGen.iter(-30,30,1,true);
console.log("Iterations: "+iterator.length);
var range = [];
while (iterator.hasNext()) {
range.push(iterator.next());
};
console.log(range.join(','));
} catch (e) {
console.log(e);
};
console.log("\n--- Throw errors! ---");
try {
console.log("- Invalid next() call. -");
var iterator = RangeGen.iterator(-30,30,1,true);
console.log("Iterations: "+iterator.length);
var range = [];
while (iterator.hasNext()) {
range.push(iterator.next());
};
iterator.next();
} catch (e) {
console.log(e);
};
try {
console.log("\n- Inalid range. [a-30] -");
var iterator = RangeGen.iterator("a",30,1,true);
} catch (e) {
console.log(e);
};
console.log("\n--- Return false! ---");
try {
var iterator = RangeGen.iterator("a",30,1);
if (!iterator) {
console.log("`iterator` is false!");
}
} catch (e) {
console.log(e);
};
```
```html
<html>
<head>
<title>RangeGen test</title>
<script src="./rangegen.js"></script>

@@ -97,3 +152,22 @@ <script>

};
window.onload = function () {
try {
var iterator = RangeGen.iter(-30,30,5,true);
alert("Iterations: "+iterator.length);
var range = [];
while (iterator.hasNext()) {
range.push(iterator.next());
};
document.getElementById('iter').innerHTML = range.join(',');
} catch (e) {
document.getElementById('iter').innerHTML = e;
};
};
</script>
</head>
<body>
<div id="iter">
</div>
</body>
</html>
```
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