UNPKG

@glitterprotocol/glitter-sdk

Version:

The JavaScript SDK for Glitter

122 lines 4.54 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.DateRangeQuery = exports.NumericRangeQuery = exports.RegexpQuery = exports.MatchPhraseQuery = exports.MatchQuery = exports.prepareSQL = exports.escapeString = exports.queryStringPrepare = exports.escapeQuery = exports.reserveQuery = void 0; var reservedCharsReg = /[-+=&|><!(){}[\]^"~*?:\\/ ]/g; var escapeCharReg = /\\/g; var reserveQuery = function (value) { var trimmedValue = value; return trimmedValue.replace(reservedCharsReg, '\\$&'); }; exports.reserveQuery = reserveQuery; var escapeQuery = function (value) { return value.replace(escapeCharReg, '\\'); }; exports.escapeQuery = escapeQuery; var queryStringPrepare = function (queries) { return queries.map(function (query) { return query.toString(); }).join(' '); }; exports.queryStringPrepare = queryStringPrepare; var escapeTable = {}; escapeTable[String.fromCharCode(0)] = '\\0'; escapeTable['\\'] = '\\\\'; escapeTable['\n'] = '\\n'; escapeTable['\r'] = '\\r'; escapeTable[String.fromCharCode(26)] = '\\Z'; escapeTable['"'] = '\\"'; escapeTable["'"] = "\\'"; var escapeString = function (value) { return value .split('') .map(function (char) { return escapeTable[char] || char; }) .join(''); }; exports.escapeString = escapeString; var prepareSQL = function (sql, sqlString) { var queryString = "'".concat((0, exports.escapeString)(sqlString), "'"); return sql.replace('?', queryString); }; exports.prepareSQL = prepareSQL; // reserved var MatchQuery = /** @class */ (function () { function MatchQuery(field, value, boost) { if (boost === void 0) { boost = 1.0; } this.field = field; this.value = value; this.boost = boost; } MatchQuery.prototype.prepareQuery = function () { return "".concat(this.field, ":").concat((0, exports.reserveQuery)(this.value), "^").concat(this.boost); }; MatchQuery.prototype.toString = function () { return this.prepareQuery(); }; return MatchQuery; }()); exports.MatchQuery = MatchQuery; var MatchPhraseQuery = /** @class */ (function () { function MatchPhraseQuery(field, value, boost) { if (boost === void 0) { boost = 1.0; } this.field = field; this.value = value; this.boost = boost; } MatchPhraseQuery.prototype.prepareQuery = function () { return "".concat(this.field, ":\"").concat(this.value, "\"^").concat(this.boost); }; MatchPhraseQuery.prototype.toString = function () { return this.prepareQuery(); }; return MatchPhraseQuery; }()); exports.MatchPhraseQuery = MatchPhraseQuery; var RegexpQuery = /** @class */ (function () { function RegexpQuery(field, value, boost) { if (boost === void 0) { boost = 1.0; } this.field = field; this.value = value; this.boost = boost; } RegexpQuery.prototype.prepareQuery = function () { return "".concat(this.field, ":/").concat(this.value, "/^").concat(this.boost); }; RegexpQuery.prototype.toString = function () { return this.prepareQuery(); }; return RegexpQuery; }()); exports.RegexpQuery = RegexpQuery; var NumericRangeQuery = /** @class */ (function () { function NumericRangeQuery(field, operator, value, boost) { if (boost === void 0) { boost = 1.0; } this.field = field; this.operator = operator; this.value = value; this.boost = boost; } NumericRangeQuery.prototype.prepareQuery = function () { return "".concat(this.field, ":").concat(this.operator).concat(this.value, "^").concat(this.boost); }; NumericRangeQuery.prototype.toString = function () { return this.prepareQuery(); }; return NumericRangeQuery; }()); exports.NumericRangeQuery = NumericRangeQuery; var DateRangeQuery = /** @class */ (function () { function DateRangeQuery(field, operator, value, boost) { if (boost === void 0) { boost = 1.0; } this.field = field; this.operator = operator; this.value = value; this.boost = boost; } DateRangeQuery.prototype.prepareQuery = function () { return "".concat(this.field, ":").concat(this.operator, "\"").concat(this.value, "\"^").concat(this.boost); }; DateRangeQuery.prototype.toString = function () { return this.prepareQuery(); }; return DateRangeQuery; }()); exports.DateRangeQuery = DateRangeQuery; //# sourceMappingURL=parseQueryStr.js.map