Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: replace fuse.js with leven to fix no similar matching name error #452

Merged
merged 3 commits into from
Jul 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,6 @@
"eslint-plugin-prettier": "^5.0.1",
"eslint-plugin-react": "^7.33.2",
"framer-motion": "^10.16.4",
"fuse.js": "^7.0.0",
"gh-pages": "^4.0.0",
"github-slugger": "^1.0.0",
"globby": "^11.0.1",
Expand All @@ -115,6 +114,7 @@
"husky": "^7.0.2",
"is-ci-cli": "^2.2.0",
"lcov-result-merger": "^3.1.0",
"leven":"4.0.0",
"lint-staged": "^11.2.3",
"lucide-react": "^0.316.0",
"lunr": "^2.3.9",
Expand Down
16 changes: 8 additions & 8 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion react/player/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@ js_pipeline(
":node_modules/@player-ui/react-subscribe",
"//:node_modules/react-error-boundary",
"//:node_modules/tapable-ts",
"//:node_modules/fuse.js",
"//:node_modules/leven",
],
)
35 changes: 30 additions & 5 deletions react/player/src/asset/__tests__/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,42 @@ test("it prioritizes local type and id", () => {
expect(asset.getByText("foo")).not.toBeUndefined();
});

test("throws an error for an asset missing implementation or not registered", () => {
test("throws an error for an asset missing implementation or not registered WITHOUT similar matching type", () => {
const assetDef = {
asset: {
id: "bar-id",
type: "bar",
type: "test",
},
} as unknown as AssetType;

const registry: AssetRegistryType = new Registry([
[{ type: "bar", key: "bar-key" }, () => <div>bar</div>],
[{ type: "foo", key: "foo-key" }, () => <div>foo</div>],
]);

expect(() =>
render(
<AssetContext.Provider value={{ registry }}>
<ReactAsset {...assetDef} />
</AssetContext.Provider>,
),
)
.toThrowError(`No implementation found for id: bar-id type: test. Did you mean bar? \n
Registered Asset matching functions are listed below: \n
[{"type":"foo","key":"foo-key"},{"type":"bar","key":"bar-key"}]`);
});

test("throws an error for an asset missing implementation or not registered WITH similar matching type", () => {
const assetDef = {
asset: {
id: "foo-id",
type: "foo1",
},
} as unknown as AssetType;

const registry: AssetRegistryType = new Registry([
[{ type: "bar", key: "bar-key" }, () => <div>bar</div>],
[{ type: "foo", key: "foo-key" }, () => <div>foo</div>],
[{ type: "bar1", key: "bar-key" }, () => <div>bar</div>],
]);

expect(() =>
Expand All @@ -50,9 +75,9 @@ test("throws an error for an asset missing implementation or not registered", ()
</AssetContext.Provider>,
),
)
.toThrowError(`No implementation found for id: bar-id type: bar. Did you mean {"type":"bar1","key":"bar-key"}? \n
.toThrowError(`No implementation found for id: foo-id type: foo1. Did you mean foo? \n
Registered Asset matching functions are listed below: \n
[{"type":"bar1","key":"bar-key"},{"type":"foo","key":"foo-key"}]`);
[{"type":"foo","key":"foo-key"},{"type":"bar","key":"bar-key"}]`);
});

test("throws an error for an asset missing type", () => {
Expand Down
22 changes: 17 additions & 5 deletions react/player/src/asset/index.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from "react";
import leven from "leven";
import type { Asset as AssetType, AssetWrapper } from "@player-ui/player";
import type { Registry } from "@player-ui/partial-match-registry";
import Fuse from "fuse.js";

export type AssetRegistryType = Registry<React.ComponentType<any>>;

Expand Down Expand Up @@ -66,13 +66,25 @@ export const ReactAsset = (
matchList.push(asset.key);
});

const fuse = new Fuse(matchList, { keys: ["type", "key"] });
const similarType = JSON.stringify(
fuse.search(unwrapped.type as string)[0].item,
const typeList = matchList.map(
(match) => JSON.parse(JSON.stringify(match)).type,
);

const similarType = typeList.reduce((prev, curr) => {
const next = {
value: leven(unwrapped.type, curr),
type: curr,
};

if (prev !== undefined && prev.value < next.value) {
return prev;
}

return next;
}, undefined);

throw Error(
`No implementation found for id: ${unwrapped.id} type: ${unwrapped.type}. Did you mean ${similarType}? \n
`No implementation found for id: ${unwrapped.id} type: ${unwrapped.type}. Did you mean ${similarType.type}? \n
Registered Asset matching functions are listed below: \n
${JSON.stringify(matchList)}`,
);
Expand Down
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"resolveJsonModule": true,
"composite": true,
"lib": ["DOM", "ES2020"],
"isolatedDeclarations": true
"isolatedDeclarations": true,
"noUncheckedIndexedAccess": true,
}
}