Skip to content

Commit

Permalink
Use case insensitive regex
Browse files Browse the repository at this point in the history
  • Loading branch information
kmcginnes committed Aug 9, 2024
1 parent 364cbf5 commit dff6b50
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 11 deletions.
12 changes: 9 additions & 3 deletions packages/graph-explorer/src/hooks/usePrefixesUpdater.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ const usePrefixesUpdater = () => {
const setSchema = useSetRecoilState(schemaAtom);
const { enqueueNotification } = useNotification();

const supportsPrefixes = config?.connection?.queryEngine === "sparql"
const existingPrefixes = config?.schema?.prefixes
const supportsPrefixes = config?.connection?.queryEngine === "sparql";
const existingPrefixes = config?.schema?.prefixes;
const activeConfigId = config?.id;

return useCallback(
Expand Down Expand Up @@ -62,7 +62,13 @@ const usePrefixesUpdater = () => {
});
}
},
[supportsPrefixes, existingPrefixes, setSchema, activeConfigId, enqueueNotification]
[
supportsPrefixes,
existingPrefixes,
setSchema,
activeConfigId,
enqueueNotification,
]
);
};

Expand Down
56 changes: 50 additions & 6 deletions packages/graph-explorer/src/utils/generatePrefixes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,15 @@ describe("generatePrefixes", () => {
[
{ prefix: "owl", uri: "https://www.w3.org/2002/07/owl#" },
{ prefix: "dbr", uri: "https://dbpedia.org/resource/" },
{
__inferred: true,
prefix: "loc-r",
uri: "http://www.example.com/location/resource#",
__matches: new Set([
"http://www.example.com/location/resource#London",
"http://www.example.com/location/resource#Manchester",
]),
},
]
);

Expand All @@ -102,25 +111,60 @@ describe("generatePrefixes", () => {
__matches: new Set(["https://dbpedia.org/resource/Qualifying_Rounds"]),
});
expect(updatedPrefixes?.[2]).toEqual({
__inferred: true,
uri: "http://www.example.com/location/resource#",
prefix: "loc-r",
__matches: new Set([
"http://www.example.com/location/resource#London",
"http://www.example.com/location/resource#Manchester",
]),
});
expect(updatedPrefixes?.[3]).toEqual({
__inferred: true,
uri: "http://www.example.com/soccer/ontology/",
prefix: "soc",
__matches: new Set(["http://www.example.com/soccer/ontology/League"]),
});
expect(updatedPrefixes?.[3]).toEqual({
expect(updatedPrefixes?.[4]).toEqual({
__inferred: true,
uri: "http://www.example.com/soccer/resource#",
prefix: "soc-r",
__matches: new Set(["http://www.example.com/soccer/resource#EPL"]),
});
expect(updatedPrefixes?.[4]).toEqual({
});

it("Should update existing prefixes when casing doesn't match", () => {
const updatedPrefixes = generatePrefixes(
[
"http://SecretSpyOrg/entity/quantity",
"http://SecretSpyOrg/entity/other",
"http://SecretSpyOrg/data/hasText",
],
[
{
__inferred: true,
prefix: "ent",
uri: "http://secretspyorg/entity/",
__matches: new Set(["http://SecretSpyOrg/entity/quantity"]),
},
]
);

expect(updatedPrefixes).toHaveLength(2);
expect(updatedPrefixes?.[0]).toEqual({
__inferred: true,
uri: "http://www.example.com/location/resource#",
prefix: "loc-r",
uri: "http://secretspyorg/entity/",
prefix: "ent",
__matches: new Set([
"http://www.example.com/location/resource#London",
"http://www.example.com/location/resource#Manchester",
"http://SecretSpyOrg/entity/quantity",
"http://SecretSpyOrg/entity/other",
]),
});
expect(updatedPrefixes?.[1]).toEqual({
__inferred: true,
uri: "http://secretspyorg/data/",
prefix: "dat",
__matches: new Set(["http://SecretSpyOrg/data/hasText"]),
});
});
});
6 changes: 4 additions & 2 deletions packages/graph-explorer/src/utils/generatePrefixes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,17 +95,18 @@ const generatePrefixes = (
const updatedPrefixes: PrefixTypeConfig[] = cloneDeep(currentPrefixes);
uris.forEach(uri => {
const existInCommon = cPrefixes.some(prefixConfig => {
return !!uri.match(new RegExp(`^${prefixConfig.uri}`));
return !!uri.match(new RegExp(`^${prefixConfig.uri}`, "i"));
});
if (existInCommon) {
return;
}

const existPrefixIndex = updatedPrefixes.findIndex(prefixConfig => {
return !!uri.match(new RegExp(`^${prefixConfig.uri}`));
return !!uri.match(new RegExp(`^${prefixConfig.uri}`, "i"));
});

if (existPrefixIndex === -1) {
// Create a new prefix entry
try {
const url = new URL(uri);
let newPrefix: PrefixTypeConfig;
Expand All @@ -120,6 +121,7 @@ const generatePrefixes = (
// Catching wrong URLs and skip them
}
} else {
// Update existing prefix entry
if (!updatedPrefixes[existPrefixIndex].__matches) {
updatedPrefixes[existPrefixIndex].__matches = new Set();
}
Expand Down

0 comments on commit dff6b50

Please sign in to comment.