]> git.kianting.info Git - uann/blob - src/index.js
add matchRange
[uann] / src / index.js
1 "use strict";
2 Object.defineProperty(exports, "__esModule", { value: true });
3 exports.thenDo = exports.charToCodepoint = exports.matchRange = exports.match1Char = void 0;
4 var fs = require('fs');
5 /**
6 * @description
7 * it returns a function which test if the first char of the `remained` part of
8 * the argument of the function is `c`, if it's true, update the `MatchedPair` wrapped
9 * in `Some`. Otherwise, it returns `None`.
10 * * @param c : the char to be test.
11 * @returns the updated `MatchedPair` wrapped in `Some(x)` or `None`.
12 */
13 function match1Char(c) {
14 return (m) => {
15 const charToBeMatched = m.remained[0];
16 if (charToBeMatched === c) {
17 return { _tag: "Some", value: {
18 matched: m.matched + charToBeMatched,
19 remained: m.remained.substring(1)
20 } };
21 }
22 else {
23 return { _tag: "None" };
24 }
25 };
26 }
27 exports.match1Char = match1Char;
28 ;
29 /**
30 * @description
31 * it returns a function which test if the first char of the `remained` part of
32 * the argument of the function is between `l` and `u`, if it's true, update the `MatchedPair` wrapped
33 * in `Some`. Otherwise, it returns `None`.
34 * * @param l : lower bound char, 1-char string
35 * * @param u : upper bound char, 1-char string
36 * @returns the updated `MatchedPair` wrapped in `Some(x)` or `None`.
37 */
38 function matchRange(l, u) {
39 let lCodepoint = charToCodepoint(l);
40 let uCodepoint = charToCodepoint(u);
41 if (l > u) {
42 throw new Error("Error: the codepoint of `" + l + "` is not smaller than `" + u + "`)");
43 }
44 return (m) => {
45 const charToBeMatched = m.remained[0];
46 const codePointToBeMatched = charToCodepoint(charToBeMatched);
47 if (codePointToBeMatched >= lCodepoint && codePointToBeMatched <= uCodepoint) {
48 return { _tag: "Some", value: {
49 matched: m.matched + charToBeMatched,
50 remained: m.remained.substring(1)
51 } };
52 }
53 else {
54 return { _tag: "None" };
55 }
56 };
57 }
58 exports.matchRange = matchRange;
59 ;
60 /**
61 * convert the one-char string to codepoint.
62 * @param s : the string to code point.
63 * @returns if `s.length > 1` return error; otherwise, return the codepoint of `s`.
64 */
65 function charToCodepoint(s) {
66 if (s.length > 1) {
67 throw new Error("Error: the length of input string for " + s + "is " + s.length + `,
68 however, it should be 1.`);
69 }
70 else {
71 return s.charCodeAt(0);
72 }
73 }
74 exports.charToCodepoint = charToCodepoint;
75 /**
76 * @description thendo(input, f, ...) like
77 * a ==> f
78 */
79 function thenDo(input, f) {
80 if (input._tag == "None") {
81 return input;
82 }
83 else {
84 let inner = input.value;
85 return f(inner);
86 }
87 }
88 exports.thenDo = thenDo;