-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnumber.js
71 lines (55 loc) · 1.43 KB
/
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
'use strict';
import { isNumber } from '@emmetio/stream-reader-utils';
import Token from './token';
import { eatIdent } from './ident';
import { consume } from '../utils';
const DOT = 46; // .
/**
* Consumes number from given string, e.g. `10px`
* @param {StreamReader} stream
* @return {NumberToken}
*/
export default function number(stream) {
if (eatNumericPart(stream)) {
const start = stream.start;
const num = new Token(stream, 'value');
const unit = eatUnitPart(stream) ? new Token(stream, 'unit') : null;
return new Token(stream, 'number', start).add(num).add(unit);
}
}
export function eatNumber(stream) {
if (eatNumericPart(stream)) {
const start = stream.start;
eatUnitPart(stream);
stream.start = start;
return true;
}
return false;
}
function eatNumericPart(stream) {
const start = stream.pos;
stream.eat(isOperator);
if (stream.eatWhile(isNumber)) {
stream.start = start;
const decimalEnd = stream.pos;
if (!(stream.eat(DOT) && stream.eatWhile(isNumber))) {
stream.pos = decimalEnd;
}
return true;
} else if (stream.eat(DOT) && stream.eatWhile(isNumber)) {
stream.start = start;
return true;
}
// TODO eat exponent part
stream.pos = start;
return false;
}
function eatUnitPart(stream) {
return eatIdent(stream) || eatPercent(stream);
}
function eatPercent(stream) {
return consume(stream, 37 /* % */);
}
function isOperator(code) {
return code === 45 /* - */ || code === 43 /* + */;
}