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.

148 lines (105 loc) 5.34 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); exports.NotInNode = exports.InNode = exports.GeNode = exports.GtNode = exports.LeNode = exports.LtNode = exports.NeqNode = exports.EqNode = exports.OpNode = 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 operator node */ var OpNode = exports.OpNode = function () { /** * Initialises operator node * @param {string} selector The operator selector (left-side) * @param {string} operator The operator (==, !=, etc.) * @param {FiqlNode} args Argument for operator (right-side) */ function OpNode(selector, operator, args) { _classCallCheck(this, OpNode); this.selector = selector; this.operator = operator; this.args = args; } _createClass(OpNode, [{ key: 'build', value: function build() { if (!(this.selector instanceof _.LeafNode)) { throw new _exception.InvalidSelectorError('Selectors must be a LeafNode.'); } if (typeof this.operator !== 'string') { throw new _exception.InvalidOperatorError('Operator must be a string.'); } return '' + this.selector.build() + this.operator + this.args.build(); } }]); return OpNode; }(); var EqNode = exports.EqNode = function (_OpNode) { _inherits(EqNode, _OpNode); function EqNode(selector, args) { _classCallCheck(this, EqNode); return _possibleConstructorReturn(this, (EqNode.__proto__ || Object.getPrototypeOf(EqNode)).call(this, selector, '==', args)); } return EqNode; }(OpNode); var NeqNode = exports.NeqNode = function (_OpNode2) { _inherits(NeqNode, _OpNode2); function NeqNode(selector, args) { _classCallCheck(this, NeqNode); return _possibleConstructorReturn(this, (NeqNode.__proto__ || Object.getPrototypeOf(NeqNode)).call(this, selector, '!=', args)); } return NeqNode; }(OpNode); var LtNode = exports.LtNode = function (_OpNode3) { _inherits(LtNode, _OpNode3); function LtNode(selector, args) { _classCallCheck(this, LtNode); return _possibleConstructorReturn(this, (LtNode.__proto__ || Object.getPrototypeOf(LtNode)).call(this, selector, '=lt=', args)); } return LtNode; }(OpNode); var LeNode = exports.LeNode = function (_OpNode4) { _inherits(LeNode, _OpNode4); function LeNode(selector, args) { _classCallCheck(this, LeNode); return _possibleConstructorReturn(this, (LeNode.__proto__ || Object.getPrototypeOf(LeNode)).call(this, selector, '=le=', args)); } return LeNode; }(OpNode); var GtNode = exports.GtNode = function (_OpNode5) { _inherits(GtNode, _OpNode5); function GtNode(selector, args) { _classCallCheck(this, GtNode); return _possibleConstructorReturn(this, (GtNode.__proto__ || Object.getPrototypeOf(GtNode)).call(this, selector, '=gt=', args)); } return GtNode; }(OpNode); var GeNode = exports.GeNode = function (_OpNode6) { _inherits(GeNode, _OpNode6); function GeNode(selector, args) { _classCallCheck(this, GeNode); return _possibleConstructorReturn(this, (GeNode.__proto__ || Object.getPrototypeOf(GeNode)).call(this, selector, '=ge=', args)); } return GeNode; }(OpNode); var InNode = exports.InNode = function (_OpNode7) { _inherits(InNode, _OpNode7); function InNode(selector, args) { _classCallCheck(this, InNode); return _possibleConstructorReturn(this, (InNode.__proto__ || Object.getPrototypeOf(InNode)).call(this, selector, '=in=', args)); } return InNode; }(OpNode); var NotInNode = exports.NotInNode = function (_OpNode8) { _inherits(NotInNode, _OpNode8); function NotInNode(selector, args) { _classCallCheck(this, NotInNode); return _possibleConstructorReturn(this, (NotInNode.__proto__ || Object.getPrototypeOf(NotInNode)).call(this, selector, '=out=', args)); } return NotInNode; }(OpNode);