From e64a65d2b6363956a3dc0455ea13c6f0d74515a6 Mon Sep 17 00:00:00 2001 From: Vincent Taverna Date: Sun, 15 Jan 2017 20:06:12 -0500 Subject: [PATCH] deliver #180 zero pad vi template rename --- app/screens/Detail/components/Nickname.js | 45 +++++++++++++---------- 1 file changed, 26 insertions(+), 19 deletions(-) diff --git a/app/screens/Detail/components/Nickname.js b/app/screens/Detail/components/Nickname.js index 878f61e..8703d4d 100644 --- a/app/screens/Detail/components/Nickname.js +++ b/app/screens/Detail/components/Nickname.js @@ -15,6 +15,31 @@ function getNumberInCircle(num) { return `${num}` } +// 07 instead of 7, but 10 is still 10 +function zeroPad(str, len = 3) { + return `${Array(len - str.length).join('0')}${str}` +} + +function templateRename(pokemon, name) { + const totalEnergy = parseInt(Math.floor(100 / pokemon.move_2.energy_cost), 10) + const energy = getNumberInCircle(totalEnergy) + const vi = zeroPad((100 - pokemon.iv).toFixed(0)) + const attack = getNumberInCircle(pokemon.attack) + const defense = getNumberInCircle(pokemon.defense) + const stamina = getNumberInCircle(pokemon.stamina) + + return name + .replace('[IV]', pokemon.iv.toFixed(0)) + .replace('[VI]', vi) + .replace('[ATT]', attack) + .replace('[DEF]', defense) + .replace('[STA]', stamina) + .replace('[FAST]', pokemon.move_1.power.toFixed(0)) + .replace('[CHARGE]', pokemon.move_2.power.toFixed(0)) + .replace('[ENERGY]', energy) + .replace('[HP]', pokemon.stamina_max) +} + class Nickname extends React.Component { static propTypes = { pokemon: PropTypes.object.isRequired, @@ -81,25 +106,7 @@ class Nickname extends React.Component { } = this.props if (e.key === 'Enter') { - let newName = e.target.value - - const totalEnergy = parseInt(Math.floor(100 / pokemon.move_2.energy_cost), 10) - const energy = getNumberInCircle(totalEnergy) - const vi = (100 - pokemon.iv).toFixed(0) - const attack = getNumberInCircle(pokemon.attack) - const defense = getNumberInCircle(pokemon.defense) - const stamina = getNumberInCircle(pokemon.stamina) - - newName = newName - .replace('[IV]', pokemon.iv.toFixed(0)) - .replace('[VI]', vi) - .replace('[ATT]', attack) - .replace('[DEF]', defense) - .replace('[STA]', stamina) - .replace('[FAST]', pokemon.move_1.power.toFixed(0)) - .replace('[CHARGE]', pokemon.move_2.power.toFixed(0)) - .replace('[ENERGY]', energy) - .replace('[HP]', pokemon.stamina_max) + const newName = templateRename(pokemon, e.target.value) if (newName.length > 12) { ipcRenderer.send('error-message', 'The name must contain 12 characters or less.')