Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): add metadatum parsing utils
Cardano.util.asMetadatumMap and Cardano.util.asMetadatumArray
- Loading branch information
1 parent
51bcfb6
commit 51a57ab
Showing
5 changed files
with
53 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export * from './coalesceValueQuantities'; | ||
export * from './computeMinUtxoValue'; | ||
export * from './primitives'; | ||
export * as metadatum from './metadatum'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { Metadatum, MetadatumMap } from '../types'; | ||
|
||
/** | ||
* @returns {MetadatumMap | null} null if Metadatum is not MetadatumMap | ||
*/ | ||
export const asMetadatumMap = (metadatum: Metadatum): MetadatumMap | null => { | ||
if (typeof metadatum === 'object' && !Array.isArray(metadatum) && !(metadatum instanceof Uint8Array)) { | ||
return metadatum; | ||
} | ||
return null; | ||
}; | ||
|
||
/** | ||
* @returns {Metadatum[] | null} null if Metadatum is not an array of metadatum | ||
*/ | ||
export const asMetadatumArray = (metadatum: Metadatum): Metadatum[] | null => { | ||
if (Array.isArray(metadatum)) { | ||
return metadatum; | ||
} | ||
return null; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { Cardano } from '@cardano-sdk/core'; | ||
|
||
describe('Cardano.util.metadatum', () => { | ||
describe('asMetadatumMap', () => { | ||
it('returns argument if it is a MetadatumMap', () => { | ||
const metadatum: Cardano.Metadatum = { some: 'metadatum' }; | ||
expect(Cardano.util.metadatum.asMetadatumMap(metadatum)).toBe(metadatum); | ||
}); | ||
|
||
it('returns null for any other metadatum type', () => { | ||
const metadatum: Cardano.Metadatum = [{ some: 'metadatum' }]; | ||
expect(Cardano.util.metadatum.asMetadatumMap(metadatum)).toBeNull(); | ||
}); | ||
}); | ||
|
||
describe('asMetadatumArray', () => { | ||
it('returns argument if it is Metadatum[]', () => { | ||
const metadatum: Cardano.Metadatum = [{ some: 'metadatum' }]; | ||
expect(Cardano.util.metadatum.asMetadatumArray(metadatum)).toBe(metadatum); | ||
}); | ||
|
||
it('returns null for any other metadatum type', () => { | ||
const metadatum: Cardano.Metadatum = { some: 'metadatum' }; | ||
expect(Cardano.util.metadatum.asMetadatumArray(metadatum)).toBeNull(); | ||
}); | ||
}); | ||
}); |