diff --git a/__tests__/__snapshots__/includes.test.ts.snap b/__tests__/__snapshots__/includes.test.ts.snap index 35a8c5c..d6f3582 100644 --- a/__tests__/__snapshots__/includes.test.ts.snap +++ b/__tests__/__snapshots__/includes.test.ts.snap @@ -1,6 +1,80 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP +exports[`AssetList Modification 0 1`] = ` +"{ + assets: [ + { + denomUnits: [ + { + denom: 'ucmdx' + }, + { + denom: 'cmdx' + } + ], + base: 'ucmdx', + assetType: 'unknown' + }, + { + assetType: 'sdk.Factory', + denomUnits: [ + { + denom: 'uharbor' + }, + { + denom: 'harbor' + } + ], + base: 'uharbor' + }, + { + denomUnits: [ + { + denom: 'ucmst' + }, + { + denom: 'cmst' + } + ], + base: 'ucmst', + assetType: 'unknown' + }, + { + denomUnits: [ + { + denom: 'uibc' + }, + { + denom: 'ibc' + } + ], + base: 'ibc/A358FDF578F3483381C3191F86E4C6859E76100', + assetType: 'ibc' + } + ] +}" +`; + exports[`AssetList Modification 1`] = ` +"{ + assets: [ + { + assetType: 'unknown' + }, + { + assetType: 'sdk.Factory' + }, + { + assetType: 'unknown' + }, + { + assetType: 'ibc' + } + ] +}" +`; + +exports[`AssetList Modification 1 1`] = ` "{ assets: [ { @@ -12,7 +86,8 @@ exports[`AssetList Modification 1`] = ` denom: 'cmdx' } ], - base: 'ucmdx' + base: 'ucmdx', + assetType: 'unknown' }, { assetType: 'sdk.Factory', @@ -35,7 +110,8 @@ exports[`AssetList Modification 1`] = ` denom: 'cmst' } ], - base: 'ucmst' + base: 'ucmst', + assetType: 'unknown' }, { denomUnits: [ @@ -46,7 +122,73 @@ exports[`AssetList Modification 1`] = ` denom: 'ibc' } ], - base: 'ibc/A358FDF578F3483381C3191F86E4C6859E76100' + base: 'ibc/A358FDF578F3483381C3191F86E4C6859E76100', + assetType: 'ibc' + } + ] +}" +`; + +exports[`AssetList Modification 2`] = ` +"{ + $schema: '../assetlist.schema.json', + chainName: 'comdex', + assets: [ + { + description: 'Native Token of Comdex Protocol', + base: 'ucmdx', + name: 'Comdex', + display: 'cmdx', + symbol: 'CMDX', + coingeckoId: 'comdex', + images: [ + { + png: 'https://raw.githubusercontent.com/cosmos/chain-registry/master/comdex/images/cmdx.png' + } + ], + assetType: 'unknown' + }, + { + assetType: 'sdk.Factory', + description: 'Governance Token of Harbor protocol on Comdex network', + base: 'uharbor', + name: 'Harbor', + display: 'harbor', + symbol: 'HARBOR', + coingeckoId: 'harbor-2', + images: [ + { + png: 'https://raw.githubusercontent.com/cosmos/chain-registry/master/comdex/images/harbor.png' + } + ] + }, + { + description: 'Stable Token of Harbor protocol on Comdex network', + base: 'ucmst', + name: 'CMST', + display: 'cmst', + symbol: 'CMST', + coingeckoId: 'composite', + images: [ + { + png: 'https://raw.githubusercontent.com/cosmos/chain-registry/master/comdex/images/cmst.png' + } + ], + assetType: 'unknown' + }, + { + description: 'My ibc token', + base: 'ibc/A358FDF578F3483381C3191F86E4C6859E76100', + name: 'IBC', + display: 'ibc', + symbol: 'IBC', + coingeckoId: 'ibc', + images: [ + { + png: 'https://raw.githubusercontent.com/cosmos/chain-registry/master/comdex/images/cmst.png' + } + ], + assetType: 'ibc' } ] }" @@ -66,7 +208,8 @@ exports[`AssetList Modification 2 1`] = ` exponent: 6 } ], - base: 'ucmdx' + base: 'ucmdx', + assetType: 'unknown' }, { assetType: 'sdk.Factory', @@ -93,7 +236,8 @@ exports[`AssetList Modification 2 1`] = ` exponent: 6 } ], - base: 'ucmst' + base: 'ucmst', + assetType: 'unknown' }, { denomUnits: [ @@ -106,7 +250,8 @@ exports[`AssetList Modification 2 1`] = ` exponent: 6 } ], - base: 'ibc/A358FDF578F3483381C3191F86E4C6859E76100' + base: 'ibc/A358FDF578F3483381C3191F86E4C6859E76100', + assetType: 'ibc' } ] }" @@ -197,6 +342,7 @@ exports[`Chain Modification 3 1`] = ` ] }, codebase: { + cosmosSdkVersion: 'v0.47.5', cosmwasmVersion: 'v0.41.0', cosmwasmEnabled: true, versions: [ diff --git a/__tests__/includes.test.ts b/__tests__/includes.test.ts index 747a8dc..bde28c0 100644 --- a/__tests__/includes.test.ts +++ b/__tests__/includes.test.ts @@ -1,8 +1,90 @@ import { jsStringify, JSStringifyOptions, JSStringifySetterOptions } from "../src"; import assetList from '../__fixtures__/assets.json'; +import { Asset, AssetList } from '../test-utils'; import chain from '../__fixtures__/chain.json'; it('AssetList Modification', () => { + const options: JSStringifyOptions = { + camelCase: true, + space: 2, + propertyRenameMap: { + '/assets/*/type_asset': 'asset_type' + }, + defaultValuesSetter: { + "/assets/*/type_asset": function (options: JSStringifySetterOptions): any { + const asset = options.obj; + switch (true) { + case asset.base.startsWith('factory/'): + return 'sdk.Factory'; + + case asset.base.startsWith('ft') && options.root.chain_name === 'bitsong': + return 'bitsong'; + + case asset.base.startsWith('erc20/'): + return 'erc.Token'; + + case asset.base.startsWith('ibc/'): + return 'ibc' + + case asset.base.startsWith('cw20:'): + return 'cw20' + + default: + return 'unknown' + } + } + }, + include: [ + '/assets/*/type_asset' + ], + exclude: [ + '/assets/*/denom_units' + ] + }; + const jsonString = jsStringify(assetList, options); + expect(jsonString).toMatchSnapshot(); +}); + + +it('AssetList Modification', () => { + const options: JSStringifyOptions = { + camelCase: true, + space: 2, + propertyRenameMap: { + '/assets/*/type_asset': 'asset_type' + }, + exclude: [ + '/assets/*/denom_units', + '/assets/*/logo_URIs', + '/assets/*/images/*/svg', + ] + }; + const jsonString = jsStringify(assetList, options); + expect(jsonString).toMatchSnapshot(); +}); + + +it('AssetList Modification 0', () => { + const options: JSStringifyOptions = { + camelCase: true, + space: 2, + propertyRenameMap: { + '/assets/*/type_asset': 'asset_type' + }, + include: [ + '/assets/*/type_asset', + '/assets/*/denom_units', + '/assets/*/base' + ], + exclude: [ + '/assets/*/denom_units/*/exponent' + ] + }; + const jsonString = jsStringify(assetList, options); + expect(jsonString).toMatchSnapshot(); +}); + +it('AssetList Modification 1', () => { const options: JSStringifyOptions = { camelCase: true, space: 2, @@ -69,7 +151,7 @@ it('Chain Modification 3', () => { '/chain_id', '/pretty_name', '/chain_name', - '/codebase/cosmso_sdk_version', + '/codebase/cosmos_sdk_version', '/codebase/cosmwasm_version', '/codebase/cosmwasm_enabled', '/codebase/versions/*/name', @@ -79,4 +161,5 @@ it('Chain Modification 3', () => { }; const jsonString = jsStringify(chain, options); expect(jsonString).toMatchSnapshot(); -}); \ No newline at end of file +}); + diff --git a/src/utils.ts b/src/utils.ts index 6019953..c8d645b 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -93,11 +93,6 @@ function sortPaths(paths) { return paths; } - -function hasAncestor(currentPath, includePaths) { - return !!findNearestAncestor(currentPath, includePaths); -} - function findNearestAncestor(currentPath, includePaths) { let pathToCheck = currentPath; while (pathToCheck !== '/' && pathToCheck !== '') { @@ -120,8 +115,8 @@ export const shouldInclude = (type: string, options: ShouldIncludeOptions): bool patterns.some(pattern => globPattern.test(pattern) ? minimatch(type, pattern) : type === pattern); const isIncluded = includesEffectivelySet ? matchesPattern(expandPaths(options.include), type) : true; - const isIncludedByAncestor = !!findNearestAncestor(type, options.include); const isExcluded = excludesEffectivelySet ? matchesPattern(options.exclude, type) : false; + const isIncludedByAncestor = !!findNearestAncestor(type, options.include); const toInclude = isIncluded;