Skip to content

Commit

Permalink
Merge 8550e42 into 7d801c2
Browse files Browse the repository at this point in the history
  • Loading branch information
javiercejudo committed Feb 14, 2016
2 parents 7d801c2 + 8550e42 commit 4305dd5
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 40 deletions.
3 changes: 3 additions & 0 deletions .gitignore
@@ -1,3 +1,6 @@
disc_generator.sh
tmp

# Logs
logs
*.log
Expand Down
14 changes: 6 additions & 8 deletions README.md
Expand Up @@ -35,24 +35,22 @@ fn.raw(d, 10, [2, 3/10]); // d(30)
## Use case: functional hex to dec

```js
var R = require('ramda');
var fn = require('positional-notation');
const R = require('ramda');
const fn = require('positional-notation');

var symbols = {
const symbols = {
'0': '0', '1': '1', '2': '2', '3': '3', '4': '4', '5': '5', '6': '6', '7': '7', '8': '8', '9': '9',
'A': '10', 'B': '11', 'C': '12', 'D': '13', 'E': '14', 'F': '15',
};

var posNotation = fn(Object.keys(symbols).length);
const posNotation = fn(Object.keys(symbols).length);

var hexToDec = R.pipe(
const hexToDec = R.pipe(
R.toUpper,
R.split(''),
R.reverse,
R.map(R.prop(R.__, symbols)),
R.addIndex(R.map)(function(val, index) {
return posNotation([index, val]);
}),
R.addIndex(R.map)(fn.mapper(posNotation)),
R.sum
);

Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -33,7 +33,7 @@
"should": "^8.2.2"
},
"dependencies": {
"core-arbitrary-precision": "^2.0.0",
"core-arbitrary-precision": "^2.1.0",
"floating-adapter": "^1.3.0",
"pow-arbitrary-precision": "^2.1.0",
"ramda": "^0.19.1",
Expand Down
11 changes: 8 additions & 3 deletions src/index.js
Expand Up @@ -13,13 +13,18 @@ var floatingAdapter = require('floating-adapter');

var Decimal = pipe(times, pow)(coreArbitraryPrecision(floatingAdapter));
var toDecimal = toDecimalFactory(Decimal);
var curry3 = curryN(3);

var positionalNotationRaw = curryN(3, function (d, base, posValPair) {
return d(base).pow(d(posValPair[0]))
.times(d(posValPair[1]));
var positionalNotationRaw = curry3(function(d, base, pair) {
return d(base).pow(d(pair[0])).times(d(pair[1]));
});

var positionalNotation = curryN(2, pipe(positionalNotationRaw(toDecimal), Number));

positionalNotation.raw = positionalNotationRaw;

positionalNotation.mapper = curry3(function(f, val, index) {
return f([index, val]);
});

module.exports = positionalNotation;
91 changes: 63 additions & 28 deletions test/spec.js
Expand Up @@ -36,6 +36,7 @@ describe('positional notation', function() {
var d = toDecimalFactory(Decimal);

fn.raw(d, 60, [1, 32]).equals(d(1920)).should.be.exactly(true);
fn.raw(d, 60)([1, 32]).equals(d(1920)).should.be.exactly(true);
fn.raw(d)(60, [1, 32]).equals(d(1920)).should.be.exactly(true);
fn.raw(d)(60)([1, 32]).equals(d(1920)).should.be.exactly(true);

Expand All @@ -49,35 +50,69 @@ describe('positional notation', function() {
});
});

describe('use case', function() {
var symbols = {
'0': '0', '1': '1', '2': '2', '3': '3', '4': '4', '5': '5', '6': '6', '7': '7', '8': '8', '9': '9',
'A': '10', 'B': '11', 'C': '12', 'D': '13', 'E': '14', 'F': '15',
describe('use case: base any to any', function() {
var d = toDecimalFactory(Decimal);

// To avoid large numbers to go into exponential notation
Decimal.Impl.E_POS = 45;

var symbols = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';

var fromBase = function(d, base) {
return R.pipe(
R.split(''),
R.reverse,
R.map(R.indexOf(R.__, symbols)),
R.addIndex(R.map)(fn.mapper(fn.raw(d, base))),
R.reduce(R.invoker(1, 'plus'), d(0))
);
};

var toBase = function(d, base) {
return R.pipe(
R.unfold(function(cur) {
return cur === '0' ? false : [
d(cur).mod(d(base)).toString(),
d(cur).div(d(base)).toString().split('.')[0]
];
}),
R.reverse,
R.map(R.nth(R.__, symbols)),
R.join('')
);
};

var posNotation = fn(Object.keys(symbols).length);

var hexToDec = R.pipe(
R.toUpper,
R.split(''),
R.reverse,
R.map(R.prop(R.__, symbols)),
R.addIndex(R.map)(function(val, index) {
return posNotation([index, val]);
}),
R.sum
);

it('hexadecimal to decimal', function() {
hexToDec('AB').should.be.exactly(171);

// https://github.com/NerdDiffer/all-your-base/blob/ce258b2ca1430dd506b2a9e326f00219e8f8673c/test/convert_spec.js#L73-L79
hexToDec('1556').should.be.exactly(5462);
hexToDec('AE91').should.be.exactly(44689);
hexToDec('512').should.be.exactly(1298);
hexToDec('7DE').should.be.exactly(2014);
hexToDec('20').should.be.exactly(32);
hexToDec('9C4').should.be.exactly(2500);
hexToDec('ff001d').should.be.exactly(16711709);
var convertBasesRaw = R.curryN(4, function(d, oldBase, newBase, n) {
return R.pipe(fromBase(d, oldBase), toBase(d, newBase))(n);
});

var convertBases = convertBasesRaw(d);

it('hexadecimal to binary', function() {
var hexToBin = convertBases(16, 2);

hexToBin('A').should.be.exactly('1010');
hexToBin('1E').should.be.exactly('11110');
});

it('large decimal to base 9', function() {
convertBases(10, 9, '5678364565345634563456346757364563534534645745')
.should.be.exactly('802531310452364303450750087576673257456135727727');
});

it.skip('non-integer decimal to base 9', function() {
convertBases(10, 9, '10.10')
.should.be.exactly('11.08');
});

it('octal to duodecimal', function() {
convertBases(8, 12, '73').should.be.exactly('4B');
});

it('quinary to base 32', function() {
var quiToB32 = convertBases(5, 32);

quiToB32('2312124222213').should.be.exactly('JAVIER');
quiToB32('30333330434').should.be.exactly('TAOCP');
});
});

0 comments on commit 4305dd5

Please sign in to comment.