Skip to content

Commit 5da4ed8

Browse files
committed
includes
1 parent da03908 commit 5da4ed8

File tree

3 files changed

+238
-14
lines changed

3 files changed

+238
-14
lines changed

__tests__/__snapshots__/includes.test.ts.snap

+152-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,80 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3+
exports[`AssetList Modification 0 1`] = `
4+
"{
5+
assets: [
6+
{
7+
denomUnits: [
8+
{
9+
denom: 'ucmdx'
10+
},
11+
{
12+
denom: 'cmdx'
13+
}
14+
],
15+
base: 'ucmdx',
16+
assetType: 'unknown'
17+
},
18+
{
19+
assetType: 'sdk.Factory',
20+
denomUnits: [
21+
{
22+
denom: 'uharbor'
23+
},
24+
{
25+
denom: 'harbor'
26+
}
27+
],
28+
base: 'uharbor'
29+
},
30+
{
31+
denomUnits: [
32+
{
33+
denom: 'ucmst'
34+
},
35+
{
36+
denom: 'cmst'
37+
}
38+
],
39+
base: 'ucmst',
40+
assetType: 'unknown'
41+
},
42+
{
43+
denomUnits: [
44+
{
45+
denom: 'uibc'
46+
},
47+
{
48+
denom: 'ibc'
49+
}
50+
],
51+
base: 'ibc/A358FDF578F3483381C3191F86E4C6859E76100',
52+
assetType: 'ibc'
53+
}
54+
]
55+
}"
56+
`;
57+
358
exports[`AssetList Modification 1`] = `
59+
"{
60+
assets: [
61+
{
62+
assetType: 'unknown'
63+
},
64+
{
65+
assetType: 'sdk.Factory'
66+
},
67+
{
68+
assetType: 'unknown'
69+
},
70+
{
71+
assetType: 'ibc'
72+
}
73+
]
74+
}"
75+
`;
76+
77+
exports[`AssetList Modification 1 1`] = `
478
"{
579
assets: [
680
{
@@ -12,7 +86,8 @@ exports[`AssetList Modification 1`] = `
1286
denom: 'cmdx'
1387
}
1488
],
15-
base: 'ucmdx'
89+
base: 'ucmdx',
90+
assetType: 'unknown'
1691
},
1792
{
1893
assetType: 'sdk.Factory',
@@ -35,7 +110,8 @@ exports[`AssetList Modification 1`] = `
35110
denom: 'cmst'
36111
}
37112
],
38-
base: 'ucmst'
113+
base: 'ucmst',
114+
assetType: 'unknown'
39115
},
40116
{
41117
denomUnits: [
@@ -46,7 +122,73 @@ exports[`AssetList Modification 1`] = `
46122
denom: 'ibc'
47123
}
48124
],
49-
base: 'ibc/A358FDF578F3483381C3191F86E4C6859E76100'
125+
base: 'ibc/A358FDF578F3483381C3191F86E4C6859E76100',
126+
assetType: 'ibc'
127+
}
128+
]
129+
}"
130+
`;
131+
132+
exports[`AssetList Modification 2`] = `
133+
"{
134+
$schema: '../assetlist.schema.json',
135+
chainName: 'comdex',
136+
assets: [
137+
{
138+
description: 'Native Token of Comdex Protocol',
139+
base: 'ucmdx',
140+
name: 'Comdex',
141+
display: 'cmdx',
142+
symbol: 'CMDX',
143+
coingeckoId: 'comdex',
144+
images: [
145+
{
146+
png: 'https://raw.githubusercontent.com/cosmos/chain-registry/master/comdex/images/cmdx.png'
147+
}
148+
],
149+
assetType: 'unknown'
150+
},
151+
{
152+
assetType: 'sdk.Factory',
153+
description: 'Governance Token of Harbor protocol on Comdex network',
154+
base: 'uharbor',
155+
name: 'Harbor',
156+
display: 'harbor',
157+
symbol: 'HARBOR',
158+
coingeckoId: 'harbor-2',
159+
images: [
160+
{
161+
png: 'https://raw.githubusercontent.com/cosmos/chain-registry/master/comdex/images/harbor.png'
162+
}
163+
]
164+
},
165+
{
166+
description: 'Stable Token of Harbor protocol on Comdex network',
167+
base: 'ucmst',
168+
name: 'CMST',
169+
display: 'cmst',
170+
symbol: 'CMST',
171+
coingeckoId: 'composite',
172+
images: [
173+
{
174+
png: 'https://raw.githubusercontent.com/cosmos/chain-registry/master/comdex/images/cmst.png'
175+
}
176+
],
177+
assetType: 'unknown'
178+
},
179+
{
180+
description: 'My ibc token',
181+
base: 'ibc/A358FDF578F3483381C3191F86E4C6859E76100',
182+
name: 'IBC',
183+
display: 'ibc',
184+
symbol: 'IBC',
185+
coingeckoId: 'ibc',
186+
images: [
187+
{
188+
png: 'https://raw.githubusercontent.com/cosmos/chain-registry/master/comdex/images/cmst.png'
189+
}
190+
],
191+
assetType: 'ibc'
50192
}
51193
]
52194
}"
@@ -66,7 +208,8 @@ exports[`AssetList Modification 2 1`] = `
66208
exponent: 6
67209
}
68210
],
69-
base: 'ucmdx'
211+
base: 'ucmdx',
212+
assetType: 'unknown'
70213
},
71214
{
72215
assetType: 'sdk.Factory',
@@ -93,7 +236,8 @@ exports[`AssetList Modification 2 1`] = `
93236
exponent: 6
94237
}
95238
],
96-
base: 'ucmst'
239+
base: 'ucmst',
240+
assetType: 'unknown'
97241
},
98242
{
99243
denomUnits: [
@@ -106,7 +250,8 @@ exports[`AssetList Modification 2 1`] = `
106250
exponent: 6
107251
}
108252
],
109-
base: 'ibc/A358FDF578F3483381C3191F86E4C6859E76100'
253+
base: 'ibc/A358FDF578F3483381C3191F86E4C6859E76100',
254+
assetType: 'ibc'
110255
}
111256
]
112257
}"
@@ -197,6 +342,7 @@ exports[`Chain Modification 3 1`] = `
197342
]
198343
},
199344
codebase: {
345+
cosmosSdkVersion: 'v0.47.5',
200346
cosmwasmVersion: 'v0.41.0',
201347
cosmwasmEnabled: true,
202348
versions: [

__tests__/includes.test.ts

+85-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,90 @@
11
import { jsStringify, JSStringifyOptions, JSStringifySetterOptions } from "../src";
22
import assetList from '../__fixtures__/assets.json';
3+
import { Asset, AssetList } from '../test-utils';
34
import chain from '../__fixtures__/chain.json';
45

56
it('AssetList Modification', () => {
7+
const options: JSStringifyOptions = {
8+
camelCase: true,
9+
space: 2,
10+
propertyRenameMap: {
11+
'/assets/*/type_asset': 'asset_type'
12+
},
13+
defaultValuesSetter: {
14+
"/assets/*/type_asset": function (options: JSStringifySetterOptions<Asset, AssetList>): any {
15+
const asset = options.obj;
16+
switch (true) {
17+
case asset.base.startsWith('factory/'):
18+
return 'sdk.Factory';
19+
20+
case asset.base.startsWith('ft') && options.root.chain_name === 'bitsong':
21+
return 'bitsong';
22+
23+
case asset.base.startsWith('erc20/'):
24+
return 'erc.Token';
25+
26+
case asset.base.startsWith('ibc/'):
27+
return 'ibc'
28+
29+
case asset.base.startsWith('cw20:'):
30+
return 'cw20'
31+
32+
default:
33+
return 'unknown'
34+
}
35+
}
36+
},
37+
include: [
38+
'/assets/*/type_asset'
39+
],
40+
exclude: [
41+
'/assets/*/denom_units'
42+
]
43+
};
44+
const jsonString = jsStringify(assetList, options);
45+
expect(jsonString).toMatchSnapshot();
46+
});
47+
48+
49+
it('AssetList Modification', () => {
50+
const options: JSStringifyOptions = {
51+
camelCase: true,
52+
space: 2,
53+
propertyRenameMap: {
54+
'/assets/*/type_asset': 'asset_type'
55+
},
56+
exclude: [
57+
'/assets/*/denom_units',
58+
'/assets/*/logo_URIs',
59+
'/assets/*/images/*/svg',
60+
]
61+
};
62+
const jsonString = jsStringify(assetList, options);
63+
expect(jsonString).toMatchSnapshot();
64+
});
65+
66+
67+
it('AssetList Modification 0', () => {
68+
const options: JSStringifyOptions = {
69+
camelCase: true,
70+
space: 2,
71+
propertyRenameMap: {
72+
'/assets/*/type_asset': 'asset_type'
73+
},
74+
include: [
75+
'/assets/*/type_asset',
76+
'/assets/*/denom_units',
77+
'/assets/*/base'
78+
],
79+
exclude: [
80+
'/assets/*/denom_units/*/exponent'
81+
]
82+
};
83+
const jsonString = jsStringify(assetList, options);
84+
expect(jsonString).toMatchSnapshot();
85+
});
86+
87+
it('AssetList Modification 1', () => {
688
const options: JSStringifyOptions = {
789
camelCase: true,
890
space: 2,
@@ -69,7 +151,7 @@ it('Chain Modification 3', () => {
69151
'/chain_id',
70152
'/pretty_name',
71153
'/chain_name',
72-
'/codebase/cosmso_sdk_version',
154+
'/codebase/cosmos_sdk_version',
73155
'/codebase/cosmwasm_version',
74156
'/codebase/cosmwasm_enabled',
75157
'/codebase/versions/*/name',
@@ -79,4 +161,5 @@ it('Chain Modification 3', () => {
79161
};
80162
const jsonString = jsStringify(chain, options);
81163
expect(jsonString).toMatchSnapshot();
82-
});
164+
});
165+

src/utils.ts

+1-6
Original file line numberDiff line numberDiff line change
@@ -93,11 +93,6 @@ function sortPaths(paths) {
9393
return paths;
9494
}
9595

96-
97-
function hasAncestor(currentPath, includePaths) {
98-
return !!findNearestAncestor(currentPath, includePaths);
99-
}
100-
10196
function findNearestAncestor(currentPath, includePaths) {
10297
let pathToCheck = currentPath;
10398
while (pathToCheck !== '/' && pathToCheck !== '') {
@@ -120,8 +115,8 @@ export const shouldInclude = (type: string, options: ShouldIncludeOptions): bool
120115
patterns.some(pattern => globPattern.test(pattern) ? minimatch(type, pattern) : type === pattern);
121116

122117
const isIncluded = includesEffectivelySet ? matchesPattern(expandPaths(options.include), type) : true;
123-
const isIncludedByAncestor = !!findNearestAncestor(type, options.include);
124118
const isExcluded = excludesEffectivelySet ? matchesPattern(options.exclude, type) : false;
119+
const isIncludedByAncestor = !!findNearestAncestor(type, options.include);
125120

126121
const toInclude = isIncluded;
127122

0 commit comments

Comments
 (0)