• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

microlinkhq / metascraper / 7126546548

07 Dec 2023 09:58AM UTC coverage: 94.127%. First build
7126546548

Pull #673

github

web-flow
Merge 67e65920a into b4ec42d7d
Pull Request #673: feat: better type definitions

1709 of 1927 branches covered (0.0%)

Branch coverage included in aggregate %.

2506 of 2551 relevant lines covered (98.24%)

415.19 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

97.47
/packages/metascraper/src/rules.js
1
'use strict'
121✔
2

121✔
3
const {
121✔
4
  cloneDeep,
121✔
5
  concat,
121✔
6
  first,
121✔
7
  findIndex,
121✔
8
  forEach,
121✔
9
  chain,
121✔
10
  castArray,
121✔
11
  has,
121✔
12
  set
121✔
13
} = require('lodash')
121✔
14

121✔
15
const forEachRule = (collection, fn) => forEach(castArray(collection), fn)
121✔
16

121✔
17
const loadRules = rulesBundle =>
121✔
18
  chain(rulesBundle)
235✔
19
    .reduce((acc, { test, ...rules }) => {
235✔
20
      forEach(rules, (innerRules, propName) => {
1,829✔
21
        if (test) forEachRule(innerRules, rule => (rule.test = test))
2,874!
22

2,874✔
23
        set(
2,874✔
24
          acc,
2,874✔
25
          propName,
2,874✔
26
          has(acc, propName)
2,874✔
27
            ? concat(acc[propName], innerRules)
2,874✔
28
            : concat(innerRules)
2,874✔
29
        )
2,874✔
30

2,874✔
31
        return acc
2,874✔
32
      })
1,829✔
33
      return acc
1,829✔
34
    }, {})
235✔
35
    .toPairs()
235✔
36
    .value()
235✔
37

121✔
38
const mergeRules = (rules, baseRules) =>
121✔
39
  chain(rules)
264✔
40
    .reduce((acc, { test, ...rules }) => {
264✔
41
      forEach(rules, (innerRules, propName) => {
3✔
42
        if (test) forEachRule(innerRules, rule => (rule.test = test))
4!
43
        // find the rules associated with `propName`
4✔
44
        const index = findIndex(acc, item => first(item) === propName)
4✔
45
        // if `propName` has more rule, add the new rule from the end
4✔
46
        if (index !== -1) acc[index][1] = concat(innerRules, ...acc[index][1])
4✔
47
        // otherwise, create an array of rules
3✔
48
        else acc.push([propName, castArray(innerRules)])
3✔
49
      })
3✔
50
      return acc
3✔
51
    }, cloneDeep(baseRules))
264✔
52
    .value()
264✔
53

121✔
54
module.exports = { mergeRules, loadRules }
121✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc