Skip to content
This repository was archived by the owner on Jun 6, 2023. It is now read-only.

Commit 4d211bd

Browse files
committed
Add Convert Base.lbaction
1 parent 90ea50b commit 4d211bd

File tree

6 files changed

+320
-0
lines changed

6 files changed

+320
-0
lines changed
+95
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
6+
<key>CFBundleIdentifier</key>
7+
<string>io.henrik.launchbar.ConvertBase</string>
8+
9+
<key>CFBundleName</key>
10+
<string>Convert Base</string>
11+
12+
<!-- <key>CFBundleShortVersionString</key> -->
13+
<!-- <value>1.0</value> -->
14+
15+
<key>CFBundleVersion</key>
16+
<string>1.0.0</string>
17+
18+
<key>CFBundleIconFile</key>
19+
<string>icon</string>
20+
21+
<key>LBDebugLogEnabled</key>
22+
<false />
23+
24+
<!-- LB stuff -->
25+
<!-- <key>LSMinimumSystemVersion</key> -->
26+
<!-- <string>10.9.1</string> -->
27+
28+
<!-- <key>LBMinimumLaunchBarVersion</key> -->
29+
<!-- <string>6011</string> -->
30+
31+
<!-- Per-action keys -->
32+
<key>LBTextInputTitle</key>
33+
<string>Convert</string>
34+
35+
<key>LBScripts</key>
36+
<dict>
37+
<key>LBDefaultScript</key>
38+
<dict>
39+
<key>LBScriptName</key>
40+
<string>default.js</string>
41+
42+
<key>LBRequiresArgument</key>
43+
<true />
44+
45+
<key>LBAcceptedArgumentTypes</key>
46+
<array>
47+
<string>string</string>
48+
</array>
49+
50+
<key>LBReturnsResult</key>
51+
<true />
52+
</dict>
53+
54+
<key>LBSuggestionsScript</key>
55+
<dict>
56+
<key>LBScriptName</key>
57+
<string>suggestions.js</string>
58+
59+
<key>LBBackgroundKillEnabled</key>
60+
<true />
61+
</dict>
62+
</dict>
63+
64+
<key>LBDescription</key>
65+
<dict>
66+
<key>LBAuthor</key>
67+
<string>Henrik Lissner</string>
68+
69+
<key>LBWebsite</key>
70+
<string>http://v0.io/launchbar</string>
71+
72+
<key>LBEmail</key>
73+
<string>[email protected]</string>
74+
75+
<key>LBTwitter</key>
76+
<string>@vnought</string>
77+
78+
<key>LBSummary</key>
79+
<string>Converts a number from any base (up to base-36) to any other base (up to base-36)</string>
80+
81+
<key>LBArgument</key>
82+
<string>&lt;NUMBER&gt;[[ &lt;RADIX-FROM&gt;] &lt;RADIX-TO&gt;]</string>
83+
84+
<key>LBResult</key>
85+
<string>The converted number</string>
86+
87+
<key>LBUpdateURL</key>
88+
<string>https://raw.githubusercontent.com/hlissner/lb6-actions/master/Convert Base.lbaction/Contents/Info.plist</string>
89+
90+
<key>LBDownloadURL</key>
91+
<string>http://dl.v0.io/launchbar/Convert Base.lbaction</string>
92+
</dict>
93+
94+
</dict>
95+
</plist>
Loading
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
1+
include("shared/lib/notify.js");
2+
3+
function runWithString(string) {
4+
// Easy access to preferences and bug reporting if Command is pressed.
5+
if (LaunchBar.options.commandKey) {
6+
Lib.Notify.force_prompt();
7+
return [];
8+
}
9+
10+
try {
11+
var data = parse(string);
12+
13+
// Convert it to intermediary radix (base 10)
14+
var result = from10(to10(data.num, data.from), data.to);
15+
16+
// Formatted result
17+
var formatted_result = format(result, data.to);
18+
19+
return [
20+
{
21+
title: formatted_result,
22+
subtitle: "Result",
23+
label: "Base " + data.to
24+
},
25+
formatted_result != result ? {
26+
title: result,
27+
subtitle: "Unformatted result",
28+
label: "Base " + data.to
29+
} : {},
30+
{
31+
title: format(data.num, data.from),
32+
subtitle: "From",
33+
label: "Base " + data.from + (data.guessed ? " (guessed)" : "")
34+
}
35+
];
36+
} catch (err) {
37+
Lib.Notify.error(err);
38+
return [];
39+
}
40+
}
41+
42+
/**
43+
* Format a number with commas (or spaces with binary) as appropriate.
44+
*
45+
* @param {string} nstr - The number string to be formatted
46+
* @param {int} base - The base nstr is in
47+
* @param {int} [from] - The base nstr was in (only relevant when base is 2)
48+
* @return {string} - The formatted number
49+
*/
50+
function format(nstr, base, from) {
51+
assert(typeof nstr == "string", "format(" + nstr + ", ...): not a string!");
52+
assert(isNumeric(base), "format(..., " + base + "): not a number!");
53+
54+
if (base == 2 && from != 2) {
55+
var log = Math.log2(from);
56+
if (log != parseInt(log)) {
57+
log = 4;
58+
}
59+
return nstr.replace(new RegExp("\B(?=(\d{" + log + "})+(?!\d))", "gi"), " ");
60+
} else {
61+
return nstr.replace(/\B(?=(\d{3})+(?!\d))/gi, ",");
62+
}
63+
}
64+
65+
/**
66+
* Parses the input.
67+
*
68+
* @param {string} string - The raw input from Launchbar
69+
* @return {Object} data - Information about the number
70+
* @return {string} data.num - The number to be converted
71+
* @return {int} data.from - The radix to be converted from
72+
* @return {int} data.to - The radix to be converted to
73+
* @return {bool} data.guessed - Whether or not data.from was guessed from data.num
74+
*/
75+
function parse(string) {
76+
assert(typeof string == "string", "parse(string): not an string!");
77+
assert(string.trim() != "", "parse(string): string is empty!");
78+
assert(string.search(/[^0-9a-zA-Z,._ ]+/) == -1, "Invalid input!");
79+
80+
parts = string.split(" ");
81+
if (parts.length == 1) {
82+
parts[1] = 10;
83+
}
84+
85+
var data = {
86+
num: parts[0].toLowerCase(),
87+
to: parseInt(parts[1]),
88+
from: 10,
89+
guessed: true
90+
};
91+
data.num = data.num.replace(",", "");
92+
93+
if (data.num.indexOf("_") != -1) {
94+
var _num = data.num.split("_");
95+
data.num = _num[0];
96+
data.from = parseInt(_num[1]);
97+
data.guessed = false;
98+
} else {
99+
data.from = guessBase(data.num);
100+
}
101+
102+
if (data.from < 2 || data.from > 36 || data.to < 2 || data.to > 36) {
103+
throw "Invalid radix; this converter only supports 2 <= r <= 36";
104+
}
105+
106+
return data;
107+
}
108+
109+
/**
110+
* Converts [num] to base 10, from base [from]
111+
*
112+
* @param {string} num - The number to convert
113+
* @param {int} from - The source radix
114+
* @return {float} - The converted number in decimal
115+
*/
116+
function to10(num, from) {
117+
assert(typeof num == "string", "to10(" + num + ", ...): not a string!");
118+
assert(isNumeric(from), "to10(..., " + from + "): not a number!");
119+
120+
if (from == 10) {
121+
return parseFloat(num);
122+
}
123+
124+
var digits = num;
125+
var n, m;
126+
127+
if ((n = digits.indexOf(".")) == -1) {
128+
n = digits.length;
129+
m = 0;
130+
} else {
131+
m = -(digits.length - (n + 1));
132+
}
133+
--n;
134+
135+
var result = 0;
136+
for (var j = 0, i = n; i >= m; --i, j = (n - m) - i) {
137+
if (digits[j] != "0") {
138+
result += (parseInt(digits[j], from) * Math.pow(from, i));
139+
}
140+
}
141+
return result;
142+
}
143+
144+
/**
145+
* Converts [num] to N in base [to]
146+
*
147+
* @param {Number} num - The number to convert
148+
* @param {int} to - Radix to convert to
149+
* @return {Number} - Converted amount in new base
150+
*/
151+
function from10(num, to) {
152+
assert(isNumeric(num), "from10(" + num + ", ...): not a number!");
153+
assert(isNumeric(to), "from10(..., " + to + "): not a number!");
154+
155+
return num.toString(to);
156+
}
157+
158+
/**
159+
* Tries to guess the radix of data.num, and removes prefixes if any. Defaults to 10 if it
160+
* fails to guess. Hold down shift to force the guess to be 10.
161+
*
162+
* @param {string} nstr - The number the radix should be deduced from
163+
* @return {int} - The guessed radix
164+
*/
165+
function guessBase(nstr) {
166+
assert(typeof nstr == "string", "guessBase(nstr): not an string!");
167+
168+
if (!LaunchBar.options.shiftKey) {
169+
if (nstr.search(/[g-z]+/) != -1) {
170+
return 36;
171+
}
172+
else if (nstr.search(/[a-f]+/) != -1) {
173+
return 16;
174+
}
175+
else if (nstr.search(/[^2-9a-zA-Z,._]+/) != -1) {
176+
return 2;
177+
}
178+
}
179+
return 10;
180+
}
181+
182+
/**
183+
* Tests whether n is numeric or not (as a string or number).
184+
*
185+
* @private
186+
* @param {string|number} n - The value to test
187+
* @return {bool} - Whether n is numeric or not
188+
*/
189+
function isNumeric(n) {
190+
return !isNaN(parseFloat(n)) && isFinite(n);
191+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
function runWithString(string) {
3+
if (string.trim().length === 0) {
4+
return {title: "<number>[[ <source-radix>] <dest-radix>]"};
5+
}
6+
return {};
7+
}
8+

Convert Base.lbaction/README.md

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Base Convert
2+
3+
Converts a number in any base N to base M (where N and M are greater than 2 and less than
4+
36).
5+
6+
## Installation
7+
8+
[Download the action](https://v0.io/dl/launchbar/Base%20Convert.lbaction.zip), unzip it,
9+
and double the resulting lbaction file to install it.
10+
11+
## Usage
12+
13+
The command accepts the following syntax:
14+
15+
`<number>[[_<source-radix>] <dest-radix>]`
16+
17+
+ If `source-radix` is omitted, it will try to guess between base 2, 16 and 36 (defaults
18+
to 10 otherwise).
19+
+ If `dest-radix` is omitted, the number is converted to several common bases (binary,
20+
octal, decimal and hexidecimal).
21+
22+
### Examples
23+
24+
+ `0x81F9.4 10` returns `33273.25`
25+
+ `81F9_16 2` returns `1000000111111001`
26+
+ `7501_8 2` returns `111101000001`

0 commit comments

Comments
 (0)