Skip to content

Commit

Permalink
Merge branch 'master' into all-contributors/add-ShaneLee
Browse files Browse the repository at this point in the history
  • Loading branch information
AngeloGiacco authored Apr 5, 2020
2 parents 4557822 + fc9f82c commit 9e2f74b
Show file tree
Hide file tree
Showing 5 changed files with 139 additions and 27 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
.DS_Store
.swp
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ Step 7: go to a product page on amazon.co.uk

Step 8: watch bugs appear

## Running Tests

Open test/SpecRunner.html in a webbrowser


## Examples

![example](images/ex1.png)
Expand Down
77 changes: 50 additions & 27 deletions content.js
Original file line number Diff line number Diff line change
@@ -1,37 +1,60 @@
function priceTime(p) {
var minutes = Math.round(p / 0.14); //minimum wage in uk is £8.20 which values one minute at 14p
var hours = 0;
var string;
if (minutes >= 60) {
while (minutes >= 60) {
minutes = minutes - 60;
hours++
}
if (minutes == 0) {
string = "This costs "+hours + " hours on minimum wage";
} else {
string = "This costs "+hours + " hours and " + minutes + " minutes on minimum wage";
}
} else {
string = "This costs " + minutes + " minutes on minimum wage";
'use strict'

function priceTime(price) {
if ((price !== 0 && !price) || isNegative(price)) {
return 'Unable to calculate price'
}
return string;
const ukMinimumWage = 0.14
const timeInMinutes = Math.round(price / ukMinimumWage)
const hours = calculateHours(timeInMinutes)
const minutes = calculateMinutes(timeInMinutes, hours)
if (timeInMinutes >= 60) {
return minutes === 0 && hours
? `This costs ${generateHours(hours)} on minimum wage`
: `This costs ${generateHours(hours)} and ${generateMinutes(minutes)}`
}
return minutes < 1
? 'This costs less than a minute on minimum wage'
: `This costs ${generateMinutes(minutes)}`
}

function isNegative(num) {
return Math.sign(num) === -1
}

function calculateMinutes(minutes, hours) {
return hours < 0 ? minutes : minutes - (60 * hours)
}

var prices = document.getElementsByClassName('a-color-price'); //amazon class for prices
for (var i = 0, l = prices.length; i < l; i++) {
function calculateHours(minutes) {
return Math.floor(minutes / 60)
}

function generateMinutes(minutes) {
return minutes < 1 ? `less than a minute on minimum wage`
: `${minutes} ${minutes > 1 ? 'minutes' : 'minute'} on minimum wage`
}

function generateHours(hours) {
return `${hours} ${hours > 1 ? 'hours' : 'hour'}`
}

const prices = document.getElementsByClassName('a-color-price'); //amazon class for prices
for (const priceElement of prices) {
try {
var price = prices[i].innerText.split("£")[1];
if (Number.isNaN(price)) {
continue
} else if (typeof price === "undefined") {
continue
} else {
var message = priceTime(price);
prices[i].innerText = string;
const price = getPriceNumber(priceElement)
if (!price) {
priceElement.innerText = priceTime(price);
}
}
catch(err){
continue
}
}

function getPriceNumber(price) {
if (!price || !price.innerText) {
return 0
}
return Number(price.innerText.split('£')[1].split(' ')[0])
}
14 changes: 14 additions & 0 deletions test/SpecRunner.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<!DOCTYPE html>
<html lang="en">
<head>
<title>Testing with Jasmine</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jasmine/2.8.0/jasmine.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jasmine/2.8.0/jasmine.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jasmine/2.8.0/jasmine-html.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jasmine/2.8.0/boot.min.js"></script>
<script src="../content.js"></script>
<script src="contentSpec.js"></script>
</head>
<body>
</body>
</html>
69 changes: 69 additions & 0 deletions test/contentSpec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
describe('calculating the price', () => {
it('should cost 10 minutes when the price is 1.40', () => {
const expectation = '10'
expect(priceTime(1.40)).toContain(expectation)
})

it('should cost 1 hour and 1 minute when the price is 8.54', () => {
const hours = '1 hour '
const minutes = '1 minute '
const result = priceTime(8.54)
expect(result).toContain(hours)
expect(result).toContain(minutes)
})

it('should cost less than a minute when the price is 0.01', () => {
expect(priceTime(0.01)).toContain('less than a minute')
})

it('should cost less than a minute when the price is 0.00', () => {
expect(priceTime(0.00)).toContain('less than a minute')
})

it('should return unable able to calculate price when price null', () => {
expect(priceTime(null)).toContain('Unable to calculate')
})

it('should return unable able to calculate price when price less than 0', () => {
expect(priceTime(-1)).toContain('Unable to calculate')
})


})

describe('calculating minutes', () => {
it('should return 59 when minutes 59 and hours 0', () => {
expect(calculateMinutes(59, 0)).toBe(59)
})

it('should return 1 when minutes 61 and hours 1', () => {
expect(calculateMinutes(61, 1)).toBe(1)
})
})


describe('getting the price number from amazon price element', () => {
it('should return 1.99 when element inner text is £1.99', () => {
const priceElement = { 'innerText' : '£1.99' }
expect(getPriceNumber(priceElement)).toBe(1.99)
})

it('should return 0.99 when element inner text is £0.99', () => {
const priceElement = { 'innerText' : '£0.99' }
expect(getPriceNumber(priceElement)).toBe(0.99)
})

it('should return 0.99 when element inner text is £0.99 (40%)', () => {
const priceElement = { 'innerText' : '£0.99 (40%)' }
expect(getPriceNumber(priceElement)).toBe(0.99)

})
it('should return 0 when element inner text is not present', () => {
const priceElement = { }
expect(getPriceNumber(priceElement)).toBe(0)
})

it('should return 0 when element is not present', () => {
expect(getPriceNumber(null)).toBe(0)
})
})

0 comments on commit 9e2f74b

Please sign in to comment.