Skip to content

Instantly share code, notes, and snippets.

@rjhilgefort
Last active May 1, 2019 17:30
Show Gist options
  • Save rjhilgefort/903ed1da0734483aa3b1564e0ee0792d to your computer and use it in GitHub Desktop.
Save rjhilgefort/903ed1da0734483aa3b1564e0ee0792d to your computer and use it in GitHub Desktop.
Example of vanilla JS to Ramda (Reason in Notion)
const { log, clear } = console
clear()
const logHof = (fn) => (...args) => pipe(
tap(() => log('-----------------------------')),
tap((args) => log(`args: ${args}`)),
fn,
tap((res) => log(`res: ${res}`)),
)(...args)
///////////////////////////////////////////////////////////////////////
// Ramda
///////////////////////////////////////////////////////////////////////
/*
// offsetStringToNumber :: String -> Number
const offsetStringToNumber = pipe(
replace('plus', '+'),
replace('minus', '-'),
Number,
)
// dayToOffset :: String -> Number
const dayToOffset = cond([
[isNil, always(0)],
[equals('tomorrow'), always(1)],
[equals('today'), always(0)],
[equals('yesterday'), always(-1)],
[T, offsetStringToNumber],
])
const dayToOffsetLog = logHof(dayToOffset)
*/
///////////////////////////////////////////////////////////////////////
// Vanilla
///////////////////////////////////////////////////////////////////////
// offsetStringToNumber :: String -> Number
const offsetStringToNumber = (offset) => {
const offsetReplaced = offset.replace('plus', '+').replace('minus', '-')
return Number(offsetReplaced)
}
// dayToOffset :: String -> Number
const dayToOffset = (day) => {
if (day === undefined || day === null) {
return 0
} else if (day === 'tomorrow') {
return 1
} else if (day === 'today') {
return 0
} else if (day === 'yesterday') {
return -1
} else {
return offsetStringToNumber(day)
}
}
const dayToOffsetLog = logHof(dayToOffset)
dayToOffsetLog(null)
dayToOffsetLog('tomorrow')
dayToOffsetLog('today')
dayToOffsetLog('yesterday')
dayToOffsetLog('plus1')
dayToOffsetLog('plus5')
dayToOffsetLog('minus1')
dayToOffsetLog('minus5')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment