Skip to content

Commit

Permalink
refactor: enrich types from server config api (#200)
Browse files Browse the repository at this point in the history
  • Loading branch information
dhhyi authored and SGrueber committed May 25, 2020
1 parent a838a8a commit 1c71b37
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export interface ServerConfigDataEntry {
id: string;
elements?: ServerConfigDataEntry[];
[key: string]: string | boolean | string[] | ServerConfigDataEntry[];
[key: string]: string | boolean | number | string[] | ServerConfigDataEntry[];
}

export interface ServerConfigData {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ describe('Server Config Mapper', () => {
id: 'services',
elements: [
{ id: 'captcha', siteKey: 'ASDF' },
{ id: 'gtm', token: 'QWERTY', monitor: true },
{ id: 'deeper', elements: [{ id: 'hidden', foo: 'bar' }] },
{ id: 'gtm', token: 'QWERTY', monitor: 'true' },
{ id: 'deeper', elements: [{ id: 'hidden', foo: 'bar', num: 123, alt: '123' }] },
],
},
],
Expand All @@ -41,7 +41,9 @@ describe('Server Config Mapper', () => {
},
"deeper": Object {
"hidden": Object {
"alt": 123,
"foo": "bar",
"num": 123,
},
},
"gtm": Object {
Expand Down
23 changes: 20 additions & 3 deletions src/app/core/models/server-config/server-config.mapper.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,35 @@
import { omit } from 'lodash-es';
import { mapValues, omit } from 'lodash-es';

import { ServerConfigData, ServerConfigDataEntry } from './server-config.interface';
import { ServerConfig } from './server-config.model';

export class ServerConfigMapper {
private static transformType(val) {
if (typeof val === 'string') {
if (!isNaN(+val)) {
return +val;
} else if (val === 'true') {
return true;
} else if (val === 'false') {
return false;
}
}
return val;
}

private static mapEntries(entries: ServerConfigDataEntry[]) {
return entries.reduce(
(acc, entry) => ({
...acc,
[entry.id]: Array.isArray(entry.elements)
? // do recursion if elements array is set
ServerConfigMapper.mapEntries(entry.elements)
: // filter out unnecessary 'id' attribute
omit(entry, 'id'),
: mapValues(
// filter out unnecessary 'id' attribute
omit(entry, 'id'),
// transform string types to better values
ServerConfigMapper.transformType
),
}),
{}
);
Expand Down
2 changes: 1 addition & 1 deletion tslint.json
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@
"from": "lodash.*"
},
{
"import": "^(?!(range|uniq|memoize|once|groupBy|countBy|flatten|isEqual|intersection|omit|pick)$).*",
"import": "^(?!(range|uniq|memoize|once|groupBy|countBy|flatten|isEqual|intersection|omit|pick|mapValues)$).*",
"from": "lodash.*"
},
{
Expand Down

0 comments on commit 1c71b37

Please sign in to comment.