(o: T)
| 457 | } |
| 458 | |
| 459 | export function randomMutation<T>(o: T): void { |
| 460 | if (o == null || typeof o !== 'object') { |
| 461 | return; |
| 462 | } |
| 463 | |
| 464 | if (Array.isArray(o)) { |
| 465 | o.forEach(v => randomMutation(v)); |
| 466 | switch (randomNumber(0, 4)) { |
| 467 | case 0: |
| 468 | o.splice(randomNumber(0, o.length), randomNumber(0, o.length), randomValue(1)); |
| 469 | break; |
| 470 | case 1: |
| 471 | o[randomNumOrStr()] = randomValue(1); |
| 472 | break; |
| 473 | case 2: |
| 474 | o.length ? o.pop() : o.push(randomValue(1)); |
| 475 | break; |
| 476 | case 3: |
| 477 | o.length ? o.shift() : o.push(randomValue(1)); |
| 478 | break; |
| 479 | case 4: |
| 480 | if (o.length) { |
| 481 | delete o[selectRandom(Object.keys(o))]; |
| 482 | } else { |
| 483 | o.push(randomValue(1)); |
| 484 | } |
| 485 | } |
| 486 | } else if (isDict(o)) { |
| 487 | const keys = Object.keys(o); |
| 488 | |
| 489 | keys.forEach(k => randomMutation(o[k])); |
| 490 | |
| 491 | if (!keys.length || randomBoolean()) { |
| 492 | o[randomNumOrStr()] = randomValue(1); |
| 493 | } else { |
| 494 | delete o[selectRandom(keys)]; |
| 495 | } |
| 496 | } |
| 497 | } |
| 498 | |
| 499 | export function safeStringify( |
| 500 | o: any, |
no test coverage detected