shaman-website-compiler
Version:
Compile raw HTML, CSS and Javascript into the smallest possible, SEO friendly website.
103 lines • 4.8 kB
JavaScript
;
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
if (typeof b !== "function" && b !== null)
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.JsonRepoAdapter = void 0;
require("reflect-metadata");
var inversify_1 = require("inversify");
var json_repo_1 = require("json-repo");
var JsonRepoAdapter = /** @class */ (function () {
function JsonRepoAdapter(config) {
var _this = this;
this.openConnection = function () {
return _this.context.initialize().then(function (_) {
_this._open = Promise.resolve();
});
};
this.run = function (query) {
return _this.open().then(function (_) {
if (!query.path)
throw new Error("Query parameter missing");
if (!_this.context.models[query.path]) {
throw new Error("Model '".concat(query.path, "' not found, please check configuration."));
}
var result;
if (!Array.isArray(query.args) || !query.args[0] || query.args[0] == "*") {
result = _this.getAllEntities(query.path);
}
else
result = _this.filterEntities(query);
if (query.sort)
result = _this.sortEntities(query, result);
if (query.limit)
result = _this.limitEntities(query, result);
return result;
});
};
this.open = function () {
if (_this._open)
return _this._open;
return _this.openConnection();
};
this.getAllEntities = function (model) {
return _this.context.models[model].filter(function (x) { return !!x; });
};
this.filterEntities = function (query) {
var prop = query.args[0];
var val = query.args[1];
return _this.context.models[query.path].filter(function (x) { return x[prop] == val; });
};
this.sortEntities = function (query, entities) {
var key = query.sort.key;
var result = entities.sort(function (a, b) { return a[key] < b[key] ? -1 : 1; });
if (query.sort.descending)
result = result.reverse();
return result;
};
this.limitEntities = function (query, entities) {
if (entities.length <= query.limit)
return entities;
return entities.slice(0, query.limit);
};
this.context = new DynamicJsonRepoContext(config.dataPath, config.models);
}
JsonRepoAdapter = __decorate([
(0, inversify_1.injectable)(),
__metadata("design:paramtypes", [Object])
], JsonRepoAdapter);
return JsonRepoAdapter;
}());
exports.JsonRepoAdapter = JsonRepoAdapter;
var DynamicJsonRepoContext = /** @class */ (function (_super) {
__extends(DynamicJsonRepoContext, _super);
function DynamicJsonRepoContext(dataPath, models) {
var _this = _super.call(this, dataPath) || this;
_this.models = {};
models.forEach(function (model) { return _this.models[model] = new json_repo_1.Repository(); });
return _this;
}
return DynamicJsonRepoContext;
}(json_repo_1.RepositoryContext));
//# sourceMappingURL=json-repo.adapter.js.map