/lib/mock.js

https://github.com/jgallen23/mockdata · JavaScript · 178 lines · 150 code · 24 blank · 4 comment · 32 complexity · f79691e7edce2e221b4b5ea0d3a1a098 MD5 · raw file

  1. var data = require('./data');
  2. var mock = {
  3. real:false,
  4. words: data.words.split(","),
  5. wurds: [],
  6. names: data.names.split(","),
  7. letters: data.letters,
  8. sites: data.sites.split(","),
  9. toArray: function(thing){
  10. if (typeof(thing) == "object" && !(!!thing.push || !!thing.item)) {
  11. var a = [];
  12. for(var nm in thing){ a.push(thing[nm]); }
  13. thing = a;
  14. } else if (typeof(thing) == "string"){
  15. if (/\./.test(thing)) {
  16. thing = thing.split(".");
  17. thing.pop();
  18. var i = thing.length; while(i--) {
  19. thing[i] = this.trim(thing[i]) + ".";
  20. }
  21. } else if (/,/.test(thing)) {
  22. thing = thing.split(",");
  23. } else if (/\s/.test(thing)) {
  24. thing = thing.split(" ");
  25. } else {
  26. thing = thing.split("");
  27. }
  28. }
  29. return thing; //Array
  30. },
  31. trim: function(s){ // thanks to Dojo:
  32. return String.prototype.trim ? s.trim() :
  33. s.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
  34. },
  35. pad: function(n, amt, chr){
  36. var c = chr || "0"; amt = amt || 2;
  37. return (c+c+c+c+c+c+c+c+c+c+n).slice(-amt);
  38. },
  39. cap: function(w){
  40. return w.charAt(0).toUpperCase() + w.substring(1);
  41. },
  42. weight: function(n, exp){
  43. var rev = exp < 0;
  44. exp = exp===undefined ? 1 : Math.abs(exp)+1;
  45. var res = Math.pow(n, exp);
  46. return rev ? 1 - res : res;
  47. },
  48. n: function(n, w){
  49. return Math.floor((n || 10) * this.weight(Math.random(), w));
  50. },
  51. range: function(min, max, w){
  52. max = max || 0;
  53. return this.n(Math.abs(max-min)+1, w) + (min<max?min:max);
  54. },
  55. element: function(thing, w){
  56. // return slot, char, prop or range
  57. if(typeof(thing) == "number") return this.n(thing, w);
  58. thing = this.toArray(thing);
  59. return thing[this.n(thing.length, w)];
  60. },
  61. scramble: function(ary){
  62. var a = ary.concat([]), sd = [];
  63. var i = a.length; while(i--){
  64. sd.push(a.splice(this.n(a.length), 1)[0]);
  65. }
  66. return sd;
  67. },
  68. bignumber: function(len){
  69. var t="";
  70. while(len--){
  71. t += this.n(9);
  72. }
  73. return t;
  74. },
  75. date: function(o){
  76. o = o || {};
  77. var d1 = new Date(o.min || new Date());
  78. var d2 = new Date(o.max || new Date().setFullYear(d1.getFullYear()+(o.yearRange||1))).getTime();
  79. d1 = d1.getTime();
  80. var d = new Date(this.range(d1,d2,o.weight));
  81. if(o.seconds){
  82. return d.getTime();
  83. }else if(o.delimiter){
  84. return this.pad(d.getMonth()+1)+o.delimiter+this.pad(d.getDate()+1)+o.delimiter+(d.getFullYear());
  85. }
  86. return d;
  87. },
  88. bool: function(w){
  89. return this.n(2, w) < 1;
  90. },
  91. color: function(w){
  92. return "#"+this.pad(this.n(255, w).toString(16))+this.pad(this.n(255, w).toString(16))+this.pad(this.n(255, w).toString(16));
  93. },
  94. chars:function(min, max, w){
  95. var s = "", i = this.range(min, max, w); while(i--){
  96. s += this.letters[this.n(this.letters.length)];
  97. }
  98. return s;
  99. },
  100. name: function(cse){
  101. // cse: 0 title case, 1 lowercase, 2 upper case
  102. var s = this.names[this.n(this.names.length)];
  103. return !cse ? s : cse==1 ? s.toLowerCase() : s.toUpperCase();
  104. },
  105. site: function(cse){
  106. // cse: 0 title case, 1 lowercase, 2 upper case
  107. var s = this.sites[this.n(this.sites.length)];
  108. return !cse ? s : cse==1 ? s.toLowerCase() : s.toUpperCase();
  109. },
  110. url: function(usewww, xt){
  111. var w = usewww ? "www." : "";
  112. xt = xt || ".com";
  113. return "http://" + w + this.site(1) + xt;
  114. },
  115. word: function(){
  116. var w = this.real ? this.words : this.wurds;
  117. return w[this.n(w.length)];
  118. },
  119. sentences: function(minAmt, maxAmt, minLen, maxLen){
  120. // amt: sentences, len: words
  121. minAmt = minAmt || 1;
  122. maxAmt = maxAmt || minAmt;
  123. minLen = minLen || 5;
  124. maxLen = maxLen || minLen;
  125. var t = "";
  126. var w = this.real ? this.words : this.wurds;
  127. var i = this.range(minAmt, maxAmt); while(i--){
  128. var s = [];
  129. var ii = this.range(minLen, maxLen); while(ii--){
  130. s.push(w[this.n(w.length)]);
  131. }
  132. t += this.cap(s.join(" ")) +". ";
  133. }
  134. return t;
  135. },
  136. title: function(min, max){
  137. min = min || 1; max = max || min; var a=[];
  138. var w = this.real ? this.words : this.wurds;
  139. var i = this.range(min, max); while(i--){
  140. a.push(this.cap(w[this.n(w.length)]));
  141. }
  142. return a.join(" ");
  143. },
  144. image: function(minW, maxW, minH, maxH) {
  145. var w = this.range(minW, maxW);
  146. var h = this.range(minH, maxH);
  147. return "http://dummyimage.com/"+w+"x"+h+"/000/fff";
  148. },
  149. randomItem: function(arr) {
  150. return arr[this.n(arr.length)];
  151. }
  152. };
  153. mock.wurds = data.words.replace(/a|e|i|o|u/g, function(c){ return ("aeiou")[mock.n(5)]; }).split(",");
  154. module.exports = mock;