Skip to content

Commit

Permalink
Add tests (GustavoFernandes#27)
Browse files Browse the repository at this point in the history
* add tests

* hook up browserify to clients

* add with's back in

* remove unneccessary task

* update tern file

* update yarn
  • Loading branch information
dumbbillyhardy authored Jun 15, 2017
1 parent 160a568 commit 89515a4
Show file tree
Hide file tree
Showing 15 changed files with 1,096 additions and 308 deletions.
1 change: 0 additions & 1 deletion .tern-project
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
"browser"
],
"loadEagerly": [
"!node_modules/**",
"./elements/*",
"./common/*js"
]
Expand Down
2 changes: 2 additions & 0 deletions chrome_extension/contentScript.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const Order = require('../common/order.js');

chrome.storage.onChanged.addListener((changes, namespace) => {
if(namespace !== "local") {
return;
Expand Down
5 changes: 0 additions & 5 deletions chrome_extension/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,6 @@
{
"matches": ["*://orderup.com/orders/*"],
"js": ["./contentScript.js"]
},
{
"matches": ["*://orderup.com/orders/*"],
"js": ["./order.js"],
"run_at": "document_start"
}
],
"web_accessible_resources": ["./order.js"],
Expand Down
60 changes: 27 additions & 33 deletions chrome_extension/popup.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,12 @@
<!-- Tile icon for Win8 (144x144) -->
<meta name="msapplication-TileImage" content="ordersplitter144.png" async>

<!--
<script src="../bower_components/webcomponentsjs/custom-elements-es5-adapter.js"></script>
<!--
<script src="../bower_components/webcomponentsjs/webcomponents-lite.js"></script>
-->

<script src="../common/parsers.js"></script>
<script src="../common/main.js"></script>
<script src="../common/order.js"></script>
<link rel="manifest" href="manifest.json">

<link rel="import" href="../elements/example-order.html">
Expand Down Expand Up @@ -114,36 +112,32 @@
<div align="right" id="sha">INSERT_SHA</div>
<div align="right">INSERT_BUILD_TIME</div>
<script>
// this is to help with debugging any SW caching issues if they appear
var scriptSha = 'INSERT_SHA';
var htmlSha = document.querySelector('#sha').innerText;
console.debug(`script version: ${scriptSha}`);
console.debug(`html version: ${htmlSha}`);
if (scriptSha !== htmlSha) {
alert('Whoops. The cached files on your machine are out of sync with each other. That\'s our bad. Please hard-refresh the page?');
}
let table = document.querySelector("#table");
if(chrome && chrome.storage) {
chrome.storage.onChanged.addListener((changes, namespace) => {
if(namespace !== "local") {
return;
}
parseLocalStorage();
});
}
function parseLocalStorage() {
chrome.storage.local.get('order', keyValueMap => {
let order = Order.fromJSON(JSON.parse(keyValueMap.order));
table.order = order;
});
}
let notifier = Number(window.localStorage.getItem("notifierToParse"));
function requestDom() {
notifier = (notifier === notifier ? notifier: 0) + 1;
chrome.storage.local.set({notifierToParse: notifier});
}
requestDom();
document.getElementById("parse").addEventListener("click", requestDom);
(function() {
const Order = require('../common/order.js');

let table = document.querySelector("#table");
if(chrome && chrome.storage) {
chrome.storage.onChanged.addListener((changes, namespace) => {
if(namespace !== "local") {
return;
}
parseLocalStorage();
});
}
function parseLocalStorage() {
chrome.storage.local.get('order', keyValueMap => {
let order = Order.fromJSON(JSON.parse(keyValueMap.order));
table.order = order;
});
}
let notifier = Number(window.localStorage.getItem("notifierToParse"));
function requestDom() {
notifier = (notifier === notifier ? notifier: 0) + 1;
chrome.storage.local.set({notifierToParse: notifier});
}
requestDom();
document.getElementById("parse").addEventListener("click", requestDom);
})();
</script>
<button id="parse">Parse Dom? Wut?</button>
<order-split-results-table id="table"></order-split-results-table>
Expand Down
1 change: 1 addition & 0 deletions common/order.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,3 +125,4 @@ class Order {
return order.split();
}
}
module.exports = Order;
187 changes: 96 additions & 91 deletions common/parsers.js
Original file line number Diff line number Diff line change
@@ -1,104 +1,109 @@
class QueryStringParser {
/**
* Parses input from a URL query string into an Order.
* @example
* parse('tax=0.30&fee=1.50&tip=1.25&Gus=5.00');
* @param {string} queryString - The URL query string
* @returns {Order} An order parsed from the URL query string
*/
parse(queryString) {
var pairs = queryString.split('&');
let order = new Order();

for (var i = 0; i < pairs.length; i++) {
var pairValues = pairs[i].split('=');

pairValues[1] = Number(pairValues[1]);

if(pairValues[0] === 'fee') {
order.withNonTaxedFees(pairValues[1]);
}
else if(pairValues[0] === 'tax') {
order.withTax(pairValues[1]);
}
else if(pairValues[0] === 'tip') {
order.withTip(pairValues[1]);
}
else {
order.withPerson(decodeURIComponent(pairValues[0]), pairValues[1]);
}
}
(function() {
const Order = require('./order.js');

return order;
}
}

class OrderUpParser {

/**
* Parses the confirmation summary from an OrderUp.com order
* @param {string} orderUpText - The confirmation summary from OrderUp.com
* @param {number} fee
* @param {number} tax
* @param {number} tip - The tip (either a fixed value or percentage)
* @param {boolean} isTipPercentage - True if the tip is a percentage as opposed to a fixed value
* @return {Order} An order parsed from the OrderUp.com confirmation summary
*/
parse(orderUpText, fee=0, tax=0, tip=0, isTipPercentage=false) {
let order = new Order()
.withNonTaxedFees(fee)
.withTax(tax)
.withTip(tip, isTipPercentage);

var lines = orderUpText.split('\n');

lines.reduce((lastItemCost, line) => {
let itemCostMatch, nameMatch;

if(!lastItemCost) {
if (itemCostMatch = line.match('.*\\$([0-9.]+)')) {
let itemCost = Number(itemCostMatch[1]);
return itemCost;
}
}
class QueryStringParser {
/**
* Parses input from a URL query string into an Order.
* @example
* parse('tax=0.30&fee=1.50&tip=1.25&Gus=5.00');
* @param {string} queryString - The URL query string
* @returns {Order} An order parsed from the URL query string
*/
parse(queryString) {
var pairs = queryString.split('&');
let order = new Order();

if (nameMatch = line.match('.*Label for:(.*)')) {
let name = nameMatch[1];
order.withPerson(name, lastItemCost);
return;
}
for (var i = 0; i < pairs.length; i++) {
var pairValues = pairs[i].split('=');

return lastItemCost;
}, null);
pairValues[1] = Number(pairValues[1]);

return order;
}
}

class CsvParser {
parse(csv) {
const order = new Order();

const lines = csv.split('\n');
for(const line of lines) {
if(line.trim() !== '') {
const [name, ...priceStrings] = line.split(',');
const price = priceStrings.map(ps => Number(ps.trim().replace('$',''))).reduce((p,acc) => p+acc, 0);
if(name === 'fee') {
order.withNonTaxedFees(price);
if(pairValues[0] === 'fee') {
order.withNonTaxedFees(pairValues[1]);
}
else if(name === 'tax') {
order.withTax(price);
else if(pairValues[0] === 'tax') {
order.withTax(pairValues[1]);
}
else if(name === 'tip') {
order.withTip(price);
else if(pairValues[0] === 'tip') {
order.withTip(pairValues[1]);
}
else {
order.withPerson(name, price);
order.withPerson(decodeURIComponent(pairValues[0]), pairValues[1]);
}
}

return order;
}
}

class OrderUpParser {

/**
* Parses the confirmation summary from an OrderUp.com order
* @param {string} orderUpText - The confirmation summary from OrderUp.com
* @param {number} fee
* @param {number} tax
* @param {number} tip - The tip (either a fixed value or percentage)
* @param {boolean} isTipPercentage - True if the tip is a percentage as opposed to a fixed value
* @return {Order} An order parsed from the OrderUp.com confirmation summary
*/
parse(orderUpText, fee=0, tax=0, tip=0, isTipPercentage=false) {
let order = new Order()
.withNonTaxedFees(fee)
.withTax(tax)
.withTip(tip, isTipPercentage);

var lines = orderUpText.split('\n');

lines.reduce((lastItemCost, line) => {
let itemCostMatch, nameMatch;

return order;
if(!lastItemCost) {
if (itemCostMatch = line.match('.*\\$([0-9.]+)')) {
let itemCost = Number(itemCostMatch[1]);
return itemCost;
}
}

if (nameMatch = line.match('.*Label for:(.*)')) {
let name = nameMatch[1];
order.withPerson(name, lastItemCost);
return;
}

return lastItemCost;
}, null);

return order;
}
}

class CsvParser {
parse(csv) {
const order = new Order();

const lines = csv.split('\n');
for(const line of lines) {
if(line.trim() !== '') {
const [name, ...priceStrings] = line.split(',');
const price = priceStrings.map(ps => Number(ps.trim().replace('$',''))).reduce((p,acc) => p+acc, 0);
if(name === 'fee') {
order.withNonTaxedFees(price);
}
else if(name === 'tax') {
order.withTax(price);
}
else if(name === 'tip') {
order.withTip(price);
}
else {
order.withPerson(name, price);
}
}
}

return order;
}
}
}
module.exports = {OrderUpParser, QueryStringParser, CsvParser};
})();
Loading

0 comments on commit 89515a4

Please sign in to comment.