-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add support for belongsTo and hasOne filters on related data (#715
- Loading branch information
1 parent
0d1106a
commit 2bc769e
Showing
6 changed files
with
243 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import _ from 'lodash'; | ||
|
||
/** Do object1 and object2 have at least one common key or Symbol? */ | ||
exports.plainObjectsShareNoKeys = (object1, object2) => { | ||
if (!_.isPlainObject(object1) || !_.isPlainObject(object2)) { | ||
return false; | ||
} | ||
|
||
const keys1 = [...Object.getOwnPropertyNames(object1), ...Object.getOwnPropertySymbols(object1)]; | ||
const keys2 = [...Object.getOwnPropertyNames(object2), ...Object.getOwnPropertySymbols(object2)]; | ||
const commonKeys = keys1.filter((key) => keys2.includes(key)); | ||
|
||
return commonKeys.length === 0; | ||
}; | ||
|
||
/** | ||
* Clone object recursively while rewriting keys with the callback function. | ||
* Symbols are copied without modification (Sequelize.Ops are javascript symbols). | ||
* | ||
* @example | ||
* mapKeysDeep({a: {b: 1}}, key => `_${key}_`); | ||
* => {_a_: {_b_: 1}} | ||
*/ | ||
exports.mapKeysDeep = (object, callback) => { | ||
if (Array.isArray(object)) { | ||
return object.map((child) => exports.mapKeysDeep(child, callback)); | ||
} | ||
|
||
if (_.isPlainObject(object)) { | ||
const newObject = {}; | ||
|
||
Object.getOwnPropertyNames(object).forEach((name) => { | ||
newObject[callback(name)] = exports.mapKeysDeep(object[name], callback); | ||
}); | ||
|
||
Object.getOwnPropertySymbols(object).forEach((symbol) => { | ||
newObject[symbol] = exports.mapKeysDeep(object[symbol], callback); | ||
}); | ||
|
||
return newObject; | ||
} | ||
|
||
return object; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.