UNPKG

4.43 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6
7var _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; }; }();
8
9var _hashIt = require('hash-it');
10
11var _hashIt2 = _interopRequireDefault(_hashIt);
12
13function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
14
15function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
16
17var Fact = function () {
18 /**
19 * Returns a new fact instance
20 * @param {string} id - fact unique identifer
21 * @param {object} options
22 * @param {boolean} options.cache - whether to cache the fact's value for future rules
23 * @param {primitive|function} valueOrMethod - constant primitive, or method to call when computing the fact's value
24 * @return {Fact}
25 */
26 function Fact(id, valueOrMethod, options) {
27 _classCallCheck(this, Fact);
28
29 this.id = id;
30 var defaultOptions = { cache: true };
31 if (typeof options === 'undefined') {
32 options = defaultOptions;
33 }
34 if (typeof valueOrMethod !== 'function') {
35 this.value = valueOrMethod;
36 this.type = this.constructor.CONSTANT;
37 } else {
38 this.calculationMethod = valueOrMethod;
39 this.type = this.constructor.DYNAMIC;
40 }
41
42 if (!this.id) throw new Error('factId required');
43 if (typeof this.value === 'undefined' && typeof this.calculationMethod === 'undefined') {
44 throw new Error('facts must have a value or method');
45 }
46
47 this.priority = parseInt(options.priority || 1, 10);
48 this.options = Object.assign({}, defaultOptions, options);
49 this.cacheKeyMethod = this.defaultCacheKeys;
50 return this;
51 }
52
53 _createClass(Fact, [{
54 key: 'isConstant',
55 value: function isConstant() {
56 return this.type === this.constructor.CONSTANT;
57 }
58 }, {
59 key: 'isDynamic',
60 value: function isDynamic() {
61 return this.type === this.constructor.DYNAMIC;
62 }
63
64 /**
65 * Return the fact value, based on provided parameters
66 * @param {object} params
67 * @param {Almanac} almanac
68 * @return {any} calculation method results
69 */
70
71 }, {
72 key: 'calculate',
73 value: function calculate(params, almanac) {
74 // if constant fact w/set value, return immediately
75 if (Object.prototype.hasOwnProperty.call(this, 'value')) {
76 return this.value;
77 }
78 return this.calculationMethod(params, almanac);
79 }
80
81 /**
82 * Return a cache key (MD5 string) based on parameters
83 * @param {object} obj - properties to generate a hash key from
84 * @return {string} MD5 string based on the hash'd object
85 */
86
87 }, {
88 key: 'defaultCacheKeys',
89
90
91 /**
92 * Default properties to use when caching a fact
93 * Assumes every fact is a pure function, whose computed value will only
94 * change when input params are modified
95 * @param {string} id - fact unique identifer
96 * @param {object} params - parameters passed to fact calcution method
97 * @return {object} id + params
98 */
99 value: function defaultCacheKeys(id, params) {
100 return { params: params, id: id };
101 }
102
103 /**
104 * Generates the fact's cache key(MD5 string)
105 * Returns nothing if the fact's caching has been disabled
106 * @param {object} params - parameters that would be passed to the computation method
107 * @return {string} cache key
108 */
109
110 }, {
111 key: 'getCacheKey',
112 value: function getCacheKey(params) {
113 if (this.options.cache === true) {
114 var cacheProperties = this.cacheKeyMethod(this.id, params);
115 var _hash = Fact.hashFromObject(cacheProperties);
116 return _hash;
117 }
118 }
119 }], [{
120 key: 'hashFromObject',
121 value: function hashFromObject(obj) {
122 return (0, _hashIt2.default)(obj);
123 }
124 }]);
125
126 return Fact;
127}();
128
129Fact.CONSTANT = 'CONSTANT';
130Fact.DYNAMIC = 'DYNAMIC';
131
132exports.default = Fact;
\No newline at end of file