:rocket: Generify `transformPolarities`
aj-rom opened this issue Β· comments
The transform methods for the wikia bother me slightly. Currently for the transformPolarities()
function we are passing the entire object and then using variable assignment to override the polarities on the new item. Example
Redundancy
This seems a little redundant as we are passing the entire oldFrame
object into the parameters and then deconstructing to get the values of the AuraPolarity and Polarities key, and then overriding the newFrame
object.
One way I was thinking we could get around this would be to use Object.assign
destructivley:
// We don't want the entire object passed, only the necessary data.
const transformPolarities = (AuraPolarities = false, Polarities = false, targetWeapon) => {
// No longer need to create a new object
// const outputFrame = { ...targetWeapon }
// Let's create the variables we need instead
let auraPolarity, polarities
if (AuraPolarity) {
auraPolarity = (POLARITIES[AuraPolarity] || AuraPolarity || '').toLowerCase()
if (auraPolarity && !auraPolarity.length) auraPolarity = undefined
if (auraPolarity === 'none') auraPolarity = undefined
}
if (Polarities) {
polarities = Polarities.map(polarity => {
let out
out = (POLARITIES[polarity] || polarity || '').toLowerCase()
if (out && !out.length) out = undefined
return out
}).filter(p => p)
} else {
polarities = []
}
const objToOverrideOrAdd = { auraPolarity: auraPolarity, polarities: polarities }
return Object.assign(targetWeapon, objToOverrideOrAdd)
}
Feel like this cleans up the function a little and then instead of re-creating the objects we can simply call:
transformPolarities(AuraPolarity, Polarities, newFrame)
// instead of
newFrame = transformPolarities(oldFrame, newFrame)
Generalization
Currently we have a transform
function for each submodule. What if we had one main transform function that handled this?
Describe the upgrade you'd like
In summary, clean up the transformPolarities
function and generalize it.
Describe alternatives you've considered
As listed above, although we could create some sort of method to get the current Wikia polarities keys (if they change in the future).
personally, i'd make a separate utility function in a file:
// transformPolarities.js
const POLARITIES = require('./polarities')
const transform = (field) => {
let output
if (field) {
output = (POLARITIES[field] || field || '').toLowerCase()
if (output && !output.length) output = undefined
if (output === 'none') output = undefined
}
return output
}
module.exports = ({ AuraPolarity, StancePolarity, Polarity, Polarities }, target) => {
const output = { ...target }
output.auraPolarity = transform(AuraPolarity)
output.stancePolarity = transform(StancePolarity)
output.polarity = transform(Polarity)
output.polarities = Polarities && Polarities.length ? Polarities.map(transform) : undefined
return output
}
personally, i'd make a separate utility function in a file:
// transformPolarities.js const POLARITIES = require('./polarities') const transform = (field) => { let output if (field) { output = (POLARITIES[field] || field || '').toLowerCase() if (output && !output.length) output = undefined if (output === 'none') output = undefined } return output } module.exports = ({ AuraPolarity, StancePolarity, Polarity, Polarities }, target) => { const output = { ...target } output.auraPolarity = transform(AuraPolarity) output.stancePolarity = transform(StancePolarity) output.polarity = transform(Polarity) output.polarities = Polarities && Polarities.length ? Polarities.map(transform) : undefined return output }
This would be great.
i'll put a PR out once the other gets merged
π This issue has been resolved in version 1.1253.0 π
The release is available on:
Your semantic-release bot π¦π