X-Git-Url: https://git.kianting.info/?a=blobdiff_plain;f=src%2Findex.ts;h=26cd10df0e081ec2ce445b72c26aeef17bdde0bb;hb=13c9e7026b3a5ba274ef046db3311b76aaaac244;hp=3c3384d520bc8bf8d6e54bd6f00bbf47cf909ee2;hpb=6aa3c9a604ffd40cc0d3fdfa75bc575170e15044;p=uann diff --git a/src/index.ts b/src/index.ts index 3c3384d..26cd10d 100644 --- a/src/index.ts +++ b/src/index.ts @@ -45,6 +45,36 @@ export function match1Char(c : string) : (m: MatcheePair) => Maybe } }; +/** + * @description + * it returns a function which test if the first char of the `remained` part of + * the argument of the function is between `l` and `u`, if it's true, update the `MatchedPair` wrapped + * in `Some`. Otherwise, it returns `None`. + * * @param l : lower bound char, 1-char string + * * @param u : upper bound char, 1-char string + * @returns the updated `MatchedPair` wrapped in `Some(x)` or `None`. + */ +export function matchRange(l : string, u : string) : (m: MatcheePair) => Maybe { + let lCodepoint = charToCodepoint(l); + let uCodepoint = charToCodepoint(u); + if (l > u){ + throw new Error("Error: the codepoint of `"+l+"` is not smaller than `"+u+"`)"); + } + return (m : MatcheePair)=>{ + + const charToBeMatched = m.remained[0]; + const codePointToBeMatched = charToCodepoint(charToBeMatched); + if (codePointToBeMatched >= lCodepoint && codePointToBeMatched <= uCodepoint){ + return {_tag: "Some", value :{ + matched : m.matched + charToBeMatched, + remained : m.remained.substring(1)}}; + } + else{ + return {_tag: "None"}; + } + } +}; + /** * convert the one-char string to codepoint. * @param s : the string to code point.