RandomJson.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.RandomJson = void 0;
  4. const defaultOpts = {
  5. rootNode: 'object',
  6. nodeCount: 32,
  7. odds: {
  8. null: 1,
  9. boolean: 2,
  10. number: 10,
  11. string: 8,
  12. binary: 0,
  13. array: 2,
  14. object: 2,
  15. },
  16. };
  17. const ascii = () => {
  18. return String.fromCharCode(Math.floor(32 + Math.random() * (126 - 32)));
  19. };
  20. const alphabet = [
  21. 'a',
  22. 'b',
  23. 'c',
  24. 'd',
  25. 'e',
  26. 'f',
  27. 'g',
  28. 'h',
  29. 'i',
  30. 'j',
  31. 'k',
  32. 'l',
  33. 'm',
  34. 'n',
  35. 'o',
  36. 'p',
  37. 'q',
  38. 'r',
  39. 's',
  40. 't',
  41. 'u',
  42. 'v',
  43. 'w',
  44. 'x',
  45. 'y',
  46. 'z',
  47. 'A',
  48. 'B',
  49. 'C',
  50. 'D',
  51. 'E',
  52. 'F',
  53. 'G',
  54. 'H',
  55. 'I',
  56. 'J',
  57. 'K',
  58. 'L',
  59. 'M',
  60. 'N',
  61. 'O',
  62. 'P',
  63. 'Q',
  64. 'R',
  65. 'S',
  66. 'T',
  67. 'U',
  68. 'V',
  69. 'W',
  70. 'X',
  71. 'Y',
  72. 'Z',
  73. '0',
  74. '1',
  75. '2',
  76. '3',
  77. '4',
  78. '5',
  79. '6',
  80. '7',
  81. '8',
  82. '9',
  83. '-',
  84. '_',
  85. '.',
  86. ',',
  87. ';',
  88. '!',
  89. '@',
  90. '#',
  91. '$',
  92. '%',
  93. '^',
  94. '&',
  95. '*',
  96. '\\',
  97. '/',
  98. '(',
  99. ')',
  100. '+',
  101. '=',
  102. '\n',
  103. '👍',
  104. '🏻',
  105. '😛',
  106. 'ä',
  107. 'ö',
  108. 'ü',
  109. 'ß',
  110. 'а',
  111. 'б',
  112. 'в',
  113. 'г',
  114. '诶',
  115. '必',
  116. '西',
  117. ];
  118. const utf16 = () => {
  119. return alphabet[Math.floor(Math.random() * alphabet.length)];
  120. };
  121. class RandomJson {
  122. static generate(opts) {
  123. const rnd = new RandomJson(opts);
  124. return rnd.create();
  125. }
  126. static genBoolean() {
  127. return Math.random() > 0.5;
  128. }
  129. static genNumber() {
  130. const num = Math.random() > 0.2
  131. ? Math.random() * 1e9
  132. : Math.random() < 0.2
  133. ? Math.round(0xff * (2 * Math.random() - 1))
  134. : Math.random() < 0.2
  135. ? Math.round(0xffff * (2 * Math.random() - 1))
  136. : Math.round(Number.MAX_SAFE_INTEGER * (2 * Math.random() - 1));
  137. if (num === -0)
  138. return 0;
  139. return num;
  140. }
  141. static genString(length = Math.ceil(Math.random() * 16)) {
  142. let str = '';
  143. if (Math.random() < 0.1)
  144. for (let i = 0; i < length; i++)
  145. str += utf16();
  146. else
  147. for (let i = 0; i < length; i++)
  148. str += ascii();
  149. if (str.length !== length)
  150. return ascii().repeat(length);
  151. return str;
  152. }
  153. static genBinary(length = Math.ceil(Math.random() * 16)) {
  154. const buf = new Uint8Array(length);
  155. for (let i = 0; i < length; i++)
  156. buf[i] = Math.floor(Math.random() * 256);
  157. return buf;
  158. }
  159. static genArray(options = { odds: defaultOpts.odds }) {
  160. return RandomJson.generate({
  161. nodeCount: 6,
  162. ...options,
  163. rootNode: 'array',
  164. });
  165. }
  166. static genObject(options = { odds: defaultOpts.odds }) {
  167. return RandomJson.generate({
  168. nodeCount: 6,
  169. ...options,
  170. rootNode: 'object',
  171. });
  172. }
  173. constructor(opts = {}) {
  174. this.containers = [];
  175. this.opts = { ...defaultOpts, ...opts };
  176. this.oddTotals = {};
  177. this.oddTotals.null = this.opts.odds.null;
  178. this.oddTotals.boolean = this.oddTotals.null + this.opts.odds.boolean;
  179. this.oddTotals.number = this.oddTotals.boolean + this.opts.odds.number;
  180. this.oddTotals.string = this.oddTotals.number + this.opts.odds.string;
  181. this.oddTotals.binary = this.oddTotals.string + this.opts.odds.binary;
  182. this.oddTotals.array = this.oddTotals.string + this.opts.odds.array;
  183. this.oddTotals.object = this.oddTotals.array + this.opts.odds.object;
  184. this.totalOdds =
  185. this.opts.odds.null +
  186. this.opts.odds.boolean +
  187. this.opts.odds.number +
  188. this.opts.odds.string +
  189. this.opts.odds.binary +
  190. this.opts.odds.array +
  191. this.opts.odds.object;
  192. this.root =
  193. this.opts.rootNode === 'object'
  194. ? {}
  195. : this.opts.rootNode === 'array'
  196. ? []
  197. : this.pickContainerType() === 'object'
  198. ? {}
  199. : [];
  200. this.containers.push(this.root);
  201. }
  202. create() {
  203. for (let i = 0; i < this.opts.nodeCount; i++)
  204. this.addNode();
  205. return this.root;
  206. }
  207. addNode() {
  208. const container = this.pickContainer();
  209. const newNodeType = this.pickNodeType();
  210. const node = this.generate(newNodeType);
  211. if (node && typeof node === 'object')
  212. this.containers.push(node);
  213. if (Array.isArray(container)) {
  214. const index = Math.floor(Math.random() * (container.length + 1));
  215. container.splice(index, 0, node);
  216. }
  217. else {
  218. const key = RandomJson.genString();
  219. container[key] = node;
  220. }
  221. }
  222. generate(type) {
  223. switch (type) {
  224. case 'null':
  225. return null;
  226. case 'boolean':
  227. return RandomJson.genBoolean();
  228. case 'number':
  229. return RandomJson.genNumber();
  230. case 'string':
  231. return RandomJson.genString();
  232. case 'binary':
  233. return RandomJson.genBinary();
  234. case 'array':
  235. return [];
  236. case 'object':
  237. return {};
  238. }
  239. }
  240. pickNodeType() {
  241. const odd = Math.random() * this.totalOdds;
  242. if (odd <= this.oddTotals.null)
  243. return 'null';
  244. if (odd <= this.oddTotals.boolean)
  245. return 'boolean';
  246. if (odd <= this.oddTotals.number)
  247. return 'number';
  248. if (odd <= this.oddTotals.string)
  249. return 'string';
  250. if (odd <= this.oddTotals.binary)
  251. return 'binary';
  252. if (odd <= this.oddTotals.array)
  253. return 'array';
  254. return 'object';
  255. }
  256. pickContainerType() {
  257. const sum = this.opts.odds.array + this.opts.odds.object;
  258. if (Math.random() < this.opts.odds.array / sum)
  259. return 'array';
  260. return 'object';
  261. }
  262. pickContainer() {
  263. return this.containers[Math.floor(Math.random() * this.containers.length)];
  264. }
  265. }
  266. exports.RandomJson = RandomJson;
  267. //# sourceMappingURL=RandomJson.js.map