Skip to content

Commit

Permalink
refactor: use definePattern from @metamask/utils (#173)
Browse files Browse the repository at this point in the history
Now that `definePattern` has been moved to `@metamask/utils` we can our
local one.
  • Loading branch information
ccharly authored Jan 30, 2025
1 parent e52e5bd commit b0715fb
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 45 deletions.
4 changes: 2 additions & 2 deletions packages/keyring-api/src/eth/types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { object, definePattern } from '@metamask/keyring-utils';
import { object } from '@metamask/keyring-utils';
import type { Infer } from '@metamask/superstruct';
import { nonempty, array, enums, literal } from '@metamask/superstruct';
import { CaipChainIdStruct } from '@metamask/utils';
import { definePattern, CaipChainIdStruct } from '@metamask/utils';

import { EthScope } from '.';
import { EthAccountType, KeyringAccountStruct } from '../api';
Expand Down
4 changes: 2 additions & 2 deletions packages/keyring-api/src/sol/types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { object, definePattern } from '@metamask/keyring-utils';
import { object } from '@metamask/keyring-utils';
import type { Infer } from '@metamask/superstruct';
import { array, enums, literal, nonempty } from '@metamask/superstruct';
import { CaipChainIdStruct } from '@metamask/utils';
import { definePattern, CaipChainIdStruct } from '@metamask/utils';

import { KeyringAccountStruct, SolAccountType } from '../api';

Expand Down
40 changes: 1 addition & 39 deletions packages/keyring-utils/src/superstruct.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
import {
Struct,
assert,
define,
object as stObject,
} from '@metamask/superstruct';
import { Struct, assert, object as stObject } from '@metamask/superstruct';
import type {
Infer,
Context,
Expand Down Expand Up @@ -112,39 +107,6 @@ export function exactOptional<Type, Schema>(
});
}

/**
* Defines a new string-struct matching a regular expression.
*
* Example:
*
* ```ts
* const EthAddressStruct = definePattern('EthAddress', /^0x[0-9a-f]{40}$/iu);
* type EthAddress = Infer<typeof EthAddressStruct>; // string
*
* const CaipChainIdStruct = defineTypedPattern<`${string}:${string}`>(
* 'CaipChainId',
* /^[-a-z0-9]{3,8}:[-_a-zA-Z0-9]{1,32}$/u;
* );
* type CaipChainId = Infer<typeof CaipChainIdStruct>; // `${string}:${string}`
*
* ```
*
* @param name - Type name.
* @param pattern - Regular expression to match.
* @template Pattern - The pattern type, defaults to `string`.
* @returns A new string-struct that matches the given pattern.
*/
export function definePattern<Pattern extends string = string>(
name: string,
pattern: RegExp,
): Struct<Pattern, null> {
return define<Pattern>(
name,
(value: unknown): boolean =>
typeof value === 'string' && pattern.test(value),
);
}

/**
* Assert that a value is valid according to a struct.
*
Expand Down
3 changes: 1 addition & 2 deletions packages/keyring-utils/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { define, type Infer } from '@metamask/superstruct';

import { definePattern } from './superstruct';
import { definePattern } from '@metamask/utils';

/**
* UUIDv4 struct.
Expand Down

0 comments on commit b0715fb

Please sign in to comment.