diff --git a/.changeset/three-cows-jog.md b/.changeset/three-cows-jog.md new file mode 100644 index 0000000000..88e6218cb5 --- /dev/null +++ b/.changeset/three-cows-jog.md @@ -0,0 +1,5 @@ +--- +'@kadena/client': minor +--- + +Exported isIPactDecimal and isIPactInt type guards diff --git a/packages/libs/client/etc/client.api.md b/packages/libs/client/etc/client.api.md index 218d421f9b..8376415158 100644 --- a/packages/libs/client/etc/client.api.md +++ b/packages/libs/client/etc/client.api.md @@ -14,6 +14,8 @@ import type { IExecPayload } from '@kadena/types'; import { IKeyPair } from '@kadena/types'; import type { ILocalCommandResult } from '@kadena/chainweb-node-client'; import type { ILocalOptions } from '@kadena/chainweb-node-client'; +import type { IPactDecimal } from '@kadena/types'; +import type { IPactInt } from '@kadena/types'; import { IPollResponse } from '@kadena/chainweb-node-client'; import { IPreflightResult } from '@kadena/chainweb-node-client'; import type { ISigningCap } from '@kadena/types'; @@ -439,6 +441,12 @@ export interface ISingleSignFunction { (transaction: IUnsignedCommand): Promise; } +// @public (undocumented) +export const isIPactDecimal: (arg: unknown) => arg is IPactDecimal; + +// @public (undocumented) +export const isIPactInt: (arg: unknown) => arg is IPactInt; + // @public export function isSignedTransaction(command: IUnsignedCommand | ICommand): command is ICommand; diff --git a/packages/libs/client/src/index.ts b/packages/libs/client/src/index.ts index 35c17501ab..0f31cbb809 100644 --- a/packages/libs/client/src/index.ts +++ b/packages/libs/client/src/index.ts @@ -14,6 +14,8 @@ export { Pact } from './pact'; export type * from './interfaces/IPactCommand'; export type * from './interfaces/ISigningRequest'; +export { isIPactDecimal, isIPactInt } from './utils/type-guards'; + export { ClientRequestInit, ICommandResult, diff --git a/packages/libs/client/src/utils/tests/type-guards.test.ts b/packages/libs/client/src/utils/tests/type-guards.test.ts new file mode 100644 index 0000000000..71e3c80a5d --- /dev/null +++ b/packages/libs/client/src/utils/tests/type-guards.test.ts @@ -0,0 +1,43 @@ +import { describe, expect, it } from 'vitest'; +import { hasField, isIPactDecimal, isIPactInt, isObject } from '../type-guards'; + +describe('isObject', () => { + it('returns true for object', () => { + expect(isObject({})).toBe(true); + }); + it('returns false for null', () => { + expect(isObject(null)).toBe(false); + }); + it('returns false for non-object types', () => { + [1, 'hello', true, undefined, Symbol('test')].forEach((item) => + expect(isObject(item)).toBe(false), + ); + }); +}); + +describe('hasField', () => { + it('returns true if object has the filed', () => { + expect(hasField({ test: '1' }, 'test')).toBe(true); + }); + it('returns false if object does not has the filed', () => { + expect(hasField({ one: '1' }, 'two')).toBe(false); + }); +}); + +describe('isIPactDecimal', () => { + it('returns true for IPactDecimal', () => { + expect(isIPactDecimal({ decimal: '1' })).toBe(true); + }); + it('returns false for non-IPactDecimal', () => { + expect(isIPactDecimal({ int: '1' })).toBe(false); + }); +}); + +describe('isIPactInt', () => { + it('returns true for IPactInt', () => { + expect(isIPactInt({ int: '1' })).toBe(true); + }); + it('returns false for non-IPactInt', () => { + expect(isIPactInt({ decimal: '1' })).toBe(false); + }); +}); diff --git a/packages/libs/client/src/utils/type-guards.ts b/packages/libs/client/src/utils/type-guards.ts new file mode 100644 index 0000000000..7c111cab6a --- /dev/null +++ b/packages/libs/client/src/utils/type-guards.ts @@ -0,0 +1,30 @@ +import type { IPactDecimal, IPactInt } from '@kadena/types'; + +export const isObject = (obj: unknown): obj is Record => { + return typeof obj === 'object' && obj !== null; +}; + +export const hasField = ( + obj: Record, + field: T, +): obj is Record => { + return field in obj; +}; + +/** + * + * @param arg - the argument to check + * @returns boolean + * @public + */ +export const isIPactDecimal = (arg: unknown): arg is IPactDecimal => + isObject(arg) && hasField(arg, 'decimal') && typeof arg.decimal === 'string'; + +/** + * + * @param arg - the argument to check + * @returns boolean + * @public + */ +export const isIPactInt = (arg: unknown): arg is IPactInt => + isObject(arg) && hasField(arg, 'int') && typeof arg.int === 'string';