Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/himanoa/ntd
https://github.com/himanoa/ntd
Last synced: 20 days ago
JSON representation
- Host: GitHub
- URL: https://github.com/himanoa/ntd
- Owner: himanoa
- License: mit
- Created: 2023-04-02T07:37:04.000Z (almost 2 years ago)
- Default Branch: master
- Last Pushed: 2023-04-16T21:41:24.000Z (over 1 year ago)
- Last Synced: 2024-12-08T21:41:52.056Z (25 days ago)
- Language: TypeScript
- Size: 20.5 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# @himanoa/ntd
ntd is TypeScript library that helps implement the new type pattern.
whici is used to treat two types with same structure as different types in
TypeScript.## Installation
For npm users:
```bash
npm install @himanoa/ntd
```## Usages
```typescript
// Type definition(user.ts)import {
makeValidator,
makeValidationSmartConstructor,
Ok,
Err
} from "@himanoa/ntd";declare const UrlStringSym: unique symbol;
const urlVaidator: Validator = makeVaidator((ok, err) => (value) => {
try {
new URL(value)
return ok(value)
} catch {
return err({kind: 'invalidFormat'})
}
})export const makeUrlString = makeValidationSmartConstructor<
string,
typeof UserNameSym
>()(urlValidator);export type UrlString = FromValidationSmartConstructor<
typeof makeUrlString
>;// Use defined types
const validUrlStringResult = makeUrlString("https://example.com/");
const invalidUrlStringResult = makeUrlString("example!!!"); //if (validUrlStringResult.ok) {
validUrlStringResult.v // https://example.com/
}if (!invalidUrlStringResult.ok) {
invalidUrlStringResult.e // { kind: 'invalidFormat' }
}
```## Contributing
Contributions are welcome! Please feel free to open an issue or pull request.
## License
This library is licensed under the MIT license. See the [LICENSE](./LICENSE)
file for more information.