Skip to content

Commit

Permalink
no longer throw on unregistered has gets by default (#278)
Browse files Browse the repository at this point in the history
  • Loading branch information
matt-gadd authored Mar 18, 2019
1 parent 5856e22 commit 39698f9
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/has/has.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ export function add(feature: string, value: FeatureTest | FeatureTestResult, ove
*
* @param feature The name of the feature to test.
*/
export default function has(feature: string): FeatureTestResult {
export default function has(feature: string, strict: boolean = false): FeatureTestResult {
let result: FeatureTestResult;

const normalizedFeature = feature.toLowerCase();
Expand All @@ -185,7 +185,7 @@ export default function has(feature: string): FeatureTestResult {
delete testFunctions[normalizedFeature];
} else if (normalizedFeature in testCache) {
result = testCache[normalizedFeature];
} else {
} else if (strict) {
throw new TypeError(`Attempt to detect unregistered has feature "${feature}"`);
}

Expand Down
2 changes: 1 addition & 1 deletion src/shim/support/decorators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ import has from '../../has/has';
export function hasClass(feature: string, trueClass: Function, falseClass: Function): ClassDecorator {
return function(target: Function) {
/* Return type generics aren't catching the fact that Function is assignable to the generic */
return (has(feature) ? trueClass : falseClass) as any;
return (has(feature, true) ? trueClass : falseClass) as any;
};
}
11 changes: 9 additions & 2 deletions tests/has/unit/has.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,17 +59,24 @@ registerSuite('has', {
assert.isTrue(hasCache['abc']);
hasAdd('def', false);
assert.isFalse(hasCache['def']);

delete hasCache['abc'];
},

'throws when unregistered in strict mode'() {
assert.throws(
() => {
has('abc');
has('abc', true);
},
TypeError,
'Attempt to detect unregistered has feature'
);
},

'returns undefined when unregistered'() {
const abc = has('abc');
assert.equal(abc, undefined);
},

'deferred feature test should not populate cache until evaluated'() {
hasAdd('deferred-cache', function() {
return true;
Expand Down

0 comments on commit 39698f9

Please sign in to comment.