Skip to content

Commit

Permalink
fixup! chore: add guard-for-in eslint rule
Browse files Browse the repository at this point in the history
  • Loading branch information
JKRhb committed Feb 1, 2024
1 parent 4f2663f commit 3e98736
Showing 1 changed file with 17 additions and 6 deletions.
23 changes: 17 additions & 6 deletions packages/td-tools/src/td-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,16 @@ import * as TDHelpers from "./td-helpers";

import isAbsoluteUrl = require("is-absolute-url");
import URLToolkit = require("url-toolkit");
import { ThingContext } from "wot-thing-description-types";
import { ThingContext, PropertyElement, ActionElement, EventElement } from "wot-thing-description-types";

// TODO: Refactor and reuse debug solution from core package
import debug from "debug";
const namespace = "node-wot:td-tools:td-parser";
const logDebug = debug(`${namespace}:debug`);
const logWarn = debug(`${namespace}:warn`);

type AffordanceElement = PropertyElement | ActionElement | EventElement;

/** Parses a TD into a Thing object */
export function parseTD(td: string, normalize?: boolean): Thing {
logDebug(`parseTD() parsing\n\`\`\`\n${td}\n\`\`\``);
Expand Down Expand Up @@ -98,15 +100,24 @@ export function parseTD(td: string, normalize?: boolean): Thing {
thing["@type"] = [TD.DEFAULT_THING_TYPE, semType];
}

const adjustBooleanField = (affordance: AffordanceElement, key: string) => {
const currentValue = affordance[key];

if (currentValue === undefined || typeof currentValue !== "boolean") {
affordance[key] = false;
}
};

for (const property of Object.values(thing.properties ?? {})) {
property.readOnly ??= false;
property.writeOnly ??= false;
property.observable ??= false;
for (const key of ["readOnly", "writeOnly", "observable"]) {
adjustBooleanField(property, key);
}
}

for (const action of Object.values(thing.actions ?? {})) {
action.safe ??= false;
action.idempotent ??= false;
for (const key of ["safe", "idempotent"]) {
adjustBooleanField(action, key);
}
}

// avoid errors due to 'undefined'
Expand Down

0 comments on commit 3e98736

Please sign in to comment.