@eagleoutice/flowr
Version:
Static Dataflow Analyzer and Program Slicer for the R Programming Language
34 lines • 1.03 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.findByPrefixIfUnique = findByPrefixIfUnique;
/**
* given a potentially partial prefix like `hell`, this finds the matching name in the map, but only
* if it is unique!
*
* @example
* ```typescript
* findByPrefixIfUnique('hell', { 'hello', 'bar' }) // => 'hello'
* findByPrefixIfUnique('hell', { 'hello', 'hell' }) // => 'hell' (full match)
* findByPrefixIfUnique('h', { 'hello', 'hell' }) // => undefined (not unique)
* findByPrefixIfUnique('', { 'hello', 'hell' }) // => undefined (empty prefix)
* ```
*/
function findByPrefixIfUnique(prefix, keys) {
if (prefix === '') {
return undefined;
}
let found = undefined;
for (const key of keys) {
if (key === prefix) {
return key;
}
if (key.startsWith(prefix)) {
if (found) {
return undefined;
}
found = key;
}
}
return found;
}
//# sourceMappingURL=prefix.js.map