Skip to content

Commit

Permalink
feat(mapper): add convenience mapper method
Browse files Browse the repository at this point in the history
  • Loading branch information
javiercejudo committed Feb 13, 2016
1 parent 7d801c2 commit 6b9eb90
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 38 deletions.
14 changes: 6 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
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
11 changes: 8 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
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;
79 changes: 52 additions & 27 deletions test/spec.js
Original file line number Diff line number Diff line change
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,59 @@ 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);
Decimal.setPrecision(10000);

var symbols = R.pipe(
R.split(''),
R.toPairs,
R.fromPairs
)('0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz');

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

var posNotation = fn(Object.keys(symbols).length);
var toBase = function(d, base) {
return R.pipe(
function(decimal) {
return R.unfold(function(cur) {
return cur > 0 ? [d(cur).mod(d(base)).toString(), d(cur).div(d(base)).toString().split('.')[0]] : false;
}, decimal);
},
R.reverse,
R.map(R.prop(R.__, symbols)),
R.join('')
);
};

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');
});

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 6b9eb90

Please sign in to comment.