-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
49 lines (41 loc) · 1.35 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
const capitalize = require("capitalize");
module.exports = unitTypes => {
const conversionParser = require("./lib/conversionParser")(unitTypes);
class MezurUnit {
constructor(units, name) {
this.units = units;
this.name = name;
unitTypes.forEach(klassTo => {
this.constructor.prototype[
`in${capitalize(klassTo.name)}s`
] = function() {
return this._convertTo(klassTo);
};
this.constructor.prototype[
`to${capitalize(klassTo.name)}s`
] = this.constructor.prototype[`in${capitalize(klassTo.name)}s`];
this.constructor.prototype[`as${capitalize(klassTo.name)}s`] = this.constructor.prototype[`in${capitalize(klassTo.name)}s`];
});
}
_inBaseUnit() {
const mezuringUnit = unitTypes.filter(x => this.name === x.name)[0];
return this.units * conversionParser(mezuringUnit.value);
}
_convertTo(otherUnit) {
return (
this._inBaseUnit() /
conversionParser(
unitTypes.filter(x => otherUnit.name === x.name)[0].value
)
);
}
}
const mezuringFunctions = {};
unitTypes.forEach(mezuringUnit => {
const functionName = mezuringUnit.name + "s";
mezuringFunctions[functionName] = function(units) {
return new MezurUnit(units, mezuringUnit.name);
};
});
return mezuringFunctions;
};