Skip to content

Commit

Permalink
fix textAverageRelativeLineHeight by using parseFloat instead of pars…
Browse files Browse the repository at this point in the history
…eInt, closes #8
  • Loading branch information
fbuchinger committed Jan 29, 2017
1 parent b4408c3 commit 8fbe3d4
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 4 deletions.
6 changes: 4 additions & 2 deletions src/layoutstats.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
}


function LayoutStats (){
window.LayoutStats = function (){
var self = this;

this.metrics = this.constructor.metrics;
Expand Down Expand Up @@ -146,7 +146,7 @@ var reducers = {
//return the total count if we arrived at the last element
if (itemIndex === array.length -1){
var avg = Object.keys(acc).reduce(function(avg,key){
avg.sum += (parseInt(key,10) * acc[key]);
avg.sum += (parseFloat(key,10) * acc[key]);
avg.amount += acc[key]
return avg
},{sum: 0,amount:0});
Expand All @@ -159,6 +159,8 @@ var reducers = {
}
}

LayoutStats.reducers = reducers;

var rgbToHex = function (rgbStr){

var rgbParts = rgbStr.split('(')[1].split(',').map(function(rgbPart){
Expand Down
1 change: 1 addition & 0 deletions test/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<script src="../node_modules/qunitjs/qunit/qunit.js"></script>
<script src="../src/layoutstats.js"></script>
<script src="setup.js"></script>
<script src="spec/layoutstats.reducers.js"></script>
<script src="spec/jquery.layoutstats.textmetrics.spec.js"></script>
</body>
</html>
4 changes: 2 additions & 2 deletions test/spec/jquery.layoutstats.textmetrics.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,12 @@
var topPropertyTests = [
{
node: '<div style="font-family: Arial, sans-serif; font-size: 11px;">1234</div>',
expected: {topFont: "arial", topStyle: "arial 11px #000000", fontList: ["arial"], topSize: "11px", avgFontSize: 11, topColor: "#000000", avgRelativeLineHeight: 1},
expected: {topFont: "arial", topStyle: "arial 11px #000000", fontList: ["arial"], topSize: "11px", avgFontSize: 11, topColor: "#000000", avgRelativeLineHeight: 1.2},
assertion: 'returns the top font size/style/color/variant as well as the list of fonts and the average font size/line height used in an html document'
},
{
node: '<div style="font-family: Arial, sans-serif; font-size: 11px;"><small style="font-family: serif; font-size: 7px;line-height:21px;">1</small><b>234</b></div>',
expected: {topFont: "arial", topStyle: "arial 11px #000000 bold", fontList: ["arial","serif"],topSize: "11px", avgFontSize: 10, avgRelativeLineHeight: 1.5, topColor: "#000000"},
expected: {topFont: "arial", topStyle: "arial 11px #000000 bold", fontList: ["arial","serif"],topSize: "11px", avgFontSize: 10, avgRelativeLineHeight: 1.65, topColor: "#000000"},
assertion: 'uses inherited styles for its calculations'
},
];
Expand Down
14 changes: 14 additions & 0 deletions test/spec/layoutstats.reducers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
( function( window, QUnit ) {

"use strict";

QUnit.module( "jQuery Layoutstats Reducers");

QUnit.test( "The average reducer ", function( assert ) {
var lineheightsByCharacters = [{"key":"1.50px","value":20},{"key":"1.50px","value":12},{"key":"2.25px","value":4},{"key":"2.25px","value":4}];
var average = LayoutStats.reducers.average;
assert.equal(lineheightsByCharacters.reduce(average.fn, average.initialValue), 1.65, "can calculate the average of a key/value based metric");

})

})( window, QUnit );

0 comments on commit 8fbe3d4

Please sign in to comment.