Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
javiercejudo committed Feb 28, 2016
1 parent 2bd308a commit 7078bef
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 30 deletions.
2 changes: 2 additions & 0 deletions src/R.js
Expand Up @@ -4,6 +4,7 @@

module.exports = Object.freeze({
__: require('ramda/src/__'),
adjust: require('ramda/src/adjust'),
curryN: require('ramda/src/curryN'),
identical: require('ramda/src/identical'),
identity: require('ramda/src/identity'),
Expand All @@ -15,5 +16,6 @@ module.exports = Object.freeze({
nth: require('ramda/src/nth'),
pipe: require('ramda/src/pipe'),
reverse: require('ramda/src/reverse'),
split: require('ramda/src/split'),
unfold: require('ramda/src/unfold')
});
22 changes: 22 additions & 0 deletions src/U.js
@@ -0,0 +1,22 @@
/*jshint node:true */

'use strict';

var R = require('./R');

module.exports = Object.freeze({
indexOfSymbol: R.memoize(function(symbols) {
return R.memoize(R.indexOf(R.__, symbols));
}),

joinWithoutSep: R.join(''),
joinWithDot: R.join('.'),

nthSymbol: R.memoize(function(symbols) {
return R.memoize(R.nth(R.__, symbols));
}),

splitByDot: R.split('.'),

toString: R.invoker(0, 'toString')
});
48 changes: 18 additions & 30 deletions src/index.js
Expand Up @@ -6,48 +6,36 @@ var posNotation = require('positional-notation');
var toBigFactory = require('to-decimal-arbitrary-precision');

var R = require('./R');
var U = require('./U');
var translate = require('./translate');
// var fracUnfolder = require('./fracUnfolder');

var defaultB = toBigFactory(require('./Big'));
var defaultBig = toBigFactory(require('./Big'));
var defaultSymbols = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
var joinWithoutSep = R.join('');
var toString = R.invoker(0, 'toString');

var indexOfSymbol = R.memoize(function(symbols) {
return R.memoize(R.indexOf(R.__, symbols));
});

var nthSymbol = R.memoize(function(symbols) {
return R.memoize(R.nth(R.__, symbols));
});

var preprocess = R.memoize(function(symbols) {
return R.identical(symbols, defaultSymbols) ?
R.identity :
R.pipe(
toString,
R.map(indexOfSymbol(symbols)),
joinWithoutSep
);
});

var fromDecimalRaw = R.curryN(4, function(b, symbols, base, n) {
var fromDecimalRaw = R.curryN(4, function(big, symbols, base, n) {
return R.pipe(
preprocess(symbols),
R.unfold(posNotation.unfolder(b, base)),
R.map(nthSymbol(symbols)),
R.reverse,
joinWithoutSep
U.toString,
translate(defaultSymbols, symbols),
U.splitByDot,
R.adjust(R.unfold(posNotation.unfolder(big, base)), 0),
// R.adjust(fracUnfolder(posNotation.unfolder(big, base)), 1),
R.adjust(R.reverse, 0),
R.map(R.map(U.nthSymbol(symbols))),
R.adjust(U.joinWithoutSep, 0),
U.joinWithDot
)(n);
});


var fromDecimal = fromDecimalRaw(defaultB, defaultSymbols);
var fromDecimal = fromDecimalRaw(defaultBig, defaultSymbols);

fromDecimal.big = fromDecimalRaw(R.__, defaultSymbols);
fromDecimal.symbols = fromDecimalRaw(defaultB);
fromDecimal.symbols = fromDecimalRaw(defaultBig);
fromDecimal.raw = fromDecimalRaw;

fromDecimal.defaultSymbols = defaultSymbols;
fromDecimal.defaultB = defaultB;
fromDecimal.defaultBig = defaultBig;
fromDecimal.__ = R.__;

module.exports = fromDecimal;
15 changes: 15 additions & 0 deletions src/translate.js
@@ -0,0 +1,15 @@
/*jshint node:true */

'use strict';

var R = require('./R');
var U = require('./U');

module.exports = R.memoize(function(defaultSymbols, symbols) {
return R.identical(symbols, defaultSymbols) ?
R.identity :
R.pipe(
R.map(U.indexOfSymbol(symbols)),
U.joinWithoutSep
);
});

0 comments on commit 7078bef

Please sign in to comment.