Skip to content

Commit

Permalink
Fix: Registry Support for Case Insensitive Functions (elastic#807)
Browse files Browse the repository at this point in the history
* Change registry get function to case insenstive comparison

* Change registry to register lower version of function names

* Added check for undefined name in registry get function

* Throws when prop isn't a string in registry constructor

* Added unit tests for registry
  • Loading branch information
cqliu1 authored and Rashid Khan committed Jul 30, 2018
1 parent f49b56b commit c7c3497
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
12 changes: 12 additions & 0 deletions common/lib/__tests__/registry.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ function validateRegistry(registry, elements) {
expect(registry.get('__test2')).to.eql(elements[1]());
});

it('ignores case when getting items', () => {
expect(registry.get('__TeSt2')).to.eql(elements[1]());
expect(registry.get('__tESt2')).to.eql(elements[1]());
});

it('gets a shallow clone', () => {
expect(registry.get('__test2')).to.not.equal(elements[1]());
});
Expand Down Expand Up @@ -169,4 +174,11 @@ describe('Registry', () => {
expect(val[name].baseFunc).to.be.a('function');
});
});

describe('throws when lookup prop is not a string', () => {
const check = () => new Registry(2);
expect(check).to.throwException(e => {
expect(e.message).to.be('Registry property name must be a string');
});
});
});
7 changes: 5 additions & 2 deletions common/lib/registry.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import clone from 'lodash.clone';

export class Registry {
constructor(prop = 'name') {
if (typeof prop !== 'string') throw new Error('Registry property name must be a string');
this._prop = prop;
this._indexed = new Object();
}
Expand All @@ -19,7 +20,7 @@ export class Registry {
if (typeof obj !== 'object' || !obj[this._prop]) {
throw new Error(`Registered functions must return an object with a ${this._prop} property`);
}
this._indexed[obj[this._prop]] = this.wrapper(obj);
this._indexed[obj[this._prop].toLowerCase()] = this.wrapper(obj);
}

toJS() {
Expand All @@ -34,7 +35,9 @@ export class Registry {
}

get(name) {
return this._indexed[name] ? clone(this._indexed[name]) : null;
if (name === undefined) return null;
const lowerCaseName = name.toLowerCase();
return this._indexed[lowerCaseName] ? clone(this._indexed[lowerCaseName]) : null;
}

getProp() {
Expand Down

0 comments on commit c7c3497

Please sign in to comment.