mirror of
https://github.com/alexgo-io/stacks.js.git
synced 2026-01-12 09:34:05 +08:00
feat: implement isClarityType
This commit is contained in:
@@ -13,6 +13,8 @@ import {
|
||||
StringUtf8CV,
|
||||
NoneCV,
|
||||
SomeCV,
|
||||
TrueCV,
|
||||
FalseCV,
|
||||
} from '.';
|
||||
|
||||
import { principalToString } from './types/principalCV';
|
||||
@@ -169,3 +171,41 @@ export function getCVTypeString(val: ClarityValue): string {
|
||||
return `(string-utf8 ${utf8ToBytes(val.data).length})`;
|
||||
}
|
||||
}
|
||||
|
||||
type ClarityTypetoValue = {
|
||||
[ClarityType.OptionalNone]: NoneCV;
|
||||
[ClarityType.OptionalSome]: SomeCV;
|
||||
[ClarityType.ResponseOk]: ResponseOkCV;
|
||||
[ClarityType.ResponseErr]: ResponseErrorCV;
|
||||
[ClarityType.BoolTrue]: TrueCV;
|
||||
[ClarityType.BoolFalse]: FalseCV;
|
||||
[ClarityType.Int]: IntCV;
|
||||
[ClarityType.UInt]: UIntCV;
|
||||
[ClarityType.StringASCII]: StringAsciiCV;
|
||||
[ClarityType.StringUTF8]: StringUtf8CV;
|
||||
[ClarityType.PrincipalStandard]: StandardPrincipalCV;
|
||||
[ClarityType.PrincipalContract]: ContractPrincipalCV;
|
||||
[ClarityType.List]: ListCV;
|
||||
[ClarityType.Tuple]: TupleCV;
|
||||
[ClarityType.Buffer]: BufferCV;
|
||||
};
|
||||
|
||||
/**
|
||||
* @description narrow down the type of a generic ClarityValue
|
||||
* @example
|
||||
* ```ts
|
||||
* // some functions can return a generic `ClarityValue` type
|
||||
* let value = callReadOnlyFunction();
|
||||
* // ^ ClarityValue
|
||||
* // use `isClarityType` to narrow down the type
|
||||
* assert(isClarityType(value, ClarityType.Int))
|
||||
* console.log(value)
|
||||
* // ^ IntCV
|
||||
* ```
|
||||
*/
|
||||
export function isClarityType<T extends ClarityType>(
|
||||
input: ClarityValue,
|
||||
withType: T
|
||||
): input is ClarityTypetoValue[T] {
|
||||
return input.type === withType;
|
||||
}
|
||||
|
||||
@@ -1,4 +1,11 @@
|
||||
import { ClarityValue, getCVTypeString, cvToString, cvToJSON, cvToValue } from './clarityValue';
|
||||
import {
|
||||
ClarityValue,
|
||||
getCVTypeString,
|
||||
cvToString,
|
||||
cvToJSON,
|
||||
cvToValue,
|
||||
isClarityType,
|
||||
} from './clarityValue';
|
||||
import { ClarityType } from './constants';
|
||||
import { BooleanCV, TrueCV, FalseCV, trueCV, falseCV, boolCV } from './types/booleanCV';
|
||||
import { IntCV, UIntCV, intCV, uintCV } from './types/intCV';
|
||||
@@ -91,6 +98,7 @@ export {
|
||||
stringAsciiCV,
|
||||
stringUtf8CV,
|
||||
getCVTypeString,
|
||||
isClarityType,
|
||||
};
|
||||
|
||||
// Serialization
|
||||
|
||||
@@ -10,6 +10,7 @@ import { BytesReader } from '../src/bytesReader';
|
||||
import {
|
||||
bufferCV,
|
||||
BufferCV,
|
||||
ClarityType,
|
||||
ClarityValue,
|
||||
contractPrincipalCV,
|
||||
contractPrincipalCVFromStandard,
|
||||
@@ -38,7 +39,14 @@ import {
|
||||
uintCV,
|
||||
UIntCV,
|
||||
} from '../src/clarity';
|
||||
import { cvToJSON, cvToString, cvToValue, getCVTypeString } from '../src/clarity/clarityValue';
|
||||
import { Cl } from '../src';
|
||||
import {
|
||||
cvToJSON,
|
||||
cvToString,
|
||||
cvToValue,
|
||||
getCVTypeString,
|
||||
isClarityType,
|
||||
} from '../src/clarity/clarityValue';
|
||||
import { addressToString } from '../src/common';
|
||||
import { deserializeAddress } from '../src/types';
|
||||
|
||||
@@ -423,9 +431,8 @@ describe('Clarity Types', () => {
|
||||
|
||||
// Test lexicographic ordering of tuple keys (to match Node Buffer compare)
|
||||
const lexicographic = Object.keys(tuple.data).sort((a, b) => {
|
||||
// eslint-disable-next-line node/prefer-global/buffer
|
||||
const bufA = Buffer.from(a);
|
||||
// eslint-disable-next-line node/prefer-global/buffer
|
||||
|
||||
const bufB = Buffer.from(b);
|
||||
return bufA.compare(bufB);
|
||||
});
|
||||
@@ -678,4 +685,16 @@ describe('Clarity Types', () => {
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe('Clarity type narrowing', () => {
|
||||
it('can narrow strings', () => {
|
||||
const vUint = Cl.uint(1) as ClarityValue;
|
||||
expect(isClarityType(vUint, ClarityType.UInt)).toBeTruthy();
|
||||
expect(isClarityType(vUint, ClarityType.Int)).toBeFalsy();
|
||||
|
||||
const vInt = Cl.int(1) as ClarityValue;
|
||||
expect(isClarityType(vInt, ClarityType.Int)).toBeTruthy();
|
||||
expect(isClarityType(vInt, ClarityType.UInt)).toBeFalsy();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user