Skip to content

Commit

Permalink
feat: add types to TS files (src & test)
Browse files Browse the repository at this point in the history
  • Loading branch information
florianstancioiu committed Mar 26, 2024
1 parent 3a2d781 commit b72aa6a
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 12 deletions.
47 changes: 39 additions & 8 deletions src/cosmoz-treenode.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,37 @@
import { component, html } from '@pionjs/pion';
import { Tree, Node } from '@neovici/cosmoz-tree/cosmoz-tree';

export const computePathToRender = (path, hideFromRoot, showMaxNodes) => {
export type ComputePathTextType = {
ownerTree: Tree;
ellipsis: string;
pathToRender: string | undefined;
path: string;
valueProperty: string;
pathSeparator: string | undefined;
};

export type RenderType = {
title: string;
text: string;
};

export type TreenodeType = {
valueProperty: string;
pathSeparator: string;
hideFromRoot: number;
showMaxNodes: number;
keyProperty: string;
keyValue: string;
ownerTree: Tree;
ellipsis: string;
fallback: string;
};

export const computePathToRender = (
path?: string,
hideFromRoot?: number,
showMaxNodes?: number,
) => {
if (!path) {
return;
}
Expand All @@ -20,7 +51,7 @@ export const computePathToRender = (path, hideFromRoot, showMaxNodes) => {
* @param {Array} inputPath Array of path parts
* @returns {Array} Array with defined parts
*/
getKnownPath = (inputPath) => {
getKnownPath = (inputPath: (Node | undefined)[] | null) => {
let path = inputPath;
if (!Array.isArray(path) || path.length === 0) {
return path;
Expand All @@ -36,7 +67,7 @@ export const computePathToRender = (path, hideFromRoot, showMaxNodes) => {
}
return path;
},
computePath = (ownerTree, keyProperty, keyValue) => {
computePath = (ownerTree: Tree, keyProperty: string, keyValue: string) => {
// HACK(pasleq): Cosmoz.Tree API needs to be fixed to avoid such code in components
let path = null;

Expand All @@ -62,13 +93,13 @@ export const computePathToRender = (path, hideFromRoot, showMaxNodes) => {
path,
valueProperty,
pathSeparator,
}) => {
}: ComputePathTextType) => {
if (!pathToRender) {
return '';
}

const stringParts = pathToRender.map((node) =>

Check failure on line 101 in src/cosmoz-treenode.ts

View workflow job for this annotation

GitHub Actions / build / build

Property 'map' does not exist on type 'string'.

Check failure on line 101 in src/cosmoz-treenode.ts

View workflow job for this annotation

GitHub Actions / build / build

Parameter 'node' implicitly has an 'any' type.
ownerTree.getProperty(node, valueProperty)
ownerTree.getProperty(node, valueProperty),
);

let text = stringParts.join(pathSeparator);
Expand All @@ -78,7 +109,7 @@ export const computePathToRender = (path, hideFromRoot, showMaxNodes) => {
}
return text;
},
render = ({ title, text }) => html`
render = ({ title, text }: RenderType) => html`
<style>
:host {
display: block;
Expand Down Expand Up @@ -111,7 +142,7 @@ export const computePathToRender = (path, hideFromRoot, showMaxNodes) => {
ownerTree,
ellipsis = '… / ',
fallback,
}) => {
}: TreenodeType) => {
const path = computePath(ownerTree, keyProperty, keyValue);
if (!path) return render({ text: fallback, title: fallback });
const pathToRender = computePathToRender(path, hideFromRoot, showMaxNodes),

Check failure on line 148 in src/cosmoz-treenode.ts

View workflow job for this annotation

GitHub Actions / build / build

Argument of type '(Node | undefined)[]' is not assignable to parameter of type 'string'.
Expand Down Expand Up @@ -152,5 +183,5 @@ customElements.define(
'show-max-nodes',
'fallback',
],
})
}),
);
8 changes: 4 additions & 4 deletions test/cosmoz-treenode.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const treeBaseUrl = '/node_modules/@neovici/cosmoz-tree/test/data',
};

suite('basic', () => {
let basicFixture, basicTree;
let basicFixture, basicTree: DefaultTree;

suiteSetup(async () => {
basicTree = await treeFromJsonUrl(basicTreeUrl);
Expand Down Expand Up @@ -82,7 +82,7 @@ suite('basic', () => {
});

suite('lookupNodeById', () => {
let basicFixture, basicTree;
let basicFixture, basicTree: DefaultTree;

suiteSetup(async () => {
basicTree = await treeFromJsonUrl(basicTreeUrl);
Expand All @@ -107,7 +107,7 @@ suite('lookupNodeById', () => {
});

suite('multiRoot', () => {
let multiRootFixture, multiRootTree;
let multiRootFixture, multiRootTree: DefaultTree;

suiteSetup(async () => {
multiRootTree = await treeFromJsonUrl(multiRootTreeUrl);
Expand All @@ -132,7 +132,7 @@ suite('multiRoot', () => {
});

suite('missingAncestor', () => {
let missingAncestorFixture, missingAncestorTree;
let missingAncestorFixture, missingAncestorTree: DefaultTree;

suiteSetup(async () => {
missingAncestorTree = await treeFromJsonUrl(missingAncestorTreeUrl);
Expand Down

0 comments on commit b72aa6a

Please sign in to comment.