Skip to content

Commit

Permalink
Major core API changes; code style changes
Browse files Browse the repository at this point in the history
Changes to the core API
- Base() doesn't take arrays anymore
- Base.convertToMultiple() can be used to efficiently convert a number from one base into multiple others
- Base.suggest() now always returns grouped results; use the new Base.suggestList() for an array
- Extensions are now just unnamed functions, not allowing for easy inspection

Code style changes
- strings are written with apostrophes
- functions and variables are camelCase
- files are dashed-lowercase
  • Loading branch information
Skalman committed Mar 19, 2017
1 parent f7bb28b commit da8793a
Show file tree
Hide file tree
Showing 39 changed files with 1,563 additions and 1,711 deletions.
6 changes: 3 additions & 3 deletions baseconvert.com/Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ module.exports = function (grunt) {

logs: {
files: {
'dist/logs/human_access.log': 'logs/human_access.log.example',
'dist/logs/interested_access.log': 'logs/interested_access.log.example',
'dist/logs/use_access.log': 'logs/use_access.log.example',
'dist/logs/human-access.log': 'logs/human-access.log.example',
'dist/logs/interested-access.log': 'logs/interested-access.log.example',
'dist/logs/use-access.log': 'logs/use-access.log.example',
},
},

Expand Down
3 changes: 2 additions & 1 deletion baseconvert.com/high-precision.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@
<script src="../lib/bignumber.min.js" defer></script>
<script src="js/lib/angular.min.js" defer></script>
<script src="../src/core.js" defer></script>
<script src="../src/twos_complement.js" defer></script>
<script src="../src/twos-complement.js" defer></script>
<script src="../src/standard.js" defer></script>
<script src="../src/leet.js" defer></script>

<script src="js/converter-init-high-precision.js" defer></script>
<script src="js/polyfills.js" defer></script>
<script src="js/constants.js" defer></script>
<script src="js/calculation-examples-high-precision.js" defer></script>
Expand Down
3 changes: 2 additions & 1 deletion baseconvert.com/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@
<script src="js/lib/angular.min.js" defer></script>
<script src="../src/core.js" defer></script>
<script src="../src/roman.js" defer></script>
<script src="../src/twos_complement.js" defer></script>
<script src="../src/twos-complement.js" defer></script>
<script src="../src/standard.js" defer></script>
<script src="../src/leet.js" defer></script>

<script src="js/converter-init-index.js" defer></script>
<script src="js/polyfills.js" defer></script>
<script src="js/constants.js" defer></script>
<script src="js/calculation-examples-index.js" defer></script>
Expand Down
18 changes: 9 additions & 9 deletions baseconvert.com/js/calculation-examples-high-precision.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
var e = '2.718281828459045235360287471352662497757247093699959574966967627724076630353547594571382178525166427427466391932003059921817413596629043572900334295260595630738132328627943490763233829880753195251019011573834187930702154089149934884167509244761460668082264800168477411853742345442437107539077744992069551702761838606261331384583000752044933826560297606737113200709328709127443747047230696977209310141692836819025515108657463772111252389784425056953696770785449969967946864454905987931636889230098793127736178215424999229576351482208269895193668033182528869398496465105820939239829488793320362509443117301238197068416140397019837679320683282376464804295311802328782509819455815301756717361332069811250996181881593041690351598888519345807273866738589422879228499892086805825749279610484198444363463244968487560233624827041978623209002160990235304369941849146314093431738143640546253152096183690888707016768396424378140592714563549061303107208510383750510115747704171898610687396965521267154688957035035402123';
var ln2 = '0.693147180559945309417232121458176568075500134360255254120680009493393621969694715605863326996418687542001481020570685733685520235758130557032670751635075961930727570828371435190307038623891673471123350115364497955239120475172681574932065155524734139525882950453007095326366642654104239157814952043740430385500801944170641671518644712839968171784546957026271631064546150257207402481637773389638550695260668341137273873722928956493547025762652098859693201965058554764703306793654432547632744951250406069438147104689946506220167720424524529612687946546193165174681392672504103802546259656869144192871608293803172714367782654877566485085674077648451464439940461422603193096735402574446070308096085047486638523138181676751438667476647890881437141985494231519973548803751658612753529166100071053558249879414729509293113897155998205654392871700072180857610252368892132449713893203784393530887748259701715591070882368362758984258918535302436342143670611892367891923723146723217205340164925687274778234453534764811';

var sqrt1_2 = Base.Big(1/2).sqrt() + '';
var sqrt1_3 = Base.Big(1).div(3).sqrt() + '';
var sqrt2 = Base.Big(2).sqrt() + '';
var sqrt3 = Base.Big(3).sqrt() + '';
var sqrt5 = Base.Big(5).sqrt() + '';
var sqrt7 = Base.Big(7).sqrt() + '';
var pi_2 = Base.Big(pi).mul(pi) + '';
var tau = Base.Big(pi).mul(2) + '';
var golden = Base.Big(1).add(sqrt5).div(2) + '';
var sqrt1_2 = converter.Big(1/2).sqrt() + '';
var sqrt1_3 = converter.Big(1).div(3).sqrt() + '';
var sqrt2 = converter.Big(2).sqrt() + '';
var sqrt3 = converter.Big(3).sqrt() + '';
var sqrt5 = converter.Big(5).sqrt() + '';
var sqrt7 = converter.Big(7).sqrt() + '';
var pi_2 = converter.Big(pi).mul(pi) + '';
var tau = converter.Big(pi).mul(2) + '';
var golden = converter.Big(1).add(sqrt5).div(2) + '';

var CALCULATION_EXAMPLES_DEFAULT = window.CALCULATION_EXAMPLES_DEFAULT = [
{ name: 'pi (π)', numberDisplay: '3.14', number: pi },
Expand Down
3 changes: 1 addition & 2 deletions baseconvert.com/js/calculation-examples-index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ var CALCULATION_EXAMPLES = [
'roman numerals': {
roman: [
'IV', 'IIII', 'MCMLXXXVIII', 'LXII', 'XII', 'MDCCLXXVI',
Base('10', 'roman', new Date().getFullYear()),
converter.convert('10', 'roman', new Date().getFullYear()),
],
},
'any base': {
Expand All @@ -55,4 +55,3 @@ var CALCULATION_EXAMPLES = [
});
});
}());

