Skip to content

Commit d003660

Browse files
refactor(dif): Correct the diff percentage output
1 parent d805908 commit d003660

File tree

3 files changed

+14
-14
lines changed

3 files changed

+14
-14
lines changed

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
"lodash.isequal": "^4.4.0",
4141
"lodash.lowercase": "^4.3.0",
4242
"lodash.merge": "^4.6.0",
43+
"lodash.uniq": "^4.5.0",
4344
"lonlng": "0.1.0",
4445
"mapbox.js": "^2.4.0",
4546
"moment": "^2.15.1",

src/components/route-card.js

+12-13
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import Color from 'color'
22
import React from 'react'
33
import toCapitalCase from 'lodash.capitalize'
44
import toSpaceCase from 'lodash.lowercase'
5+
import unique from 'lodash.uniq'
56

67
import DeepEqual from './deep-equal'
78
import Icon from './icon'
@@ -164,13 +165,6 @@ function findRouteForPattern ({id, patterns, routes}) {
164165
return routes.find((r) => r.route_id === patterns.find((p) => p.pattern_id === id).route_id)
165166
}
166167

167-
function unique (a) {
168-
return a.reduce(function (n, e) {
169-
if (n.indexOf(e) === -1) n.push(e)
170-
return n
171-
}, [])
172-
}
173-
174168
const typeToIcon = {
175169
0: 'subway',
176170
1: 'subway',
@@ -196,23 +190,28 @@ function showAccess (keys, base) {
196190
}
197191

198192
function AccessDiffPercentage ({
199-
diff
193+
newAccess,
194+
originalAccess
200195
}) {
201-
if (diff > 0) return <span className='pull-right increase'><strong>{diff}</strong>%<Icon type='level-up' /></span>
202-
else if (diff < 0) return <span className='pull-right decrease'><strong>{diff * -1}</strong>%<Icon className='fa-rotate-180' type='level-up' /></span>
203-
else return <span />
196+
const actualDiff = newAccess - originalAccess
197+
const nume = actualDiff > 0
198+
? newAccess - originalAccess
199+
: originalAccess - newAccess
200+
const diff = parseInt((nume / originalAccess * 100).toFixed(1))
201+
if (diff === 0) return <span />
202+
else if (actualDiff > 0) return <span className='pull-right increase'><strong>{diff}</strong>%<Icon type='level-up' /></span>
203+
else return <span className='pull-right decrease'><strong>{diff * -1}</strong>%<Icon className='fa-rotate-180' type='level-up' /></span>
204204
}
205205

206206
function showDiff (keys, base, comparison) {
207207
return (
208208
<div className='CardAccess'>
209209
<div className='heading'>Access to</div>
210210
{keys.map((key, i) => {
211-
const diff = parseInt(((base[key] - comparison[key]) / base[key] * 100).toFixed(1))
212211
return (
213212
<div className='Metric' key={key}>
214213
<MetricIcon name={key} /><strong> {(base[key] | 0).toLocaleString()} </strong> {toSpaceCase(key)}
215-
<AccessDiffPercentage diff={diff} />
214+
<AccessDiffPercentage newAccess={base[key]} originalAccess={comparison[key]} />
216215
</div>
217216
)
218217
})}

yarn.lock

+1-1
Original file line numberDiff line numberDiff line change
@@ -4136,7 +4136,7 @@ lodash.templatesettings@^4.0.0:
41364136
dependencies:
41374137
lodash._reinterpolate "~3.0.0"
41384138

4139-
lodash.uniq@^4.3.0:
4139+
lodash.uniq, lodash.uniq@^4.3.0:
41404140
version "4.5.0"
41414141
resolved "https://registry.yarnpkg.com/lodash.uniq/-/lodash.uniq-4.5.0.tgz#d0225373aeb652adc1bc82e4945339a842754773"
41424142

0 commit comments

Comments
 (0)