UNPKG

@magicbell/react-headless

Version:
30 lines 1.44 kB
import { isNil } from 'ramda'; function eq(value, other) { return value === other || (value !== value && other !== other); } /** * Check if a notification satisfies all conditions of the given `context`. It * Uses equal to compare. * * @param notification Notification to test * @param context Set of rules to test the notification against * @param comparator Function used to compare notification attributes and context values */ export function objMatchesContext(notification, context, comparator = eq) { const diff = []; // backend defaults to unarchived notifications, so we need to do the same context = { archived: false, ...context }; Object.keys(context).forEach((attr) => { const condition = context[attr]; if ((attr === 'read' && !comparator(!isNil(notification.readAt), condition)) || (attr === 'seen' && !comparator(!isNil(notification.seenAt), condition)) || (attr === 'archived' && !comparator(!isNil(notification.archivedAt), condition)) || (attr === 'category' && !comparator(notification.category, condition)) || (attr === 'topic' && !comparator(notification.topic, condition)) || (Object.hasOwnProperty.call(notification, attr) && !comparator(notification[attr], condition))) { diff.push(attr); } }); return { result: diff.length === 0, delta: diff }; } //# sourceMappingURL=strategies.js.map