Skip to content
New issue

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

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

Already on GitHub? # to your account

feat(ontology-find-icon-helper): add package #117

Merged
merged 12 commits into from
Dec 2, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -254,13 +254,13 @@ describe("OntologyService - Integration Test with Fuseki - Create Data", () => {
});

it("should retrieve all styles if an empty array is passed in", async () => {
const styles = await os.getFlattenedStyles([]);
const styles = await os.PROPOSED_getFlattenedStyles([]);
expect(styles.length).toEqual(3);
});

it("should retrieve the icon for the target uri", async () => {
const styles = await os.getFlattenedStyles([]);
const icon = os.findIcon(
const styles = await os.PROPOSED_getFlattenedStyles([]);
const icon = os.PROPOSED_findIcon(
styles,
"http://ies.data.gov.uk/ontology/ies4#Accent",
);
Expand All @@ -278,8 +278,8 @@ describe("OntologyService - Integration Test with Fuseki - Create Data", () => {
});

it("should retrieve fallback text and colours if the targeturi does not exist", async () => {
const styles = await os.getFlattenedStyles([]);
const icon = os.findIcon(
const styles = await os.PROPOSED_getFlattenedStyles([]);
const icon = os.PROPOSED_findIcon(
styles,
"http://ies.data.gov.uk/ontology/ies4#IDontExist",
);
Expand All @@ -294,8 +294,8 @@ describe("OntologyService - Integration Test with Fuseki - Create Data", () => {
});

it("should handle an incorrect uri without crashing the app", async () => {
const styles = await os.getFlattenedStyles([]);
const icon = os.findIcon(
const styles = await os.PROPOSED_getFlattenedStyles([]);
const icon = os.PROPOSED_findIcon(
styles,
"InvalidUrl",
);
Expand Down
6 changes: 4 additions & 2 deletions packages/OntologyService/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ export const FlattenedStyle = z.object({

export type FlattenedStyleType = z.infer<typeof FlattenedStyle>;

export type FlattenedStyleTypeForFindIcon = Omit<FlattenedStyleType, "shape" | "faUnicode" | "faIcon">;
export const IconStyle = z.object({
classUri: z.string(),
backgroundColor: z.string(),
Expand All @@ -110,6 +111,7 @@ export const IconStyle = z.object({
faUnicode: z.string().optional(),
shape: z.string().optional(),
});
export type IconStyleType = z.infer<typeof IconStyle>;

export const IconStyleArray = z.array(IconStyle);

Expand Down Expand Up @@ -1403,7 +1405,7 @@ export class OntologyService extends RdfService {
}

// DS specific methods
async getFlattenedStyles(classes: LongURI[]) {
async PROPOSED_getFlattenedStyles(classes: LongURI[]) {
const styles = await this.getStyles(classes);
return Object.entries(styles).map(([classUri, style]) => {
return ({
Expand All @@ -1419,7 +1421,7 @@ export class OntologyService extends RdfService {
});
}

findIcon(styles: FlattenedStyleType[], classUri: LongURI) {
PROPOSED_findIcon(styles: FlattenedStyleType[], classUri: LongURI) {
const foundIcon = styles.find((style) => style.classUri === classUri);

if (foundIcon) return foundIcon;
Expand Down
42 changes: 42 additions & 0 deletions packages/RdfService/schema.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { URISegmentOrHashSchema } from "./schema";

test("Fail when input is not a uri", () => {
expect(() => URISegmentOrHashSchema.parse("abd"))
.toThrowErrorMatchingInlineSnapshot(`
"[
{
"validation": "regex",
"code": "invalid_string",
"message": "\\n Invalid URI format. \\n Ensure it starts with a valid scheme and is followed by '://',\\n then a valid resource part without spaces.",
"path": []
},
{
"code": "custom",
"message": "URI must include either a hash or at least one URI segment.",
"path": []
}
]"
`);
});
test("Fail when input is uri without hash or url segment", () => {
expect(() => URISegmentOrHashSchema.parse("http://example.com"))
.toThrowErrorMatchingInlineSnapshot(`
"[
{
"code": "custom",
"message": "URI must include either a hash or at least one URI segment.",
"path": []
}
]"
`);
});
test("Pass when input is uri with hash", () => {
expect(() =>
URISegmentOrHashSchema.parse("http://example.com#blah")
).not.toThrow();
});
test("Pass when input is uri with url segment", () => {
expect(() =>
URISegmentOrHashSchema.parse("http://example.com/blah")
).not.toThrow();
});
29 changes: 28 additions & 1 deletion packages/RdfService/schema.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,32 @@
import z from 'zod';
import { isValidURI } from './isValidURI';
import { isValidURI, permissiveUriRegex } from './isValidURI';


/**
* RE: Name "URISegmentOrHashSchema"
* the requirement for a url segment or a hash is likely mentioned
* in specs - I prefer naming it literally for readability and grepping
*/
export const URISegmentOrHashSchema = z.string().regex(permissiveUriRegex, {
message: `
Invalid URI format.
Ensure it starts with a valid scheme and is followed by '://',
then a valid resource part without spaces.`,
}).refine(value => {
try {
const url = new URL(value);
// Check for hash or at least one path segment
return url.hash !== '' || (url.pathname !== '/' && url.pathname !== '');
} catch {
return false;
}
}, {
message: "URI must include either a hash or at least one URI segment."
});






// Schema for the object representing "o", "p", or "s" within each triple
Expand Down
3 changes: 2 additions & 1 deletion packages/RdfService/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { z } from 'zod';
import { RDFSchema, RDFTripleSchema } from './schema';
import { RDFSchema, RDFTripleSchema, URISegmentOrHashSchema } from './schema';

// Schema for the RDF triple
export type RDFTripleType = z.infer<typeof RDFTripleSchema>;
export type RDFType = z.infer<typeof RDFSchema>;
export type URISegmentOrHashType = z.infer<typeof URISegmentOrHashSchema>; // Lossy type
24 changes: 24 additions & 0 deletions packages/ontology-find-icon-helper/.eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
module.exports = {
plugins: [
"@typescript-eslint/eslint-plugin",
"eslint-plugin-tsdoc"
],
extends: [
'plugin:@typescript-eslint/recommended'
],
ignorePatterns: ["dist/"],
parser: '@typescript-eslint/parser',
parserOptions: {
project: "./tsconfig.json",
tsconfigRootDir: __dirname,
ecmaVersion: 2018,
sourceType: "module"
},
rules: {
"tsdoc/syntax": "warn",
"@typescript-eslint/no-floating-promises": ["error", {
"ignoreVoid": false,
"ignoreIIFE": false
}]
}
};
1 change: 1 addition & 0 deletions packages/ontology-find-icon-helper/.nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
20.14.0
48 changes: 48 additions & 0 deletions packages/ontology-find-icon-helper/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# @telicent-oss/ontology-find-icon-helper

*Monorepo Location: `./packages/ontology-find-icon-helper`*

A helper for frontend apps to load and access ontology icons from ontology styles

### Install

```bash
yarn install @telicent-oss/ontology-find-icon-helper
```

### Usage

```ts
import { OntologyFindIconHelper } from "@company-oss/ontology-find-icon-helper";

// Initialize the CatalogService
const catalogService = await CatalogService.createAsync({
writeEnabled: true,
triplestoreUri: "http://localhost:3030",
dataset: "dataset",
});

// Create Catalog
const catalog1 = await DCATCatalog.createAsync(
catalogService,
"http://mysche.ma/data/catalog1",
"Catalog",
"2023-01-01"
);
```

## Development

### Build

```bash
git clone https://github.com/Telicent-oss/rdf-libraries
cd rdf-libraries
yarn install
cd /packages/ontology-find-icon-helper
# make changes
npx nx affected:build
```

* [./packages/OntologyService](https://github.com/telicent-oss/rdf-libraries/tree/main/packages/OntologyService) - consumes to get ontology styles (and icons)f

15 changes: 15 additions & 0 deletions packages/ontology-find-icon-helper/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/* eslint-disable */
export default {
displayName: "@telicent-oss/ontology-find-icon-helper",
preset: "../../jest.preset.js",
coverageDirectory: "../../coverage/packages/ontology-find-icon-helper",
automock: false,
setupFiles: ["./setupTests.ts"],
globals: {
"ts-jest": {
tsconfig: {
tsconfig: "./tsconfig.spec.json",
},
},
},
};
62 changes: 62 additions & 0 deletions packages/ontology-find-icon-helper/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
{
"name": "@telicent-oss/ontology-find-icon-helper",
"version": "0.0.1",
"private": false,
"description": "Simple client-side library retrieving and accessing ontology style/icon objects.",
"main": "./dist/ontology-find-icon-helper.js",
"module": "./dist/ontology-find-icon-helper.js",
"files": [
"dist"
],
"type": "module",
"exports": {
".": {
"import": "./dist/ontology-find-icon-helper.js",
"require": "./dist/ontology-find-icon-helper.umd.js",
"types": "./dist/types.d.ts"
},
"./index": {
"import": "./dist/ontology-find-icon-helper.js",
"require": "./dist/ontology-find-icon-helper.umd.cjs",
"types": "./dist/types.d.ts"
}
},
"types": "./dist/types.d.ts",
"scripts": {
"test": "jest --runInBand ",
"build": "rm -rf ./dist && vite build",
"lint": "eslint **/*.ts"
},
"author": "",
"license": "Apache-2.0",
"repository": {
"type": "git",
"url": "git+https://github.com/telicent-oss/rdf-libraries.git"
},
"publishConfig": {
"registry": "https://www.npmjs.com/"
},
"dependencies": {
"@telicent-oss/ontologyservice": "*",
"lodash.startcase": "^4.4.0",
"zod": "^3.22.4"
},
"devDependencies": {
"@types/jest": "^29.4.0",
"@types/lodash.startcase": "^4.4.9",
"@types/node": "16.11.7",
"ts-jest": "^29.1.1",
"ts-node": "^10.9.1",
"tsc": "^2.0.4",
"typescript": "^5.5.4",
"vite": "^4.5.3",
"vite-plugin-dts": "^3.6.3",
"vite-tsconfig-paths": "^4.2.1"
},
"engines": {
"node": ">=20.14.0"
},
"resolutions": {
"rollup": "3.29.5"
}
}
25 changes: 25 additions & 0 deletions packages/ontology-find-icon-helper/project.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"name": "@telicent-oss/ontology-find-icon-helper",
"$schema": "../../node_modules/nx/schemas/project-schema.json",
"targets": {
"test": {
"executor": "nx:run-commands",
"options": {
"commands": [
{
"command": "yarn test"
}
],
"cwd": "packages/ontology-find-icon-helper",
"parallel": false
}
},
"printhello": {
"executor": "nx:run-commands",
"outputs": [],
"options": {
"command": "echo hello"
}
}
}
}
Empty file.
Loading