Skip to content

Commit c60130b

Browse files
changed config format
1 parent 3ea8125 commit c60130b

File tree

5 files changed

+77
-43
lines changed

5 files changed

+77
-43
lines changed

src/config.ts

Lines changed: 47 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,51 @@
1-
type typeofTypes = 'string' | 'boolean' | 'function' | 'number' | 'object' | 'symbol' | 'undefined' | 'bigint';
1+
import { ConfigType, TypeofTypes } from 'src/config.types';
22

3-
export const brackets_colors = ['#ffd700', '#da70d6', '#179fff'];
4-
5-
export const typeColors = (type: typeofTypes) => {
6-
switch (type) {
7-
case 'bigint': return '#b5cea8';
8-
case 'number': return '#b5cea8';
9-
case 'object': return '#569cd6';
10-
case 'undefined': return '#569cd6';
11-
case 'boolean': return '#569cd6';
12-
default: return '#ce9178';
3+
const config: ConfigType = {
4+
colors: {
5+
main_text: '#ffffff',
6+
background: '#1e1e1e',
7+
header: '#252526',
8+
icon_color: '#ffd700',
9+
indent_lines: '#404040',
10+
line_index: '#6e7681',
11+
keys: '#9cdcfe'
12+
},
13+
/**
14+
* Get the color associated with a JavaScript data type.
15+
*
16+
* @param {TypeofTypes} type - String-represented JavaScript data type of the current value.
17+
* @returns {string} Hex color code corresponding to the provided data type.
18+
*/
19+
typeColor: (type: TypeofTypes): string => {
20+
switch (type) {
21+
case 'bigint': return '#b5cea8';
22+
case 'number': return '#b5cea8';
23+
case 'object': return '#569cd6';
24+
case 'undefined': return '#569cd6';
25+
case 'boolean': return '#569cd6';
26+
default: return '#ce9178';
27+
}
28+
},
29+
/**
30+
* Get value quotes if needed.
31+
*
32+
* @param {TypeofTypes} type - String-represented JavaScript data type of the current value.
33+
* @returns {string} Quotes if needed.
34+
*/
35+
typeQuotes: (type: TypeofTypes): string => {
36+
const quotesNeeded: TypeofTypes[] = ['string', 'symbol'];
37+
return quotesNeeded.includes(type) ? '"' : '';
38+
},
39+
/**
40+
* Get the color of brackets depending on the deep nesting of the object.
41+
*
42+
* @param {number} depth - Current object depth.
43+
* @returns {string} Hex color code corresponding to the provided depth.
44+
*/
45+
bracketsColors: (depth: number): string => {
46+
const brackets_colors = ['#ffd700', '#da70d6', '#179fff'];
47+
return brackets_colors[depth % brackets_colors.length];
1348
}
1449
}
1550

16-
export const typeQuotes = (type: typeofTypes) => {
17-
const quotesNeeded: typeofTypes[] = ['string', 'symbol'];
18-
return quotesNeeded.includes(type) ? '"' : '';
19-
}
20-
21-
export const colors = {
22-
main_text: '#ffffff',
23-
background: '#1e1e1e',
24-
header: '#252526',
25-
icon_color: '#ffd700',
26-
indent_lines: '#404040',
27-
line_index: '#6e7681',
28-
keys: '#9cdcfe'
29-
}
51+
export default config;

src/config.types.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
export type TypeofTypes = 'string' | 'boolean' | 'function' | 'number' | 'object' | 'symbol' | 'undefined' | 'bigint';
2+
3+
export type ConfigType = {
4+
colors: {
5+
main_text: string,
6+
background: string,
7+
header: string,
8+
icon_color: string,
9+
indent_lines: string,
10+
line_index: string,
11+
keys: string
12+
},
13+
typeColor: (type: TypeofTypes) => string,
14+
typeQuotes: (type: TypeofTypes) => string,
15+
bracketsColors: (depth: number) => string
16+
}

src/json_parser/parser.service.ts

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Injectable } from '@nestjs/common';
2-
import { brackets_colors, colors, typeColors, typeQuotes } from 'src/config';
2+
import config from 'src/config';
33

44
type AnyObject = Record<string, any>;
55

@@ -21,8 +21,8 @@ export class ParserService {
2121
const nextIndent = indent * (depth + 1);
2222

2323
if (typeof obj !== "object" || obj === null || obj === undefined) {
24-
const quotes = typeQuotes(typeof obj);
25-
const color = typeColors(typeof obj);
24+
const quotes = config.typeQuotes(typeof obj);
25+
const color = config.typeColor(typeof obj);
2626
return `<tspan style="fill: ${color};">${quotes}${obj}${quotes}</tspan>`;
2727
}
2828

@@ -32,16 +32,12 @@ export class ParserService {
3232
const comma = index < array.length - 1 ? ',' : '';
3333

3434
return `<tspan x="${nextIndent}" dy="19">` +
35-
`<tspan style="fill: ${colors.keys};">"${key}"</tspan>: ${formattedValue}${comma}` +
35+
`<tspan style="fill: ${config.colors.keys};">"${key}"</tspan>: ${formattedValue}${comma}` +
3636
`</tspan>`;
3737
})
3838
.join(`\n`);
3939

40-
const bracket_color = brackets_colors[
41-
depth < brackets_colors.length ?
42-
depth :
43-
depth - (Math.floor(depth / brackets_colors.length) * brackets_colors.length)
44-
];
40+
const bracket_color = config.bracketsColors(depth);
4541

4642
if (currentIndent === 0) {
4743
return (

src/widget/widget.controller.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { Get, Controller, Render, Header, Res, Query } from '@nestjs/common';
22
import type { Response } from 'express';
33
import { WidgetService } from './widget.service';
44
import { ParserService } from 'src/json_parser/parser.service';
5-
import { colors } from 'src/config';
5+
import config from 'src/config';
66

77

88
@Controller()
@@ -35,11 +35,11 @@ export class WidgetController {
3535
height_: 60 + (lines_count * 19),
3636
height_main_line: (lines_count - 2) * 19,
3737

38-
background_color: colors.background,
39-
header_color: colors.header,
40-
icon_color: colors.icon_color,
41-
indent_color: colors.indent_lines,
42-
main_color: colors.main_text
38+
background_color: config.colors.background,
39+
header_color: config.colors.header,
40+
icon_color: config.colors.icon_color,
41+
indent_color: config.colors.indent_lines,
42+
main_color: config.colors.main_text
4343
};
4444
}
4545
}

src/widget/widget.service.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Injectable } from '@nestjs/common';
22
import { APIService } from 'src/apis/apis.service';
3-
import { colors } from 'src/config';
3+
import config from 'src/config';
44
import { ObjectStructureInfo } from 'src/json_parser/parser.service';
55

66

@@ -116,7 +116,7 @@ export class WidgetService {
116116
generate_indexes(count: number) {
117117
const array = [];
118118
for (let index = 1; index <= count; index++) {
119-
array.push(`<tspan x="${index < 10 ? '9' : '0'}" dy="${index === 1 ? '0' : '19'}" fill="${colors.line_index}">${index}</tspan>`);
119+
array.push(`<tspan x="${index < 10 ? '9' : '0'}" dy="${index === 1 ? '0' : '19'}" fill="${config.colors.line_index}">${index}</tspan>`);
120120
}
121121

122122
return array.join('\n');
@@ -126,7 +126,7 @@ export class WidgetService {
126126
const array = [];
127127
for (const indent of indents) {
128128
array.push(
129-
`<rect fill="${colors.indent_lines}" x="${indent.depth * indent_width}" ` +
129+
`<rect fill="${config.colors.indent_lines}" x="${indent.depth * indent_width}" ` +
130130
`y="${indent.startLine * 19}" width="1" height="${((indent.endLine - indent.startLine) - 1) * 19}" />`
131131
);
132132
}

0 commit comments

Comments
 (0)