Skip to content

Commit 99c998f

Browse files
committed
feat: 🎸 add limited support for multiplication
1 parent 1375a13 commit 99c998f

File tree

9 files changed

+111
-4
lines changed

9 files changed

+111
-4
lines changed

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,3 +362,16 @@ eighteenInches.unit === inch;
362362
```
363363

364364
`subtract` does not support operating on more than 2 measurements at a time.
365+
366+
#### `.multiply(measurement, ...numbers)`
367+
368+
`multiply` will take the a measurement and multiply it with one or more numbers. The order of arguments is not important. Only one measurement can be supplied.
369+
370+
```js
371+
const twelveInches = system.multiply(
372+
m`4 inches`,
373+
3
374+
);
375+
twelveInches.value === 12;
376+
twelveInches.unit === inch;
377+
```

src/UnitSystem/UnitSystem.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const { add, subtract } = require('../math');
1+
const { add, subtract, multiply } = require('../math');
22
const Measurement = require('../Measurement');
33
const Unit = require('../Unit');
44
const Aliases = require('./Aliases');
@@ -102,6 +102,9 @@ class UnitSystem {
102102
const unit = measurements[0].unit;
103103
return subtract(...measurements.map(m => this.convert(m, unit)));
104104
}
105+
multiply(...values) {
106+
return multiply(...values);
107+
}
105108
}
106109

107110
module.exports = UnitSystem;

src/UnitSystem/UnitSystem.test.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,4 +311,15 @@ describe(UnitSystem, () => {
311311
).toEqual(new Measurement(1, foot));
312312
});
313313
});
314+
315+
describe('#multiply', () => {
316+
it('multiplies', () => {
317+
const inch = new Unit('inch');
318+
const system = new UnitSystem([[inch]]);
319+
320+
expect(system.multiply(new Measurement(3, inch), 2)).toEqual(
321+
new Measurement(6, inch)
322+
);
323+
});
324+
});
314325
});

src/createUnitSystem.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,11 @@ function createUnitSystem(units) {
1818
const convert = bound(system, 'convert');
1919
const add = bound(system, 'add');
2020
const subtract = bound(system, 'subtract');
21+
const multiply = bound(system, 'multiply');
2122

2223
const m = createMeasurement(system);
2324

24-
return { m, createUnit, convert, add, subtract, system };
25+
return { m, createUnit, convert, add, subtract, multiply, system };
2526
}
2627

2728
module.exports = createUnitSystem;

src/createUnitSystem.test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ describe(createUnitSystem, () => {
1111
convert: jasmine.any(Function),
1212
add: jasmine.any(Function),
1313
subtract: jasmine.any(Function),
14+
multiply: jasmine.any(Function),
1415
});
1516
});
1617

src/index.test.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,14 @@ describe('createUnitSystem', () => {
1414
const { createUnitSystem, conversion } = index;
1515

1616
test('creates a functional unit system', () => {
17-
const { createUnit, m, convert, add, subtract } = createUnitSystem();
17+
const {
18+
createUnit,
19+
m,
20+
convert,
21+
add,
22+
subtract,
23+
multiply,
24+
} = createUnitSystem();
1825

1926
const kilometer = createUnit('kilometer', {
2027
alias: 'km',
@@ -56,7 +63,7 @@ describe('createUnitSystem', () => {
5663
subtract(m`4 feet`, m`12 inches`),
5764
m`2 inches`,
5865
m(15.24, centimeter),
59-
m`10 inches`,
66+
multiply(m`5 inches`, 2),
6067
m`3 feet`
6168
)
6269
).toEqual(m`7.5 feet`);

src/math/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
const add = require('./add');
22
const subtract = require('./subtract');
3+
const multiply = require('./multiply');
34

45
exports.add = add;
56
exports.subtract = subtract;
7+
exports.multiply = multiply;

src/math/multiply.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
const Measurement = require('../Measurement');
2+
3+
function _multiply(...numbers) {
4+
return numbers.reduce((total, number) => total * number, 1);
5+
}
6+
7+
function multiply(...values) {
8+
const measurements = values.filter(value => value instanceof Measurement);
9+
if (measurements.length === 0) {
10+
throw new TypeError('Expected a Measurement but got none');
11+
}
12+
if (measurements.length > 1) {
13+
throw new TypeError('Cannot multiply more than one Measurement');
14+
}
15+
const [measurement] = measurements;
16+
17+
const numbers = values.filter(value => !(value instanceof Measurement));
18+
if (numbers.length === 0) {
19+
throw new TypeError('Cannot multiply a Measurement with nothing');
20+
}
21+
22+
return new Measurement(
23+
_multiply(measurement.value, ...numbers),
24+
measurement.unit
25+
);
26+
}
27+
28+
module.exports = multiply;

src/math/multiply.test.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
const Measurement = require('../Measurement');
2+
const Unit = require('../Unit');
3+
const multiply = require('./multiply');
4+
5+
describe(multiply, () => {
6+
it('throws if a Measurement is not supplied', () => {
7+
expect(() => multiply(2, 3)).toThrowError(
8+
new TypeError('Expected a Measurement but got none')
9+
);
10+
});
11+
12+
it('throws if more than one Measurement is supplied', () => {
13+
const foot = new Unit('foot');
14+
expect(() =>
15+
multiply(new Measurement(2, foot), new Measurement(3, foot))
16+
).toThrowError(new TypeError('Cannot multiply more than one Measurement'));
17+
});
18+
19+
it('throws if just a single value is provided', () => {
20+
const foot = new Unit('foot');
21+
expect(() => multiply(new Measurement(2, foot))).toThrowError(
22+
new TypeError('Cannot multiply a Measurement with nothing')
23+
);
24+
});
25+
26+
it('multiplies a Measurement and a number', () => {
27+
const foot = new Unit('foot');
28+
expect(multiply(new Measurement(2, foot), 3)).toEqual(
29+
new Measurement(6, foot)
30+
);
31+
expect(multiply(5, new Measurement(4, foot))).toEqual(
32+
new Measurement(20, foot)
33+
);
34+
});
35+
36+
it('multiplies a Measurement and multiple numbers', () => {
37+
const foot = new Unit('foot');
38+
const values = [4, 5, new Measurement(2, foot), 3];
39+
expect(multiply(...values)).toEqual(new Measurement(120, foot));
40+
});
41+
});

0 commit comments

Comments
 (0)