-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
25 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,24 @@ | ||
import { O, Ord } from './imports' | ||
import { B, O } from './imports' | ||
|
||
const stringSymbol: unique symbol = Symbol() | ||
const _: unique symbol = Symbol() | ||
|
||
export interface NonEmptyTrimmedString { | ||
[stringSymbol]: string | ||
} | ||
|
||
export const fromString = (string: string) => | ||
O.gen(function* () { | ||
const str = string.trim() | ||
if (!isNonBlank(str)) { | ||
return yield* O.none() | ||
} | ||
export type NonEmptyTrimmedString = string & | ||
B.Brand<typeof _> | ||
|
||
return { [stringSymbol]: str } | ||
}) | ||
|
||
export const toString = ( | ||
string: NonEmptyTrimmedString, | ||
) => string[stringSymbol] | ||
export const isNonBlank = (string: string) => | ||
/\S/.test(string) | ||
|
||
export const unsafe_fromString = ( | ||
string: string, | ||
value: string, | ||
) => { | ||
const str = string.trim() | ||
const string = value.trim() | ||
|
||
if (!isNonBlank(str)) { | ||
throw new Error('Not a non-empty string') | ||
} | ||
return { [stringSymbol]: str } | ||
return B.refined<NonEmptyTrimmedString>( | ||
isNonBlank, | ||
() => B.error('String is blank'), | ||
)(string) | ||
} | ||
|
||
export const isNonBlank = (string: string) => | ||
/\S/.test(string) | ||
|
||
export const order: Ord.Order< | ||
NonEmptyTrimmedString | ||
> = (self, that) => | ||
Ord.string( | ||
self[stringSymbol], | ||
that[stringSymbol], | ||
) | ||
export const fromString = O.liftThrowable( | ||
unsafe_fromString, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters