unit.js
4.42 KB
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
/*!
* Stylus - Unit
* Copyright (c) Automattic <developer.wordpress.com>
* MIT Licensed
*/
/**
* Module dependencies.
*/
var Node = require('./node')
, nodes = require('./');
/**
* Unit conversion table.
*/
var FACTOR_TABLE = {
'mm': {val: 1, label: 'mm'},
'cm': {val: 10, label: 'mm'},
'in': {val: 25.4, label: 'mm'},
'pt': {val: 25.4/72, label: 'mm'},
'ms': {val: 1, label: 'ms'},
's': {val: 1000, label: 'ms'},
'Hz': {val: 1, label: 'Hz'},
'kHz': {val: 1000, label: 'Hz'}
};
/**
* Initialize a new `Unit` with the given `val` and unit `type`
* such as "px", "pt", "in", etc.
*
* @param {String} val
* @param {String} type
* @api public
*/
var Unit = module.exports = function Unit(val, type){
Node.call(this);
this.val = val;
this.type = type;
};
/**
* Inherit from `Node.prototype`.
*/
Unit.prototype.__proto__ = Node.prototype;
/**
* Return Boolean based on the unit value.
*
* @return {Boolean}
* @api public
*/
Unit.prototype.toBoolean = function(){
return nodes.Boolean(this.type
? true
: this.val);
};
/**
* Return unit string.
*
* @return {String}
* @api public
*/
Unit.prototype.toString = function(){
return this.val + (this.type || '');
};
/**
* Return a clone of this node.
*
* @return {Node}
* @api public
*/
Unit.prototype.clone = function(){
var clone = new Unit(this.val, this.type);
clone.lineno = this.lineno;
clone.column = this.column;
clone.filename = this.filename;
return clone;
};
/**
* Return a JSON representation of this node.
*
* @return {Object}
* @api public
*/
Unit.prototype.toJSON = function(){
return {
__type: 'Unit',
val: this.val,
type: this.type,
lineno: this.lineno,
column: this.column,
filename: this.filename
};
};
/**
* Operate on `right` with the given `op`.
*
* @param {String} op
* @param {Node} right
* @return {Node}
* @api public
*/
Unit.prototype.operate = function(op, right){
var type = this.type || right.first.type;
// swap color
if ('rgba' == right.nodeName || 'hsla' == right.nodeName) {
return right.operate(op, this);
}
// operate
if (this.shouldCoerce(op)) {
right = right.first;
// percentages
if ('%' != this.type && ('-' == op || '+' == op) && '%' == right.type) {
right = new Unit(this.val * (right.val / 100), '%');
} else {
right = this.coerce(right);
}
switch (op) {
case '-':
return new Unit(this.val - right.val, type);
case '+':
// keyframes interpolation
type = type || (right.type == '%' && right.type);
return new Unit(this.val + right.val, type);
case '/':
return new Unit(this.val / right.val, type);
case '*':
return new Unit(this.val * right.val, type);
case '%':
return new Unit(this.val % right.val, type);
case '**':
return new Unit(Math.pow(this.val, right.val), type);
case '..':
case '...':
var start = this.val
, end = right.val
, expr = new nodes.Expression
, inclusive = '..' == op;
if (start < end) {
do {
expr.push(new nodes.Unit(start));
} while (inclusive ? ++start <= end : ++start < end);
} else {
do {
expr.push(new nodes.Unit(start));
} while (inclusive ? --start >= end : --start > end);
}
return expr;
}
}
return Node.prototype.operate.call(this, op, right);
};
/**
* Coerce `other` unit to the same type as `this` unit.
*
* Supports:
*
* mm -> cm | in
* cm -> mm | in
* in -> mm | cm
*
* ms -> s
* s -> ms
*
* Hz -> kHz
* kHz -> Hz
*
* @param {Unit} other
* @return {Unit}
* @api public
*/
Unit.prototype.coerce = function(other){
if ('unit' == other.nodeName) {
var a = this
, b = other
, factorA = FACTOR_TABLE[a.type]
, factorB = FACTOR_TABLE[b.type];
if (factorA && factorB && (factorA.label == factorB.label)) {
var bVal = b.val * (factorB.val / factorA.val);
return new nodes.Unit(bVal, a.type);
} else {
return new nodes.Unit(b.val, a.type);
}
} else if ('string' == other.nodeName) {
// keyframes interpolation
if ('%' == other.val) return new nodes.Unit(0, '%');
var val = parseFloat(other.val);
if (isNaN(val)) Node.prototype.coerce.call(this, other);
return new nodes.Unit(val);
} else {
return Node.prototype.coerce.call(this, other);
}
};