randexp
Advanced tools
Comparing version 0.3.2 to 0.3.3
@@ -1,28 +0,56 @@ | ||
var ret = require('ret') | ||
, types = ret.types | ||
; | ||
var ret = require('ret'); | ||
var types = ret.types; | ||
// returns random number in the rane [a, b] | ||
var randInt = function(a, b) { | ||
/** | ||
* Returns random number in the range [a, b]. | ||
* | ||
* @param {Number} a | ||
* @param {Number} b | ||
* @return {Number} | ||
*/ | ||
function randInt(a, b) { | ||
return a + Math.floor(Math.random() * (1 + b - a)); | ||
}; | ||
} | ||
// if code is alphabetic, converts to other case | ||
// if not alphabetic, returns back code | ||
var toOtherCase = function(code) { | ||
/** | ||
* If code is alphabetic, converts to other case. | ||
* If not alphabetic, returns back code. | ||
* | ||
* @param {Number} code | ||
* @return {Number} | ||
*/ | ||
function toOtherCase(code) { | ||
return code + (97 <= code && code <= 122 ? -32 : | ||
65 <= code && code <= 90 ? 32 : 0); | ||
}; | ||
} | ||
// returns subset of [a, b] if [from, to] is in it | ||
var range = function(a, b, from, to) { | ||
/** | ||
* Returns subset of [a, b] if [from, to] is in it. | ||
* | ||
* @param {Number} a | ||
* @param {Number} b | ||
* @param {Number} from | ||
* @param {Number} to | ||
* @return {Object|Boolean} | ||
* {Number} from | ||
* {Number} to | ||
*/ | ||
function range(a, b, from, to) { | ||
return a <= from && from <= b ? { from: from, to: Math.min(b, to) } : | ||
a <= to && to <= b ? { from: Math.max(a, from), to: to } : | ||
false; | ||
}; | ||
} | ||
// returns true if all properties of a are equal to b | ||
// a and b are arrays of objects | ||
var deepEqual = function(a, b) { | ||
/** | ||
* Returns true if all properties of a are equal to b. | ||
* a and b are arrays of objects. | ||
* | ||
* @param {Number} a | ||
* @param {Number} b | ||
*/ | ||
function deepEqual(a, b) { | ||
var i, l, key, obj; | ||
@@ -41,7 +69,14 @@ if ((l = a.length) !== b.length) return false; | ||
return true; | ||
}; | ||
} | ||
// returns true if negated needle set is inside of sets array | ||
// using deepEqual as comparator | ||
var findNotSet = function(sets, needle) { | ||
/** | ||
* Returns true if negated needle set is inside of sets array. | ||
* Using deepEqual as comparator. | ||
* | ||
* @param {Array.<Object>} sets | ||
* @param {String} needle | ||
* @param {Boolean} | ||
*/ | ||
function findNotSet(sets, needle) { | ||
for (var i = 0, l = sets.length; i < l; i++) { | ||
@@ -55,12 +90,19 @@ var cset = sets[i]; | ||
return false; | ||
}; | ||
} | ||
// returns true if character is in class set | ||
var inClass = function(set, code, ignoreCase) { | ||
var token | ||
, v | ||
, sets = [] | ||
, infLoop = false | ||
; | ||
/** | ||
* Returns true if character is in class set. | ||
* | ||
* @param {Object} set | ||
* @param {Number} code | ||
* @param {Boolean} ignoreCase | ||
* @return {Boolean} | ||
*/ | ||
function inClass(set, code, ignoreCase) { | ||
var token; | ||
var v; | ||
var sets = []; | ||
var infLoop = false; | ||
for (var i = 0, l = set.length; i < l; i++) { | ||
@@ -78,4 +120,4 @@ token = set[i]; | ||
case types.RANGE: | ||
// if ignoreCase is on, and alphabetic character ranges fall | ||
// inside this range, check against both cases | ||
// If ignoreCase is on, and alphabetic character ranges fall | ||
// inside this range, check against both cases. | ||
if (token.from <= code && code <= token.to || (ignoreCase && | ||
@@ -91,4 +133,4 @@ (((v = range(97, 122, token.from, token.to)) !== false && | ||
case types.SET: | ||
// use these to detect an infinite loop with 2 sets | ||
// that cancel out each other | ||
// Use these to detect an infinite loop with 2 sets | ||
// that cancel out each other. | ||
if (sets.length > 0 && findNotSet(sets, token)) { | ||
@@ -108,14 +150,28 @@ infLoop = true; | ||
return false; | ||
}; | ||
} | ||
// determines if a character code is alphabetic and decide | ||
// to switch case randomly | ||
var char = function(code, ignoreCase) { | ||
/** | ||
* Determines if a character code is alphabetic and decide | ||
* to switch case randomly. | ||
* | ||
* @param {Number} code | ||
* @param {Boolean} ignoreCase | ||
* @return {String} | ||
*/ | ||
function char(code, ignoreCase) { | ||
return String.fromCharCode(ignoreCase && Math.random() > 0.5 ? | ||
toOtherCase(code) : code); | ||
}; | ||
} | ||
// generate random string modeled after given tokens | ||
var gen = function(token, groups, negate) { | ||
/** | ||
* Generate random string modeled after given tokens. | ||
* | ||
* @param {Object} token | ||
* @param {Array.<String>} groups | ||
* @param {Boolean} negate | ||
* @return {String} | ||
*/ | ||
function gen(token, groups, negate) { | ||
var groupNumber, stack, str, n, i, l, not; | ||
@@ -130,3 +186,3 @@ | ||
// insert placeholder until group string is generated | ||
// Insert placeholder until group string is generated. | ||
if (token.remember) { | ||
@@ -152,3 +208,3 @@ groupNumber = groups.push(false) - 1; | ||
case types.POSITION: | ||
// do nothing for now | ||
// Do nothing for now. | ||
return ''; | ||
@@ -159,5 +215,5 @@ | ||
// if this class is an except class i.e. [^abc] | ||
// If this class is an except class, i.e. [^abc], | ||
// generate a random character until one that isnt in this class | ||
// is found | ||
// is found. | ||
negate = !!negate; | ||
@@ -167,5 +223,4 @@ not = negate !== token.not; | ||
while (true) { | ||
var c = this.anyRandChar() | ||
, code = c.charCodeAt(0) | ||
; | ||
var c = this.anyRandChar(); | ||
var code = c.charCodeAt(0); | ||
@@ -176,7 +231,11 @@ if (inClass(token.set, code, this.ignoreCase)) { continue; } | ||
// otherwise, pick a random token in the class set | ||
// Otherwise, pick a random token in the class set. | ||
} else { | ||
return gen.call(this, | ||
token.set[Math.floor(Math.random() * token.set.length)], | ||
groups, not); | ||
if (token.set.length) { | ||
return gen.call(this, | ||
token.set[Math.floor(Math.random() * token.set.length)], | ||
groups, not); | ||
} else { | ||
return ''; | ||
} | ||
} | ||
@@ -191,3 +250,3 @@ break; | ||
case types.REPETITION: | ||
// randomly generate number between min and max | ||
// Randomly generate number between min and max. | ||
n = randInt(token.min, | ||
@@ -211,7 +270,10 @@ token.max === Infinity ? token.min + this.max : token.max); | ||
} | ||
}; | ||
} | ||
// constructor | ||
// takes either a regexp or a string with modifiers | ||
/** | ||
* @constructor | ||
* @param {RegExp|String} regexp | ||
* @param {String} m | ||
*/ | ||
var RandExp = module.exports = function(regexp, m) { | ||
@@ -240,9 +302,9 @@ if (regexp instanceof RegExp) { | ||
// when a repetitional token has its max set to Infinite, | ||
// When a repetitional token has its max set to Infinite, | ||
// randexp won't actually generate a random amount between min and Infinite | ||
// instead it will see Infinite as min + 100 | ||
// instead it will see Infinite as min + 100. | ||
RandExp.prototype.max = 100; | ||
// returns any random character | ||
// Returns any random character. | ||
RandExp.prototype.anyRandChar = function() { | ||
@@ -253,3 +315,3 @@ return String.fromCharCode(randInt(0, 65535)); | ||
// generates the random string | ||
// Generates the random string. | ||
RandExp.prototype.gen = function() { | ||
@@ -260,4 +322,4 @@ return gen.call(this, this.tokens, []); | ||
// enables use of randexp with a shorter calls | ||
// saves the RandExp object into the regex | ||
// Enables use of randexp with a shorter call. | ||
// Saves the RandExp object into the regex. | ||
var randexp = RandExp.randexp = function(regexp, m) { | ||
@@ -283,3 +345,3 @@ var randexp; | ||
// this enables sugary /regexp/.gen syntax | ||
// This enables sugary /regexp/.gen syntax. | ||
RandExp.sugar = function() { | ||
@@ -286,0 +348,0 @@ RegExp.prototype.gen = function() { |
@@ -5,3 +5,3 @@ { | ||
"keywords": ["regex", "regexp", "regular expression", "random"], | ||
"version": "0.3.2", | ||
"version": "0.3.3", | ||
"homepage": "http://fent.github.io/randexp.js/", | ||
@@ -8,0 +8,0 @@ "repository": { |
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
25601
7
278
0