From 88330e2edaa8d578def38947d727ada3ed192cd9 Mon Sep 17 00:00:00 2001 From: danielpeintner Date: Wed, 20 Sep 2023 10:16:30 +0200 Subject: [PATCH] refactor: revert "!== undefined" to "!= null" OR "=== undefined" to "== null" --- packages/td-tools/src/td-parser.ts | 16 ++-- packages/td-tools/src/thing-description.ts | 2 +- packages/td-tools/src/thing-model-helpers.ts | 20 ++--- .../src/util/asset-interface-description.ts | 78 ++++++++----------- .../test/AssetInterfaceDescriptionTest.ts | 2 +- 5 files changed, 54 insertions(+), 64 deletions(-) diff --git a/packages/td-tools/src/td-parser.ts b/packages/td-tools/src/td-parser.ts index 6612767aa..1311c776f 100644 --- a/packages/td-tools/src/td-parser.ts +++ b/packages/td-tools/src/td-parser.ts @@ -158,7 +158,7 @@ export function parseTD(td: string, normalize?: boolean): Thing { throw new Error(`Form of Property '${propName}' has no href field`); } // check if base field required - if (!isAbsoluteUrl(form.href) && thing.base === undefined) + if (!isAbsoluteUrl(form.href) && thing.base == null) throw new Error(`Form of Property '${propName}' has relative URI while TD has no base field`); // add allForms.push(form); @@ -176,7 +176,7 @@ export function parseTD(td: string, normalize?: boolean): Thing { throw new Error(`Form of Action '${actName}' has no href field`); } // check if base field required - if (!isAbsoluteUrl(form.href) && thing.base === undefined) + if (!isAbsoluteUrl(form.href) && thing.base == null) throw new Error(`Form of Action '${actName}' has relative URI while TD has no base field`); // add allForms.push(form); @@ -194,7 +194,7 @@ export function parseTD(td: string, normalize?: boolean): Thing { throw new Error(`Form of Event '${evtName}' has no href field`); } // check if base field required - if (!isAbsoluteUrl(form.href) && thing.base === undefined) + if (!isAbsoluteUrl(form.href) && thing.base == null) throw new Error(`Form of Event '${evtName}' has relative URI while TD has no base field`); // add allForms.push(form); @@ -233,9 +233,9 @@ export function serializeTD(thing: Thing): string { delete copy.forms; } - if (copy.properties !== undefined && Object.keys(copy.properties).length === 0) { + if (copy.properties != null && Object.keys(copy.properties).length === 0) { delete copy.properties; - } else if (copy.properties !== undefined) { + } else if (copy.properties != null) { // add mandatory fields (if missing): observable, writeOnly, and readOnly for (const propName in copy.properties) { const prop = copy.properties[propName]; @@ -251,9 +251,9 @@ export function serializeTD(thing: Thing): string { } } - if (copy.actions !== undefined && Object.keys(copy.actions).length === 0) { + if (copy.actions != null && Object.keys(copy.actions).length === 0) { delete copy.actions; - } else if (copy.actions !== undefined) { + } else if (copy.actions != null) { // add mandatory fields (if missing): idempotent and safe for (const actName in copy.actions) { const act = copy.actions[actName]; @@ -265,7 +265,7 @@ export function serializeTD(thing: Thing): string { } } } - if (copy.events !== undefined && Object.keys(copy.events).length === 0) { + if (copy.events != null && Object.keys(copy.events).length === 0) { delete copy.events; } diff --git a/packages/td-tools/src/thing-description.ts b/packages/td-tools/src/thing-description.ts index 7756d78a4..b308fbddb 100644 --- a/packages/td-tools/src/thing-description.ts +++ b/packages/td-tools/src/thing-description.ts @@ -67,7 +67,7 @@ export class Form implements TDT.FormElementBase { constructor(href: string, contentType?: string) { this.href = href; - if (contentType !== undefined) this.contentType = contentType; + if (contentType != null) this.contentType = contentType; } } export interface ExpectedResponse { diff --git a/packages/td-tools/src/thing-model-helpers.ts b/packages/td-tools/src/thing-model-helpers.ts index 61634bf85..e16ff50bb 100644 --- a/packages/td-tools/src/thing-model-helpers.ts +++ b/packages/td-tools/src/thing-model-helpers.ts @@ -110,7 +110,7 @@ export class ThingModelHelpers { } if ("links" in data && Array.isArray(data.links)) { const foundTmExtendsRel = data.links.find((link) => link.rel === "tm:extends"); - if (foundTmExtendsRel !== undefined) return true; + if (foundTmExtendsRel != null) return true; } if (data.properties !== undefined) { @@ -138,7 +138,7 @@ export class ThingModelHelpers { public static getModelVersion(data: ThingModel): string | undefined { if ( "version" in data && - data.version !== undefined && + data.version != null && typeof data.version === "object" && "model" in data.version && typeof data.version.model === "string" @@ -343,7 +343,7 @@ export class ThingModelHelpers { for (const aff in affRefs) { const affUri = affRefs[aff] as string; const refObj = this.parseTmRef(affUri); - if (refObj.uri === undefined) { + if (refObj.uri == null) { throw new Error(`Missing remote path in ${affUri}`); } let source = await this.fetchModel(refObj.uri); @@ -376,7 +376,7 @@ export class ThingModelHelpers { if (!options) { options = {} as CompositionOptions; } - if (options.baseUrl === undefined) { + if (options.baseUrl == null) { options.baseUrl = "."; } const newTMHref = this.returnNewTMHref(options.baseUrl, title); @@ -405,7 +405,7 @@ export class ThingModelHelpers { for (const key in submodelObj) { const sub = submodelObj[key]; - if (options.selfComposition !== undefined) { + if (options.selfComposition != null) { if (!data.links) { throw new Error( "You used self composition but links are missing; they are needed to extract the instance name" @@ -415,7 +415,7 @@ export class ThingModelHelpers { const index = data.links.findIndex((el) => el.href === key); const el = data.links[index]; const instanceName = el.instanceName; - if (instanceName === undefined) { + if (instanceName == null) { throw new Error("Self composition is not possible without instance names"); } // self composition enabled, just one TD expected @@ -449,7 +449,7 @@ export class ThingModelHelpers { } } } - if (!data.links || options.selfComposition !== undefined) { + if (!data.links || options.selfComposition != null) { data.links = []; } // add reference to the thing model @@ -475,7 +475,7 @@ export class ThingModelHelpers { private static getThingModelRef(data: Record): Record { const refs = {} as Record; - if (data === undefined) { + if (data == null) { return refs; } for (const key in data) { @@ -508,7 +508,7 @@ export class ThingModelHelpers { extendedModel.properties = {}; } for (const key in properties) { - if (dest.properties !== undefined && dest.properties[key] !== undefined) { + if (dest.properties != null && dest.properties[key] != null) { extendedModel.properties[key] = { ...properties[key], ...dest.properties[key] }; } else { extendedModel.properties[key] = properties[key]; @@ -660,7 +660,7 @@ export class ThingModelHelpers { } private removeDependency(dep?: string) { - if (dep !== undefined) { + if (dep != null) { this.deps = this.deps.filter((el) => el !== dep); } else { this.deps.pop(); diff --git a/packages/td-tools/src/util/asset-interface-description.ts b/packages/td-tools/src/util/asset-interface-description.ts index 581923c01..e7d1c677f 100644 --- a/packages/td-tools/src/util/asset-interface-description.ts +++ b/packages/td-tools/src/util/asset-interface-description.ts @@ -218,7 +218,7 @@ export class AssetInterfaceDescriptionUtil { } private updateProtocolPrefixes(forms: [FormElementBase, ...FormElementBase[]], protocols: string[]): void { - if (forms !== undefined) { + if (forms != null) { for (const interactionForm of forms) { if (interactionForm.href != null) { const positionColon = interactionForm.href.indexOf(":"); @@ -234,7 +234,7 @@ export class AssetInterfaceDescriptionUtil { } private getBaseFromEndpointMetadata(endpointMetadata?: Record): string { - if (endpointMetadata?.value !== undefined && endpointMetadata.value instanceof Array) { + if (endpointMetadata?.value != null && endpointMetadata.value instanceof Array) { for (const v of endpointMetadata.value) { if (v.idShort === "base") { // e.g., "value": "modbus+tcp://192.168.1.187:502" @@ -264,18 +264,18 @@ export class AssetInterfaceDescriptionUtil { [k: string]: SecurityScheme; } = {}; - if (endpointMetadata?.value !== undefined && endpointMetadata.value instanceof Array) { + if (endpointMetadata?.value != null && endpointMetadata.value instanceof Array) { for (const v of endpointMetadata.value) { if (v.idShort === "securityDefinitions") { // const securitySchemes: Array = []; - if (v.value !== undefined && v.value instanceof Array) { + if (v.value != null && v.value instanceof Array) { for (const securityDefinitionsValues of v.value) { - if (securityDefinitionsValues.idShort !== undefined) { + if (securityDefinitionsValues.idShort != null) { // key if (securityDefinitionsValues.value instanceof Array) { for (const securityDefinitionsValue of securityDefinitionsValues.value) { if (securityDefinitionsValue.idShort === "scheme") { - if (securityDefinitionsValue.value !== undefined) { + if (securityDefinitionsValue.value != null) { const ss: SecurityScheme = { scheme: securityDefinitionsValue.value }; securityDefinitions[securityDefinitionsValues.idShort] = ss; } @@ -295,12 +295,12 @@ export class AssetInterfaceDescriptionUtil { endpointMetadata?: Record ): string | [string, ...string[]] { const security: string[] = []; - if (endpointMetadata?.value !== undefined && endpointMetadata.value instanceof Array) { + if (endpointMetadata?.value != null && endpointMetadata.value instanceof Array) { for (const v of endpointMetadata.value) { if (v.idShort === "security") { - if (v.value !== undefined && v.value instanceof Array) { + if (v.value != null && v.value instanceof Array) { for (const securityValue of v.value) { - if (securityValue.value !== undefined) { + if (securityValue.value != null) { security.push(securityValue.value); } } @@ -346,16 +346,16 @@ export class AssetInterfaceDescriptionUtil { } } else if (typeof v.idShort === "string" && v.idShort.length > 0) { // TODO is this still relevant? - // pick *any* value (and possibly override, e. g, contentType) + // pick *any* value (and possibly override, e.g. contentType) // TODO Should we add all value's (e.g., dataMapping might be empty array) ? // if (typeof v.value === "string" ||typeof v.value === "number" || typeof v.value === "boolean") { - if (v.value !== undefined) { + if (v.value != null) { form[v.idShort] = v.value; // use valueType to convert the string value if ( - v.valueType !== undefined && - v.valueType.dataObjectType !== undefined && - v.valueType.dataObjectType.name !== undefined && + v.valueType != null && + v.valueType.dataObjectType != null && + v.valueType.dataObjectType.name != null && typeof v.valueType.dataObjectType.name === "string" ) { // XSD schemaTypes, https://www.w3.org/TR/xmlschema-2/#built-in-datatypes @@ -401,18 +401,14 @@ export class AssetInterfaceDescriptionUtil { ): void { if ( submodel instanceof Object && - submodel.idShort !== undefined && + submodel.idShort != null && submodel.idShort === "AssetInterfacesDescription" ) { - if (submodel.submodelElements !== undefined && submodel.submodelElements instanceof Array) { + if (submodel.submodelElements != null && submodel.submodelElements instanceof Array) { for (const submodelElement of submodel.submodelElements) { if (submodelElement instanceof Object) { logDebug("SubmodelElement.idShort: " + submodelElement.idShort); - if ( - submodelRegex !== undefined && - typeof submodelRegex === "string" && - submodelRegex.length > 0 - ) { + if (submodelRegex != null && typeof submodelRegex === "string" && submodelRegex.length > 0) { const regex = new RegExp(submodelRegex); if (!regex.test(submodelElement.idShort)) { logInfo("submodel not of interest"); @@ -429,7 +425,7 @@ export class AssetInterfaceDescriptionUtil { private processSubmodelElement(smInformation: SubmodelInformation, submodelElement: Record): void { // EndpointMetadata vs. InterfaceMetadata - if (submodelElement.value !== undefined && submodelElement.value instanceof Array) { + if (submodelElement.value != null && submodelElement.value instanceof Array) { // Note: iterate twice over to collect first EndpointMetadata let endpointMetadata: Record = {}; for (const smValue of submodelElement.value) { @@ -453,7 +449,7 @@ export class AssetInterfaceDescriptionUtil { if (smValue instanceof Object) { if (smValue.idShort === "InterfaceMetadata") { logInfo("InterfaceMetadata"); - if (smValue.value !== undefined && smValue.value instanceof Array) { + if (smValue.value != null && smValue.value instanceof Array) { for (const interactionValue of smValue.value) { if (interactionValue.idShort === "Properties") { if (interactionValue.value instanceof Array) { @@ -517,7 +513,7 @@ export class AssetInterfaceDescriptionUtil { thing: new Map>(), }; - if (aidModel instanceof Object && aidModel.submodels !== undefined) { + if (aidModel instanceof Object && aidModel.submodels != null) { if (aidModel.submodels instanceof Array) { for (const submodel of aidModel.submodels) { this.processSubmodel(smInformation, submodel, submodelRegex); @@ -529,7 +525,7 @@ export class AssetInterfaceDescriptionUtil { } private _transform(smInformation: SubmodelInformation, template?: string): string { - const thing: Thing = template !== undefined ? JSON.parse(template) : {}; + const thing: Thing = template != null ? JSON.parse(template) : {}; // walk over thing information and set them for (const [key, value] of smInformation.thing) { @@ -541,7 +537,7 @@ export class AssetInterfaceDescriptionUtil { } // required TD fields - if (thing["@context"] === undefined) { + if (thing["@context"] == null) { thing["@context"] = "https://www.w3.org/2022/wot/td/v1.1"; } if (!thing.title) { @@ -551,7 +547,7 @@ export class AssetInterfaceDescriptionUtil { // Security in AID is defined for each submodel // add "securityDefinitions" globally and add them on form level if necessary // TODO: possible collisions for "security" names *could* be handled by cnt - if (thing.securityDefinitions === undefined) { + if (thing.securityDefinitions == null) { thing.securityDefinitions = {}; } // let cnt = 1; @@ -602,10 +598,7 @@ export class AssetInterfaceDescriptionUtil { const tdDescription: Record = {}; if (aasDescription instanceof Array) { for (const aasDescriptionEntry of aasDescription) { - if ( - aasDescriptionEntry.language !== undefined && - aasDescriptionEntry.text !== undefined - ) { + if (aasDescriptionEntry.language != null && aasDescriptionEntry.text != null) { const language: string = aasDescriptionEntry.language; const text: string = aasDescriptionEntry.text; tdDescription[language] = text; @@ -623,10 +616,10 @@ export class AssetInterfaceDescriptionUtil { thing.properties[key].type = interactionValue.value; } } else if (interactionValue.idShort === "range") { - if (interactionValue.min !== undefined) { + if (interactionValue.min != null) { thing.properties[key].min = interactionValue.min; } - if (interactionValue.max !== undefined) { + if (interactionValue.max != null) { thing.properties[key].max = interactionValue.max; } } else if (interactionValue.idShort === "observable") { @@ -704,7 +697,7 @@ export class AssetInterfaceDescriptionUtil { const values: Array = []; // base ? - if (td.base !== undefined) { + if (td.base != null) { values.push({ idShort: "base", valueType: "xs:anyURI", @@ -725,7 +718,7 @@ export class AssetInterfaceDescriptionUtil { // security const securityValues: Array = []; - if (td.security !== undefined) { + if (td.security != null) { for (const secKey of td.security) { securityValues.push({ valueType: "xs:string", @@ -789,10 +782,7 @@ export class AssetInterfaceDescriptionUtil { let formElementPicked: FormElementBase | undefined; if (propertyValue.forms) { for (const formElementProperty of propertyValue.forms) { - if ( - formElementProperty.href !== undefined && - formElementProperty.href.startsWith(protocol) - ) { + if (formElementProperty.href != null && formElementProperty.href.startsWith(protocol)) { formElementPicked = formElementProperty; // found matching form --> abort loop break; @@ -806,7 +796,7 @@ export class AssetInterfaceDescriptionUtil { const propertyValues: Array = []; // type - if (propertyValue.type !== undefined) { + if (propertyValue.type != null) { propertyValues.push({ idShort: "type", valueType: "xs:string", @@ -815,7 +805,7 @@ export class AssetInterfaceDescriptionUtil { }); } // title - if (propertyValue.title !== undefined) { + if (propertyValue.title != null) { propertyValues.push({ idShort: "title", valueType: "xs:string", @@ -824,7 +814,7 @@ export class AssetInterfaceDescriptionUtil { }); } // observable - if (propertyValue.observable !== undefined) { + if (propertyValue.observable != null) { propertyValues.push({ idShort: "observable", valueType: "xs:boolean", @@ -836,7 +826,7 @@ export class AssetInterfaceDescriptionUtil { // range and others? Simply add them as is? // forms - if (formElementPicked !== undefined) { + if (formElementPicked != null) { const propertyForm: Array = []; // TODO AID for now supports just *one* href/form @@ -874,7 +864,7 @@ export class AssetInterfaceDescriptionUtil { text: langValue, }); } - } else if (propertyValue.description !== undefined) { + } else if (propertyValue.description != null) { // fallback description = []; description.push({ diff --git a/packages/td-tools/test/AssetInterfaceDescriptionTest.ts b/packages/td-tools/test/AssetInterfaceDescriptionTest.ts index 2ff6e8dd6..3c582d087 100644 --- a/packages/td-tools/test/AssetInterfaceDescriptionTest.ts +++ b/packages/td-tools/test/AssetInterfaceDescriptionTest.ts @@ -343,7 +343,7 @@ class AssetInterfaceDescriptionUtilTest { expect(hasObservable).to.equal(true); expect(hasForms).to.equal(true); } - if (propertyValue.description !== undefined) { + if (propertyValue.description != null) { hasPropertyStatusDescription = true; expect(propertyValue) .to.have.property("description")