RandomJson.js 6.7 KB

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