Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions packages/libs/client/etc/client.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 { IPactDecimal } from '@kadena/types';
import { IPactInt } from '@kadena/types';
import { IPollResponse } from '@kadena/chainweb-node-client';
import { IPreflightResult } from '@kadena/chainweb-node-client';
import type { ISigningCap } from '@kadena/types';
Expand Down Expand Up @@ -439,6 +441,12 @@ export interface ISingleSignFunction {
(transaction: IUnsignedCommand): Promise<ICommand | IUnsignedCommand>;
}

// @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;

Expand Down
2 changes: 2 additions & 0 deletions packages/libs/client/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
43 changes: 43 additions & 0 deletions packages/libs/client/src/utils/tests/type-guards.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});
15 changes: 15 additions & 0 deletions packages/libs/client/src/utils/type-guards.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { IPactDecimal, IPactInt } from "@kadena/types";

export const isObject = (obj: unknown): obj is Record<string, unknown> => {
return typeof obj === "object" && obj !== null;
}

export const hasField = <T extends string>(obj: Record<string, unknown>, field: T): obj is Record<T, unknown> => {
return field in obj;
}

export const isIPactDecimal = (arg: unknown): arg is IPactDecimal =>
isObject(arg) && hasField(arg, "decimal") && typeof arg.decimal === "string";

export const isIPactInt = (arg: unknown): arg is IPactInt =>
isObject(arg) && hasField(arg, "int") && typeof arg.int === "string";
Loading