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

u-wave / core / 11085094286

28 Sep 2024 03:39PM UTC coverage: 79.715% (-0.4%) from 80.131%
11085094286

Pull #637

github

web-flow
Merge 11ccf3b06 into 14c162f19
Pull Request #637: Switch to a relational database, closes #549

751 of 918 branches covered (81.81%)

Branch coverage included in aggregate %.

1891 of 2530 new or added lines in 50 files covered. (74.74%)

13 existing lines in 7 files now uncovered.

9191 of 11554 relevant lines covered (79.55%)

68.11 hits per line

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

92.86
/src/utils/toListResponse.js
1
import lodash from 'lodash';
1✔
2

1✔
3
const {
1✔
4
  isPlainObject,
1✔
5
  cloneDeep,
1✔
6
  get: getPath,
1✔
7
  set: setPath,
1✔
8
} = lodash;
1✔
9

1✔
10
/**
1✔
11
 * @typedef {Record<string, string[]>} IncludedOptions
1✔
12
 */
1✔
13

1✔
14
/** @param {{ _id: string, id?: undefined } | { _id?: undefined, id: string }} d */
1✔
15
function getID(d) {
852✔
16
  return d._id ?? d.id;
852✔
17
}
852✔
18

1✔
19
/**
1✔
20
 * @param {any[]} data
1✔
21
 * @param {IncludedOptions} included
1✔
22
 */
1✔
23
function extractIncluded(data, included) {
20✔
24
  const includedTypes = Object.keys(included);
20✔
25
  if (includedTypes.length === 0) {
20!
26
    return {
×
27
      data,
×
28
      included: {},
×
29
    };
×
30
  }
×
31

20✔
32
  /** @type {Record<string, any[]>} */
20✔
33
  const includeds = includedTypes.reduce(
20✔
34
    (map, typeName) => Object.assign(map, { [typeName]: [] }),
20✔
35
    {},
20✔
36
  );
20✔
37

20✔
38
  /** @type {Set<string>} */
20✔
39
  const had = new Set();
20✔
40

20✔
41
  /**
20✔
42
   * @param {string} type
20✔
43
   * @param {{ _id: string }} item
20✔
44
   */
20✔
45
  function include(type, item) {
20✔
46
    if (!had.has(type + getID(item))) {
284✔
47
      includeds[type].push(item);
284✔
48
      had.add(type + getID(item));
284✔
49
    }
284✔
50
  }
284✔
51

20✔
52
  /** @type {any[]} */
20✔
53
  const resultData = [];
20✔
54
  data.forEach((initialItem) => {
20✔
55
    let item = isPlainObject(initialItem) ? initialItem : initialItem.toJSON();
283!
56
    includedTypes.forEach((type) => {
283✔
57
      included[type].forEach((path) => {
283✔
58
        const includedItem = getPath(item, path);
284✔
59
        if (includedItem) {
284✔
60
          if (item === initialItem) {
284✔
61
            item = cloneDeep(item);
283✔
62
          }
283✔
63
          if (Array.isArray(includedItem)) {
284!
NEW
64
            setPath(item, path, includedItem.map(getID));
×
65
            includedItem.forEach((i) => include(type, i));
×
66
          } else {
284✔
67
            setPath(item, path, getID(includedItem));
284✔
68
            include(type, includedItem);
284✔
69
          }
284✔
70
        }
284✔
71
      });
283✔
72
    });
283✔
73
    resultData.push(item);
283✔
74
  });
20✔
75

20✔
76
  return {
20✔
77
    included: includeds,
20✔
78
    data: resultData,
20✔
79
  };
20✔
80
}
20✔
81

1✔
82
/**
1✔
83
 * @typedef {object} ListResponse
1✔
84
 * @prop {import('type-fest').JsonObject} meta
1✔
85
 * @prop {import('type-fest').JsonValue[]} data
1✔
86
 * @prop {import('type-fest').JsonObject} [included]
1✔
87
 * @prop {Record<string, string>} links
1✔
88
 */
1✔
89

1✔
90
/**
1✔
91
 * @template {any} TItem
1✔
92
 * @param {TItem[]} list
1✔
93
 * @param {object} [options]
1✔
94
 * @param {import('type-fest').JsonObject} [options.meta]
1✔
95
 * @param {IncludedOptions} [options.included]
1✔
96
 * @param {string} [options.url]
1✔
97
 */
1✔
98
function toListResponse(list, options = {}) {
31✔
99
  const {
31✔
100
    meta = {},
31✔
101
    included,
31✔
102
    url,
31✔
103
  } = options;
31✔
104

31✔
105
  let props = {
31✔
106
    data: list,
31✔
107
    /** @type {ListResponse['included']} */
31✔
108
    included: undefined,
31✔
109
  };
31✔
110
  if (included) {
31✔
111
    props = extractIncluded(list, included);
20✔
112
  }
20✔
113
  return {
31✔
114
    meta: {
31✔
115
      included,
31✔
116
      ...meta,
31✔
117
    },
31✔
118
    links: url ? { self: url } : {},
31✔
119
    ...props,
31✔
120
  };
31✔
121
}
31✔
122

1✔
123
export default toListResponse;
1✔
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

© 2026 Coveralls, Inc