Skip to content

Commit

Permalink
mv Format index to Utils
Browse files Browse the repository at this point in the history
  • Loading branch information
Ross Bulat committed Jun 13, 2024
1 parent 1700f89 commit 55a627d
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 92 deletions.
50 changes: 50 additions & 0 deletions src/model/Scraper/Format/Utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
// Copyright 2024 @polkadot-cloud/polkadot-developer-console authors & contributors
// SPDX-License-Identifier: GPL-3.0-only

import type { AnyJson } from '@w3ux/types';

// ------------------------------------------------------
// Labels.
// ------------------------------------------------------

// Gets a short label from a label input.
export const getShortLabel = (
input: string | { long: string; short: string }
Expand All @@ -19,3 +25,47 @@ export const verifyOption = (
shortLabel === 'Option' &&
variant?.[0]?.name === 'None' &&
variant?.[1]?.name === 'Some';

// ------------------------------------------------------
// Type paths and params.
// ------------------------------------------------------

// Format a string representation of the type using its path and params.
//
// E.g. BalanceOf<T>, where BalanceOf is the path and <T> is its param.
export const typeToString = (path: string[], params: string[]): string => {
const paramsStr = paramsToString(params);
const pathStr = pathToString(path);

let label = `${pathStr}`;
if (paramsStr) {
label += `${paramsStr}`;
}
return label;
};

// Format a type's params into a string.
//
// E.g. <param1>, <param2>, etc...
export const paramsToString = (params: AnyJson): string =>
params.reduce(
(formatted: string, { name }: { name: string }, index: number) => {
let str = index === 0 ? `<${name}` : `, ${name}`;
if (index === params.length - 1) {
str += `>`;
}
return (formatted += str);
},
''
);

// Format a type's path into a string.
//
// E.g. <path>::<path>::<path>
export const pathToString = (path: string[]): string =>
path.reduce((formatted: string, item: string, index: number) => {
if (index === 0) {
return item;
}
return index === 0 ? item : `${formatted}::${item}`;
}, '');
86 changes: 0 additions & 86 deletions src/model/Scraper/Format/index.ts

This file was deleted.

4 changes: 2 additions & 2 deletions src/model/Scraper/Types/BitSequence.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type { LookupItem } from '../Lookup/types';
import type { MetadataScraper } from '..';
import type { TrailParam } from '../types';
import type { BitSequenceType, MetadataType } from './types';
import { Format } from '../Format';
import { typeToString } from '../Format/Utils';

// Class to hold a bit sequence type.
export class BitSequence implements MetadataType {
Expand All @@ -26,7 +26,7 @@ export class BitSequence implements MetadataType {
label() {
const { path, params } = this.lookup.type;
return {
long: Format.typeToString(path, params),
long: typeToString(path, params),
short: path[path.length - 1],
};
}
Expand Down
4 changes: 2 additions & 2 deletions src/model/Scraper/Types/Composite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type { LookupItem } from '../Lookup/types';
import type { MetadataScraper } from '..';
import type { TrailParam } from '../types';
import type { CompositeField, CompositeType, MetadataType } from './types';
import { Format } from '../Format';
import { typeToString } from '../Format/Utils';

// Class to hold a composite type.
export class Composite implements MetadataType {
Expand All @@ -26,7 +26,7 @@ export class Composite implements MetadataType {
label() {
const { path, params } = this.lookup.type;
return {
long: Format.typeToString(path, params),
long: typeToString(path, params),
short: path[path.length - 1],
};
}
Expand Down
4 changes: 2 additions & 2 deletions src/model/Scraper/Types/Variant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type { LookupItem } from '../Lookup/types';
import type { MetadataScraper } from '..';
import type { MetadataType, VariantItem } from './types';
import type { TrailParam } from '../types';
import { Format } from '../Format';
import { typeToString } from '../Format/Utils';

// Class to hold a variant type.
export class Variant implements MetadataType {
Expand All @@ -26,7 +26,7 @@ export class Variant implements MetadataType {
label() {
const { path, params } = this.lookup.type;
return {
long: Format.typeToString(path, params),
long: typeToString(path, params),
short: path[path.length - 1],
};
}
Expand Down

0 comments on commit 55a627d

Please sign in to comment.