diff --git a/AlphabeticOrUnderscoreOrHyphenString.ts b/AlphabeticOrUnderscoreOrHyphenString.ts index 23e184c..844a6ae 100644 --- a/AlphabeticOrUnderscoreOrHyphenString.ts +++ b/AlphabeticOrUnderscoreOrHyphenString.ts @@ -1,7 +1,9 @@ -import { ValidatedString } from './ValidatedString.ts'; -const isAlphabeticOrUnderscoreOrHyphen = (s: string) => /^[a-zA-Z_-]+$/.test(s); +import { ValidatedString, type ValidatedStringFactory } from './ValidatedString.ts'; +const isAlphabeticOrUnderscoreOrHyphen = (s: string): boolean => /^[a-zA-Z_-]+$/.test(s); -// Why JSRCompatibleFactory? See https://github.com/axhxrx/validated-string/issues/1 +/** + Why JSRCompatibleFactory? See https://github.com/axhxrx/validated-string/issues/1 — consumers of this lib probably don't need to deal with this + */ const JSRCompatibleFactory = ValidatedString.create( isAlphabeticOrUnderscoreOrHyphen, { @@ -10,8 +12,8 @@ const JSRCompatibleFactory = ValidatedString.create( }, ); -const type = JSRCompatibleFactory.type; -const factory = JSRCompatibleFactory.factory; +const type: ValidatedString = JSRCompatibleFactory.type; +const factory: ValidatedStringFactory = JSRCompatibleFactory.factory; export type AlphabeticOrUnderscoreOrHyphenString = typeof type; export const AlphabeticOrUnderscoreOrHyphenString = factory; diff --git a/AlphabeticOrUnderscoreString.ts b/AlphabeticOrUnderscoreString.ts index 75c619e..744f4bb 100644 --- a/AlphabeticOrUnderscoreString.ts +++ b/AlphabeticOrUnderscoreString.ts @@ -1,11 +1,17 @@ -import { ValidatedString } from './ValidatedString.ts'; +import { ValidatedString, type ValidatedStringFactory } from './ValidatedString.ts'; const isAlphabeticOrUnderscore = (s: string): boolean => /^[a-zA-Z_]+$/.test(s); +/** + Why JSRCompatibleFactory? See https://github.com/axhxrx/validated-string/issues/1 — consumers of this lib probably don't need to deal with this + */ const JSRCompatibleFactory = ValidatedString.create(isAlphabeticOrUnderscore, { name: 'AlphabeticOrUnderscoreString', description: 'must contain only letters (A-Z, a-z) or underscores', }); -export type AlphabeticOrUnderscoreString = typeof JSRCompatibleFactory.type; -export const AlphabeticOrUnderscoreString = JSRCompatibleFactory.factory; +const type: ValidatedString = JSRCompatibleFactory.type; +const factory: ValidatedStringFactory = JSRCompatibleFactory.factory; + +export type AlphabeticOrUnderscoreString = typeof type; +export const AlphabeticOrUnderscoreString = factory; diff --git a/AlphabeticString.ts b/AlphabeticString.ts index a812537..484f945 100644 --- a/AlphabeticString.ts +++ b/AlphabeticString.ts @@ -1,11 +1,17 @@ -import { ValidatedString } from './ValidatedString.ts'; +import { ValidatedString, type ValidatedStringFactory } from './ValidatedString.ts'; const isAlphabetic = (s: string): boolean => /^[a-zA-Z]+$/.test(s); +/** + Why JSRCompatibleFactory? See https://github.com/axhxrx/validated-string/issues/1 — consumers of this lib probably don't need to deal with this + */ const JSRCompatibleFactory = ValidatedString.create(isAlphabetic, { name: 'AlphabeticString', description: 'must contain only letters (A-Z, a-z)', }); -export type AlphabeticString = typeof JSRCompatibleFactory.type; -export const AlphabeticString = JSRCompatibleFactory.factory; +const type: ValidatedString = JSRCompatibleFactory.type; +const factory: ValidatedStringFactory = JSRCompatibleFactory.factory; + +export type AlphabeticString = typeof type; +export const AlphabeticString = factory; diff --git a/AwesomeString.ts b/AwesomeString.ts index 341ce7f..170750e 100644 --- a/AwesomeString.ts +++ b/AwesomeString.ts @@ -1,11 +1,17 @@ -import { ValidatedString } from './ValidatedString.ts'; +import { ValidatedString, type ValidatedStringFactory } from './ValidatedString.ts'; const isAwesome = (s: string): boolean => s === 'awesome'; +/** + Why JSRCompatibleFactory? See https://github.com/axhxrx/validated-string/issues/1 — consumers of this lib probably don't need to deal with this + */ const JSRCompatibleFactory = ValidatedString.create(isAwesome); -export type AwesomeString = typeof JSRCompatibleFactory.type; -export const AwesomeString = JSRCompatibleFactory.factory; +const type: ValidatedString = JSRCompatibleFactory.type; +const factory: ValidatedStringFactory = JSRCompatibleFactory.factory; + +export type AwesomeString = typeof type; +export const AwesomeString = factory; // Usage: const x = AwesomeString.try('awesome'); // AwesomeString | undefined diff --git a/LowercaseAlphabeticString.ts b/LowercaseAlphabeticString.ts index 207a20e..c6cc18d 100644 --- a/LowercaseAlphabeticString.ts +++ b/LowercaseAlphabeticString.ts @@ -1,11 +1,17 @@ -import { ValidatedString } from './ValidatedString.ts'; +import { ValidatedString, type ValidatedStringFactory } from './ValidatedString.ts'; const isLowercaseAlpha = (s: string): boolean => /^[a-z]+$/.test(s); +/** + Why JSRCompatibleFactory? See https://github.com/axhxrx/validated-string/issues/1 — consumers of this lib probably don't need to deal with this + */ const JSRCompatibleFactory = ValidatedString.create(isLowercaseAlpha, { name: 'LowercaseAlphabeticString', description: 'must contain only lowercase letters (a-z)', }); -export type LowercaseAlphabeticString = typeof JSRCompatibleFactory.type; -export const LowercaseAlphabeticString = JSRCompatibleFactory.factory; +const type: ValidatedString = JSRCompatibleFactory.type; +const factory: ValidatedStringFactory = JSRCompatibleFactory.factory; + +export type LowercaseAlphabeticString = typeof type; +export const LowercaseAlphabeticString = factory; diff --git a/PrometheusMetricName.ts b/PrometheusMetricName.ts index a86ac81..156baf4 100644 --- a/PrometheusMetricName.ts +++ b/PrometheusMetricName.ts @@ -1,14 +1,20 @@ -import { ValidatedString } from './ValidatedString.ts'; +import { ValidatedString, type ValidatedStringFactory } from './ValidatedString.ts'; const isPrometheusMetricName = (s: string): boolean => /^[a-zA-Z_:][a-zA-Z0-9_:]*$/.test(s); +/** + Why JSRCompatibleFactory? See https://github.com/axhxrx/validated-string/issues/1 — consumers of this lib probably don't need to deal with this + */ const JSRCompatibleFactory = ValidatedString.create(isPrometheusMetricName, { name: 'PrometheusMetricName', description: 'must start with a letter, underscore, or colon, followed by letters, digits, underscores, or colons', }); +const type: ValidatedString = JSRCompatibleFactory.type; +const factory: ValidatedStringFactory = JSRCompatibleFactory.factory; + /** A validated string that represents a Prometheus metric name. See {@link ValidatedString} for more information. Note that this might not really exactly match the rules for Prometheus metric names, but it's close enough for our purposes; namely, generating metrics — don't rely on this to *validate* Prometheus metric names, as it is based on a quick skim of the Prometheus docs. */ -export type PrometheusMetricName = typeof JSRCompatibleFactory.type; -export const PrometheusMetricName = JSRCompatibleFactory.factory; +export type PrometheusMetricName = typeof type; +export const PrometheusMetricName = factory; diff --git a/PrometheusMetricNameWithoutColon.ts b/PrometheusMetricNameWithoutColon.ts index 1446056..1686b75 100644 --- a/PrometheusMetricNameWithoutColon.ts +++ b/PrometheusMetricNameWithoutColon.ts @@ -1,14 +1,21 @@ -import { ValidatedString } from './ValidatedString.ts'; +import { ValidatedString, type ValidatedStringFactory } from './ValidatedString.ts'; const isPrometheusMetricNameWithoutColon = (s: string): boolean => /^[a-zA-Z_][a-zA-Z0-9_]*$/.test(s); +/** + Why JSRCompatibleFactory? See https://github.com/axhxrx/validated-string/issues/1 — consumers of this lib probably don't need to deal with this + */ const JSRCompatibleFactory = ValidatedString.create( isPrometheusMetricNameWithoutColon, { name: 'PrometheusMetricNameWithoutColon', - description: 'must start with a letter or underscore, followed by letters, digits, or underscores', + description: + 'must start with a letter or underscore, followed by letters, digits, or underscores', }, ); -export type PrometheusMetricNameWithoutColon = typeof JSRCompatibleFactory.type; -export const PrometheusMetricNameWithoutColon = JSRCompatibleFactory.factory; +const type: ValidatedString = JSRCompatibleFactory.type; +const factory: ValidatedStringFactory = JSRCompatibleFactory.factory; + +export type PrometheusMetricNameWithoutColon = typeof type; +export const PrometheusMetricNameWithoutColon = factory; diff --git a/ValidatedString.ts b/ValidatedString.ts index 4688ae3..cab1b80 100644 --- a/ValidatedString.ts +++ b/ValidatedString.ts @@ -1,7 +1,7 @@ /** - An interface for factories that create validated strings. Consumers of this library shouldn't need to use it directly. + An interface for factories that create validated strings. Consumers of this library shouldn't need to use it directly. However, it's required for the public API due to JSR ['slow types; restrictions](https://jsr.io/docs/about-slow-types#no-destructuring-in-exports) — see https://github.com/axhxrx/validated-string/issues/1 */ -interface ValidatedStringFactory +export interface ValidatedStringFactory { /** The `try()` method returns the validated string if it's valid, or `undefined` if it isn't. @@ -16,7 +16,7 @@ interface ValidatedStringFactory /** The `ValidatedString` is an internal [branded string type](https://news.ycombinator.com/item?id=40146751) used to enforce the validation. Consumers of this library shouldn't need to use it directly. */ -type ValidatedString = string & { readonly __ブランド: ValidatorT }; +export type ValidatedString = string & { readonly __ブランド: ValidatorT }; /** The `ValidatedString.create()` function creates a factory and a type intended to be used to create new validated string types. It does a wee bit of TypeScript type system jiggery-pokery to make this usage work: