Last active
September 30, 2021 07:29
-
-
Save gunzip/59b514848a725a3a27158f1d7a2a9ed1 to your computer and use it in GitHub Desktop.
MobileNumberT
This file contains hidden or 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
import { isRight } from "fp-ts/lib/Either"; | |
import { MobileNumberFromString, PatternString } from "../strings"; | |
describe("MobileNumberFromString", () => { | |
it("should fail", () => { | |
const ps = MobileNumberFromString.decode("ABCD"); | |
expect(isRight(ps)).toBeFalsy(); | |
}); | |
it("should success", () => { | |
const ps = MobileNumberFromString.decode("123456789"); | |
expect(isRight(ps)).toBeTruthy(); | |
}); | |
it("should fail", () => { | |
const ps = MobileNumberFromString.decode("12345678"); | |
expect(isRight(ps)).toBeFalsy(); | |
}); | |
it("should success", () => { | |
const ps = MobileNumberFromString.decode("123 45 67 89"); | |
expect(isRight(ps)).toBeTruthy(); | |
}); | |
it("should success", () => { | |
const ps = MobileNumberFromString.decode("123asdad45$%&/(67 89"); | |
expect(isRight(ps)).toBeTruthy(); | |
if (isRight(ps)) { | |
expect(ps.right).toEqual("123456789"); | |
} | |
}); | |
}); |
This file contains hidden or 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
export const MobileNumber = PatternString("^[0-9]{9,10}$"); | |
export type MobileNumber = t.TypeOf<typeof MobileNumber>; | |
export const MobileNumberFromString = new t.Type<MobileNumber, string>( | |
"MobileNumberFromString", | |
MobileNumber.is, | |
(m, c) => | |
pipe( | |
t.string.validate(m, c), | |
E.map(s => s.replace(/[^0-9]/g, "")), | |
E.chain(s => MobileNumber.validate(s, c)) | |
), | |
String | |
); | |
export type MobileNumberFromString = t.TypeOf<typeof MobileNumberFromString>; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment