Skip to content

Commit

Permalink
just.dataGenerator
Browse files Browse the repository at this point in the history
  • Loading branch information
kopipejst committed Jun 30, 2014
1 parent 37a9e1c commit 95c3570
Show file tree
Hide file tree
Showing 4 changed files with 184 additions and 157 deletions.
24 changes: 12 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ Collection of useful JS functions
just.range(`start`, `end`, `step`)
------------------------------
Create array containing arithmetic progressions.
If the step argument is omitted, it defaults to 1.
If the start argument is omitted, it defaults to 0.
If the step argument is omitted, it defaults to 1.
If the start argument is omitted, it defaults to 0.
In case of invalid argument empty array will be returned.

### example
just.range(4,15,2);
### returns
### example

just.dataGenerator.range(4,15,2);

### returns

[4,6,8,10,12,14]


Expand All @@ -25,9 +25,9 @@ just.rrange(`valuesRange`, `lengthRange`)

### example

just.rrange([10, 150], [5,10]);
just.dataGenerator.rrange([10, 150], [5,10]);

### returns
### returns

array of 5-10 elements where each value is between 10-150
eg. [12, 99, 29, 136, 71, 100]
Expand All @@ -45,7 +45,7 @@ just.orange(`template`, `length`)
name: ['ten ','one','two']
}

just.orange(template, 3);
just.dataGenerator.orange(template, 3);

### returns

Expand All @@ -59,7 +59,7 @@ just.arrayShuffle(`arr`)

### example

just.arrayShuffle([5, 10, 12]);
just.dataGenerator.arrayShuffle([5, 10, 12]);

### returns

Expand Down
153 changes: 153 additions & 0 deletions just.datagenerator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
/**
* Define namespace for just
* @author Ivan Lazarevic (workshop.rs)
* @var just
*/
var just = just || {};


just.dataGenerator = {


/**
* Create array containing arithmetic progressions.
* If the step argument is omitted, it defaults to 1.
* If the start argument is omitted, it defaults to 0.
* In case of invalid argument empty array will be returned.
*
* @param integer start
* @param integer end
* @param integer step
* @return array
*/
range: function(start, end, step) {

step = step ? parseInt(step) : 1;
start = parseInt(start) || 0;
end = parseInt(end) || 0;

var range = [],
current = start;

if (arguments.length == 1) {
end = start;
start = 0;
current = 0;
}

if (start <= end) {
if (step < 0) {
return range;
}
while (current <= end) {
range.push(current);
current += step;
}
} else {
if (step > 0) {
return range;
}
while (current > end) {
range.push(current);
current += step;
}
}

return range;
},

/**
* Create array of random length withing given scope
* with random integer values withing given scope
* values of params can be number or array
* in other case default will be [0,10] for both params
*
* @example just.rrange([4,10],[12,15]);
* @param array values
* @param array length
* @return array
*/
rrange: function(valuesRange, lengthRange) {

var valuesRangeDefault = !isNaN(valuesRange) ? [0, valuesRange] : [0, 10];
var lengthRangeDefault = !isNaN(lengthRange) ? [0, lengthRange] : [0, 10];

valuesRange = valuesRange.constructor === Array ? valuesRange : valuesRangeDefault;
lengthRange = lengthRange.constructor === Array ? lengthRange : lengthRangeDefault;

var len = parseInt(lengthRange[0] + Math.random() * (lengthRange[1] - lengthRange[0])); // length of array
var range = [];

for (var i = 0; i < len; i++) {
range.push(parseInt(valuesRange[0] + Math.random() * (valuesRange[1] - valuesRange[0])));
}

return range;

},

/**
* Create array of objects based on provided template
*
* @example just.orange( { value: [1,4,5], title: ['title1','title2','title3'] }, 5);
* @param object template
* @param integer length
* @return array
*/
orange: function(template, length) {
var range = [];

length = parseInt(length) || 10;

for (var i = 0; i < length; i++) {
var temp = {};
for (var prop in template) {
if (template.hasOwnProperty(prop)) {
var tempValue = template[prop];
/**
* If element is array we'll take elements by order
* in case that array have less elements than number of elements we want
* we'll use last one from array
* @TODO in case that array have less elements it should be reset
*/
if (tempValue.constructor === Array) {
var position = i < tempValue.length - 1 ? i : tempValue.length - 1;
tempValue = tempValue[position];
}

temp[prop] = tempValue;
}
}
range.push(temp);
}

return range;
},

/**
* Array shuffle with Fisher-Yates algorithm
*
* @param array arr
* @return array
*/
arrayShuffle: function(arr) {
var i = arr.length;
if (i === 0) {
return false;
}
while (--i) {
var j = Math.floor(Math.random() * (i + 1));
var tempi = arr[i];
var tempj = arr[j];
arr[i] = tempj;
arr[j] = tempi;
}

return arr;
}

};

if (typeof module !== 'undefined' && module.exports) {
module.exports = just.dataGenerator;
}
145 changes: 0 additions & 145 deletions just.js

This file was deleted.

19 changes: 19 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"name": "just.datagenerator",
"version": "0.1.1",
"homepage": "http://workshop.rs/2012/04/just-javascript-useful-stuff/",
"keywords": [
"strings",
"keys",
"random",
"passwords"
],
"description": "generate fake data ...",
"main": "just.datagenerator.js",
"repository": {
"type": "git",
"url": "git@github.com:kopipejst/just.datagenerator.git"
},
"author": "Ivan Lazarevic",
"license": "MIT"
}

0 comments on commit 95c3570

Please sign in to comment.