All files / validator/yupSource/util sortFields.js

61.11% Statements 11/18
41.18% Branches 7/17
33.33% Functions 1/3
76.92% Lines 10/13

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33                15x 15x                   15x 38x 38x   38x   38x 38x 38x     15x    
import { has } from '../../../utils'
import toposort from 'toposort'
import { split } from 'property-expr'
 
import Ref from '../Reference'
import isSchema from './isSchema'
 
export default function sortFields(fields, excludes = []) {
  let edges = []
  let nodes = []
 
  function addNode(depPath, key) {
    var node = split(depPath)[0]
 
    if (!~nodes.indexOf(node)) nodes.push(node)
 
    if (!~excludes.indexOf(`${key}-${node}`)) edges.push([key, node])
  }
 
  for (const key in fields)
    if (has(fields, key)) {
      let value = fields[key]
 
      if (!~nodes.indexOf(key)) nodes.push(key)
 
      Iif (Ref.isRef(value) && value.isSibling) addNode(value.path, key)
      else if (isSchema(value) && value._deps)
        value._deps.forEach(path => addNode(path, key))
    }
 
  return toposort.array(nodes, edges).reverse()
}