Skip to content

Commit

Permalink
Merge pull request #9 from taylorjdawson/update/latest-chains
Browse files Browse the repository at this point in the history
Update with latest chains
  • Loading branch information
taylorjdawson authored Dec 7, 2021
2 parents aef6269 + e7d8a3c commit e898ed7
Show file tree
Hide file tree
Showing 7 changed files with 4,674 additions and 2,300 deletions.
10 changes: 9 additions & 1 deletion index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,9 @@
export {default as chains, Chain, ChainId, ChainName, NativeCurrency, Explorer } from './src'
export {
default as chains,
Chain,
ChainId,
ChainName,
NativeCurrency,
Explorer,
Parent
} from './src'
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "eth-chains",
"version": "0.3.5",
"version": "0.4.0",
"description": "Helper module for getting Ethereum chains info.",
"author": "Taylor Dawson",
"main": "dist/index.js",
Expand Down
32 changes: 19 additions & 13 deletions src/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,30 @@ const getEnumKey = (chainName: string) => {
let enumKey = capitalize(chainName.replace(/\s/g, ''))
// If the key starts with a number or contains ',' or '.' then wrap it in quotes
enumKey = !!chainName.match(/^\d|[\-\.]/) ? `'${enumKey}'` : enumKey
return enumKey // `${enumKey} = '${chainName}'`
return enumKey
}

const getBuildEnums = (chains: Chain[]) => chains.reduce( (enumStrings, chain, index, array) => {
const key = getEnumKey(chain.name)
const tail = index === array.length - 1 ? ' }': ', '
enumStrings[0] += `${key} = '${chain.name}'${tail}`
enumStrings[1] += `${key} = ${chain.chainId}${tail}`
return enumStrings
}, ['export enum ChainName { ', 'export enum ChainId { ']).join('\n')
const getBuildEnums = (chains: Chain[]) =>
chains
.reduce(
(enumStrings, chain, index, array) => {
const key = getEnumKey(chain.name)
const tail = index === array.length - 1 ? ' }' : ', '
enumStrings[0] += `${key} = '${chain.name}'${tail}`
enumStrings[1] += `${key} = ${chain.chainId}${tail}`
return enumStrings
},
['export enum ChainName { ', 'export enum ChainId { ']
)
.join('\n')

const generateEnumFile = async (chains: Chain[]) => {
fs.writeFile(
'./src/enums.ts',
format(
getBuildEnums(chains),
{ ...(prettierOptions as Options), parser: 'typescript' }
)
format(getBuildEnums(chains), {
...(prettierOptions as Options),
parser: 'typescript'
})
)
}

Expand All @@ -46,7 +52,7 @@ const generateChainsFile = async () => {
await generateEnumFile(chains)

const chainsJs = chains
.map(chain => `${chain.chainId}: ${inspect(chain)}`)
.map(chain => `${chain.chainId}: ${inspect(chain, { depth: null })}`)
.join(',\n')

fs.writeFile(
Expand Down
Loading

0 comments on commit e898ed7

Please sign in to comment.