Skip to content

Commit b0c4ad1

Browse files
committed
refactoring
1 parent c8f6400 commit b0c4ad1

5 files changed

Lines changed: 60 additions & 30 deletions

File tree

src/app.module.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import {VersionCommand} from "./commands/VersionCommand";
44
import {WalletCommand} from "./commands/wallet/WalletCommand";
55
import {ConfigModule} from "@nestjs/config";
66
import {ConfirmQuestion} from "./questions/ConfirmQuestion";
7+
import {ContentExporter} from "./services/ContentExporter";
8+
import {MagicPipConvert} from "./questions/MagicPipConvert";
79

810
@Module({
911
imports: [ConfigModule.forRoot()],
@@ -13,6 +15,8 @@ import {ConfirmQuestion} from "./questions/ConfirmQuestion";
1315
...ApiCommand.registerWithSubCommands(),
1416
...WalletCommand.registerWithSubCommands(),
1517
ConfirmQuestion,
18+
ContentExporter,
19+
MagicPipConvert,
1620
],
1721
})
1822
export class AppModule {

src/commands/api/CandidateCommand.ts

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
11
import {Option, CommandRunner, SubCommand} from 'nest-commander';
22
import {MinterApiService} from '../../services/minter-api/minter-api.service';
3-
import {JsonPatches} from "../../utils/JsonPatches";
4-
5-
import {search} from 'jmespath'
6-
import {MagicPipConvert} from "../../questions/MagicPipConvert";
7-
8-
// import * as jmespath from 'jmespath'
3+
import {ContentExporter} from "../../services/ContentExporter";
94

105
@SubCommand({
116
name: 'candidate',
@@ -14,8 +9,9 @@ import {MagicPipConvert} from "../../questions/MagicPipConvert";
149
description: 'Candidate returns candidate’s info by provided public key',
1510
})
1611
export class CandidateCommand extends CommandRunner {
12+
private skip_pip2bip = false;
1713

18-
constructor() {
14+
constructor(private contentExporter: ContentExporter) {
1915
super();
2016
}
2117

@@ -36,29 +32,15 @@ export class CandidateCommand extends CommandRunner {
3632

3733
minterApi.api().getCandidateGrpc(candidate, options.not_show_stakes, options.height).then((r) => {
3834
const result = r.toObject()
39-
let out: any;
40-
// const mpc = new MagicPipConvert()
41-
if (options.patches) {
42-
new JsonPatches().printPropertyNames(result)
43-
} else if (options.patch && options.patch.length > 0) {
44-
// out=result[options.patch]
45-
out = search(result, options.patch)
46-
} else {
47-
out = result
48-
}
49-
out = new MagicPipConvert().replaceLongNumericStrings(out)
50-
if (!options.pretty) {
51-
process.stdout.write(JSON.stringify(out));
52-
} else {
53-
console.warn((out))
54-
// console.warn(out)
55-
}
35+
this.contentExporter.print(result, this.skip_pip2bip, options)
5636
}
5737
)
5838
.catch(console.log);
5939
return Promise.resolve(undefined);
6040
}
6141

42+
43+
6244
@Option({
6345
flags: '--height [number]',
6446
name: 'Height',
@@ -114,4 +96,16 @@ export class CandidateCommand extends CommandRunner {
11496
parsePretty(val: string): boolean {
11597
return JSON.parse(val);
11698
}
99+
100+
@Option({
101+
flags: '--skip_pip2bip',
102+
name: 'skip_pip2bip',
103+
description: 'Skip convert PIP to BIP',
104+
defaultValue: false,
105+
})
106+
parseSkipPip2Bip(val: string): boolean {
107+
this.skip_pip2bip = true;
108+
return JSON.parse(val);
109+
}
110+
117111
}

src/questions/MagicPipConvert.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
11
import {ConvertAmount} from "minter-typescript-sdk";
2+
import {Injectable} from "@nestjs/common";
23

4+
@Injectable()
35
export class MagicPipConvert {
46
private minLength = 12;
5-
67
private convertAmount = new ConvertAmount()
78

8-
replaceLongNumericStrings(input: any): any {
9+
mbPipToBip(input: any): any {
910
if (Array.isArray(input)) {
10-
return input.map(item => this.replaceLongNumericStrings(item));
11+
return input.map(item => this.mbPipToBip(item));
1112
} else if (typeof input === 'object' && input !== null) {
1213
const result: { [key: string]: any } = {};
1314
for (const key in input) {
1415
if (input.hasOwnProperty(key)) {
15-
result[key] = this.replaceLongNumericStrings(input[key]);
16+
result[key] = this.mbPipToBip(input[key]);
1617
}
1718
}
1819
return result;
1920
} else if (typeof input === 'string') {
20-
2121
if (/^\d+$/.test(input) && input.length >= this.minLength) {
2222
return this.convertAmount.toBip(input);
2323
}

src/services/ContentExporter.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import {Injectable} from "@nestjs/common";
2+
import {JsonPatches} from "../utils/JsonPatches";
3+
import {search} from "jmespath";
4+
import {MagicPipConvert} from "../questions/MagicPipConvert";
5+
6+
@Injectable()
7+
export class ContentExporter {
8+
9+
print(
10+
result: Record<string, any> | string,
11+
skip_pip2bip: boolean,
12+
options: {
13+
patch?: string;
14+
patches: boolean;
15+
pretty: boolean
16+
}) {
17+
let out: any;
18+
if (options.patches) {
19+
new JsonPatches().printPropertyNames(result)
20+
} else if (options.patch && options.patch.length > 0) {
21+
out = search(result, options.patch)
22+
} else {
23+
out = result
24+
}
25+
if (!skip_pip2bip) out = new MagicPipConvert().mbPipToBip(out)
26+
if (!options.pretty) {
27+
process.stdout.write(JSON.stringify(out));
28+
} else {
29+
console.info((out))
30+
}
31+
}
32+
}

src/utils/JsonPatches.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ export class JsonPatches {
22

33
printPropertyNames(obj: any, prefix = ""): void {
44
this.propertyNames(obj, prefix).forEach((propertyName) => {
5-
console.log(propertyName);
5+
console.info(propertyName);
66
})
77
}
88

0 commit comments

Comments
 (0)