Skip to content

Instantly share code, notes, and snippets.

@MinimumViablePerson
Last active December 22, 2019 12:41
Show Gist options
  • Save MinimumViablePerson/94aae4e600d001ed0940f9d49d80ffeb to your computer and use it in GitHub Desktop.
Save MinimumViablePerson/94aae4e600d001ed0940f9d49d80ffeb to your computer and use it in GitHub Desktop.
const { keyEquals, any, hasNo, has } = require('./utils')
const itemIdEquals = keyEquals('itemId')
const isScheduledItemSuccess = collectionItem =>
any(
itemIdEquals(collectionItem._id),
hasNo('publishingError')
)
const isScheduledItemFailure = collectionItem =>
any(
itemIdEquals(collectionItem._id),
has('publishingError')
)
module.exports = {
isScheduledItemSuccess,
isScheduledItemFailure
}
const { isScheduledItemFailure, isScheduledItemSuccess } = require('./main')
const scheduledItems = [
{ itemId: 1, publishingError: 'Timeout' },
{ itemId: 2 }
]
const failedItem = { _id: 1 }
const successfulItem = { _id: 2 }
console.assert(
isScheduledItemSuccess(failedItem)(scheduledItems) === false,
'isScheduledItemSuccess returns false for failed item'
)
console.assert(
isScheduledItemSuccess(successfulItem)(scheduledItems) === true,
'isScheduledItemSuccess returns true for successful item'
)
console.assert(
isScheduledItemFailure(failedItem)(scheduledItems) === true,
'isScheduledItemFailure returns true for failed item'
)
console.assert(
isScheduledItemFailure(successfulItem)(scheduledItems) === false,
'isScheduledItemFailure returns false for successful item'
)
console.log('All tests ended.')
const any = (...predicates) => array =>
array.some(item => predicates.every(predicate => predicate(item)))
const pipe = (...funcs) => initialValue =>
funcs.reduce((value, func) => func(value), initialValue)
const prop = key => obj => obj[key]
const not = boolean => !boolean
const has = key => obj => obj.hasOwnProperty(key)
const hasNo = key => pipe(has(key), not)
const equals = a => b => a === b
const keyEquals = key => target => pipe(prop(key), equals(target))
module.exports = {
any,
pipe,
prop,
not,
has,
hasNo,
equals,
keyEquals
}
@MinimumViablePerson
Copy link
Author

if changing the function signature is not an option
we can always keep scheduledItems as a param

const isScheduledItemSuccess = (collectionItem, scheduledItems) =>
    any(
      itemIdEquals(collectionItem._id),
      hasNo('publishingError')
    )(scheduledItems)

const isScheduledItemFailure = (collectionItem, scheduledItems) =>
    any(
      itemIdEquals(collectionItem._id),
      has('publishingError')
    )(scheduledItems)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment