Skip to content

Commit

Permalink
Merge pull request #3 from HarvestProfit/2-source-maps
Browse files Browse the repository at this point in the history
Add source maps
  • Loading branch information
jarydkrish authored Mar 6, 2018
2 parents 4696b43 + f637f65 commit 3347d82
Show file tree
Hide file tree
Showing 7 changed files with 3,514 additions and 85 deletions.
21 changes: 20 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,26 @@

## Installation

TODO
To add this package to your project
```bash
yarn add @harvest-profit/units
```

To use, try the following:
```js
import { UnitsHelper } from '@harvest-profit/units'

const isCompatibleUnit = UnitsHelper.isCompatibleUnit('lbs', 'tons');
console.log(isCompatibleUnit); // true
```

You can also import our flavor of `math.js` if you need it:
```js
import { math } from '@harvest-profit/units'

const total = math.add(1, 2)
console.log(total); // 3
```

## Development
To deploy a new version to NPM, bump the version number, commit/merge to `master`, and run the following:
Expand Down
23 changes: 19 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
{
"name": "harvest-profit-units",
"version": "1.0.0-alpha.3",
"name": "@harvest-profit/units",
"version": "1.0.0",
"description": "Units helper for Harvest Profit javascript applications",
"main": "dist/index.js",
"repository": "https://github.com/HarvestProfit/harvest-profit-units",
"author": "Jaryd Krishnan <[email protected]>",
"license": "MIT",
"private": false,
"scripts": {
"build": "babel src -d dist",
"build": "rm -rf ./dist && webpack --config webpack.config.js --progress --profile -p",
"clean": "rm -rf ./dist",
"report-coverage": "coveralls < ./coverage/lcov.info",
"test": "jest test/ --coverage"
Expand All @@ -26,15 +26,30 @@
"^.+\\.jsx?$": "babel-jest"
}
},
"peerDependencies": {
"lodash": "^4.17.4",
"mathjs": "^3.17.0"
},
"dependencies": {
"lodash": "^4.17.4",
"mathjs": "^3.17.0"
},
"devDependencies": {
"babel-cli": "^6.26.0",
"babel-core": "^6.26.0",
"babel-eslint": "^8.2.2",
"babel-loader": "^7.1.3",
"babel-preset-airbnb": "^2.4.0",
"babel-preset-stage-2": "^6.24.1",
"coveralls": "^3.0.0",
"jest": "^21.2.1"
"eslint": "^4.18.2",
"eslint-config-airbnb": "^16.1.0",
"eslint-plugin-import": "^2.9.0",
"eslint-plugin-jsx-a11y": "^6.0.3",
"eslint-plugin-react": "^7.7.0",
"jest": "^21.2.1",
"uglifyjs-webpack-plugin": "^1.2.2",
"webpack": "^4.1.0",
"webpack-cli": "^2.0.10"
}
}
49 changes: 47 additions & 2 deletions src/UnitsHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ export const availableLiquidUnits = [
'floz',
'liters',
'milliliters',
'pints',
'quarts',
];

export default class UnitsHelper {
Expand Down Expand Up @@ -88,21 +90,64 @@ export default class UnitsHelper {
lbsPerGallon,
);
const finalCalc = finalUnit.to(productUnit).toNumber();
totalPerUnit = product.price * finalCalc;
if (finalCalc > 0) {
totalPerUnit = product.price * finalCalc;
}
} else {
// If all else fails, try to convert
const lineItemInProductUnits = Math
.unit(1, lineItemUnit)
.to(productUnit)
.toNumber();
totalPerUnit = product.price * lineItemInProductUnits;
if (lineItemInProductUnits > 0) {
totalPerUnit = product.price * lineItemInProductUnits;
}
}
} catch (error) {
totalPerUnit = product.price;
}
return totalPerUnit;
}

static toProductUnits = (item, product) => {
const productUnit = UnitsHelper.parseUnit(product.units);
const lineItemUnit = UnitsHelper.parseUnit(item.units);

const isProductLiquid = product.liquid && product.density > 0;
let totalAmount = 0;
try {
if (productUnit === 'custom') {
const amount = lineItemUnit === 'custom' ? product.multiplier : 1;
const unit = lineItemUnit === 'custom' ? 'seed' : lineItemUnit;
const lineItemInSeedUnits = Math.unit(amount, unit).to('seeds').toNumber();
const lineIteminCustomUnits = lineItemInSeedUnits / product.multiplier;
totalAmount = lineIteminCustomUnits * item.amount;
} else if (isProductLiquid && !UnitsHelper.isCompatibleUnit(lineItemUnit, productUnit)) {
const lbsPerGallon = Math.unit(`${product.density} lbs/gal`);
const finalUnit = Math.multiply(
Math.unit(1, lineItemUnit),
lbsPerGallon,
);
const finalCalc = finalUnit.to(productUnit).toNumber();
if (finalCalc > 0) {
totalAmount = finalCalc * item.amount;
}
} else {
// If all else fails, try to convert
const lineItemInProductUnits = Math
.unit(1, lineItemUnit)
.to(productUnit)
.toNumber();
if (lineItemInProductUnits > 0) {
totalAmount = lineItemInProductUnits * item.amount;
}
}
} catch (error) {
totalAmount = item.amount;
}
return totalAmount;
}

static parseUnit = unit =>
unit
.replace('per ', '')
Expand Down
3 changes: 2 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import math from './Math';
import UnitsHelper from './UnitsHelper';

export default UnitsHelper;
export default { math, UnitsHelper };
29 changes: 29 additions & 0 deletions test/UnitsHelper.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ const lineItemInGallons = {
units: 'gallons',
};

const lineItemInSeeds = {
applied_acres: 100,
amount: 300,
units: 'seeds',
};

/**
* Here are the actual tests
*/
Expand Down Expand Up @@ -161,6 +167,29 @@ describe('UnitsHelper', () => {
});
});

describe('line item amount per acre in product units', () => {
it('should convert the line item amount per acre into product units', () => {
const product = productInTons;
const lineItem = lineItemInLbs;
const amountInProductUnits = UnitsHelper.toProductUnits(lineItem, product);
expect(amountInProductUnits).toBeCloseTo(0.005, 3);
});

it('should convert the line item amount per acre into product units given a liquid product', () => {
const product = liquidProductInTons;
const lineItem = lineItemInGallons;
const amountInProductUnits = UnitsHelper.toProductUnits(lineItem, product);
expect(amountInProductUnits).toBeCloseTo(0.5, 1);
});

it('should convert the line item amount per acre into product units given a custom product', () => {
const product = productInCustomUnits;
const lineItem = lineItemInSeeds;
const amountInProductUnits = UnitsHelper.toProductUnits(lineItem, product);
expect(amountInProductUnits).toBeCloseTo(0.3, 1);
});
});

describe('per acre costs', () => {
it('should calculate per acre costs from a given product, input, and acres', () => {
const product = productInTons;
Expand Down
48 changes: 48 additions & 0 deletions webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
const path = require('path');
const UglifyJSPlugin = require('uglifyjs-webpack-plugin');

/**
* This is our webpack config for the final built product.
* It pumps out a dist/index.js file that is ready for web-based use.
*/
module.exports = {
entry: './src/index.js',
output: {
filename: 'index.js',
library: 'HarvestProfitUnits',
libraryTarget: 'umd',
umdNamedDefine: true,
path: path.resolve(__dirname, 'dist'),
},
/* We use uglifyjs to compile/minify our bundle from this project */
plugins: [
new UglifyJSPlugin({
parallel: true,
sourceMap: true,
}),
],
/* We use both math.js and lodash, but have them externally */
externals: {
lodash: 'lodash',
mathjs: 'mathjs',
},
devtool: 'source-map',
/* We use babel-loader to replicate the .babelrc file in webpack */
module: {
rules: [
{
test: /\.js$/,
exclude: /(node_modules|bower_components)/,
use: {
loader: 'babel-loader',
options: {
presets: [
'airbnb',
'stage-2',
],
},
},
},
],
},
};
Loading

0 comments on commit 3347d82

Please sign in to comment.