UNPKG

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