New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

all-your-base

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

all-your-base - npm Package Compare versions

Comparing version 0.2.1 to 0.3.0

bin/cli.js

57

index.js

@@ -16,2 +16,59 @@ var convert = require('./src/convert.js');

octToHex: convert.octToHex,
parseInt: local_parseInt
};
/**
* A buffed-up version of the standard `parseInt` function
* @param str, the value to convert. Is coerced into a String.
* @param from, [Number], a base to convert from
* @param to, [Number] a base to convert to
* @return, the result of a call to a base conversion function
*/
function local_parseInt(str, from, to) {
var fn = assignFn(from, to);
return convert[fn](str + '');
}
/**
* Return a property to pass to the 'convert' object
* @param from, [Number], a base to convert from
* @param to, [Number] a base to convert to
* @return, [String] a generated property name
* @throws, an error if the base is not supported by this module
*/
function assignFn(from, to) {
from = setDefault(10, from);
to = setDefault(10, to);
from = numToWord(from);
to = capitalize(numToWord(to));
return from + 'To' + to;
// return a copy of a string with its first letter capitalized
function capitalize(str) {
return str[0].toUpperCase() + str.slice(1);
}
// translate a number to its correlating module-specific namespace
function numToWord(num) {
num += '';
switch(num) {
case '2':
return 'bin';
case '8':
return 'oct';
case '10':
return 'dec';
case '16':
return 'hex';
default:
throw new Error('Base "' + num + '" is not supported');
}
}
// if `arg` is not provided, return a default value
function setDefault(defaultVal, arg) {
return (typeof arg === 'undefined' ? defaultVal : arg);
}
}

4

package.json
{
"name": "all-your-base",
"version": "0.2.1",
"version": "0.3.0",
"description": "convert from/to binary, octal, hexadecimal, decimal values",

@@ -8,3 +8,3 @@ "preferGlobal": false,

"bin": {
"ayb": "./cli.js"
"ayb": "./bin/cli.js"
},

@@ -11,0 +11,0 @@ "scripts": {

@@ -0,12 +1,5 @@

# All your base
Convert from one base to another
##### Supported bases
| base/radix | name | module's abbr. |
|:-----------|:------------|:---------------|
| base 2 | binary | `bin` |
| base 8 | octal | `oct` |
| base 10 | decimal | `dec` |
| base 16 | hexadecimal | `hex` |
## Installation

@@ -16,29 +9,46 @@

## Usage
#### Install as command line tool
Use this in your own scripts and/or as a stand-alone command-line tool.
`npm link` from your local repo's root. The executable is named `ayb`.
### From a script
## Usage
`decToBin(22);`
`hexToDec('10000');`
Use this in your own scripts and/or as a quick stand-alone command-line tool.
##### Parameter data types
#### From a script
to convert from decimal, pass in a non-negative integer
to convert from any base *except for* decimal, pass in the value as a string
```javascript
var ayb = require('all-your-base');
### As command line tool
// convert 'abcdef', from base-16 to base-2
ayb.parseInt('abcdef', 16, 2)
##### Install as command line tool
// Or directly call the conversion methods
ayb.decToBin(22);
ayb.hexToDec('10000');
```
`npm link` from your local repo's root. The executable is named `ayb`.
#### From command line
##### From command line
First, type `npm link` from your local repo's root directory. The executable
is named `ayb`. Name a method & space separate your arguments.
`ayb decToBin 65536`
`ayb hexToDec FF001D`
```bash
$ ayb parseInt 65536 10 16
10000
$ ayb hexToDec 10000
65536
```
---
### Supported bases
| base/radix | name | module's abbr. |
|:-----------|:------------|:---------------|
| base 2 | binary | `bin` |
| base 8 | octal | `oct` |
| base 10 | decimal | `dec` |
| base 16 | hexadecimal | `hex` |
### List of all operations

@@ -48,3 +58,21 @@

From binary
#### `parseInt`
The native `parseInt` method will let you convert another base to base-10.
But the one provided in this module will let you convert to another base. It's
merely a wrapper for the other conversion methods.
* `ayb.parseInt(value, fromBase, toBase)`
* `value`, String. The value in the base you want to convert.
* `fromBase`, Number. Specify the base you are converting from.
* If you are using a script & pass in `undefined`, then it is assumed you
are converting from base-10
* `toBase`, Number.Specify the base you are converting to.
* If you don't pass in anything, then it is assumed you are converting
from base-10.
For the following methods, pass in a numerical as a string.
##### From binary
* `binToDec`

@@ -54,3 +82,4 @@ * `binToHex`

From octal
##### From octal
* `octToDec`

@@ -60,3 +89,4 @@ * `octToBin`

From decimal
##### From decimal
* `decToBin`

@@ -66,3 +96,4 @@ * `decToHex`

From hexadecimal
##### From hexadecimal
* `hexToDec`

@@ -69,0 +100,0 @@ * `hexToBin`

@@ -35,2 +35,8 @@ var assert = require('assert');

});
it('also accepts a string as an argument', function() {
assert.equal(decToBin('156'), '10011100');
assert.equal(decToBin('298'), '100101010');
assert.equal(decToBin('55'), '110111');
assert.equal(decToBin('11'), '1011');
});
});

@@ -50,2 +56,12 @@

});
it('also accepts a string as an argument', function() {
assert.equal(decToHex('317547'), '4D86B');
assert.equal(decToHex('677'), '2A5');
assert.equal(decToHex('79'), '4F');
assert.equal(decToHex('120'), '78');
assert.equal(decToHex('1728'), '6C0');
assert.equal(decToHex('1298'), '512');
assert.equal(decToHex('256'), '100');
assert.equal(decToHex('2500'), '9C4');
});
});

@@ -96,2 +112,8 @@

});
it('also accepts a string as an argument', function() {
assert.equal(decToOct('891'), '1573');
assert.equal(decToOct('99'), '143');
assert.equal(decToOct('363'), '553');
assert.equal(decToOct('5210'), '12132');
});
});

@@ -98,0 +120,0 @@

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc