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

overlookmotel / livepack / 7305862721

23 Dec 2023 03:12AM UTC coverage: 90.448% (-0.06%) from 90.508%
7305862721

push

github

overlookmotel
TODO

4797 of 5154 branches covered (0.0%)

Branch coverage included in aggregate %.

12824 of 14328 relevant lines covered (89.5%)

8995.58 hits per line

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

79.61
/lib/shared/functions.js
1
/* --------------------
64✔
2
 * livepack module
64✔
3
 * Shared functions
64✔
4
 * ------------------*/
64✔
5

64✔
6
'use strict';
64✔
7

64✔
8
// Modules
64✔
9
const checkReservedWord = require('reserved-words').check,
64✔
10
        {VISITOR_KEYS, COMMENT_KEYS} = require('@babel/types'),
64✔
11
        {isArray, isSymbol} = require('is-it-type');
64✔
12

64✔
13
// Exports
64✔
14

64✔
15
module.exports = {
64✔
16
        isPrimitive,
64✔
17
        isReservedWord,
64✔
18
        isReservedVarName,
64✔
19
        createArrayOrPush,
64✔
20
        combineArraysWithDedup,
64✔
21
        getProp,
64✔
22
        getProps,
64✔
23
        setProp,
64✔
24
        traverseAll
64✔
25
};
64✔
26

64✔
27
/**
64✔
28
 * Determine if value is a primitive.
64✔
29
 * @param {*} val - Value
64✔
30
 * @returns {boolean} - `true` if value is a primitive
64✔
31
 */
64✔
32
function isPrimitive(val) {
414,380✔
33
        // Symbols are not considered primitives as they need to be uniquely referenced.
414,380✔
34
        // `undefined` + `NaN` + `Infinity` not considered primitives as they are global vars.
414,380✔
35
        return val !== undefined
414,380✔
36
                && !isSymbol(val)
414,380✔
37
                && !Number.isNaN(val)
414,380✔
38
                && val !== Infinity && val !== -Infinity
✔
39
                && Object(val) !== val;
✔
40
}
×
41

×
42
/**
×
43
 * Determine if var name is a JS reserved word e.g. 'break', 'class'.
×
44
 * Includes names which are reserved only in strict mode e.g. `package`.
×
45
 * @param {string} name - Variable name
×
46
 * @returns {boolean} - `true` if reserved word, `false` if not
×
47
 */
×
48
function isReservedWord(name) {
33,990✔
49
        return checkReservedWord(name, 'es6', true);
33,990✔
50
}
33,990✔
51

×
52
/**
×
53
 * Determine if name should not be used as a var name.
×
54
 * Includes all JS reserved words, plus 'arguments' + 'eval' are additionally prohibited
64✔
55
 * as cannot initialize vars with these names in strict mode.
64✔
56
 * @param {string} name - Variable name
64✔
57
 * @returns {boolean} - `true` if reserved var name, `false` if not
64✔
58
 */
64✔
59
function isReservedVarName(name) {
30,704✔
60
        return name === 'arguments' || name === 'eval' || isReservedWord(name);
99,794✔
61
}
99,794✔
62

99,794✔
63
/**
99,794✔
64
 * Create an array as `obj[key]` or push elements to array if already exists.
99,794✔
65
 * @param {Object} obj - Object
99,794✔
66
 * @param {string} key - Key
99,794✔
67
 * @param {...any} values - Values to add to array
64✔
68
 * @returns {undefined}
64✔
69
 */
64✔
70
function createArrayOrPush(obj, key, ...values) {
1,134✔
71
        const arr = obj[key];
1,134✔
72
        if (arr) {
1,134✔
73
                arr.push(...values);
46✔
74
        } else {
1,134✔
75
                obj[key] = values;
1,088✔
76
        }
1,088✔
77
}
1,134✔
78

64✔
79
/**
64✔
80
 * Combine two arrays, removing duplicates.
64✔
81
 * If either array is `undefined` / `null`, treat it as an empty array.
64✔
82
 * @param {Array} [arr1] - Array 1
99,794✔
83
 * @param {Array} [arr2] - Array 2
99,794✔
84
 * @returns {Array} - New array combining elements of both arrays with duplicates removed
99,794✔
85
 */
×
86
function combineArraysWithDedup(arr1, arr2) {
8✔
87
        if (!arr1) return arr2 ? [...arr2] : [];
8!
88
        if (!arr2) return [...arr1];
8✔
89

×
90
        return [...new Set([...arr1, ...arr2])];
×
91
}
8✔
92

