-
Notifications
You must be signed in to change notification settings - Fork 0
/
measure.js
191 lines (163 loc) · 4.42 KB
/
measure.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
var ENV = 'testing';
function Measurement(params) {
}
Measurement.prototype.format = function (style) {
return new Measurement(style);
};
Measurement.prototype.config = function (tree) {
tree.forEach(function(leaf){
if(leaf.multiplier === 0){
_err('config error', 'multiplier cannot equal 0', JSON.stringify(leaf));
}
});
this.conversions = this.conversions.concat(tree);
return this;
};
Measurement.prototype.to = function(newUnit){
var applied = false;
var val = this.value;
var current = this.current;
this.conversions.forEach(function(unit){
/* handle all types to convert */
if(matching_list(newUnit, unit)){
val = (val / current.multiplier) * unit.multiplier;
applied = unit;
}
});
if(applied){
/* stores the state fo the transition */
this.current = applied;
this.value = val;
}
else{
console.log(this);
/* if trying to convert to the same type just return measurement */
if(newUnit === current){return this;}
_err('input error', 'The unit you are trying to convert does not exsit in the conversion list', newUnit);
}
return this;
};
function find_conversion(measurement, toLook){
var index = -1;
measurement.conversions.forEach(function(unit, i){
if(matching_list(toLook, unit)){
index = i;
}
});
return index;
}
function matching_list(newUnit, unit){
if (newUnit == unit.unit || newUnit == unit.name || newUnit == unit.plural){
return true;
}
return false;
}
Measurement.prototype.add = function(measurement){
if (typeof(measurement) === 'number'){
this.value += measurement;
return this;
}
var to_add = parse_params(measurement);
var index = find_conversion(this, to_add.current.unit);
console.log('index is: ', index);
if(index != -1){
//console.log(this.conversions[index]);
this.value = ((this.value / this.current.multiplier) + ((to_add.value) / this.conversions[index].multiplier)) * this.current.multiplier;
}
else{
_err('input error', 'The unit you are trying to add does not exsit in the conversion list', to_add.current.unit);
}
return this;
};
Measurement.prototype.sub = function(measurement){
if (typeof(measurement) === 'number'){
this.value -= measurement;
return this;
}
var to_add = parse_params(measurement);
var index = find_conversion(this, to_add.current.unit);
console.log('index is: ', index);
if(index != -1){
//console.log(this.conversions[index]);
this.value = ((this.value / this.current.multiplier) - ((to_add.value) / this.conversions[index].multiplier)) * this.current.multiplier;
}
else{
_err('input error', 'The unit you are trying to add does not exsit in the conversion list', to_add.current.unit);
}
return this;
};
Measurement.prototype.times = function(measurement){
var to_multiply;
if(measurement.value){
to_multiply = measurement;
}
else{
to_multiply = parse_params(measurement);
}
if(matching_list(to_multiply, this.current) || matching_list(this.current, to_multiply)){
console.log('same');
}
else{
console.log('different');
}
};
function parse_params(params){
var instance = new Measurement();
instance.current = {};
instance.conversions = [];
if(params){
var value = params.match(/[-+]?([0-9]*\.[0-9]+|[0-9]+)/);
var unit = params.match(/[a-z]+[^0-9]/);
if(!unit){
_err('parsing error', 'unable to parse values given', params);
}
else{
if(!value){
/* defualts to 1 if no value is provided */
value = 1;
}
instance.value = Number(value[0]);
instance.current.unit = unit[0];
instance.current.multiplier = 1;
instance.state = 1;
/* if it doesn't exist in conversions add it to the list */
if(!contains(instance)){
//_err('conflicting types warning', 'measure js is about to add this type to the conversions list', params);
instance.conversions = instance.conversions.concat([instance.current]);
}
}
}
else{
_err('input error', 'no value was provided', params);
}
return instance;
}
function contains(unit){
var cur = unit.current;
return unit.conversions.forEach(function(c){
if(c === cur){return true;}
});
}
module.exports = function (params) {
var current_template = {
value: 0,
conversions : [{
unit: 'g',
multiplier: 0.001,
name: 'gram',
plural: 'grams'
}],
state: 1,
current: {
unit: 'g',
multiplier: 0.001,
name: 'gram',
plural: 'grams'
}
};
return parse_params(params);
};
function _err(errTyp, err, value){
console.log(errTyp.toUpperCase() + ": " + err + ' ' + '\'' + value + '\'');
}
return exports;