UNPKG

fiql-query-builder

Version:

This module provides the utility to generate valid FIQL query strings by using a JSON objects or the custom classes provided.

77 lines (58 loc) 3.43 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); exports.OrNode = exports.AndNode = exports.ExpNode = undefined; var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); var _exception = require('../exception'); var _ = require('./'); function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } /** * Generic boolean expression node */ var ExpNode = exports.ExpNode = function () { /** * Initialise the node * @param {FiqlNode[]} children A list of child nodes to evaulate * @param {String} operator The boolean operator (e.g. and, or) */ function ExpNode(operator, children) { _classCallCheck(this, ExpNode); this.operator = operator; this.children = children; } _createClass(ExpNode, [{ key: 'build', value: function build() { if (!Array.isArray(this.children)) { throw new _exception.InvalidExpressionChildError('Expression must have Array child.'); } // Recursively build children and then join using the operator return '' + this.children.map(function (child) { if (child instanceof _.OpNode || child instanceof ExpNode || child instanceof _.GroupNode || child instanceof _.LeafNode) { return child.build(); } throw new _exception.InvalidExpressionChildError('Boolean expression child must be operators, values or (grouped) boolean expression.'); }).join(this.operator); } }]); return ExpNode; }(); var AndNode = exports.AndNode = function (_ExpNode) { _inherits(AndNode, _ExpNode); function AndNode(children) { _classCallCheck(this, AndNode); return _possibleConstructorReturn(this, (AndNode.__proto__ || Object.getPrototypeOf(AndNode)).call(this, ';', children)); } return AndNode; }(ExpNode); var OrNode = exports.OrNode = function (_ExpNode2) { _inherits(OrNode, _ExpNode2); function OrNode(children) { _classCallCheck(this, OrNode); return _possibleConstructorReturn(this, (OrNode.__proto__ || Object.getPrototypeOf(OrNode)).call(this, ',', children)); } return OrNode; }(ExpNode);