This repository has been archived on 2020-11-02. You can view files and clone it, but cannot push or open issues or pull requests.
2020-11-01 22:46:04 +00:00

78 lines
2.5 KiB
JavaScript

"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
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 _search_result = require("./search_result");
var _search_result2 = _interopRequireDefault(_search_result);
var _strategy = require("./strategy");
var _strategy2 = _interopRequireDefault(_strategy);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
/**
* Encapsulate matching condition between a Strategy and current editor's value.
*/
var Query = function () {
_createClass(Query, null, [{
key: "build",
/**
* Build a Query object by the given string if this matches to the string.
*
* @param {string} text - Head to input cursor.
*/
value: function build(strategy, text) {
if (typeof strategy.props.context === "function") {
var context = strategy.props.context(text);
if (typeof context === "string") {
text = context;
} else if (!context) {
return null;
}
}
var match = strategy.matchText(text);
return match ? new Query(strategy, match[strategy.index], match) : null;
}
}]);
function Query(strategy, term, match) {
_classCallCheck(this, Query);
this.strategy = strategy;
this.term = term;
this.match = match;
}
/**
* Invoke search strategy and callback the given function.
*/
_createClass(Query, [{
key: "execute",
value: function execute(callback) {
var _this = this;
this.strategy.search(this.term, function (results) {
callback(results.map(function (result) {
return new _search_result2.default(result, _this.term, _this.strategy);
}));
}, this.match);
}
}]);
return Query;
}();
exports.default = Query;
//# sourceMappingURL=query.js.map