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

albe / node-event-storage / 27071355731

06 Jun 2026 07:11PM UTC coverage: 98.842% (+0.1%) from 98.734%
27071355731

push

github

albe
Add performance section for matchers

1320 of 1355 branches covered (97.42%)

Branch coverage included in aggregate %.

6448 of 6504 relevant lines covered (99.14%)

818.97 hits per line

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

98.64
/src/utils/apiHelpers.js
1
/**
4✔
2
 * Normalize commit overloads into a single argument object.
4✔
3
 *
4✔
4
 * @param {object|object[]} events Event or event list.
4✔
5
 * @param {number|object|function} expectedVersion Expected version, CommitCondition, or already metadata/callback.
4✔
6
 * @param {object|function} metadata Commit metadata or callback.
4✔
7
 * @param {function} callback Completion callback.
4✔
8
 * @param {number} ExpectedVersionAny Fallback value for "any" expectedVersion.
4✔
9
 * @param {Function} CommitConditionClass Class used for CommitCondition checks.
4✔
10
 * @returns {{events: object[], expectedVersion: number|object, metadata: object, callback: function}} Normalized commit arguments.
4✔
11
 */
4✔
12
function fixCommitArgumentTypes(events, expectedVersion, metadata, callback, ExpectedVersionAny, CommitConditionClass) {
1,236✔
13
    if (!(events instanceof Array)) {
1,236✔
14
        events = [events];
116✔
15
    }
116✔
16
    if (typeof expectedVersion !== 'number' && !(expectedVersion instanceof CommitConditionClass)) {
1,236✔
17
        callback = metadata;
352✔
18
        metadata = expectedVersion;
352✔
19
        expectedVersion = ExpectedVersionAny;
352✔
20
    }
352✔
21
    if (typeof metadata !== 'object') {
1,236✔
22
        callback = metadata;
360✔
23
        metadata = {};
360✔
24
    }
360✔
25
    if (typeof callback !== 'function') {
1,236✔
26
        callback = () => {};
864✔
27
    }
864✔
28
    return { events, expectedVersion, metadata, callback };
1,236✔
29
}
1,236✔
30

4✔
31
/**
4✔
32
 * Derive the stream name from an index name.
4✔
33
 *
4✔
34
 * @param {string} indexName Index file/index name.
4✔
35
 * @returns {string} Corresponding stream name.
4✔
36
 */
4✔
37
function parseStreamFromIndexName(indexName) {
8✔
38
    if (indexName === '_all') {
8✔
39
        return '_all';
4✔
40
    }
4✔
41
    if (indexName.startsWith('stream-')) {
4✔
42
        return indexName.slice(7);
4✔
43
    }
4✔
44
    return indexName;
×
45
}
8✔
46

4✔
47
/**
4✔
48
 * Support predicate/raw shorthand (`predicate=true`).
4✔
49
 *
4✔
50
 * @param {object|function|boolean|null} predicate Filter predicate or raw shorthand.
4✔
51
 * @param {boolean} raw Raw flag from the call signature.
4✔
52
 * @returns {{predicate: object|function|null, raw: boolean}} Normalized predicate/raw pair.
4✔
53
 */
4✔
54
function normalizePredicateRaw(predicate, raw) {
428✔
55
    if (typeof predicate === 'boolean' && raw === false) {
428✔
56
        return { predicate: null, raw: predicate };
16✔
57
    }
16✔
58
    return { predicate, raw };
412✔
59
}
428✔
60

4✔
61
/**
4✔
62
 * Support constructor overloads with optional `name`.
4✔
63
 *
4✔
64
 * @param {string|object} name Name or already the options object.
4✔
65
 * @param {object} options Options object when `name` is provided.
4✔
66
 * @param {string|undefined} [fallbackName=undefined] Fallback name when no explicit `name` is passed.
4✔
67
 * @returns {{name: string|undefined, options: object}} Normalized name/options pair.
4✔
68
 */
4✔
69
function normalizeNamedCtorArgs(name, options, fallbackName = undefined) {
8,596✔
70
    if (typeof name !== 'string') {
8,596✔
71
        return { name: fallbackName, options: name };
664✔
72
    }
664✔
73
    return { name, options };
7,932✔
74
}
8,596✔
75

4✔
76
/**
4✔
77
 * Normalize negative revisions relative to stream length.
4✔
78
 *
4✔
79
 * @param {number} version Requested revision.
4✔
80
 * @param {number} length Current stream length.
4✔
81
 * @returns {number} Resolved revision.
4✔
82
 */
4✔
83
function normalizeRevision(version, length) {
2,928✔
84
    return version < 0 ? version + length + 1 : version;
2,928✔
85
}
2,928✔
86

4✔
87
/**
4✔
88
 * Clamp and normalize maxRevision, including negative values.
4✔
89
 *
4✔
90
 * @param {number} length Current stream length.
4✔
91
 * @param {number} maxRevision Requested max revision.
4✔
92
 * @returns {number} Effective max revision in valid range.
4✔
93
 */
4✔
94
function normalizeMaxRevision(length, maxRevision) {
1,380✔
95
    return Math.min(length, maxRevision < 0 ? length + maxRevision + 1 : maxRevision);
1,380✔
96
}
1,380✔
97

4✔
98
/**
4✔
99
 * Support the consumer overload where the first argument is a numeric start offset.
4✔
100
 *
4✔
101
 * @param {object|number} initialState Initial state or numeric start offset.
4✔
102
 * @param {number} startFrom Start offset from the call signature.
4✔
103
 * @returns {{initialState: object, startFrom: number}} Normalized consumer initialization values.
4✔
104
 */
4✔
105
function normalizeConsumerStateArgs(initialState, startFrom) {
188✔
106
    if (typeof initialState === 'number') {
188✔
107
        return { initialState: {}, startFrom: initialState };
8✔
108
    }
8✔
109
    return { initialState, startFrom };
180✔
110
}
188✔
111

4✔
112
export {
4✔
113
    fixCommitArgumentTypes,
4✔
114
    parseStreamFromIndexName,
4✔
115
    normalizePredicateRaw,
4✔
116
    normalizeNamedCtorArgs,
4✔
117
    normalizeRevision,
4✔
118
    normalizeMaxRevision,
4✔
119
    normalizeConsumerStateArgs
4✔
120
};
4✔
121

122

123

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