JavaScript
Filter array by several conditions

by Benny Neugebauer

const isOTRMessage = notification => notification.type === 'conversation.otr-message-add';
const isInCurrentConversation = notification => notification.conversation === conversationId;
const wasSentByOurCurrentClient = notification =>
  notification.from === userId && (notification.data && notification.data.sender === clientId);
const hasExpectedTimestamp = notification => notification.time === dateTime.toISOString();

notificationService
  .getNotifications(undefined, undefined, 10000)
  .then(({notifications}) =>
    notifications
      .map(notification => notification.payload)
      .reduce((acc, payload) => acc.concat(payload))
      .filter(isOTRMessage)
      .filter(isInCurrentConversation)
      .filter(wasSentByOurCurrentClient)
      .filter(hasExpectedTimestamp)
  );

const isOTRMessage = notification => notification.type === 'conversation.otr-message-add';
const isInCurrentConversation = notification => notification.conversation === conversationId;
const wasSentByOurCurrentClient = notification =>
  notification.from === userId && (notification.data && notification.data.sender === clientId);
const hasExpectedTimestamp = notification => notification.time === dateTime.toISOString();

notificationService
  .getNotifications(undefined, undefined, 10000)
  .then(({notifications}) =>
    notifications
      .map(notification => notification.payload)
      .reduce((acc, payload) => acc.concat(payload))
          .filter(notification => {
            return (
              isOTRMessage(notification) &&
              isInCurrentConversation(notification) &&
              wasSentByOurCurrentClient(notification) &&
              hasExpectedTimestamp(notification)
            );
          });
  );