forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for function aliases, case-insensitive matching (elastic#748
) * feat: add support for function aliases * fix: rename to getByAlias * feat: case-insensitive matching * fix: remove isAlias in arg def since it is no longer used
- Loading branch information
1 parent
b293545
commit df733c6
Showing
7 changed files
with
77 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import expect from 'expect.js'; | ||
import { getByAlias } from '../get_by_alias'; | ||
|
||
describe('getByAlias', () => { | ||
const fns = { | ||
foo: { aliases: ['f'] }, | ||
bar: { aliases: ['b'] }, | ||
}; | ||
|
||
it('returns the function by name', () => { | ||
expect(getByAlias(fns, 'foo')).to.be(fns.foo); | ||
expect(getByAlias(fns, 'bar')).to.be(fns.bar); | ||
}); | ||
|
||
it('returns the function by alias', () => { | ||
expect(getByAlias(fns, 'f')).to.be(fns.foo); | ||
expect(getByAlias(fns, 'b')).to.be(fns.bar); | ||
}); | ||
|
||
it('returns the function by case-insensitive name', () => { | ||
expect(getByAlias(fns, 'FOO')).to.be(fns.foo); | ||
expect(getByAlias(fns, 'BAR')).to.be(fns.bar); | ||
}); | ||
|
||
it('returns the function by case-insensitive alias', () => { | ||
expect(getByAlias(fns, 'F')).to.be(fns.foo); | ||
expect(getByAlias(fns, 'B')).to.be(fns.bar); | ||
}); | ||
|
||
it('handles empty strings', () => { | ||
const emptyStringFns = { '': {} }; | ||
const emptyStringAliasFns = { foo: { aliases: [''] } }; | ||
expect(getByAlias(emptyStringFns, '')).to.be(emptyStringFns['']); | ||
expect(getByAlias(emptyStringAliasFns, '')).to.be(emptyStringAliasFns.foo); | ||
}); | ||
|
||
it('handles "undefined" strings', () => { | ||
const emptyStringFns = { undefined: {} }; | ||
const emptyStringAliasFns = { foo: { aliases: ['undefined'] } }; | ||
expect(getByAlias(emptyStringFns, 'undefined')).to.be(emptyStringFns.undefined); | ||
expect(getByAlias(emptyStringAliasFns, 'undefined')).to.be(emptyStringAliasFns.foo); | ||
}); | ||
|
||
it('returns undefined if not found', () => { | ||
expect(getByAlias(fns, 'baz')).to.be(undefined); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
/** | ||
* This is used for looking up function/argument definitions. It looks through | ||
* the given object for a case-insensitive match, which could be either the | ||
* name of the key itself, or something under the `aliases` property. | ||
*/ | ||
export function getByAlias(specs, name) { | ||
const lowerCaseName = name.toLowerCase(); | ||
const key = Object.keys(specs).find(key => { | ||
if (key.toLowerCase() === lowerCaseName) return true; | ||
return (specs[key].aliases || []).some(alias => { | ||
return alias.toLowerCase() === lowerCaseName; | ||
}); | ||
}); | ||
if (typeof key !== undefined) return specs[key]; | ||
} |