Comparing version 0.2.3 to 0.2.6
@@ -11,3 +11,3 @@ var RangeGen = require('../'); | ||
console.log("[ZZ-A,10] "+d.join(",")+"\n"); | ||
var e = RangeGen(0,100,10); | ||
var e = RangeGen(0,100,10,1); | ||
console.log("[0-100,10] "+e.join(",")+"\n"); | ||
@@ -14,0 +14,0 @@ var f = RangeGen(100,0,10); |
var range = require('../'); | ||
try { | ||
console.log("--- As a callback ---"); | ||
range(1,10,3,null,function (x) { x.forEach(function (x) { console.log(x); }); }); | ||
range(1,10,3,null,function (x) { console.log(x.join(',')) }); | ||
console.log("\n--- As a filter ---"); | ||
console.log(range(1,100,3,null,function (x) { return x.filter(function(y) { return y%4===0; }); }).join(',')); | ||
console.log("\n-- inRange examples --"); | ||
console.log("[c in a..z] "+range.inRange('c','a','z')); | ||
console.log("[aa in a..z] "+range.inRange('aa','a','z')); | ||
console.log("[ab in a..az] "+range.inRange('ab','a','az')); | ||
console.log("[ab in a..zz, 5] "+range.inRange('ab','a','az',5)); | ||
console.log("[cjsr in a..zzzzzzz] "+range.inRange('cjsr','a','zzzzzzz')); | ||
console.log("[zzzzzzz in a..zzzzz] "+range.inRange('zzzzzzz','a','zzzzz')); | ||
console.log("\n-- byIndex examples --"); | ||
console.log("[10 in a..z] "+range.byIndex(10,"a","z")); | ||
console.log("[100 in a..zz] "+range.byIndex(100,"a","zz")); | ||
console.log("[26 in a..z] "+range.byIndex(26,"a","z")); | ||
console.log("\n-- byValue examples --"); | ||
console.log("[g in a..z] "+range.byValue("g","a","z")); | ||
console.log("[g in a..z, 2] "+range.byValue("g","a","z",2)); | ||
console.log("[g in a..z, 5] "+range.byValue("g","a","z",5)); | ||
console.log("\n-- String prototype examples --"); | ||
range.addPro(); // Inject range(); | ||
console.log("[\"a..z\".range()] "+"a..z".range()); | ||
console.log("[\"z..a\".range()] "+"z..a".range()); | ||
console.log("[\"a..zz\".range(40)] "+"a..zz".range(40)); | ||
console.log("[\"0..100\".range(5)] "+"0..100".range(5)); | ||
console.log("[\"-30..30\".range(5)] "+"-30..30".range(5)); | ||
} catch (e) { | ||
console.log(e); | ||
}; |
81
index.js
@@ -9,3 +9,3 @@ /* | ||
var range = [], | ||
invalid = RangeGen.validate(to,from), | ||
invalid = RangeGen.validate(from,to), | ||
cb = cb||function(x){return x}; | ||
@@ -19,13 +19,13 @@ if (invalid) { | ||
calc["from"] += calc["incr"]; | ||
if (calc["from"] < 0) { | ||
break; | ||
}; | ||
}; | ||
// Return an error if no range is made. | ||
// This should NOT happen, but error if it does! | ||
return cb(!!(range.length)?range:RangeGen.handleError(RangeGen.Errors("NotGenerated"),error)); | ||
return cb((!!(range.length)?range:RangeGen.handleError(RangeGen.Errors("NotGenerated"),error))); | ||
}; | ||
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)) { | ||
RangeGen.validate = function (from, to, str) { | ||
var isValid = function (str) { | ||
return (typeof(str) !== "undefined" && str !== null && /^[a-z]+$|^[+-]?(\d*\.)?\d+$/i.test(str)); | ||
}; | ||
if ((isValid(from) && isValid(to) && (!str || isValid(str)))) { | ||
if ((isNaN(from) || isNaN(to)) || (str && isNaN(str))) { | ||
if (!isNaN(from)) { | ||
@@ -37,2 +37,5 @@ return RangeGen.Errors("InvalidFrom"); | ||
}; | ||
if (str && !isNaN(str)) { | ||
return RangeGen.Errors("InvalidStr"); | ||
}; | ||
}; | ||
@@ -49,3 +52,28 @@ return false; | ||
}; | ||
RangeGen.enc = function (num,lcase) { | ||
RangeGen.byIndex = function (num, from, to, step, error, cb) { | ||
var invalid = RangeGen.validate(from,to), | ||
cb = cb||function(x){return x}; | ||
if (invalid) { | ||
return cb(RangeGen.handleError(invalid,error)); | ||
}; | ||
var calc = RangeGen.calculate(from,to,step,2), | ||
added = calc["from"]+(calc["incr"]*num), | ||
ret = (added<=calc["to"]?RangeGen.enc(added,calc["lcase"]):false); | ||
return cb((ret?ret:RangeGen.handleError(RangeGen.Errors("NotGenerated"),error,1))); | ||
}; | ||
RangeGen.byValue = function (str, from, to, step, error, cb) { | ||
var invalid = RangeGen.validate(from,to,str), | ||
cb = cb||function(x){return x}; | ||
if (invalid) { | ||
return cb(RangeGen.handleError(invalid,error,1)); | ||
}; | ||
var step = RangeGen.getStep(step), | ||
index = Math.floor(RangeGen.getNum(str)/step); | ||
return cb((RangeGen.byIndex(index, from, to, step, error) === str?index:false)); | ||
}; | ||
RangeGen.inRange = function (str, from, to, step, error, cb) { | ||
var cb = cb||function(x){return x}; | ||
return cb(!!RangeGen.byValue(str,from,to,step,error)); | ||
}; | ||
RangeGen.enc = function (num, lcase) { | ||
if (lcase == null) { | ||
@@ -82,2 +110,3 @@ return Number(num); | ||
from: from, | ||
to: to, | ||
incr: (direction?step:-step), | ||
@@ -87,4 +116,18 @@ loops: Math.floor((end-start)/step+ext), | ||
}; | ||
RangeGen.iterator = function (from, to, step, error) { | ||
var invalid = RangeGen.validate(to,from); | ||
RangeGen.addPro = RangeGen.addPrototype = function () { | ||
String.prototype.range = function (step, error, cb) { | ||
var res = this.match(/^([a-z]+|[+-]?(?:\d*\.)?\d+)\.{2}([a-z]+|[+-]?(?:\d*\.)?\d+)$/i), | ||
cb = cb||function(x){return x}; | ||
if (res) { | ||
var invalid = RangeGen.validate(res[1],res[2]); | ||
if (invalid) { | ||
return cb(RangeGen.handleError(invalid,error)); | ||
}; | ||
return RangeGen(res[1],res[2],step,error,cb); | ||
}; | ||
return cb(RangeGen.handleError(RangeGen.Errors("InvalidString"),error)); | ||
}; | ||
}; | ||
RangeGen.iter = RangeGen.iterator = function (from, to, step, error) { | ||
var invalid = RangeGen.validate(from,to); | ||
if (invalid) { | ||
@@ -94,3 +137,3 @@ return RangeGen.handleError(invalid,error,true); | ||
var proto = { | ||
__init: function (from, to, step) { | ||
__init: function (from, to, step, error) { | ||
var calc = RangeGen.calculate(from,to,step,1), | ||
@@ -101,2 +144,3 @@ self = this; | ||
}); | ||
this.error = error; | ||
this.length = this.left = this.loops; | ||
@@ -115,3 +159,3 @@ return this; | ||
} else { | ||
return RangeGen.handleError(RangeGen.Errors("NoSuchElement"),error,true); | ||
return RangeGen.handleError(RangeGen.Errors("NoSuchElement"),this.error,true); | ||
} | ||
@@ -122,6 +166,5 @@ }, | ||
return Object.create(proto); | ||
})().__init(from,to,step); | ||
} | ||
RangeGen.iter = RangeGen.iterator; | ||
RangeGen.handleError = function (obj,error,boolean) { | ||
})().__init(from,to,step,error); | ||
}; | ||
RangeGen.handleError = function (obj, error, boolean) { | ||
if (!error) { | ||
@@ -137,4 +180,6 @@ return (!boolean?[]:false); | ||
"InvalidInput": {name:"InvalidInput",message:"\"from\" and \"to\" must be letters or numbers only!"}, | ||
"InvalidString": {name:"InvalidString",message:"Invalid string. Must be in the form of \"FROM..TO\"!"}, | ||
"InvalidFrom": {name:"InvalidFrom",message:"\"from\" must be a letter!"}, | ||
"InvalidTo": {name:"InvalidTo",message:"\"to\" must be a letter!"}, | ||
"InvalidStr": {name:"InvalidStr",message:"\"str\" must be a letter!"}, | ||
"NoSuchElement": {name:"NoSuchElement",message:"No more elements left in the iterator!"}, | ||
@@ -153,2 +198,2 @@ "Unknown": {name:"UnknownError",message:"An unknown error has occurred!"} | ||
Setup(RangeGen); | ||
})((typeof exports!=='undefined'?function(fn){module.exports=fn;}:function(fn){this['RangeGen']=fn;})); | ||
})((typeof exports!=="undefined"?function(fn){module.exports=fn;}:function(fn){this["RangeGen"]=fn;})); |
{ | ||
"name": "rangegen", | ||
"version": "0.2.3", | ||
"version": "0.2.6", | ||
"homepage": "https://louist.github.io/RangeGen/", | ||
@@ -5,0 +5,0 @@ "author": { |
@@ -1,2 +0,2 @@ | ||
RangeGen (v0.2.3) | ||
RangeGen (v0.2.6) | ||
====== | ||
@@ -27,9 +27,55 @@ | ||
Step* - The amount to increment or decrement by. Default, 1. (Boolean, Number, Float) | ||
Exceptions* - Throw error messages. Default, return an empty array. (Boolean); | ||
Exceptions* - Throw error messages. Default, return an empty array. (Boolean) | ||
CB/filter* - Use a callback or filter results. (see example4.js) | ||
* Optional. | ||
-- Check to see if a number or letter is in a range. (Case sensitive, "AB" is not in "a..az". See "examples4.js" for usage) -- | ||
RangeGen.inRange(<str>,<from>,<to>[,<step>[,<exceptions>[,<callback>]]]); | ||
Str - The number(s) or letter(s) to validate. (Number, Float, Letters) | ||
From - The letter or number to start the range at. (Number, Float, Letters) | ||
To - The letter or number to end on/near. (Number, Float, Letters) | ||
Step* - The amount to increment or decrement by. Default, 1. (Boolean, Number, Float) | ||
Exceptions* - Throw error messages. Default, return false. (Boolean) | ||
Callback* - Use a callback instead of return. | ||
* Optional. | ||
-- Get the Nth value in a given range. (See "examples4.js" for usage) -- | ||
RangeGen.byIndex(<num>,<from>,<to>,[<step>[,<exceptions>[,<callback>]]]); | ||
Num - The index number within the range. (Number) | ||
From - The letter or number to start the range at. (Number, Float, Letters) | ||
To - The letter or number to end on/near. (Number, Float, Letters) | ||
Step* - The amount to increment or decrement by. Default, 1. (Boolean, Number, Float) | ||
Exceptions* - Throw error messages. Default, return false. (Boolean) | ||
Callback* - Use a callback instead of return. | ||
* Optional. | ||
-- Get the index by value in a given range. (See "examples4.js" for usage) -- | ||
RangeGen.byValue(<str>,<from>,<to>,[<step>[,<exceptions>[,<callback>]]]); | ||
Str - The value within the range. (Number, Float, Letters) | ||
From - The letter or number to start the range at. (Number, Float, Letters) | ||
To - The letter or number to end on/near. (Number, Float, Letters) | ||
Step* - The amount to increment or decrement by. Default, 1. (Boolean, Number, Float) | ||
Exceptions* - Throw error messages. Default, return false. (Boolean) | ||
Callback* - Use a callback instead of return. | ||
* Optional. | ||
-- Inject "range()" into the String prototype. (See "examples4.js" for usage) -- | ||
RangeGen.addPrototype(); | ||
RangeGen.addPro(); | ||
Usage: "FROM..TO".range([<step>[,<exceptions>[,<callback>]]]); | ||
From - The letter or number to start the range at. (Number, Float, Letters) | ||
To - The letter or number to end on/near. (Number, Float, Letters) | ||
Step* - The amount to increment or decrement by. Default, 1. (Boolean, Number, Float) | ||
Exceptions* - Throw error messages. Default, return false. (Boolean) | ||
Callback* - Use a callback instead of return. | ||
* Optional. | ||
-- Iterators (See "examples2.js" for usage) -- | ||
var iterator = RangeGen.iterator(<from>,<to>[,<step>[,<exceptions>]]); | ||
RangeGen.iter(<from>,<to>[,<step>[,<exceptions>]]) | ||
RangeGen.iter(<from>,<to>[,<step>[,<exceptions>]]); | ||
@@ -202,5 +248,27 @@ hasNext() - Returns true if the iteration has more elements. | ||
console.log("--- As a callback ---"); | ||
range(1,10,3,null,function (x) { x.forEach(function (x) { console.log(x); }); }); | ||
range(1,10,3,null,function (x) { console.log(x.join(',')) }); | ||
console.log("\n--- As a filter ---"); | ||
console.log(range(1,100,3,null,function (x) { return x.filter(function(y) { return y%4===0; }); }).join(',')); | ||
console.log("\n-- inRange examples --"); | ||
console.log("[c in a..z] "+range.inRange('c','a','z')); | ||
console.log("[aa in a..z] "+range.inRange('aa','a','z')); | ||
console.log("[ab in a..az] "+range.inRange('ab','a','az')); | ||
console.log("[ab in a..zz, 5] "+range.inRange('ab','a','az',5)); | ||
console.log("[cjsr in a..zzzzzzz] "+range.inRange('cjsr','a','zzzzzzz')); | ||
console.log("[zzzzzzz in a..zzzzz] "+range.inRange('zzzzzzz','a','zzzzz')); | ||
console.log("\n-- byIndex examples --"); | ||
console.log("[10 in a..z] "+range.byIndex(10,"a","z")); | ||
console.log("[100 in a..zz] "+range.byIndex(100,"a","zz")); | ||
console.log("[26 in a..z] "+range.byIndex(26,"a","z")); | ||
console.log("\n-- byValue examples --"); | ||
console.log("[g in a..z] "+range.byValue("g","a","z")); | ||
console.log("[g in a..z, 2] "+range.byValue("g","a","z",2)); | ||
console.log("[g in a..z, 5] "+range.byValue("g","a","z",5)); | ||
console.log("\n-- String prototype examples --"); | ||
range.addPro(); // Inject range(); | ||
console.log("[\"a..z\".range()] "+"a..z".range()); | ||
console.log("[\"z..a\".range()] "+"z..a".range()); | ||
console.log("[\"a..zz\".range(40)] "+"a..zz".range(40)); | ||
console.log("[\"0..100\".range(5)] "+"0..100".range(5)); | ||
console.log("[\"-30..30\".range()] "+"-30..30".range(5)); | ||
} catch (e) { | ||
@@ -210,3 +278,1 @@ console.log(e); | ||
``` | ||
[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/LouisT/rangegen/trend.png)](https://bitdeli.com/free "Bitdeli Badge") |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
26802
315
276