Skip to content

Commit

Permalink
Merge pull request #3 from axhxrx/1-cannot-publish-due-to-jsr-compati…
Browse files Browse the repository at this point in the history
…bility-issues

🔧 fix: give Windsurf another crack at dealing with JSR's restrictions… PART II THE EXCITING SEQUEL
  • Loading branch information
protiev authored Dec 14, 2024
2 parents 890dbe9 + a628c9b commit 5064e54
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 27 deletions.
12 changes: 7 additions & 5 deletions AlphabeticOrUnderscoreOrHyphenString.ts
Original file line number Diff line number Diff line change
@@ -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,
{
Expand All @@ -10,8 +12,8 @@ const JSRCompatibleFactory = ValidatedString.create(
},
);

const type = JSRCompatibleFactory.type;
const factory = JSRCompatibleFactory.factory;
const type: ValidatedString<typeof isAlphabeticOrUnderscoreOrHyphen> = JSRCompatibleFactory.type;
const factory: ValidatedStringFactory<typeof isAlphabeticOrUnderscoreOrHyphen> = JSRCompatibleFactory.factory;

export type AlphabeticOrUnderscoreOrHyphenString = typeof type;
export const AlphabeticOrUnderscoreOrHyphenString = factory;
12 changes: 9 additions & 3 deletions AlphabeticOrUnderscoreString.ts
Original file line number Diff line number Diff line change
@@ -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<typeof isAlphabeticOrUnderscore> = JSRCompatibleFactory.type;
const factory: ValidatedStringFactory<typeof isAlphabeticOrUnderscore> = JSRCompatibleFactory.factory;

export type AlphabeticOrUnderscoreString = typeof type;
export const AlphabeticOrUnderscoreString = factory;
12 changes: 9 additions & 3 deletions AlphabeticString.ts
Original file line number Diff line number Diff line change
@@ -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<typeof isAlphabetic> = JSRCompatibleFactory.type;
const factory: ValidatedStringFactory<typeof isAlphabetic> = JSRCompatibleFactory.factory;

export type AlphabeticString = typeof type;
export const AlphabeticString = factory;
12 changes: 9 additions & 3 deletions AwesomeString.ts
Original file line number Diff line number Diff line change
@@ -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<typeof isAwesome> = JSRCompatibleFactory.type;
const factory: ValidatedStringFactory<typeof isAwesome> = JSRCompatibleFactory.factory;

export type AwesomeString = typeof type;
export const AwesomeString = factory;

// Usage:
const x = AwesomeString.try('awesome'); // AwesomeString | undefined
Expand Down
12 changes: 9 additions & 3 deletions LowercaseAlphabeticString.ts
Original file line number Diff line number Diff line change
@@ -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<typeof isLowercaseAlpha> = JSRCompatibleFactory.type;
const factory: ValidatedStringFactory<typeof isLowercaseAlpha> = JSRCompatibleFactory.factory;

export type LowercaseAlphabeticString = typeof type;
export const LowercaseAlphabeticString = factory;
12 changes: 9 additions & 3 deletions PrometheusMetricName.ts
Original file line number Diff line number Diff line change
@@ -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<typeof isPrometheusMetricName> = JSRCompatibleFactory.type;
const factory: ValidatedStringFactory<typeof isPrometheusMetricName> = 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;
15 changes: 11 additions & 4 deletions PrometheusMetricNameWithoutColon.ts
Original file line number Diff line number Diff line change
@@ -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<typeof isPrometheusMetricNameWithoutColon> = JSRCompatibleFactory.type;
const factory: ValidatedStringFactory<typeof isPrometheusMetricNameWithoutColon> = JSRCompatibleFactory.factory;

export type PrometheusMetricNameWithoutColon = typeof type;
export const PrometheusMetricNameWithoutColon = factory;
6 changes: 3 additions & 3 deletions ValidatedString.ts
Original file line number Diff line number Diff line change
@@ -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<ValidatorT>
export interface ValidatedStringFactory<ValidatorT>
{
/**
The `try()` method returns the validated string if it's valid, or `undefined` if it isn't.
Expand All @@ -16,7 +16,7 @@ interface ValidatedStringFactory<ValidatorT>
/**
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<ValidatorT> = string & { readonly __ブランド: ValidatorT };
export type ValidatedString<ValidatorT> = 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:
Expand Down

0 comments on commit 5064e54

Please sign in to comment.