Skip to content

Commit

Permalink
simplify "undefined" check
Browse files Browse the repository at this point in the history
Signed-off-by: Matthias Weirich <matthias.weirich@selectcode.de>
  • Loading branch information
vavido committed Jan 18, 2021
1 parent a692b6a commit e4e25f0
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 16 deletions.
6 changes: 3 additions & 3 deletions javascript/lib/api/src/model/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ export abstract class IndexedEntityModel<EntryType extends EntityModel> implemen
*/
static fromPlainObject<T extends EntityModel>(objectToMap: Object | undefined,
mapValue: (value: any, key: string) => T,
mapKey: (key: string, value: any) => string = key => key): IndexedEntityModel<T> | undefined {
mapKey: (key: string, value: any) => string = key => key): IndexedEntityModel<T> {
if (objectToMap) {
const entries = Object.keys(objectToMap)
.map(k => {
Expand All @@ -113,7 +113,7 @@ export abstract class IndexedEntityModel<EntryType extends EntityModel> implemen
return Object.assign({}, ...entries);
}

return undefined;
return {};
}

/**
Expand All @@ -134,7 +134,7 @@ export abstract class IndexedEntityModel<EntryType extends EntityModel> implemen
*
* @returns The object representation of the Entity.
*/
static toObject<T extends EntityModel>(entityModel: IndexedEntityModel<T>): Object | undefined {
static toObject<T extends EntityModel>(entityModel: IndexedEntityModel<T> | undefined): Object | undefined {
if (entityModel != null) {
return IndexedEntityModel.toPlainObject(entityModel, element => element.toObject());
}
Expand Down
15 changes: 6 additions & 9 deletions javascript/lib/api/src/model/policies.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export class Policy extends EntityWithId {
}

public toObject(): Object {
const entriesObj = this.entries !== undefined ? Entries.toObject(this.entries) : undefined;
const entriesObj = Entries.toObject(this.entries);
return EntityModel.buildObject(new Map<string, any>([
['entries', entriesObj]
]));
Expand Down Expand Up @@ -71,8 +71,7 @@ export class Entries extends IndexedEntityModel<Entry> {
if (o === undefined) {
return o;
}
const entries = IndexedEntityModel.fromPlainObject(o, Entry.fromObject);
return entries != null ? entries : {};
return IndexedEntityModel.fromPlainObject(o, Entry.fromObject);
}

}
Expand Down Expand Up @@ -104,8 +103,8 @@ export class Entry extends EntityWithId {
}

public toObject(): Object {
const subjectsObj = this.subjects !== undefined ? Subjects.toObject(this.subjects) : undefined;
const resourcesObj = this.resources !== undefined ? Resources.toObject(this.resources) : undefined;
const subjectsObj = Subjects.toObject(this.subjects) ;
const resourcesObj = Resources.toObject(this.resources);
return EntityModel.buildObject(new Map<string, any>([
['subjects', subjectsObj],
['resources', resourcesObj]
Expand Down Expand Up @@ -141,8 +140,7 @@ export class Subjects extends IndexedEntityModel<Subject> {
if (o === undefined) {
return o;
}
const subjects = IndexedEntityModel.fromPlainObject(o, Subject.fromObject, key => key);
return subjects != null ? subjects : {};
return IndexedEntityModel.fromPlainObject(o, Subject.fromObject, key => key);
}
}

Expand All @@ -161,8 +159,7 @@ export class Resources extends IndexedEntityModel<Resource> {
if (o === undefined) {
return o;
}
const resources = IndexedEntityModel.fromPlainObject(o, Resource.fromObject);
return resources != null ? resources : {};
return IndexedEntityModel.fromPlainObject(o, Resource.fromObject);
}
}

Expand Down
7 changes: 3 additions & 4 deletions javascript/lib/api/src/model/things.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ export class Thing extends EntityWithId {
}

public toObject(): object {
const featuresObj = this.features ? Features.toObject(this.features) : undefined;
const aclObj = this._acl ? Acl.toObject(this._acl) : undefined;
const featuresObj = Features.toObject(this.features);
const aclObj = Acl.toObject(this._acl);
return EntityModel.buildObject(new Map<string, any>([
['thingId', this.thingId],
['policyId', this.policyId],
Expand Down Expand Up @@ -198,8 +198,7 @@ export class Acl extends IndexedEntityModel<AclEntry> {
if (o === undefined) {
return o;
}
const acl = IndexedEntityModel.fromPlainObject<AclEntry>(o, AclEntry.fromObject);
return acl != null ? acl : {};
return IndexedEntityModel.fromPlainObject<AclEntry>(o, AclEntry.fromObject);
}
}

Expand Down

0 comments on commit e4e25f0

Please sign in to comment.