-
Notifications
You must be signed in to change notification settings - Fork 9
/
numbers.js
59 lines (51 loc) · 1.5 KB
/
numbers.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
"use strict";
var Parser = require("./parser");
var array_ = Array.prototype;
module.exports = parseNumber;
function parseNumber(callback, options) {
return parseDigits(function (digits) {
if (digits) {
return parseConvert(callback, digits.join(""), options);
} else {
return callback();
}
});
}
var digits = "0123456789";
var parseDigit = function (callback) {
return function (character) {
if (character !== "" && digits.indexOf(character) !== -1) {
return callback(character);
} else {
return callback()(character);
}
};
};
function parseConvert(callback, number, options) {
return Parser.countPrimes(function (primes) {
return callback(convert(number, primes, options));
});
}
function convert(string, alt, options) {
var error;
var radix;
var duodecimal = options.duodecimal;
var font = options.font;
var makeColumn = font.makeColumn;
if (alt == 0) {
radix = duodecimal ? 12 : 10;
} else {
radix = duodecimal ? 10 : 12;
error = alt > 1;
}
var number = parseInt(string, 10);
var string = number.toString(radix).split("");
return string.map(function (character) {
var column = makeColumn(""+parseInt(character, 12));
if (error) {
column.addError("Numbers can only be parsed in either decimal or dudecimal.");
}
return column;
});
}
var parseDigits = Parser.makeParseSome(parseDigit);