spreadsheet
typeScript/javascript spreadsheet parser, with formulas.
git clone https://git.vogt.world/spreadsheet.git
Log | Files | README.md
← All files
name: dist/Formulas/Logical.js
-rw-r--r--
5550
  1"use strict";
  2exports.__esModule = true;
  3var ArgsChecker_1 = require("../Utilities/ArgsChecker");
  4var TypeConverter_1 = require("../Utilities/TypeConverter");
  5var Errors_1 = require("../Errors");
  6/**
  7 * Returns true if all of the provided arguments are logically true, and false if any of the provided arguments are
  8 * logically false.
  9 * @param values At least one expression or reference to a cell containing an expression that represents some logical
 10 * value, i.e. TRUE or FALSE, or an expression that can be coerced to a logical value.
 11 * @returns {boolean} if all values are logically true.
 12 * @constructor
 13 */
 14var AND = function () {
 15    var values = [];
 16    for (var _i = 0; _i < arguments.length; _i++) {
 17        values[_i] = arguments[_i];
 18    }
 19    ArgsChecker_1.ArgsChecker.checkAtLeastLength(values, 1, "AND");
 20    var result = true;
 21    for (var i = 0; i < values.length; i++) {
 22        if (typeof values[i] === "string") {
 23            throw new Errors_1.ValueError("AND expects boolean values. But '" + values[i]
 24                + "' is a text and cannot be coerced to a boolean.");
 25        }
 26        else if (values[i] instanceof Array) {
 27            if (!AND.apply(this, values[i])) {
 28                result = false;
 29                break;
 30            }
 31        }
 32        else if (!values[i]) {
 33            result = false;
 34            break;
 35        }
 36    }
 37    return result;
 38};
 39exports.AND = AND;
 40/**
 41 * Tests whether two strings are identical, returning true if they are.
 42 * @param one - The first string to compare
 43 * @param two - The second string to compare
 44 * @returns {boolean}
 45 * @constructor
 46 */
 47var EXACT = function (one, two) {
 48    ArgsChecker_1.ArgsChecker.checkLength(arguments, 2, "EXACT");
 49    one = TypeConverter_1.TypeConverter.firstValue(one);
 50    two = TypeConverter_1.TypeConverter.firstValue(two);
 51    return one.toString() === two.toString();
 52};
 53exports.EXACT = EXACT;
 54/**
 55 * Returns true.
 56 * @returns {boolean} true boolean
 57 * @constructor
 58 */
 59var TRUE = function () {
 60    ArgsChecker_1.ArgsChecker.checkLength(arguments, 0, "TRUE");
 61    return true;
 62};
 63exports.TRUE = TRUE;
 64/**
 65 * Returns false.
 66 * @returns {boolean} false boolean
 67 * @constructor
 68 */
 69var FALSE = function () {
 70    ArgsChecker_1.ArgsChecker.checkLength(arguments, 0, "FALSE");
 71    return false;
 72};
 73exports.FALSE = FALSE;
 74/**
 75 * Returns the opposite of a logical value - NOT(TRUE) returns FALSE; NOT(FALSE) returns TRUE.
 76 * @param value - An expression or reference to a cell holding an expression that represents some logical value.
 77 * @returns {boolean} opposite of a logical value input
 78 * @constructor
 79 */
 80var NOT = function (value) {
 81    ArgsChecker_1.ArgsChecker.checkLength(arguments, 1, "NOT");
 82    if (typeof (value) === "boolean") {
 83        return !value;
 84    }
 85    if (typeof (value) === "string") {
 86        if (value === "") {
 87            return true;
 88        }
 89        throw new Errors_1.ValueError("Function NOT parameter 1 expects boolean values. But '" + value
 90            + "' is a text and cannot be coerced to a boolean.");
 91    }
 92    if (typeof (value) === "number") {
 93        return value === 0;
 94    }
 95    if (value instanceof Array) {
 96        if (value.length === 0) {
 97            throw new Errors_1.RefError("Reference does not exist.");
 98        }
 99        return NOT(value[0]);
100    }
101};
102exports.NOT = NOT;
103/**
104 * Returns true if any of the provided arguments are logically true, and false if all of the provided arguments are
105 * logically false.
106 * @param values An expression or reference to a cell containing an expression that represents some logical value, i.e.
107 * TRUE or FALSE, or an expression that can be coerced to a logical value.
108 * @returns {boolean}
109 * @constructor
110 */
111var OR = function () {
112    var values = [];
113    for (var _i = 0; _i < arguments.length; _i++) {
114        values[_i] = arguments[_i];
115    }
116    ArgsChecker_1.ArgsChecker.checkAtLeastLength(values, 1, "OR");
117    for (var i = 0; i < values.length; i++) {
118        if (values[i] instanceof Array) {
119            if (values[i].length === 0) {
120                throw new Errors_1.RefError("Reference does not exist.");
121            }
122            if (OR.apply(this, values[i])) {
123                return true;
124            }
125        }
126        else if (TypeConverter_1.TypeConverter.valueToBoolean(values[i])) {
127            return true;
128        }
129    }
130    return false;
131};
132exports.OR = OR;
133/**
134 * Exclusive or or exclusive disjunction is a logical operation that outputs true only when inputs differ.
135 * @param values to check for exclusivity.
136 * @returns {boolean} returns true if only one input is considered logically true.
137 * @constructor
138 */
139var XOR = function () {
140    var values = [];
141    for (var _i = 0; _i < arguments.length; _i++) {
142        values[_i] = arguments[_i];
143    }
144    ArgsChecker_1.ArgsChecker.checkAtLeastLength(values, 1, "XOR");
145    var alreadyTruthy = false;
146    for (var i = 0; i < values.length; i++) {
147        if (values[i] instanceof Array) {
148            if (values[i].length === 0) {
149                throw new Errors_1.RefError("Reference does not exist.");
150            }
151            if (XOR.apply(this, values[i])) {
152                if (alreadyTruthy) {
153                    return false;
154                }
155                alreadyTruthy = true;
156            }
157        }
158        else if (TypeConverter_1.TypeConverter.valueToBoolean(values[i])) {
159            if (alreadyTruthy) {
160                return false;
161            }
162            alreadyTruthy = true;
163        }
164    }
165    return alreadyTruthy;
166};
167exports.XOR = XOR;