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

62 lines
2.3 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 _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 an result of each search results.
*/
var SearchResult = function () {
/**
* @param {object} data - An element of array callbacked by search function.
*/
function SearchResult(data, term, strategy) {
_classCallCheck(this, SearchResult);
this.data = data;
this.term = term;
this.strategy = strategy;
}
_createClass(SearchResult, [{
key: "replace",
value: function replace(beforeCursor, afterCursor) {
var replacement = this.strategy.replace(this.data);
if (replacement !== null) {
if (Array.isArray(replacement)) {
afterCursor = replacement[1] + afterCursor;
replacement = replacement[0];
}
var match = this.strategy.matchText(beforeCursor);
if (match) {
replacement = replacement.replace(/\$&/g, match[0]).replace(/\$(\d)/g, function (_, p1) {
return match[parseInt(p1, 10)];
});
return [[beforeCursor.slice(0, match.index), replacement, beforeCursor.slice(match.index + match[0].length)].join(""), afterCursor];
}
}
}
}, {
key: "render",
value: function render() {
return this.strategy.template(this.data, this.term);
}
}]);
return SearchResult;
}();
exports.default = SearchResult;
//# sourceMappingURL=search_result.js.map