spreadsheet
typeScript/javascript spreadsheet parser, with formulas.
git clone https://git.vogt.world/spreadsheet.git
Log | Files | README.md
← All files
name: dist/Utilities/CriteriaFunctionFactory.js
-rw-r--r--
4077
 1"use strict";
 2exports.__esModule = true;
 3/**
 4 * Converts wild-card style expressions (in which * matches zero or more characters, and ? matches exactly one character)
 5 * to regular expressions. * and ? can be escaped by prefixing with ~.
 6 * For future reference, something like this might be better
 7 * http://stackoverflow.com/questions/3446170/escape-string-for-use-in-javascript-regex.
 8 * @param c input
 9 * @returns {RegExp} resulting regex
10 */
11function wildCardRegex(c) {
12    var a = c.split("~?");
13    for (var i = 0; i < a.length; i++) {
14        a[i] = a[i].split("?").join(".{1}");
15    }
16    var b = a.join("\\\?");
17    var d = b.split("~*");
18    for (var i = 0; i < d.length; i++) {
19        d[i] = d[i].split("*").join(".*");
20    }
21    return new RegExp("^" + d.join(".*") + "$", "g");
22}
23/**
24 * Creates a criteria function to evaluate elements in a range in an *IF function.
25 */
26var CriteriaFunctionFactory = /** @class */ (function () {
27    function CriteriaFunctionFactory() {
28    }
29    /**
30     * If the criteria is a number, use strict equality checking.
31     * If the criteria is a string, check to see if it is a comparator.
32     * If the criteria is a string, and it is not a comparator, check for regex.
33     * If the criteria is a string and has not matched the above, finally use strict equality checking as a fallback.
34     * If the criteria has not been set, default to false-returning criteria function.
35     * @param criteria
36     * @returns {(x:any)=>boolean}
37     */
38    CriteriaFunctionFactory.createCriteriaFunction = function (criteria) {
39        // Default criteria does nothing
40        var criteriaEvaluation = function (x) {
41            return false;
42        };
43        if (typeof criteria === "number" || typeof criteria === "boolean") {
44            criteriaEvaluation = function (x) {
45                return x === criteria;
46            };
47        }
48        else if (typeof criteria === "string") {
49            var comparisonMatches_1 = criteria.match(/^\s*(<=|>=|=|<>|>|<)\s*(-)?\s*(\$)?\s*([0-9]+([,.][0-9]+)?)\s*$/);
50            if (comparisonMatches_1 !== null && comparisonMatches_1.length >= 6 && comparisonMatches_1[4] !== undefined) {
51                criteriaEvaluation = function (x) {
52                    return eval(x + comparisonMatches_1[1] + (comparisonMatches_1[2] === undefined ? "" : "-") + comparisonMatches_1[4]);
53                };
54                if (comparisonMatches_1[1] === "=") {
55                    criteriaEvaluation = function (x) {
56                        return eval(x + "===" + (comparisonMatches_1[2] === undefined ? "" : "-") + comparisonMatches_1[4]);
57                    };
58                }
59                if (comparisonMatches_1[1] === "<>") {
60                    criteriaEvaluation = function (x) {
61                        return eval(x + "!==" + (comparisonMatches_1[2] === undefined ? "" : "-") + comparisonMatches_1[4]);
62                    };
63                }
64            }
65            else if (criteria.match(/\*|\~\*|\?|\~\?/) !== null) {
66                // Regular string
67                var matches = criteria.match(/\*|\~\*|\?|\~\?/);
68                if (matches !== null) {
69                    criteriaEvaluation = function (x) {
70                        try {
71                            // http://stackoverflow.com/questions/26246601/wildcard-string-comparison-in-javascript
72                            return wildCardRegex(criteria).test(x);
73                        }
74                        catch (e) {
75                            return false;
76                        }
77                    };
78                }
79                else {
80                    criteriaEvaluation = function (x) {
81                        return x === criteria;
82                    };
83                }
84            }
85            else {
86                criteriaEvaluation = function (x) {
87                    return x === criteria;
88                };
89            }
90        }
91        return criteriaEvaluation;
92    };
93    return CriteriaFunctionFactory;
94}());
95exports.CriteriaFunctionFactory = CriteriaFunctionFactory;