Skip to content

Commit

Permalink
refactor: revert "!== undefined" to "!= null"
Browse files Browse the repository at this point in the history
OR "=== undefined" to "== null"
  • Loading branch information
danielpeintner committed Sep 20, 2023
1 parent 100bdde commit 88330e2
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 64 deletions.
16 changes: 8 additions & 8 deletions packages/td-tools/src/td-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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];
Expand All @@ -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];
Expand All @@ -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;
}

Expand Down
2 changes: 1 addition & 1 deletion packages/td-tools/src/thing-description.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
20 changes: 10 additions & 10 deletions packages/td-tools/src/thing-model-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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"
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -475,7 +475,7 @@ export class ThingModelHelpers {

private static getThingModelRef(data: Record<string, unknown>): Record<string, unknown> {
const refs = {} as Record<string, unknown>;
if (data === undefined) {
if (data == null) {
return refs;
}
for (const key in data) {
Expand Down Expand Up @@ -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];
Expand Down Expand Up @@ -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();
Expand Down
Loading

0 comments on commit 88330e2

Please sign in to comment.