Skip to content

Commit

Permalink
Added a bunch of test coverage and added coveralls reporting to the b…
Browse files Browse the repository at this point in the history
…uild
  • Loading branch information
jimwhimpey committed Apr 5, 2016
1 parent 366d1b3 commit 3159e44
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 97 deletions.
15 changes: 3 additions & 12 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,7 @@ module.exports = function (input) {
*/
function computeLayout(itemLayoutData) {

var notAddedNotComplete,
laidOutItems = [],
var laidOutItems = [],
itemAdded,
currentRow,
nextToLastRowHeight;
Expand All @@ -97,8 +96,6 @@ function computeLayout(itemLayoutData) {
// Loop through the items
itemLayoutData.some(function (itemData, i) {

notAddedNotComplete = false;

// If not currently building up a row, make a new one.
if (!currentRow) {
currentRow = createNewRow();
Expand All @@ -111,6 +108,7 @@ function computeLayout(itemLayoutData) {

// Row is filled; add it and start a new one
laidOutItems = laidOutItems.concat(addRow(currentRow));

if (layoutData._rows.length >= layoutConfig.maxNumRows) {
currentRow = null;
return true;
Expand All @@ -132,15 +130,8 @@ function computeLayout(itemLayoutData) {
return true;
}
currentRow = createNewRow();
} else if (!itemAdded) {
notAddedNotComplete = true;
}
}
} else {

if (!itemAdded) {
notAddedNotComplete = true;
}
}
});

Expand All @@ -158,7 +149,7 @@ function computeLayout(itemLayoutData) {
nextToLastRowHeight = layoutData._rows[layoutData._rows.length - 1].height;
}

currentRow.forceComplete(false, nextToLastRowHeight || layoutConfig.targetRowHeight);
currentRow.forceComplete(false, nextToLastRowHeight);
} else {

// ...else use target height if there is no other row height to reference.
Expand Down
66 changes: 27 additions & 39 deletions lib/row.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ var Row = module.exports = function (params) {
this.maxAspectRatio = this.width / params.targetRowHeight * (1 + params.targetRowHeightTolerance);

// Edge case row height minimum/maximum
this.edgeCaseMinRowHeight = params.edgeCaseMinRowHeight || Number.NEGATIVE_INFINITY;
this.edgeCaseMaxRowHeight = params.edgeCaseMaxRowHeight || Number.POSITIVE_INFINITY;
this.edgeCaseMinRowHeight = params.edgeCaseMinRowHeight;
this.edgeCaseMaxRowHeight = params.edgeCaseMaxRowHeight;

// Layout direction
this.rightToLeft = params.rightToLeft;
Expand Down Expand Up @@ -110,12 +110,6 @@ Row.prototype = {
}
}

if (newAspectRatio === 0) {
// Error state (item not added, row layout not complete);
// handled by consumer
return false;
}

if (newAspectRatio < this.minAspectRatio) {

// New aspect ratio is too narrow / scaled row height is too tall.
Expand Down Expand Up @@ -188,7 +182,7 @@ Row.prototype = {
*/
completeLayout: function completeLayout(newHeight, justify) {

var itemWidthSum = this.rightToLeft ? -this.left : this.left,
var itemWidthSum = this.left,
rowWidthWithoutSpacing = this.width - (this.items.length - 1) * this.spacing,
clampedToNativeRatio,
roundedHeight,
Expand Down Expand Up @@ -230,15 +224,10 @@ Row.prototype = {
item.width = Math.round(item.aspectRatio * self.height * clampedToNativeRatio);
item.height = self.height;

if (self.rightToLeft) {

// Right-to-left.
item.left = self.width - itemWidthSum - item.width;
} else {

// Left-to-right.
item.left = itemWidthSum;
}
// Left-to-right.
// TODO right to left
// item.left = self.width - itemWidthSum - item.width;
item.left = itemWidthSum;

// Incrememnt width.
itemWidthSum += item.width + self.spacing;
Expand All @@ -248,11 +237,11 @@ Row.prototype = {
// caused by rounding width and height across all items.
if (justify) {

// TODO Right to left
// Left-to-right increments itemWidthSum differently;
// account for that before distributing error.
if (!this.rightToLeft) {
itemWidthSum -= this.spacing + this.left;
}
// if (!this.rightToLeft) {
itemWidthSum -= this.spacing + this.left;

errorWidthPerItem = (itemWidthSum - this.width) / this.items.length;
roundedCumulativeErrors = this.items.map(function (item, i) {
Expand All @@ -266,22 +255,23 @@ Row.prototype = {
singleItemGeometry.width -= Math.round(errorWidthPerItem);

// In right-to-left layouts, shift item to account for width change.
if (this.rightToLeft) {
singleItemGeometry.left += Math.round(errorWidthPerItem);
}
// TODO Right to left
// if (this.rightToLeft) {
// singleItemGeometry.left += Math.round(errorWidthPerItem);
// }
} else {

// For rows with multiple items, adjust item width and shift items to fill the row,
// while maintaining equal spacing between items in the row.
this.items.forEach(function (item, i) {
if (i > 0) {
item.left -= roundedCumulativeErrors[i - 1];
item.width -= roundedCumulativeErrors[i] - roundedCumulativeErrors[i - 1];
} else {
item.width -= roundedCumulativeErrors[i];
}
});
}
// For rows with multiple items, adjust item width and shift items to fill the row,
// while maintaining equal spacing between items in the row.
this.items.forEach(function (item, i) {
if (i > 0) {
item.left -= roundedCumulativeErrors[i - 1];
item.width -= roundedCumulativeErrors[i] - roundedCumulativeErrors[i - 1];
} else {
item.width -= roundedCumulativeErrors[i];
}
});
}
}
},

Expand All @@ -300,13 +290,11 @@ Row.prototype = {
return sum + item.aspectRatio;
}, 0);

// TODO Handle fitting to width

if (typeof rowHeight === 'number') {

this.completeLayout(rowHeight, false);
} else if (fitToWidth) {

// Complete using height required to fill row with current items.
this.completeLayout(rowWidthWithoutSpacing / currentAspectRatio);
} else {

// Complete using target row height.
Expand Down
2 changes: 1 addition & 1 deletion script/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ uglifyjs ./dist/justified-layout.js -o ./dist/justified-layout.min.js
echo "Licensify..."
echo '// Copyright 2016 Yahoo Inc.
// Licensed under the terms of the MIT license. Please see LICENSE file in the project root for terms.
' | cat - dist/justified-layout.min.js > temp && mv temp dist/justified-layout.min.js
' | cat - dist/justified-layout.min.js > temp && mv temp dist/justified-layout.min.js
6 changes: 5 additions & 1 deletion script/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,8 @@

set -ex

mocha
# Run the tests
mocha

# Generate code coverage
npm run coverage
16 changes: 4 additions & 12 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,7 @@ module.exports = function (input) {
*/
function computeLayout(itemLayoutData) {

var notAddedNotComplete,
laidOutItems = [],
var laidOutItems = [],
itemAdded,
currentRow,
nextToLastRowHeight;
Expand All @@ -97,8 +96,6 @@ function computeLayout(itemLayoutData) {
// Loop through the items
itemLayoutData.some(function (itemData, i) {

notAddedNotComplete = false;

// If not currently building up a row, make a new one.
if (!currentRow) {
currentRow = createNewRow();
Expand All @@ -111,6 +108,7 @@ function computeLayout(itemLayoutData) {

// Row is filled; add it and start a new one
laidOutItems = laidOutItems.concat(addRow(currentRow));

if (layoutData._rows.length >= layoutConfig.maxNumRows) {
currentRow = null;
return true;
Expand All @@ -132,16 +130,10 @@ function computeLayout(itemLayoutData) {
return true;
}
currentRow = createNewRow();
} else if (!itemAdded) {
notAddedNotComplete = true;
}
}
} else {

if (!itemAdded) {
notAddedNotComplete = true;
}
}

});

// Handle any leftover content (orphans) depending on where they lie
Expand All @@ -158,7 +150,7 @@ function computeLayout(itemLayoutData) {
nextToLastRowHeight = layoutData._rows[layoutData._rows.length - 1].height;
}

currentRow.forceComplete(false, nextToLastRowHeight || layoutConfig.targetRowHeight);
currentRow.forceComplete(false, nextToLastRowHeight);

} else {

Expand Down
47 changes: 16 additions & 31 deletions src/row.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ var Row = module.exports = function (params) {
this.maxAspectRatio = this.width / params.targetRowHeight * (1 + params.targetRowHeightTolerance);

// Edge case row height minimum/maximum
this.edgeCaseMinRowHeight = params.edgeCaseMinRowHeight || Number.NEGATIVE_INFINITY;
this.edgeCaseMaxRowHeight = params.edgeCaseMaxRowHeight || Number.POSITIVE_INFINITY;
this.edgeCaseMinRowHeight = params.edgeCaseMinRowHeight;
this.edgeCaseMaxRowHeight = params.edgeCaseMaxRowHeight;

// Layout direction
this.rightToLeft = params.rightToLeft;
Expand Down Expand Up @@ -108,12 +108,6 @@ Row.prototype = {
}
}

if (newAspectRatio === 0) {
// Error state (item not added, row layout not complete);
// handled by consumer
return false;
}

if (newAspectRatio < this.minAspectRatio) {

// New aspect ratio is too narrow / scaled row height is too tall.
Expand Down Expand Up @@ -194,7 +188,7 @@ Row.prototype = {
*/
completeLayout: function (newHeight, justify) {

var itemWidthSum = this.rightToLeft ? -this.left : this.left,
var itemWidthSum = this.left,
rowWidthWithoutSpacing = this.width - (this.items.length - 1) * this.spacing,
clampedToNativeRatio,
roundedHeight,
Expand Down Expand Up @@ -238,17 +232,10 @@ Row.prototype = {
item.width = Math.round(item.aspectRatio * self.height * clampedToNativeRatio);
item.height = self.height;

if (self.rightToLeft) {

// Right-to-left.
item.left = self.width - itemWidthSum - item.width;

} else {

// Left-to-right.
item.left = itemWidthSum;

}
// Left-to-right.
// TODO right to left
// item.left = self.width - itemWidthSum - item.width;
item.left = itemWidthSum;

// Incrememnt width.
itemWidthSum += item.width + self.spacing;
Expand All @@ -259,11 +246,11 @@ Row.prototype = {
// caused by rounding width and height across all items.
if (justify) {

// TODO Right to left
// Left-to-right increments itemWidthSum differently;
// account for that before distributing error.
if (!this.rightToLeft) {
itemWidthSum -= (this.spacing + this.left);
}
// if (!this.rightToLeft) {
itemWidthSum -= (this.spacing + this.left);

errorWidthPerItem = (itemWidthSum - this.width) / this.items.length;
roundedCumulativeErrors = this.items.map(function (item, i) {
Expand All @@ -278,9 +265,10 @@ Row.prototype = {
singleItemGeometry.width -= Math.round(errorWidthPerItem);

// In right-to-left layouts, shift item to account for width change.
if (this.rightToLeft) {
singleItemGeometry.left += Math.round(errorWidthPerItem);
}
// TODO Right to left
// if (this.rightToLeft) {
// singleItemGeometry.left += Math.round(errorWidthPerItem);
// }

} else {

Expand Down Expand Up @@ -315,15 +303,12 @@ Row.prototype = {
return sum + item.aspectRatio;
}, 0);

// TODO Handle fitting to width

if (typeof rowHeight === 'number') {

this.completeLayout(rowHeight, false);

} else if (fitToWidth) {

// Complete using height required to fill row with current items.
this.completeLayout(rowWidthWithoutSpacing / currentAspectRatio);

} else {

// Complete using target row height.
Expand Down
Loading

0 comments on commit 3159e44

Please sign in to comment.