From 6dc102263d077c3754c804752c85df33d213b4d2 Mon Sep 17 00:00:00 2001 From: Amer Trkic Date: Thu, 10 Oct 2024 16:26:08 +0200 Subject: [PATCH 1/5] Remove lodash.isobjectlike package --- package.json | 3 +-- src/almanac.js | 2 +- src/rule-result.js | 2 +- 3 files changed, 3 insertions(+), 4 deletions(-) diff --git a/package.json b/package.json index 0322ee70..69297949 100644 --- a/package.json +++ b/package.json @@ -89,7 +89,6 @@ "clone": "^2.1.2", "eventemitter2": "^6.4.4", "hash-it": "^6.0.0", - "jsonpath-plus": "^7.2.0", - "lodash.isobjectlike": "^4.0.0" + "jsonpath-plus": "^7.2.0" } } diff --git a/src/almanac.js b/src/almanac.js index 58cc2432..572336a6 100644 --- a/src/almanac.js +++ b/src/almanac.js @@ -5,7 +5,7 @@ import { UndefinedFactError } from './errors' import debug from './debug' import { JSONPath } from 'jsonpath-plus' -import isObjectLike from 'lodash.isobjectlike' +import isObjectLike from 'lodash/isObjectLike' function defaultPathResolver (value, path) { return JSONPath({ path, json: value, wrap: false }) diff --git a/src/rule-result.js b/src/rule-result.js index 5e1fcbd5..79a6c75f 100644 --- a/src/rule-result.js +++ b/src/rule-result.js @@ -1,7 +1,7 @@ 'use strict' import deepClone from 'clone' -import isObject from 'lodash.isobjectlike' +import isObject from 'lodash/isObjectLike' export default class RuleResult { constructor (conditions, event, priority, name) { From 36987ef89f6c7d8304065082008e45c6114abd70 Mon Sep 17 00:00:00 2001 From: chris-pardy Date: Fri, 11 Oct 2024 09:45:47 -0400 Subject: [PATCH 2/5] Removed Lodash from almanac.js The lodash package is being brought in via a transitive depenendcy, remove it entirely and in-line the method --- src/almanac.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/almanac.js b/src/almanac.js index 572336a6..610e2808 100644 --- a/src/almanac.js +++ b/src/almanac.js @@ -5,7 +5,6 @@ import { UndefinedFactError } from './errors' import debug from './debug' import { JSONPath } from 'jsonpath-plus' -import isObjectLike from 'lodash/isObjectLike' function defaultPathResolver (value, path) { return JSONPath({ path, json: value, wrap: false }) @@ -161,7 +160,7 @@ export default class Almanac { debug(`condition::evaluate extracting object property ${path}`) return factValuePromise .then(factValue => { - if (isObjectLike(factValue)) { + if (factValue != null && typeof factValue === 'object') { const pathValue = this.pathResolver(factValue, path) debug(`condition::evaluate extracting object property ${path}, received: ${JSON.stringify(pathValue)}`) return pathValue @@ -179,7 +178,7 @@ export default class Almanac { * Interprets value as either a primitive, or if a fact, retrieves the fact value */ getValue (value) { - if (isObjectLike(value) && Object.prototype.hasOwnProperty.call(value, 'fact')) { // value = { fact: 'xyz' } + if (value != null && typeof value === 'object' && Object.prototype.hasOwnProperty.call(value, 'fact')) { // value = { fact: 'xyz' } return this.factValue(value.fact, value.params, value.path) } return Promise.resolve(value) From c39b45a824cfe4614313ec0fc76de01ea167ea2b Mon Sep 17 00:00:00 2001 From: chris-pardy Date: Fri, 11 Oct 2024 09:47:28 -0400 Subject: [PATCH 3/5] Remove Lodash from rule result Lodash is a transitive dependency, instead of relying on it just in-line the simple isObjectLike code --- src/rule-result.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/rule-result.js b/src/rule-result.js index 79a6c75f..13171de4 100644 --- a/src/rule-result.js +++ b/src/rule-result.js @@ -1,7 +1,6 @@ 'use strict' import deepClone from 'clone' -import isObject from 'lodash/isObjectLike' export default class RuleResult { constructor (conditions, event, priority, name) { @@ -17,7 +16,7 @@ export default class RuleResult { } resolveEventParams (almanac) { - if (isObject(this.event.params)) { + if (this.event.params != null && typeof this.event.params === 'object') { const updates = [] for (const key in this.event.params) { if (Object.prototype.hasOwnProperty.call(this.event.params, key)) { From ed0429cc4826ffb93e251291d59d82032f451985 Mon Sep 17 00:00:00 2001 From: Amer Trkic Date: Mon, 14 Oct 2024 09:39:03 +0200 Subject: [PATCH 4/5] Commit @jagernet-ops suggestion to use use the instanceof operator --- src/almanac.js | 2 +- src/rule-result.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/almanac.js b/src/almanac.js index 610e2808..355fb900 100644 --- a/src/almanac.js +++ b/src/almanac.js @@ -178,7 +178,7 @@ export default class Almanac { * Interprets value as either a primitive, or if a fact, retrieves the fact value */ getValue (value) { - if (value != null && typeof value === 'object' && Object.prototype.hasOwnProperty.call(value, 'fact')) { // value = { fact: 'xyz' } + if (Boolean(value instanceof Object) && Object.prototype.hasOwnProperty.call(value, 'fact')) { // value = { fact: 'xyz' } return this.factValue(value.fact, value.params, value.path) } return Promise.resolve(value) diff --git a/src/rule-result.js b/src/rule-result.js index 13171de4..5e811a98 100644 --- a/src/rule-result.js +++ b/src/rule-result.js @@ -16,7 +16,7 @@ export default class RuleResult { } resolveEventParams (almanac) { - if (this.event.params != null && typeof this.event.params === 'object') { + if (this.event.params instanceof Object) { const updates = [] for (const key in this.event.params) { if (Object.prototype.hasOwnProperty.call(this.event.params, key)) { From 6ede34eecbbff666eb12fafb90722baa0a689894 Mon Sep 17 00:00:00 2001 From: Amer Trkic Date: Mon, 14 Oct 2024 15:31:12 +0200 Subject: [PATCH 5/5] Based on @chris-pardy suggestion using typeof because its more performant and reliable --- src/almanac.js | 2 +- src/rule-result.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/almanac.js b/src/almanac.js index 355fb900..610e2808 100644 --- a/src/almanac.js +++ b/src/almanac.js @@ -178,7 +178,7 @@ export default class Almanac { * Interprets value as either a primitive, or if a fact, retrieves the fact value */ getValue (value) { - if (Boolean(value instanceof Object) && Object.prototype.hasOwnProperty.call(value, 'fact')) { // value = { fact: 'xyz' } + if (value != null && typeof value === 'object' && Object.prototype.hasOwnProperty.call(value, 'fact')) { // value = { fact: 'xyz' } return this.factValue(value.fact, value.params, value.path) } return Promise.resolve(value) diff --git a/src/rule-result.js b/src/rule-result.js index 5e811a98..09350c7e 100644 --- a/src/rule-result.js +++ b/src/rule-result.js @@ -16,7 +16,7 @@ export default class RuleResult { } resolveEventParams (almanac) { - if (this.event.params instanceof Object) { + if (this.event.params !== null && typeof this.event.params === 'object') { const updates = [] for (const key in this.event.params) { if (Object.prototype.hasOwnProperty.call(this.event.params, key)) {