Skip to content

Instantly share code, notes, and snippets.

@obengwilliam
Created September 2, 2019 14:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • 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')
@brianmugweru
Copy link

const definedBodyAndSubject = { template: { products: { subject: 'different subject', body: 'different body' } } }

assert.deepEqual(populateDefaults(definedBodyAndSubject), {
  template: { 
    products: { 
      body: definedBodyAndSubject.template.products.body, 
      subject: definedBodyAndSubject.template.products.subject
    } 
  }
})

Given that the above implementation was supposed to help in replacing the body and subject given that they did not exist, happens to be that even in existence of body and subject, the values still gets replaced
Assert.deepEqual above fails

@obengwilliam
Copy link
Author

yes that is true but in this gists i was only trying to those cases where we have empty and so on.

@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