UNPKG

js-contracts

Version:
159 lines 6.97 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); var log_1 = __importDefault(require("./log")); var lodash_set_1 = __importDefault(require("lodash.set")); var lodash_get_1 = __importDefault(require("lodash.get")); var lodash_clone_1 = __importDefault(require("lodash.clone")); var contract_1 = __importDefault(require("./contract")); var reflect_1 = require("@tsmirror/reflect"); var RESULT = '__RESULT'; var ContractInternal = /** @class */ (function () { function ContractInternal() { } Object.defineProperty(ContractInternal, "shouldSkipContractChecks", { get: function () { return ContractInternal._settings.shouldSkipContractChecks; }, enumerable: true, configurable: true }); ContractInternal._setSettings = function (settings) { this._settings = settings; }; ContractInternal.initContextParameters = function (contractInstance, condition, target, key) { var _this = this; var hasOldValueParameter = ContractInternal.hasOldValueParameter(condition.toString()); var populateCache = function () { var args = []; for (var _i = 0; _i < arguments.length; _i++) { args[_i] = arguments[_i]; } return ContractInternal.populateCache(target.constructor.name, key, ContractInternal.getParameters(condition), args); }; var populateResultCache = function (result) { return ContractInternal.populateFunctionResultCache(result, target.constructor.name, key); }; var destroyCache = function () { return ContractInternal.destroyClassCache(target.constructor.name, key); }; var bindOldValue = function () { contract_1.default.OldValue = ContractInternal._oldValue.bind(_this, target.constructor.name, key, ContractInternal.getOldValueParameter(condition.toString())); }; var bindOldValueByPath = function (context) { contract_1.default.OldValueByPath = function (path) { return ContractInternal._oldValue.apply(context, [target.constructor.name, key, path]); }; }; var bindFunctionResult = function () { contract_1.default.ContractResult = ContractInternal._contractResult.bind(_this, target.constructor.name, key); }; return { hasOldValueParameter: hasOldValueParameter, populateCache: populateCache, populateResultCache: populateResultCache, destroyCache: destroyCache, bindOldValue: bindOldValue, bindOldValueByPath: bindOldValueByPath, bindFunctionResult: bindFunctionResult }; }; ContractInternal._ensures = function (condition, message) { if (!condition) { this.executeError(message); } return condition; }; ContractInternal._assert = function (condition, message) { if (!condition) { this.executeError(message); } return condition; }; ContractInternal._exists = function (collection, predicate, message) { var result = !!collection.find(predicate); if (!result) { this.executeError(message); } return result; }; ContractInternal._forAll = function (collection, predicate, message) { var result = collection.every(predicate); if (!result) { this.executeError(message); } return result; }; ContractInternal._requires = function (condition, message) { if (!condition) { this.executeError(message); } return condition; }; ContractInternal.executeError = function (message) { var shouldFailOnCondition = this._settings.shouldFailOnCondition; if (shouldFailOnCondition) { var error = { message: message }; console.log('reflect', reflect_1.reflect(error)); throw error; } else { log_1.default.log(message); } }; ContractInternal.getParameters = function (func) { var regex = new RegExp('(?:' + func.name + '\\s*|^)\\s*\\((.*?)\\)') .exec(func.toString().replace(/\n/g, '')); if (regex === null) { throw new Error("No parameters found for function " + func.toString()); } return regex[1] .replace(/\/\*.*?\*\//g, '') .replace(/ /g, '') .split(','); }; ContractInternal.hasOldValueParameter = function (func) { return func.indexOf('OldValue') > -1; }; ContractInternal.hasResultParameter = function (func) { return func.indexOf('ContractResult') > -1; }; ContractInternal._oldValue = function (className, functionName, path) { console.log(ContractInternal._cache[className]); var cachedValue = lodash_get_1.default(ContractInternal._cache, className + "." + functionName + "." + path); if (!cachedValue) { log_1.default.log('Cached value has not been found, replace with passed value'); } console.log('cachedValue', cachedValue); return cachedValue; }; ContractInternal.getOldValueParameter = function (func) { var result = new RegExp(/OldValue\(([^)]+)\)/).exec(func); return result && result[1]; }; ContractInternal.populateCache = function (className, functionName, paramsName, values) { lodash_set_1.default(ContractInternal._cache, className + "." + String(functionName), {}); for (var i = 0; i < paramsName.length; i++) { lodash_set_1.default(ContractInternal._cache[className][functionName], paramsName[i], lodash_clone_1.default(values[i])); } }; ContractInternal.populateFunctionResultCache = function (result, className, functionName) { if (!ContractInternal._cache[className]) { lodash_set_1.default(ContractInternal._cache, className + "." + String(functionName), {}); } ContractInternal._cache[className][String(functionName)][RESULT] = result; }; ContractInternal.destroyClassCache = function (className, functionName) { delete ContractInternal._cache[className][functionName]; }; ContractInternal._contractResult = function (className, functionName) { var cachedValue = lodash_get_1.default(ContractInternal._cache, className + "." + functionName + "." + RESULT); if (!cachedValue) { log_1.default.log('Cached value has not been found, replace undefined'); } console.log('cached result', cachedValue); return cachedValue; }; ContractInternal._cache = {}; return ContractInternal; }()); exports.default = ContractInternal; //# sourceMappingURL=contract-internal.js.map