Skip to content

Commit

Permalink
Code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
JrMasterModelBuilder committed Sep 22, 2023
1 parent bf7efca commit 3a26a60
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 60 deletions.
39 changes: 9 additions & 30 deletions src/value/array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,9 @@ import {IToXmlOptioned} from '../options';
import {IElement, xmlElementChildElements} from '../util';
import {Value} from '../value';

import {ValueBoolean} from './boolean';
import {ValueData} from './data';
import {ValueDate} from './date';
import {ValueDict} from './dict';
import {ValueInteger} from './integer';
import {ValueReal} from './real';
import {ValueString} from './string';
import * as tags from './index';

let childTagNames: Map<string, new () => Value>;
let childTagNames: Readonly<Map<string, new () => Value>>;

/**
* ValueArray object.
Expand All @@ -34,33 +28,17 @@ export class ValueArray extends Value {
public static get CHILD_TAG_NAMES() {
if (!childTagNames) {
childTagNames = new Map();
for (const Value of [
ValueArray,
ValueBoolean,
ValueData,
ValueDate,
ValueDict,
ValueInteger,
ValueReal,
ValueString
]) {
for (const t of Value.TAG_NAMES) {
childTagNames.set(t, Value);
for (const ValueType of Object.values(tags)) {
if (ValueType && ValueType.prototype instanceof Value) {
for (const t of ValueType.TAG_NAMES) {
childTagNames.set(t, ValueType);
}
}
}
}
return childTagNames;
}

/**
* Child types.
*
* @returns Child tag names map.
*/
public get childTagNames() {
return (this.constructor as typeof ValueArray).CHILD_TAG_NAMES;
}

/**
* Value value.
*/
Expand Down Expand Up @@ -193,7 +171,8 @@ export class ValueArray extends Value {
*/
public childFromXmlElement(element: Readonly<IElement>) {
const type = element.tagName;
const Value = this.childTagNames.get(type) || null;
const {CHILD_TAG_NAMES} = this.constructor as typeof ValueArray;
const Value = CHILD_TAG_NAMES.get(type) || null;
if (!Value) {
throw new Error(`Unknown element type: ${type}`);
}
Expand Down
39 changes: 9 additions & 30 deletions src/value/dict.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,9 @@ import {IToXmlOptioned} from '../options';
import {IElement, xmlElementChildElements} from '../util';
import {Value} from '../value';

import {ValueArray} from './array';
import {ValueBoolean} from './boolean';
import {ValueData} from './data';
import {ValueDate} from './date';
import {ValueInteger} from './integer';
import {ValueReal} from './real';
import {ValueString} from './string';
import * as tags from './index';

let childTagNames: Map<string, new () => Value>;
let childTagNames: Readonly<Map<string, new () => Value>>;

/**
* ValueDict object.
Expand All @@ -34,33 +28,17 @@ export class ValueDict extends Value {
public static get CHILD_TAG_NAMES() {
if (!childTagNames) {
childTagNames = new Map();
for (const Value of [
ValueArray,
ValueBoolean,
ValueData,
ValueDate,
ValueDict,
ValueInteger,
ValueReal,
ValueString
]) {
for (const t of Value.TAG_NAMES) {
childTagNames.set(t, Value);
for (const ValueType of Object.values(tags)) {
if (ValueType && ValueType.prototype instanceof Value) {
for (const t of ValueType.TAG_NAMES) {
childTagNames.set(t, ValueType);
}
}
}
}
return childTagNames;
}

/**
* Child types.
*
* @returns Child tag names map.
*/
public get childTagNames() {
return (this.constructor as typeof ValueArray).CHILD_TAG_NAMES;
}

/**
* Value value.
*/
Expand Down Expand Up @@ -178,7 +156,8 @@ export class ValueDict extends Value {
*/
public childFromXmlElement(element: Readonly<IElement>) {
const type = element.tagName;
const Value = this.childTagNames.get(type) || null;
const {CHILD_TAG_NAMES} = this.constructor as typeof ValueDict;
const Value = CHILD_TAG_NAMES.get(type) || null;
if (!Value) {
throw new Error(`Unknown element type: ${type}`);
}
Expand Down

0 comments on commit 3a26a60

Please sign in to comment.