Skip to content
This repository has been archived by the owner on Oct 21, 2019. It is now read-only.

Commit

Permalink
Improvements and added more helper functions
Browse files Browse the repository at this point in the history
  • Loading branch information
TylerVigario committed Dec 11, 2018
1 parent 0a102ef commit 4a26601
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 27 deletions.
12 changes: 6 additions & 6 deletions dist/weight.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "weight.js",
"version": "1.3.3",
"version": "1.3.4",
"description": "JavaScript classes to convert imperial mass units as well as output and parse as text.",
"scripts": {
"build": "npx webpack --config webpack.config.js",
Expand Down
50 changes: 42 additions & 8 deletions src/mass_unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*
* @author Tyler Vigario (MeekLogic)
* @license MIT
* @version 1.3.3
* @version 1.3.4
*/

/** Class representing a mass unit. */
Expand All @@ -17,28 +17,54 @@ export default class MassUnit {
}

/**
* Return weight value.
* @type {number}
*/
get value() {
return this.weight;
}

set value(weight) {
weight = this.getValue(weight);
this.weight = this.getValue(weight);
}

if (pounds === false) {
throw 'Parameter passed to constructor is not a valid weight.';
}
/**
* Round weight down.
* @returns {object}
*/
floor() {
this.weight = Math.floor(this.weight);

this.weight = weight;
return this;
}

/**
* Round weight up.
* @returns {object}
*/
ceil() {
this.weight = Math.ceil(this.weight);

return this;
}

/**
* Round weight.
* @param {number} [digits = 0]
* @returns {object}
*/
round(digits = 0) {
this.weight = this.toFixed(digits);

return this;
}

/**
* The toFixed() method formats a number using fixed-point notation.
* @param {digits} [digits = 0] - Optional. The number of digits to appear after the decimal point; this may be a value between 0 and 20, inclusive, and implementations may optionally support a larger range of values. If this argument is omitted, it is treated as 0.
* @param {number} [digits = 0] - Optional. The number of digits to appear after the decimal point; this may be a value between 0 and 20, inclusive, and implementations may optionally support a larger range of values. If this argument is omitted, it is treated as 0.
* @returns {string} A string representing the given number using fixed-point notation.
*/
toFixed(digits) {
toFixed(digits = 0) {
return this.weight.toFixed(digits);
}

Expand Down Expand Up @@ -99,4 +125,12 @@ export default class MassUnit {
isLighter(weight) {
return this.weight < this.getValue(weight);
}

/**
* Check if current object is empty.
* @returns {boolean} True if current object is empty or false if not.
*/
isEmpty() {
return (this.weight === 0);
}
}
17 changes: 9 additions & 8 deletions src/ounces.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*
* @author Tyler Vigario (MeekLogic)
* @license MIT
* @version 1.3.3
* @version 1.3.4
*/

import MassUnit from './mass_unit';
Expand Down Expand Up @@ -152,20 +152,21 @@ export default class Ounces extends MassUnit {
toString(spaces = true, roundTo = 0) {
let formattedWeight = '';

let ounces = this.weight;
let ounces = this;

// Excess ounces = pounds
if (ounces >= 16) {
// Extract pounds from ounces
let pounds = Math.floor(this.toPounds().value);
if (ounces.isHeavier(15)) {
// Extract whole pounds from ounces
let pounds = ounces.toPounds().floor();

ounces -= (new Pounds(pounds)).toOunces().value;
// Reduce ounces by whole pounds
ounces.subtract(pounds);

// Format pounds for human consumption
formattedWeight = pounds.toString() + (spaces ? ' ' : '') + (pounds === 1 ? 'lb' : 'lbs');
formattedWeight = pounds.toFixed() + (spaces ? ' ' : '') + (pounds.isSame(1) ? 'lb' : 'lbs');

// Any ounces remaining?
if (ounces === 0) {
if (ounces.isEmpty()) {
// Return if no ounces are remaining
return formattedWeight;
}
Expand Down
6 changes: 3 additions & 3 deletions src/pounds.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*
* @author Tyler Vigario (MeekLogic)
* @license MIT
* @version 1.3.3
* @version 1.3.4
*/

import MassUnit from './mass_unit';
Expand Down Expand Up @@ -98,7 +98,7 @@ export default class Pounds extends MassUnit {
* Convert weight to text.
* @returns {string} Formatted weight.
*/
toString(roundTo = 0) {
return this.toOunces().toString(roundTo);
toString(spaces = true, roundTo = 0) {
return this.toOunces().toString(spaces, roundTo);
}
}
2 changes: 1 addition & 1 deletion src/weight.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*
* @author Tyler Vigario (MeekLogic)
* @license MIT
* @version 1.3.3
* @version 1.3.4
*/

import Ounces from './ounces';
Expand Down
31 changes: 31 additions & 0 deletions tests/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,46 +5,55 @@ var Ounces = Weight.Ounces;

var problems = [{
question: '8lb 36oz',
answer: '10 lbs 4 oz',
ounces: 164,
pounds: 10.25
},
{
question: '164.85oz',
answer: '10 lbs 5 oz',
ounces: 164.85,
pounds: 10.303125
},
{
question: '10lbs',
answer: '10 lbs',
ounces: 160,
pounds: 10
},
{
question: '2lbs , 17oz',
answer: '3 lbs 1 oz',
ounces: 49,
pounds: 3.0625
},
{
question: 48,
answer: '3 lbs',
ounces: 48,
pounds: 3
},
{
question: '17 lb 14 oz',
answer: '17 lbs 14 oz',
ounces: 286,
pounds: 17.875
},
{
question: '3lbs4oz',
answer: '3 lbs 4 oz',
ounces: 52,
pounds: 3.25
},
{
question: '3lbs 4',
answer: '3 lbs 4 oz',
ounces: 52,
pounds: 3.25
},
{
question: '4 oz',
answer: '4 oz',
ounces: 4,
pounds: 0.25
}
Expand All @@ -61,6 +70,17 @@ test('Ounces parse tests', function (t) {
});
});

test('Ounces format tests', function (t) {
t.plan(problems.length);

// Validate Ounces .toString()
problems.forEach((problem) => {
let ounces = Ounces.parse(problem.question).toString();

t.equal(ounces, problem.answer, ounces);
});
});

test('Pounds parse tests', function (t) {
t.plan(problems.length);

Expand All @@ -72,6 +92,17 @@ test('Pounds parse tests', function (t) {
});
});

test('Pounds format tests', function (t) {
t.plan(problems.length);

// Validate Ounces .toString()
problems.forEach((problem) => {
let pounds = Pounds.parse(problem.question).toString();

t.equal(pounds, problem.answer, pounds);
});
});

test('Additional tests', function (t) {
t.plan(3);

Expand Down

0 comments on commit 4a26601

Please sign in to comment.