-
Notifications
You must be signed in to change notification settings - Fork 591
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4221 from apostrophecms/add-if-feature-to-context…
…_operations Add if feature to context operations
- Loading branch information
Showing
4 changed files
with
58 additions
and
11 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
|
||
export default function checkIfConditions(doc, conditions) { | ||
return Object.entries(conditions).every(([ key, value ]) => { | ||
if (key === '$or') { | ||
return checkOrConditions(doc, value); | ||
} | ||
|
||
const isNotEqualCondition = typeof value === 'object' && | ||
!Array.isArray(value) && | ||
value !== null && | ||
Object.hasOwn(value, '$ne'); | ||
|
||
if (isNotEqualCondition) { | ||
return getNestedPropValue(doc, key) !== value.$ne; | ||
} | ||
|
||
return getNestedPropValue(doc, key) === value; | ||
}); | ||
} | ||
|
||
function checkOrConditions(doc, conditions) { | ||
return conditions.some((condition) => { | ||
return checkIfConditions(doc, condition); | ||
}); | ||
} | ||
|
||
function getNestedPropValue(doc, key) { | ||
if (key.includes('.')) { | ||
const keys = key.split('.'); | ||
return keys.reduce((acc, cur) => acc[cur], doc); | ||
} | ||
|
||
return doc[key]; | ||
} |
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