Skip to content

Commit

Permalink
Alternative approach: push logic down into setByKeyPath function
Browse files Browse the repository at this point in the history
  • Loading branch information
jayaddison committed Sep 26, 2020
1 parent 3eda88a commit d610245
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 8 deletions.
4 changes: 4 additions & 0 deletions src/functions/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,10 @@ export function setByKeyPath(obj, keyPath, value) {
} else {
var period = keyPath.indexOf('.');
if (period !== -1) {
if (hasOwn(obj, keyPath)) {
obj[keyPath] = value;
return;
}
var currentKeyPath = keyPath.substr(0, period);
var remainingKeyPath = keyPath.substr(period + 1);
if (remainingKeyPath === "")
Expand Down
10 changes: 2 additions & 8 deletions src/hooks/hooks-middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
DBCoreKeyRange,
} from "../public/types/dbcore";
import { nop } from '../functions/chaining-functions';
import { getObjectDiff, hasOwn, setByKeyPath } from '../functions/utils';
import { getObjectDiff, setByKeyPath } from '../functions/utils';
import { PSD } from '../helpers/promise';
//import { LockableTableMiddleware } from '../dbcore/lockable-table-middleware';
import { getEffectiveKeys, getExistingValues } from '../dbcore/get-effective-keys';
Expand Down Expand Up @@ -87,13 +87,7 @@ export const hooksMiddleware: Middleware<DBCore> = {
if (additionalChanges) {
const requestedValue = req.values[i];
Object.keys(additionalChanges).forEach(keyPath => {
if (hasOwn(requestedValue, keyPath)) {
// keyPath is already present as a literal property of the object
requestedValue[keyPath] = additionalChanges[keyPath];
} else {
// keyPath represents a new or existing path into the object
setByKeyPath(requestedValue, keyPath, additionalChanges[keyPath]);
}
setByKeyPath(requestedValue, keyPath, additionalChanges[keyPath]);
});
}
}
Expand Down

0 comments on commit d610245

Please sign in to comment.