Skip to content

Commit

Permalink
includes
Browse files Browse the repository at this point in the history
  • Loading branch information
pyramation committed May 17, 2024
1 parent da03908 commit 5da4ed8
Show file tree
Hide file tree
Showing 3 changed files with 238 additions and 14 deletions.
158 changes: 152 additions & 6 deletions __tests__/__snapshots__/includes.test.ts.snap
Original file line number Diff line number Diff line change
@@ -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: [
{
Expand All @@ -12,7 +86,8 @@ exports[`AssetList Modification 1`] = `
denom: 'cmdx'
}
],
base: 'ucmdx'
base: 'ucmdx',
assetType: 'unknown'
},
{
assetType: 'sdk.Factory',
Expand All @@ -35,7 +110,8 @@ exports[`AssetList Modification 1`] = `
denom: 'cmst'
}
],
base: 'ucmst'
base: 'ucmst',
assetType: 'unknown'
},
{
denomUnits: [
Expand All @@ -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'
}
]
}"
Expand All @@ -66,7 +208,8 @@ exports[`AssetList Modification 2 1`] = `
exponent: 6
}
],
base: 'ucmdx'
base: 'ucmdx',
assetType: 'unknown'
},
{
assetType: 'sdk.Factory',
Expand All @@ -93,7 +236,8 @@ exports[`AssetList Modification 2 1`] = `
exponent: 6
}
],
base: 'ucmst'
base: 'ucmst',
assetType: 'unknown'
},
{
denomUnits: [
Expand All @@ -106,7 +250,8 @@ exports[`AssetList Modification 2 1`] = `
exponent: 6
}
],
base: 'ibc/A358FDF578F3483381C3191F86E4C6859E76100'
base: 'ibc/A358FDF578F3483381C3191F86E4C6859E76100',
assetType: 'ibc'
}
]
}"
Expand Down Expand Up @@ -197,6 +342,7 @@ exports[`Chain Modification 3 1`] = `
]
},
codebase: {
cosmosSdkVersion: 'v0.47.5',
cosmwasmVersion: 'v0.41.0',
cosmwasmEnabled: true,
versions: [
Expand Down
87 changes: 85 additions & 2 deletions __tests__/includes.test.ts
Original file line number Diff line number Diff line change
@@ -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<Asset, AssetList>): 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,
Expand Down Expand Up @@ -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',
Expand All @@ -79,4 +161,5 @@ it('Chain Modification 3', () => {
};
const jsonString = jsStringify(chain, options);
expect(jsonString).toMatchSnapshot();
});
});

7 changes: 1 addition & 6 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 !== '') {
Expand All @@ -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;

Expand Down

0 comments on commit 5da4ed8

Please sign in to comment.