UNPKG

4.14 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3var tslib_1 = require("tslib");
4var dimension_1 = tslib_1.__importDefault(require("../tree/dimension"));
5var anonymous_1 = tslib_1.__importDefault(require("../tree/anonymous"));
6var math_helper_js_1 = tslib_1.__importDefault(require("./math-helper.js"));
7var minMax = function (isMin, args) {
8 args = Array.prototype.slice.call(args);
9 switch (args.length) {
10 case 0: throw { type: 'Argument', message: 'one or more arguments required' };
11 }
12 var i; // key is the unit.toString() for unified Dimension values,
13 var j;
14 var current;
15 var currentUnified;
16 var referenceUnified;
17 var unit;
18 var unitStatic;
19 var unitClone;
20 var // elems only contains original argument values.
21 order = [];
22 var values = {};
23 // value is the index into the order array.
24 for (i = 0; i < args.length; i++) {
25 current = args[i];
26 if (!(current instanceof dimension_1.default)) {
27 if (Array.isArray(args[i].value)) {
28 Array.prototype.push.apply(args, Array.prototype.slice.call(args[i].value));
29 }
30 continue;
31 }
32 currentUnified = current.unit.toString() === '' && unitClone !== undefined ? new dimension_1.default(current.value, unitClone).unify() : current.unify();
33 unit = currentUnified.unit.toString() === '' && unitStatic !== undefined ? unitStatic : currentUnified.unit.toString();
34 unitStatic = unit !== '' && unitStatic === undefined || unit !== '' && order[0].unify().unit.toString() === '' ? unit : unitStatic;
35 unitClone = unit !== '' && unitClone === undefined ? current.unit.toString() : unitClone;
36 j = values[''] !== undefined && unit !== '' && unit === unitStatic ? values[''] : values[unit];
37 if (j === undefined) {
38 if (unitStatic !== undefined && unit !== unitStatic) {
39 throw { type: 'Argument', message: 'incompatible types' };
40 }
41 values[unit] = order.length;
42 order.push(current);
43 continue;
44 }
45 referenceUnified = order[j].unit.toString() === '' && unitClone !== undefined ? new dimension_1.default(order[j].value, unitClone).unify() : order[j].unify();
46 if (isMin && currentUnified.value < referenceUnified.value ||
47 !isMin && currentUnified.value > referenceUnified.value) {
48 order[j] = current;
49 }
50 }
51 if (order.length == 1) {
52 return order[0];
53 }
54 args = order.map(function (a) { return a.toCSS(this.context); }).join(this.context.compress ? ',' : ', ');
55 return new anonymous_1.default((isMin ? 'min' : 'max') + "(" + args + ")");
56};
57exports.default = {
58 min: function () {
59 var args = [];
60 for (var _i = 0; _i < arguments.length; _i++) {
61 args[_i] = arguments[_i];
62 }
63 try {
64 return minMax(true, args);
65 }
66 catch (e) { }
67 },
68 max: function () {
69 var args = [];
70 for (var _i = 0; _i < arguments.length; _i++) {
71 args[_i] = arguments[_i];
72 }
73 try {
74 return minMax(false, args);
75 }
76 catch (e) { }
77 },
78 convert: function (val, unit) {
79 return val.convertTo(unit.value);
80 },
81 pi: function () {
82 return new dimension_1.default(Math.PI);
83 },
84 mod: function (a, b) {
85 return new dimension_1.default(a.value % b.value, a.unit);
86 },
87 pow: function (x, y) {
88 if (typeof x === 'number' && typeof y === 'number') {
89 x = new dimension_1.default(x);
90 y = new dimension_1.default(y);
91 }
92 else if (!(x instanceof dimension_1.default) || !(y instanceof dimension_1.default)) {
93 throw { type: 'Argument', message: 'arguments must be numbers' };
94 }
95 return new dimension_1.default(Math.pow(x.value, y.value), x.unit);
96 },
97 percentage: function (n) {
98 var result = math_helper_js_1.default(function (num) { return num * 100; }, '%', n);
99 return result;
100 }
101};
102//# sourceMappingURL=number.js.map
\No newline at end of file