Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ISO6391.validate() type predicate #87

Merged
merged 1 commit into from
Feb 5, 2025
Merged

Conversation

alexgleason
Copy link
Contributor

Adds a type predicate to ISO6391.validate(), so you can do this:

if (ISO6391.validate(lang)) {
  lang; // type is LanguageCode
}

The current behavior without this change is like this:

if (ISO6391.validate(lang)) {
  lang; // type is string
}

This is useful for working with the LanguageCode type throughout a project, like when functions only accept a LanguageCode parameter, or a function needs to return a LanguageCode.

Currently type assertion is required: as LanguageCode. After this change it would not be.

For example:

function iso6393To6391(iso6393: string): LanguageCode | undefined {
  const locale = new Intl.Locale(iso6393);

  if (ISO6391.validate(locale.language)) {
-   return locale.language as LanguageCode;
+   return locale.language;
  }
}

@meikidd meikidd merged commit b0d25ac into meikidd:master Feb 5, 2025
@meikidd
Copy link
Owner

meikidd commented Feb 5, 2025

@alexgleason I have published a new version v3.1.5, please kindly check.

@alexgleason
Copy link
Contributor Author

@meikidd This is working great! See the improvement here: https://gitlab.com/soapbox-pub/ditto/-/merge_requests/637/diffs Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants