From 15ddd0beabb2342d9d68ff4625139ab7fb456ace Mon Sep 17 00:00:00 2001 From: Jules Laplace Date: Wed, 8 Mar 2017 16:28:00 +0100 Subject: readme and demo page --- README.md | 5 ++++ index.html | 91 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ index.js | 15 +++++++++-- 3 files changed, 109 insertions(+), 2 deletions(-) create mode 100644 README.md create mode 100644 index.html diff --git a/README.md b/README.md new file mode 100644 index 0000000..7010213 --- /dev/null +++ b/README.md @@ -0,0 +1,5 @@ +basic +===== + +This library implements numeric base conversions (à la `Number.toString(b)`). It allows for negative bases, such as negabinary and negaternary. It expresses the fact that negativity is basic. Really basic. + diff --git a/index.html b/index.html new file mode 100644 index 0000000..7371a95 --- /dev/null +++ b/index.html @@ -0,0 +1,91 @@ + + + + +
+ +
+ +
+ + + +
+ + + + + diff --git a/index.js b/index.js index 71d93d9..423295b 100644 --- a/index.js +++ b/index.js @@ -1,5 +1,9 @@ -module.exports = basic = {} +var basic = {} + +if (! ('window' in this)) { + module.exports = basic +} // Handle bases up to base 36 basic.abedecary = "0123456789abcdefghijklmnopqrstuvwxyz" @@ -38,10 +42,17 @@ basic.toNumber = function (s, b) { if (! basic.validate(s, b)) { return NaN } - var n = 0 + var n = 0, is_negative = false + if (s.indexOf("-") == 0) { + is_negative = true + s = s.substr(1) + } for (var i = 0, len = s.length; i < len; i++) { n += basic.abedecary.indexOf(s[len-1-i]) * Math.pow(b, i) } + if (is_negative) { + n *= -1 + } return n } -- cgit v1.2.3-70-g09d2