Skip to content

Instantly share code, notes, and snippets.

@obengwilliam
Created September 2, 2019 14:00
Show Gist options
  • Save obengwilliam/41e0727e1e43e6b5f46ebd939c4b28d8 to your computer and use it in GitHub Desktop.
Save obengwilliam/41e0727e1e43e6b5f46ebd939c4b28d8 to your computer and use it in GitHub Desktop.
const R = require('ramda')
const assert = require('assert')
const body = '<h1>body</>'
const subject = '<h1> subject</h1>'
// function populateDefaults (profile) {
// const cloneProfile = R.clone(profile)
// if (!cloneProfile.template || !cloneProfile.template.products) {
// console.log('No template or no products')
// cloneProfile.template = { products: { body, subject } }
// } else if (!cloneProfile.template.products.body) {
// console.log('There is products or template but no body')
// cloneProfile.template.products.body = body
// } else if (!cloneProfile.template.products.subject) {
// console.log('There is products or template but no subject')
// cloneProfile.template.products.subject = subject
// }
// return cloneProfile
// }
const withDefaults = R.mergeDeepLeft({
template: { products: { body, subject } }
})
const populateDefaults = withDefaults
//
// it should default body and subject if template is not available
const noTemplate = {}
assert.deepEqual(populateDefaults(noTemplate), {
template: { products: { body: body, subject } }
})
console.log('Asserting no template is ok', '\n')
// it should default to body and subject if product is not available
const noProduct = { template: {} }
assert.deepEqual(populateDefaults(noProduct), {
template: { products: { body: body, subject } }
})
console.log('Asserting no products is ok', '\n')
const noBody = { template: { products: { subject } } }
assert.deepEqual(populateDefaults(noBody), {
template: { products: { body: body, subject } }
})
console.log('Asserting no body is ok', '\n')
const noSubject = { template: { products: { subject } } }
assert.deepEqual(populateDefaults(noBody), {
template: { products: { body: body, subject } }
})
console.log('Asserting no subject is ok', '\n')
// const emptyBodyAndSubject = {
// template: { products: { subject: '', body: '' } }
// }
// assert.deepEqual(populateDefaults(emptyBodyAndSubject), {
// template: { products: { body: body, subject } }
// })
// console.log('Asserting empty body and subject is ok', '\n')
const undefinedBodyAndSubject = {
template: { products: { subject: undefined, body: undefined } }
}
assert.deepEqual(populateDefaults(undefinedBodyAndSubject), {
template: { products: { body: body, subject } }
})
console.log('Asserting empty body and subject is ok', '\n')
@obengwilliam
Copy link
Author

the actual implementation was changed to deepRight while acknowledging that empty values will come out as still empty.

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