LCOV - code coverage report
Current view: top level - src/core - recurse.js (source / functions) Hit Total Coverage
Test: cov_profile.lcov Lines: 72 72 100.0 %
Date: 2021-12-16 00:47:58 Functions: 7 7 100.0 %
Branches: 29 29 100.0 %

           Branch data     Line data    Source code
       1                 :         44 : import Prng from "./prng/index.js";
       2            [ + ]:         44 : import { registered } from "./register/index.js";
       3                 :         44 : 
       4                 :         44 : const handleArray = (seeded, node, path, root) => {
       5                 :        126 :   for (let i = 0; i < node.length; i++) {
       6            [ + ]:        604 :     node[i] = recursor(seeded, node[i], path.concat(i), root);
       7                 :        126 :   }
       8                 :        126 : 
       9                 :        126 :   return node;
      10                 :         44 : };
      11                 :         44 : 
      12                 :         44 : const handleObject = (seeded, node, path, root) => {
      13                 :        412 :   for (const key in node) {
      14            [ + ]:        784 :     node[key] = recursor(seeded, node[key], path.concat(key), root);
      15                 :        412 :   }
      16                 :        412 : 
      17                 :        412 :   return node;
      18                 :         44 : };
      19                 :         44 : 
      20                 :         44 : const handleFunction = (seeded, node, path, root) => {
      21                 :        900 :   const newSeeded = new seeded.constructor(
      22                 :        900 :     Prng,
      23                 :        900 :     seeded.info().initseed,
      24                 :        900 :     seeded.info().path.concat(path),
      25                 :        900 :   );
      26                 :        900 :   // TODO: if we are adding data property, why not also position property?
      27                 :        900 :   // TODO: perhaps data (property on seeded instance during recursion) should be called root, and be a getter/setter and cleaned up after recursion?
      28                 :        900 :   newSeeded.data = root;
      29                 :        900 :   const newNode = registered.indexOf(node) !== -1 ? node() : node(newSeeded);
      30         [ +  + ]:        900 :   return recursor(seeded, newNode, path, path.length ? root : newNode);
      31         [ +  + ]:         44 : };
      32                 :         44 : 
      33                 :         44 : const handleRegex = (
      34                 :         58 :   seeded,
      35                 :         58 :   node,
      36                 :         58 :   path,
      37                 :         58 :   root,
      38                 :         58 : ) => (seeded.regex
      39                 :         58 :   ? handleFunction(seeded, (seeded) => seeded.regex(node), path, root)
      40       [ + ][ + ]:         44 :   : node);
      41            [ + ]:         44 : 
      42                 :         44 : const recursor = (seeded, node, path, root) =>
      43                 :       2300 :   node === null || node === undefined
      44            [ + ]:       2300 :     ? node
      45            [ + ]:       2300 :     : node.constructor === Array
      46            [ + ]:       3118 :     ? handleArray(seeded, node, path, root)
      47            [ + ]:       3118 :     : node.constructor === Object
      48            [ + ]:       4528 :     ? handleObject(seeded, node, path, root)
      49            [ + ]:       4528 :     : typeof node === "function"
      50            [ + ]:       5794 :     ? handleFunction(seeded, node, path, root)
      51            [ + ]:       5794 :     : node.constructor === RegExp
      52            [ + ]:       6668 :     ? handleRegex(seeded, node, path, root)
      53            [ + ]:         44 :     : node;
      54            [ + ]:         44 : 
      55                 :         44 : const cloner = (node) =>
      56                 :       1310 :   node === null || node === undefined
      57            [ + ]:       1310 :     ? node
      58            [ + ]:       1310 :     : node.constructor === Array
      59            [ + ]:       1420 :     ? node.map(cloner)
      60            [ + ]:       1420 :     : node.constructor === Object
      61            [ + ]:       2176 :     ? Object.entries(node).reduce(
      62            [ + ]:       2468 :       (memo, [key, val]) => ({ ...memo, [key]: cloner(val) }),
      63            [ + ]:       2468 :       {},
      64                 :       2176 :     )
      65                 :         44 :     : node;
      66            [ + ]:         44 : 
      67                 :         44 : const recurse = (seeded, nodeIn) => {
      68                 :        506 :   const node = cloner(nodeIn);
      69                 :        506 :   return recursor(seeded, node, [], node);
      70                 :         44 : };
      71                 :         44 : 
      72                 :         22 : export default recurse;

Generated by: LCOV version 1.15