๐Ÿ“ฆ EdwonLim / node-less

๐Ÿ“„ unit.js ยท 116 lines
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(function(module) {

    var UnitConversions = require('./unitConversions.js');

    var Unit = function (numerator, denominator, backupUnit) {
        this.numerator = numerator ? numerator.slice(0).sort() : [];
        this.denominator = denominator ? denominator.slice(0).sort() : [];
        this.backupUnit = backupUnit;
    };

    Unit.prototype = {
        type: "Unit",
        clone: function () {
            return new Unit(this.numerator.slice(0), this.denominator.slice(0), this.backupUnit);
        },
        toCSS: function (env) {
            if (this.numerator.length >= 1) {
                return this.numerator[0];
            }
            if (this.denominator.length >= 1) {
                return this.denominator[0];
            }
            if ((!env || !env.strictUnits) && this.backupUnit) {
                return this.backupUnit;
            }
            return "";
        },
        toString: function () {
            var i, returnStr = this.numerator.join("*");
            for (i = 0; i < this.denominator.length; i++) {
                returnStr += "/" + this.denominator[i];
            }
            return returnStr;
        },
        compare: function (other) {
            return this.is(other.toString()) ? 0 : -1;
        },
        is: function (unitString) {
            return this.toString() === unitString;
        },
        isAngle: function () {
            return UnitConversions.angle.hasOwnProperty(this.toCSS());
        },
        isEmpty: function () {
            return this.numerator.length == 0 && this.denominator.length == 0;
        },
        isSingular: function() {
            return this.numerator.length <= 1 && this.denominator.length == 0;
        },
        map: function(callback) {
            var i;
            for (i = 0; i < this.numerator.length; i++) {
                this.numerator[i] = callback(this.numerator[i], false);
            }
            for (i = 0; i < this.denominator.length; i++) {
                this.denominator[i] = callback(this.denominator[i], true);
            }
        },
        usedUnits: function() {
            var group, groupName, result = {};
            for (groupName in UnitConversions) {
                if (UnitConversions.hasOwnProperty(groupName)) {
                    group = UnitConversions[groupName];
                    this.map(function (atomicUnit) {
                        if (group.hasOwnProperty(atomicUnit) && !result[groupName]) {
                            result[groupName] = atomicUnit;
                        }
                        return atomicUnit;
                    });
                }
            }
            return result;
        },
        cancel: function () {
            var counter = {}, atomicUnit, i, backup;
            for (i = 0; i < this.numerator.length; i++) {
                atomicUnit = this.numerator[i];
                if (!backup) {
                    backup = atomicUnit;
                }
                counter[atomicUnit] = (counter[atomicUnit] || 0) + 1;
            }
            for (i = 0; i < this.denominator.length; i++) {
                atomicUnit = this.denominator[i];
                if (!backup) {
                    backup = atomicUnit;
                }
                counter[atomicUnit] = (counter[atomicUnit] || 0) - 1;
            }
            this.numerator = [];
            this.denominator = [];
            for (atomicUnit in counter) {
                if (counter.hasOwnProperty(atomicUnit)) {
                    var count = counter[atomicUnit];
                    if (count > 0) {
                        for (i = 0; i < count; i++) {
                            this.numerator.push(atomicUnit);
                        }
                    } else if (count < 0) {
                        for (i = 0; i < -count; i++) {
                            this.denominator.push(atomicUnit);
                        }
                    }
                }
            }
            if (this.numerator.length === 0 && this.denominator.length === 0 && backup) {
                this.backupUnit = backup;
            }
            this.numerator.sort();
            this.denominator.sort();
        }
    };

    module.exports = Unit;

})(module);