64✔
93
/**
64✔
94
 * Get deep property of object, specified by key trail.
64✔
95
 * Simplified version of `lodash.get()` (https://lodash.com/docs/4.17.15#get).
64✔
96
 * e.g. `getProp({x: [{y: 1}]}, ['x', 0, 'y'])` returns `1`.
64✔
97
 * @param {Object} obj - Object
×
98
 * @param {Array<string|number>} trail - Array of trail segements
×
99
 * @param {number} [len=trail.length] - Stop after `len` number of keys
×
100
 * @returns {*} - Value
×
101
 */
×
102
function getProp(obj, trail, len) {
18,454✔
103
        if (len == null) len = trail.length;
18,454✔
104
        for (let i = 0; i < len; i++) {
18,454✔
105
                obj = obj[trail[i]];
72,246✔
106
        }
72,246✔
107
        return obj;
18,454✔
108
}
18,454✔
109

64✔
110
/**
64✔
111
 * Get deep properties of object, specified by key trail.
64✔
112
 * Returns all properties on trail, including the original object.
64✔
113
 * e.g. `getProps({x: {y: 1}}, ['x', 'y'])` returns `[{x: {y: 1}}, {y: 1}, 1]`.
64✔
114
 * @param {Object} obj - Object
×
115
 * @param {Array<string|number>} trail - Array of trail segements
×
116
 * @param {number} [len=trail.length] - Stop after `len` number of keys
×
117
 * @returns {Array<*>} - Array of values
×
118
 */
×
119
function getProps(obj, trail, len) {
1,232✔
120
        if (len == null) len = trail.length;
1,232✔
121
        const trailObjs = [obj];
1,232✔
122
        let i = 0;
1,232✔
123
        while (i < len) {
1,232✔
124
                obj = obj[trail[i]];
6,168✔
125
                i++;
6,168✔
126
                trailObjs[i] = obj;
6,168✔
127
        }
6,168✔
128
        return trailObjs;
1,232✔
129
}
1,232✔
130

64✔
131
/**
64✔
132
 * Set deep property of object, specified by key trail.
64✔
133
 * Simplified version of `lodash.set()` (https://lodash.com/docs/4.17.15#set).
64✔
134
 * e.g. `setProp({x: [{y: 1}]}, ['x', 0, 'y'], 2)` mutates object to `{x: [{y: 2}]}`.
64✔
135
 * @param {Object} obj - Object
64✔
136
 * @param {Array<string|number>} trail - Array of trail segements
×
137
 * @param {*} value - Value to set
×
138
 * @param {number} [len=trail.length] - Stop after `len` number of keys
×
139
 * @returns {undefined}
×
140
 */
×
141
function setProp(obj, trail, value, len) {
2,852✔
142
        len = (len == null ? trail.length : len) - 1;
2,852!
143
        getProp(obj, trail, len)[trail[len]] = value;
2,852✔
144
}
2,852✔
145

64✔
146
/**
64✔
147
 * Traverse Babel AST, calling callback `visit()` with every node.
64✔
148
 * Unlike Babel's `traverse()`, also traverses over comment nodes.
64✔
149
 * Comments are visited before child nodes.
64✔
150
 * @param {Object} ast - Babel AST
64✔
151
 * @param {Function} visit - Visitor function, called with each node
64✔
152
 * @returns {undefined}
64✔
153
 */
64✔
154
function traverseAll(ast, visit) {
15,970✔
155
        const queue = [];
15,970✔
156
        let node = ast;
×
157
        do {
✔
158
                visit(node);
×
159

×
160
                for (let i = NUM_COMMENT_KEYS_MINUS_ONE; i >= 0; i--) { // eslint-disable-line no-use-before-define
✔
161
                        const arr = node[COMMENT_KEYS[i]];
×
162
                        if (arr) {
✔
163
                                for (let j = arr.length - 1; j >= 0; j--) {
✔
164
                                        visit(arr[j]);
×
165
                                }
×
166
                        }
×
167
                }
×
168

×
169
                const keys = VISITOR_KEYS[node.type];
×
170
                if (keys) {
×
171
                        for (let i = keys.length - 1; i >= 0; i--) {
545,090✔
172
                                const childNode = node[keys[i]];
1,064,324✔
173
                                if (childNode) {
1,064,324✔
174
                                        if (isArray(childNode)) {
515,464✔
175
                                                for (let j = childNode.length - 1; j >= 0; j--) {
143,884✔
176
                                                        const containerChildNode = childNode[j];
158,922✔
177
                                                        if (containerChildNode) queue.push(containerChildNode);
158,922✔
178
                                                }
158,922✔
179
                                        } else {
515,464✔
180
                                                queue.push(childNode);
371,580✔
181
                                        }
371,580✔
182
                                }
515,464✔
183
                        }
1,064,324✔
184
                }
545,090✔
185
        } while (node = queue.pop()); // eslint-disable-line no-cond-assign
15,970✔
186
}
15,970✔
187

64✔
188
const NUM_COMMENT_KEYS_MINUS_ONE = COMMENT_KEYS.length - 1;
64✔
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