5 changes: 5 additions & 0 deletions baseconvert.com/js/converter-init-high-precision.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// Create the base converter object to be used in the app.
var converter = new Base({
Big: BigNumber.another({ DECIMAL_PLACES: 1000, POW_PRECISION: 1000, ERRORS: false }),
extensions: [extTwosComplement, extStandard, extLeet]
});
5 changes: 5 additions & 0 deletions baseconvert.com/js/converter-init-index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// Create the base converter object to be used in the app.
var converter = new Base({
Big: Big,
extensions: [extRoman, extTwosComplement, extStandard, extLeet]
});
18 changes: 9 additions & 9 deletions baseconvert.com/js/directives.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,34 +164,34 @@ app
})


// Debounced and cached version of Base()
.service('DebouncedBase', ['$timeout', '$q', function ($timeout, $q) {
// Debounced and cached version of converter
.service('debouncedConvert', ['$timeout', '$q', function ($timeout, $q) {
var lastTimeout;
var debounceTime = 50;

function DebouncedBase(from, to, number) {
function debouncedConvert(from, to, number) {
// This call supersedes any previous call within the debounce time.
$timeout.cancel(lastTimeout);

// If we have a cached result, always return it immediately.
var cacheKey = from + '|' + to + '|' + number;
if (DebouncedBase.cache[cacheKey]) {
return $q.when(DebouncedBase.cache[cacheKey]);
if (debouncedConvert.cache[cacheKey]) {
return $q.when(debouncedConvert.cache[cacheKey]);
}

// Run the calculation in a little while.
lastTimeout = $timeout(function () {
var before = Date.now();
var result = Base(from, to, number);
DebouncedBase.cache[cacheKey] = result;
var result = converter.convertToMultiple(from, to, number);
debouncedConvert.cache[cacheKey] = result;
debounceTime = Date.now() - before;
return result;
}, debounceTime);

return lastTimeout;
}

DebouncedBase.cache = {};
debouncedConvert.cache = {};

return DebouncedBase;
return debouncedConvert;
}]);
20 changes: 10 additions & 10 deletions baseconvert.com/js/main.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
var app = angular.module('baseconvertApp', []);

app.controller('ConversionController', ['$scope', '$window', '$document', '$http', '$timeout', 'focus', 'scrollIntoView', 'DebouncedBase',
function ($scope, $window, $document, $http, $timeout, focus, scrollIntoView, DebouncedBase) {
app.controller('ConversionController', ['$scope', '$window', '$document', '$http', '$timeout', 'focus', 'scrollIntoView', 'debouncedConvert',
function ($scope, $window, $document, $http, $timeout, focus, scrollIntoView, debouncedConvert) {
$scope.bases = [
{
id: '2',
Expand All @@ -22,7 +22,7 @@ function ($scope, $window, $document, $http, $timeout, focus, scrollIntoView, De
id: '16',
name: 'hexadecimal',
explanation: 'base 16',
}
},
];

$scope.originBase = undefined;
Expand Down Expand Up @@ -81,7 +81,7 @@ function ($scope, $window, $document, $http, $timeout, focus, scrollIntoView, De
// No actual change.
return;
}
var sug = Base.suggest($scope.newBaseName || '', true);
var sug = converter.suggest($scope.newBaseName || '', true);
var existing = {};
$scope.bases.forEach(function (base) {
existing[base.id] = true;
Expand Down Expand Up @@ -115,7 +115,7 @@ function ($scope, $window, $document, $http, $timeout, focus, scrollIntoView, De
var existingBase = $scope.bases.find(isAddedBase);
if (!existingBase) {
if ($scope.originBase) {
base.number = Base($scope.originBase.id, base.id, $scope.originBase.number);
base.number = converter.convert($scope.originBase.id, base.id, $scope.originBase.number);
}
$scope.bases.push(base);
$scope.bases.sort(function (a, b) {
Expand Down Expand Up @@ -192,7 +192,7 @@ function ($scope, $window, $document, $http, $timeout, focus, scrollIntoView, De
}
} else if ($scope.showSuggestions && (key === KEY_ENTER || (key === KEY_TAB && !e.shiftKey))) {
if ($scope.suggestions.length) {
$scope.addBase($scope.selectedSuggestion);
$scope.addBase($scope.selectedSuggestion);
} else if (e.shiftKey) {
focus($scope.bases[$scope.bases.length - 1].id);
}
Expand All @@ -209,7 +209,7 @@ function ($scope, $window, $document, $http, $timeout, focus, scrollIntoView, De
}
$scope.originBase = originBase;

originBase.hasError = originBase.number && !Base.valid(originBase.id, originBase.number);
originBase.hasError = originBase.number && !converter.valid(originBase.id, originBase.number);

var notOrigin = $scope.bases.filter(function (base) {
return base !== originBase;
Expand All @@ -218,8 +218,8 @@ function ($scope, $window, $document, $http, $timeout, focus, scrollIntoView, De
return base.id;
});

// Base(from, to, number)
DebouncedBase(originBase.id, notOriginIds, originBase.number)
// base.convert(from, to, number)
debouncedConvert(originBase.id, notOriginIds, originBase.number)
.then(function (results) {
results.forEach(function (number, index) {
notOrigin[index].hasError = false;
Expand Down Expand Up @@ -377,7 +377,7 @@ function ($scope, $window, $document, $http, $timeout, focus, scrollIntoView, De
example.name = newExample.name;
example.number = newExample.number;
example.numberDisplay = newExample.numberDisplay;
example.baseId = newExample.baseId;
example.baseId = newExample.baseId;
}).finally(function () {
// Reset the state.
example.running = false;
Expand Down
File renamed without changes.
6 changes: 3 additions & 3 deletions baseconvert.com/package.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"author": "Dan Wolff (http://danwolff.se/)",
"author": "Dan Wolff (https://danwolff.se/)",
"name": "baseconvert",
"description": "Online base converter. Convert from any base, to any base (binary, hexadecimal, even roman numerals!)",
"version": "2.3.0",
"copyright": "(c) 2016 Dan Wolff",
"version": "3.0.0",
"copyright": "(c) 2017 Dan Wolff",
"repository": {
"type": "git",
"url": "git@github.com:Skalman/baseconvert.git"
Expand Down
87 changes: 43 additions & 44 deletions build/build.js
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,56 +1,55 @@
#!/usr/bin/env node

var fs = require("fs"),
path = require("path"),
util = require("util"),
qs = require("querystring"),
http = require("http"),

base = path.normalize(__dirname + "/..") + "/",
files = [
"src/core.js",
"src/standard.js",
"src/roman.js",
"src/leet.js",
"src/twos_complement.js"
],
options = {
compilation_level: "SIMPLE_OPTIMIZATIONS",
},
dist = "dist/",
output = dist + "base_convert.js",
output_min = dist + "base_convert.min.js";
var fs = require('fs');
var path = require('path');
var util = require('util');
var qs = require('querystring');
var http = require('http');

var base = path.normalize(__dirname + '/..') + '/';
var files = [
'src/core.js',
'src/leet.js',
'src/standard.js',
'src/twos-complement.js',
'src/roman.js',
];
var options = {
compilation_level: 'SIMPLE_OPTIMIZATIONS',
};
var dist = 'dist/';
var output = dist + 'base-convert.js';
var output_min = dist + 'base-convert.min.js';

main();

// Build files to dist
function main() {
var i, code = "";
var i, code = '';

if (!fs.existsSync(base + dist)) {
console.log("Create directory %s", dist);
console.log('Create directory %s', dist);
fs.mkdirSync(base + dist, 0755);
}
for (i = 0; i < files.length; i++) {
code += fs.readFileSync(base + files[i]);
}

console.log("Write %s (concatenated source)", output);
console.log('Write %s (concatenated source)', output);
fs.writeFileSync(base + output, code);
fs.chmodSync(base + output, 0644);

compile(code, function(err, code_min) {
if (err) throw err;

var savings = Math.round((1 - (code_min.length / code.length)) * 10000) / 100;
console.log("Write %s (minified, saved %d%)", output_min, savings);
console.log('Write %s (minified, saved %d%)', output_min, savings);

fs.writeFileSync(base + output_min, code_min);
fs.chmodSync(base + output_min, 0644);
});
}


// https://github.com/weaver/scribbles/tree/master/node/google-closure

// Use the Google Closure Compiler Service to compress Javascript
Expand All @@ -61,14 +60,14 @@ function main() {
function compile(code, next) {
try {
var body, post_data, req,
host = "closure-compiler.appspot.com";
host = 'closure-compiler.appspot.com';

post_data = {
js_code: code.toString("utf-8"),
compilation_level: options.compilation_level ? options.compilation_level : "ADVANCED_OPTIMIZATIONS",
output_format: "json",
output_info: "compiled_code",
warning_level: options.warning_level ? options.warning_level : "DEFAULT",
js_code: code.toString('utf-8'),
compilation_level: options.compilation_level ? options.compilation_level : 'ADVANCED_OPTIMIZATIONS',
output_format: 'json',
output_info: 'compiled_code',
warning_level: options.warning_level ? options.warning_level : 'DEFAULT',
};
if (options.formatting) post_data.formatting = options.formatting;
if (options.js_externs) post_data.js_externs = options.js_externs;
Expand All @@ -77,19 +76,19 @@ function compile(code, next) {

req = http.request({
host: host,
path: "/compile",
method: "POST",
path: '/compile',
method: 'POST',
headers: {
"Content-Type": "application/x-www-form-urlencoded"
'Content-Type': 'application/x-www-form-urlencoded'
}
});
req.on("error", next);
req.on('error', next);

req.on("response", function(res) {
req.on('response', function(res) {
if (res.statusCode != 200)
next(new Error("Unexpected HTTP response: " + res.statusCode));
next(new Error('Unexpected HTTP response: ' + res.statusCode));
else
capture(res, "utf-8", parseResponse);
capture(res, 'utf-8', parseResponse);
});

req.end(body);
Expand All @@ -100,9 +99,9 @@ function compile(code, next) {
if (err)
next(err);
else if ((error = obj.errors || obj.serverErrors || obj.warnings))
next(new Error("Failed to compile: " + util.inspect(error)));
next(new Error('Failed to compile: ' + util.inspect(error)));
else if (obj.compiledCode.length === 0)
next(new Error("Empty result: " + data));
next(new Error('Empty result: ' + data));
else
next(null, obj.compiledCode);
});
Expand All @@ -118,17 +117,17 @@ function compile(code, next) {
// + encoding - String input encoding
// + next - Function error/success callback
function capture(input, encoding, next) {
var buffer = "";
var buffer = '';

input.on("data", function(chunk) {
input.on('data', function(chunk) {
buffer += chunk.toString(encoding);
});

input.on("end", function() {
input.on('end', function() {
next(null, buffer);
});

input.on("error", next);
input.on('error', next);
}

// Convert JSON.load() to callback-style.
Expand Down
1 change: 1 addition & 0 deletions docs/demo.css
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,5 @@ p {
}
output {
font-weight:bold;
white-space: pre-wrap;
}
Loading

0 comments on commit da8793a

Please sign in to comment.