Skip to content

Commit

Permalink
don't throw for empty list
Browse files Browse the repository at this point in the history
  • Loading branch information
clord committed Mar 2, 2021
1 parent be2145e commit 1d1479f
Show file tree
Hide file tree
Showing 5 changed files with 6 additions and 15 deletions.
5 changes: 1 addition & 4 deletions lib/cjs/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,8 @@ var tslib_1 = require("tslib");
var React = tslib_1.__importStar(require("react"));
var EnableContext_1 = require("./EnableContext");
function testAndConvert(input) {
if ((Array.isArray(input) && input.length === 0) || input == null) {
throw new Error("Can't have empty array of features");
}
var test = React.useContext(EnableContext_1.EnableContext);
var converted = React.useMemo(function () { return (Array.isArray(input) ? input : [input]); }, [input]);
var converted = React.useMemo(function () { return (input == null ? [] : (Array.isArray(input) ? input : [input])); }, [input]);
return [test, converted];
}
exports.testAndConvert = testAndConvert;
5 changes: 1 addition & 4 deletions lib/esm/utils.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import * as React from "react";
import { EnableContext } from "./EnableContext";
export function testAndConvert(input) {
if ((Array.isArray(input) && input.length === 0) || input == null) {
throw new Error("Can't have empty array of features");
}
var test = React.useContext(EnableContext);
var converted = React.useMemo(function () { return (Array.isArray(input) ? input : [input]); }, [input]);
var converted = React.useMemo(function () { return (input == null ? [] : (Array.isArray(input) ? input : [input])); }, [input]);
return [test, converted];
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-enable",
"version": "2.0.2",
"version": "2.0.4",
"description": "feature flags to enable and disable functionality at runtime in a react application",
"main": "./lib/cjs/index.js",
"module": "./lib/esm/index.js",
Expand Down
4 changes: 2 additions & 2 deletions src/index.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ const wrapper: React.FC<{ defaultEnabled: string[] }> = ({
</Features>
);

test("without a feature, should fail", () => {
test("without a feature, should be false", () => {
const { result } = renderHook(() => useEnabled([]));
expect(result.error).toEqual(Error("Can't have empty array of features"));
expect(result.current).toEqual(false);
});

test("should be disabled without a context", () => {
Expand Down
5 changes: 1 addition & 4 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,10 @@ import { EnableContextType, EnableContext } from "./EnableContext";
export function testAndConvert(
input?: string | string[] | null
): [EnableContextType, string[]] {
if ((Array.isArray(input) && input.length === 0) || input == null) {
throw new Error("Can't have empty array of features");
}
const test = React.useContext(EnableContext);
// We memoize just to prevent re-renders since this could be at the leaf of a tree
const converted = React.useMemo(
() => (Array.isArray(input) ? input : [input]),
() => (input == null ? [] : (Array.isArray(input) ? input : [input])),
[input]
);
return [test, converted];
Expand Down

0 comments on commit 1d1479f

Please sign in to comment.