1 | function startsWith (prefix, str) {
|
2 | console.assert(typeof str === 'string', 'expected string', str)
|
3 | return str.indexOf(prefix) === 0
|
4 | }
|
5 |
|
6 | function sameLength (a, b) {
|
7 | return a.length === b.length
|
8 | }
|
9 |
|
10 | function matchesExactly (prefix, str) {
|
11 | return startsWith(prefix, str) &&
|
12 | sameLength(prefix, str)
|
13 | }
|
14 |
|
15 | function findScripts (prefix, scripts) {
|
16 | const labels = Object.keys(scripts)
|
17 | const matchesExactlyPrefix = matchesExactly.bind(null, prefix)
|
18 | const exactMatches = labels.filter(matchesExactlyPrefix)
|
19 | if (exactMatches.length === 1) {
|
20 | return exactMatches
|
21 | }
|
22 |
|
23 | const startsWithPrefix = startsWith.bind(null, prefix)
|
24 | const matchingScripts = labels.filter(startsWithPrefix)
|
25 | return matchingScripts
|
26 | }
|
27 |
|
28 | module.exports = findScripts
|