This repository has been archived by the owner on Jul 12, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnamed-number.js
185 lines (138 loc) · 4.3 KB
/
named-number.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
"use strict";
var L = require("hsimp-library");
var namedNumberDictionary, orderedNamedNumberDictionary;
var getDictionaryName = L.prop(0);
var getDictionaryValue = L.prop(1);
var exponentSplit = function (value) {
var exponential = value.toExponential(),
match = exponential.match(/^([\.\d]+)e([\+\-]\d+)/),
significand = value,
exponent = 0;
if (match && match.length >= 3) {
significand = +match[1];
exponent = +match[2];
}
return {
significand: significand,
exponent: exponent
};
};
var zeroPad = function (amount) {
return amount > 0 ? new Array(amount + 1).join("0") : "";
};
var addCommas = function (string) {
var match = /(\d+)(\d{3})/;
while (match.test(string)) {
string = string.replace(match, "$1" + "," + "$2");
}
return string;
};
var format = function (significand, exponent) {
var split = significand.toString().split("."),
string = split[0];
if (split.length > 1) {
string += split[1].substr(0, exponent);
}
string += zeroPad(exponent - string.length + 1);
return addCommas(string);
};
var decimalShift = function (significand, exponent) {
var digits = significand.toString().replace(".", "").replace(/[09]{4,}\d$/, "");
return "0." + zeroPad(Math.abs(exponent) - 1) + digits;
};
var parseString = function (string) {
var matches = string.match(/^[\d\.]+\s+([\sa-zA-Z]+)\s*$/),
num = parseFloat(string.replace(/,/g, "")),
exponent = 0,
split,
cur,
value;
if (!matches && !num) {
return string;
}
if (matches && matches.length === 2) {
split = matches[1].split(/\s+/);
L.forEach(function (value) {
cur = value.toLowerCase();
if (namedNumberDictionary.hasOwnProperty(cur)) {
exponent += namedNumberDictionary[cur];
}
}, split);
// Avoid floating point oddities
value = (num * Math.pow(10, exponent)).toPrecision(num.toString().length);
value = +value;
} else {
value = num;
}
return value;
};
var getName = function (significand, exponent) {
var join = [],
highestNumber = orderedNamedNumberDictionary[0],
exponentCheck = L.forEach(function (number) {
if (exponent < getDictionaryValue(number)) {
return false;
}
highestNumber = number;
});
if (exponent < 0) {
return decimalShift(significand, exponent);
}
while (exponent >= getDictionaryValue(highestNumber)) {
exponentCheck(orderedNamedNumberDictionary);
exponent -= getDictionaryValue(highestNumber);
join.unshift(getDictionaryName(highestNumber));
}
significand = Math.round(significand * Math.pow(10, exponent));
var split = exponentSplit(significand),
sig = split.significand,
exp = split.exponent;
join.unshift(format(sig.toString(), exp));
return join.join(" ");
};
var namedNumber = function (value) {
var self = {},
name, split, significand, exponent;
if (!namedNumberDictionary) {
throw new Error("Named Number dictionary not set");
}
if (L.isString(value)) {
value = parseString(value);
}
if (!L.isNumber(value) || isNaN(value)) {
throw {
type: "invalidNumber",
value: value
};
}
split = exponentSplit(value);
significand = split.significand;
exponent = split.exponent;
self.getSignificand = function () {
return significand;
};
self.getExponent = function () {
return exponent;
};
self.getName = function () {
name = name || getName(significand, exponent);
return name;
};
self.getFormatted = function () {
if (exponent < 0) {
return decimalShift(significand, exponent);
}
return format(significand, exponent);
};
self.toString = self.getFormatted;
self.valueOf = function () {
return value;
};
return self;
};
namedNumber.setDictionary = function (numberDictionary) {
namedNumberDictionary = numberDictionary;
orderedNamedNumberDictionary = L.sortBy(1, L.toPairs(namedNumberDictionary));
return namedNumber;
};
module.exports = namedNumber;