]> git.kianting.info Git - uann/blob - src/index.js
00c72f49471b506f9f0f540322eb770a8a208e8d
[uann] / src / index.js
1 "use strict";
2 Object.defineProperty(exports, "__esModule", { value: true });
3 exports.thenDo = exports.charToCodepoint = 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 * convert the one-char string to codepoint.
31 * @param s : the string to code point.
32 * @returns if `s.length > 1` return error; otherwise, return the codepoint of `s`.
33 */
34 function charToCodepoint(s) {
35 if (s.length > 1) {
36 throw new Error("Error: the length of input string for " + s + "is " + s.length + `,
37 however, it should be 1.`);
38 }
39 else {
40 return s.charCodeAt(0);
41 }
42 }
43 exports.charToCodepoint = charToCodepoint;
44 /**
45 * @description thendo(input, f, ...) like
46 * a ==> f
47 */
48 function thenDo(input, f) {
49 if (input._tag == "None") {
50 return input;
51 }
52 else {
53 let inner = input.value;
54 return f(inner);
55 }
56 }
57 exports.thenDo = thenDo;