summaryrefslogtreecommitdiff
path: root/bin/wiki/ImportarDesdeURL/node_modules/metascraper/src/merge-rules.js
blob: 7cdb395c325d84eed8d5a0277e59187620908e91 (plain)
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
'use strict'

const {
  cloneDeep,
  concat,
  first,
  findIndex,
  forEach,
  chain
} = require('lodash')

module.exports = (rules, baseRules) =>
  chain(rules)
    .reduce((acc, { test, ...rules }) => {
      forEach(rules, (innerRules, propName) => {
        if (test) forEach(innerRules, rule => (rule.test = test))
        // find the rules associated with `propName`
        const index = findIndex(acc, item => first(item) === propName)
        // if `propName` has more rule, add the new rule from the end
        if (index !== -1) acc[index][1] = concat(innerRules, ...acc[index][1])
        // otherwise, create an array of rules
        else acc.push([propName, innerRules])
      })
      return acc
    }, cloneDeep(baseRules))
    .value()