MCPcopy Create free account
hub / github.com/activejs/activejs / randomMutation

Function randomMutation

packages/core/src/tests/utils.ts:459–497  ·  view source on GitHub ↗
(o: T)

Source from the content-addressed store, hash-verified

457}
458
459export 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
499export function safeStringify(
500 o: any,

Callers 2

selection.spec.tsFile · 0.90

Calls 10

isDictFunction · 0.90
randomNumberFunction · 0.85
randomValueFunction · 0.85
randomNumOrStrFunction · 0.85
selectRandomFunction · 0.85
randomBooleanFunction · 0.85
spliceMethod · 0.80
popMethod · 0.80
pushMethod · 0.80
shiftMethod · 0.80

Tested by

no test coverage detected