-
Notifications
You must be signed in to change notification settings - Fork 0
/
resistor.js
executable file
·151 lines (135 loc) · 4.73 KB
/
resistor.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
(function (exports){
/**
* Returns the color for a resistor band value
* @param {string} value resistor band value between -2 to 9
* @return {string} color corresponding to that value
*/
function getcolor(value){
var color = {'-2': "silver",
'-1': "gold",
'0': "black",
'1': "brown",
'2': "red",
'3': "orange",
'4': "yellow",
'5': "green",
'6': "blue",
'7': "violet",
'8': "grey",
'9': "white"};
return color[value];
}
/**
* Returns the color corresponding to the tolerance value
* @param {string} tol tolerance
* @return {string} color corresponding to the tolerance value
*/
function getToleranceColor(tol){
var tolerance_color={
'5':'gray',
'10':'violet',
'25':'blue',
'50':'green',
'100':'brown',
'200':'red',
'500':'gold',
'1000':'silver'
};
var val = (+tol) * 100;
if(val in tolerance_color){
return tolerance_color[val];
}
return '-1';
}
function toStandardForm(value){
var replace_char;
//remove any zeros present in the start of string
value = String(value).replace(/^0*/,'');
//find the replace character if it exist else default to 'R'
//and add R to the last of the value so as to make is consistent
//if char is not present
replace_char = value.match(/[mMkKrR]/);
if(!replace_char){
replace_char = ['R'];
value = value + 'R';
}
//replace '.' is exist with replace_char
if(value.indexOf(".")!== -1){
value = value.replace(replace_char[0], '');
value = value.replace(".",replace_char[0]);
}
return value;
}
/**
* Return the color bands
* @param {String} resistor_value resistor value
* @param {double} tol tolerance value
* @param {interger} mul multiplier([0,3,6])
* @return {Dict object} band color
*/
exports.resistorToColor = function (resistor_value, tol, bands){
console.log("Bands: " + bands);
var digit = [0, 0, 0],
digits = [],
dec = -1,
mul = {
value:'',
pos:''
},
multiplier,
significant_digit;
//defaulting parameters if not present
tol = tol || 5;
mul = mul || null;
//calculating number of digit to consider before taking the digit for multiplier
significant_digit = tol > 2 ? 2 : 3;
//allocating the bands
if (bands == 4) {
significant_digit = 2;
}else{
significant_digit = 3;
}
//check if resistor value exist or not...
if (!resistor_value){
throw ("resistor: invalid parameter");
}
//preprocess the value
resistor_value = toStandardForm(resistor_value);
//setting the multiplier value k=1000*100, m=100000*100, and default=100
//extra 100 is used to get accuracy when doing float division and multiplication
if(/[kK]/.test(resistor_value)){
mul['value'] = 100000;
}else if(/[mM]/.test(resistor_value)){
mul['value'] = 100000000;
}else{
mul['value'] = 100;
}
//remove multiplier character and replace by . if not present.
resistor_value = resistor_value.replace(/[rRkKmM]/,'.');
//convert resistor_value to float by unioperator +
//round of to precision 4 to remove float point bug eg try (1.16 * 100)
//remove trailing zeros
//finally convert to string by ""+
resistor_value = (""+ +(+resistor_value * mul['value']).toFixed(4));
for (var i = 0 ; i< significant_digit ; i++){
digits.push(resistor_value[i]?resistor_value[i]:0);
}
multiplier = resistor_value.length - significant_digit - 2;
if(significant_digit == 2){
return {
'band1':getcolor(digits[0]),
'band2':getcolor(digits[1]),
'band3':getcolor(multiplier),
'band4':'',
'band5':getToleranceColor(tol)
};
}
return {
'band1':getcolor(digits[0]),
'band2':getcolor(digits[1]),
'band3':getcolor(digits[2]),
'band4':getcolor(multiplier),
'band5':getToleranceColor(tol)
};
};
})(typeof exports === "undefined" ?this['resistor'] = {}:exports);