-
Notifications
You must be signed in to change notification settings - Fork 0
/
another-dimension.js
328 lines (270 loc) · 8.07 KB
/
another-dimension.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
// UMD header
(function (root, factory) {
/* c8 ignore start */
// AMD
if (typeof define === 'function' && define.amd) {
define([],factory);
}
// Node, CommonJS-like
else if (typeof exports === 'object') {
module.exports = factory();
}
// Browser globals
else {
let globalName = "Dimension";
if (document && document.currentScript && document.currentScript.getAttribute) {
globalName = document.currentScript.getAttribute("data-another-dimension-global") || globalName;
}
root[globalName] = factory();
}
/* c8 ignore stop */
}(this, function () {
// start library code
let config = {
defaultUnit: "mm",
defaultOutputUnit: null, // default unit when converted to number
anchorUnit: "mm", // unit to use when direct conversion is not available
pixelDensity: 96, // pixel per inch
viewingDistance: 600, // in mm
aliases: {
//'"': "in", // not configured by default, as this can be confused with arcseconds
"um": "µ",
"µm": "µ",
"°": "deg"
},
toJSON: d => ({value: d.value, unit: d.unit}),
dimensionRegEx: /^\s*(?<value>-?[0-9]*\.?[0-9]+)\s*(?<unit>[^\s\d]+)\s*$/
};
let conversions = {
"mm": {
"km": 1000000,
"m": 1000,
"cm": 10,
"hm": 0.01,
"µ": 0.001,
"in": 25.4,
"thou": 0.0254,
"pt": 25.4 / 72,
"pc": 25.4 / 6,
"px": (v,c) => v * 25.4 / c.pixelDensity,
"deg": (v,c) => Math.tan(v / 2 / 180 * Math.PI ) * 2 * c.viewingDistance,
"arcmin": (v,c) => conversions.mm.deg(v/60, c),
"arcsec": (v,c) => conversions.mm.deg(v/3600, c)
},
"in": {
"m": 1 / 0.0254,
"cm": 1 / 2.54,
"mm": 1 / 25.4,
"hm": 1 / 2540,
"µ": 1 / 25400,
"thou": 0.001,
"pt": 1 / 72,
"pc": 1 / 6,
"px": (v,c) => v / c.pixelDensity,
"deg": (v,c) => conversions.mm.deg(v,c) / 25.4,
"arcmin": (v,c) => conversions.mm.arcmin(v,c) / 25.4,
"arcsec": (v,c) => conversions.mm.arcsec(v,c) / 25.4
},
"px": {
"m": (v,c) => v * c.pixelDensity / 0.0254,
"cm": (v,c) => v * c.pixelDensity / 2.54,
"mm": (v,c) => v * c.pixelDensity / 25.4,
"hm": (v,c) => v * c.pixelDensity / 2540,
"µ": (v,c) => v * c.pixelDensity / 25400,
"in": (v,c) => v * c.pixelDensity,
"thou": (v,c) => v * c.pixelDensity / 1000,
"pt": (v,c) => v * c.pixelDensity / 72,
"pc": (v,c) => v * c.pixelDensity / 6,
"deg": (v,c) => conversions.mm.deg(v,c) * c.pixelDensity / 25.4,
"arcmin": (v,c) => conversions.mm.arcmin(v,c) * c.pixelDensity / 25.4,
"arcsec": (v,c) => conversions.mm.arcsec(v,c) * c.pixelDensity / 25.4
},
"deg": {
"mm": (v,c) => Math.atan2(v, 2 * c.viewingDistance) / Math.PI * 360,
"arcmin": 1/60,
"arcsec": 1/3600
},
"arcmin": {
"mm": (v,c) => Math.atan2(v, 2 * c.viewingDistance) / Math.PI * 360 * 60,
"deg": 60,
"arcsec": 1/60
},
"arcsec": {
"mm": (v,c) => Math.atan2(v, 2 * c.viewingDistance) / Math.PI * 360 * 3600,
"deg": 3600,
"arcmin": 60
}
};
function Dimension(spec, options) {
if (spec instanceof Dimension && (!options || !options.clone)) {
// short-circuit if spec is already a Dimension
return spec;
}
if (!(this instanceof Dimension)) {
//
return new Dimension(spec, options);
}
if (typeof options == "string") {
options = {defaultUnit: options};
}
options = Object.assign({
defaultUnit: config.defaultUnit
}, options);
if (typeof spec == "string") {
spec = Dimension.parseDimensionString(spec);
}
else if (typeof spec == "object" && "value" in spec) {
spec = {
value: spec.value,
unit: spec.unit || options.defaultUnit
}
}
else {
// Number or Object
spec = {
value: +(spec||0).valueOf(),
unit: options.defaultUnit
}
}
this.value = spec.value || 0;
this.unit = Dimension.unAlias(spec.unit);
this.valueOf = function() {
if (config.defaultOutputUnit) {
return this.toNumber(config.defaultOutputUnit);
}
return this.value;
}
this.toFixed = function(digits, targetUnit) {
let val = this.toNumber(targetUnit);
val = val.toFixed(digits);
return val;
}
this.toNumber = function(targetUnit) {
// no unit specified -> return value
if (!targetUnit) return this.value;
let func = Dimension.getConversionFunction(this.unit, targetUnit);
if (func) return func(this.value);
throw new Error("No conversion path from " + this.unit + " to " + targetUnit + " found!");
}
this.toDimension = function(targetUnit, options) {
return Dimension({value: this.toNumber(targetUnit), unit: targetUnit}, options);
}
this.toJSON = function() {
return config.toJSON(this);
}
this.toString = function(targetUnit, digits) {
// if only a number is specified, use it as digits parameter
if (typeof targetUnit == "number" && digits === undefined) {
digits = targetUnit;
targetUnit = undefined;
}
let val;
if (digits === undefined) {
val = this.toNumber(targetUnit);
}
else {
val = this.toFixed(digits, targetUnit);
}
return val + (targetUnit || this.unit);
}
return this;
}
Dimension.configure = function(options) {
/*
Attention: if you configure aliases here, the internal aliases table will be replaced.
Use Dimension.addAlias() to add alias names to the internal table.
*/
Object.assign(config, options);
if (!config.aliases) {
config.aliases = {};
}
}
Dimension.unAlias = function(unit) {
return config.aliases[unit] || unit;
}
Dimension.addAlias = function(unit, alias) {
config.aliases[alias] = unit;
}
Dimension.addConversion = function(fromUnit, toUnit, factorOrFunction) {
if (!conversions[toUnit]) {
conversions[toUnit] = {};
}
conversions[toUnit][fromUnit] = factorOrFunction;
}
Dimension.getConversionFunction = function(fromUnit, toUnit, options) {
let _config = config;
if (options && options.freezeConfig) {
_config = Object.assign({}, config);
}
fromUnit = Dimension.unAlias(fromUnit);
toUnit = Dimension.unAlias(toUnit);
if (fromUnit == toUnit) {
// no conversion - return unity function
return x => x;
}
// direct conversion
let conversionsToUnit = conversions[toUnit];
if (conversionsToUnit) {
let conversion = conversionsToUnit[fromUnit];
if (conversion) {
if (typeof conversion == "function") {
return value => conversion(value, _config)
}
return value => value * conversion;
}
}
// reverse conversion
// can only be done for numeric factors
let conversionsFromUnit = conversions[fromUnit];
if (conversionsFromUnit) {
let conversion = conversionsFromUnit[toUnit];
if (typeof conversion == "number") {
return value => value / conversion;
}
}
// indirect conversion
// check if we do not already use the anchorUnit, to avoid infinite recursion
if (fromUnit != config.anchorUnit && toUnit != config.anchorUnit) {
let conversion = Dimension.getConversionFunction(fromUnit, config.anchorUnit, options);
let conversion2 = Dimension.getConversionFunction(config.anchorUnit, toUnit, options);
if (conversion && conversion2) {
let func = (value => conversion2(conversion(value, _config), _config));
func.indirect = true;
return func;
}
}
return null;
}
Dimension.parseDimensionString = function(str) {
let match = config.dimensionRegEx.exec(str);
if (match) {
return {
value: +match.groups.value,
unit: match.groups.unit
}
}
else if (config.defaultUnit) {
let numberRegEx = /^(?<value>-?[0-9]*\.?[0-9]+)$/;
let match = numberRegEx.exec(str);
if (match) {
return {
value: parseFloat(str),
unit: config.defaultUnit
}
}
}
return null;
}
Dimension.getUnits = function() {
let units = new Set();
for (let unit1 of Object.keys(conversions)) {
units.add(unit1);
for (let unit2 of Object.keys(conversions[unit1])) {
units.add(unit2);
}
}
return Array.from(units);
}
return Dimension;
// UMD end
}));