Skip to content

Commit

Permalink
🎉 Allow enabling/disabling of padding (#3)
Browse files Browse the repository at this point in the history
* feat: allow disabling padding

* updated docs

* fixed options docs
  • Loading branch information
vasco-santos authored and LinusU committed Jun 25, 2018
1 parent e48a8bc commit 06e521d
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 6 deletions.
12 changes: 7 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,29 @@ var RFC4648 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567'
var RFC4648_HEX = '0123456789ABCDEFGHIJKLMNOPQRSTUV'
var CROCKFORD = '0123456789ABCDEFGHJKMNPQRSTVWXYZ'

module.exports = function base32Encode (buffer, variant) {
var alphabet, padding
module.exports = function base32Encode (buffer, variant, options) {
options = options || {}
var alphabet, defaultPadding

switch (variant) {
case 'RFC3548':
case 'RFC4648':
alphabet = RFC4648
padding = true
defaultPadding = true
break
case 'RFC4648-HEX':
alphabet = RFC4648_HEX
padding = true
defaultPadding = true
break
case 'Crockford':
alphabet = CROCKFORD
padding = false
defaultPadding = false
break
default:
throw new Error('Unknown base32 variant: ' + variant)
}

var padding = (options.padding !== undefined ? options.padding : defaultPadding)
var length = buffer.byteLength
var view = new Uint8Array(buffer)

Expand Down
9 changes: 8 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,21 @@ console.log(base32Encode(buffer, 'Crockford'))
console.log(base32Encode(buffer, 'RFC4648'))
//=> ORSXG5A=

console.log(base32Encode(buffer, 'RFC4648', { padding: false }))
//=> ORSXG5A

console.log(base32Encode(buffer, 'RFC4648-HEX'))
//=> EHIN6T0=
```

## API

### base32Encode(buffer, variant)
### base32Encode(buffer, variant, options)

- `buffer` <ArrayBuffer>
- `variant` <String>
- `options` <Object>
- `padding` <Boolean>

Encode the data in `buffer`. `variant` should be one of the supported variants
listed below.
Expand All @@ -39,6 +44,8 @@ listed below.
- `'RFC4648-HEX'` - [base32hex from RFC4648](https://tools.ietf.org/html/rfc4648)
- `'Crockford'` - [Crockford's Base32](http://www.crockford.com/wrmg/base32.html)

Options may have a `padding` property which provides a way to forcefully enable or disable padding. The default behavior is to follow the default of the selected variant.

## See also

- [base32-decode](https://github.com/LinusU/base32-decode) - Base32 decoder
6 changes: 6 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ var testCases = [
['Crockford', '666f6f626172', 'CSQPYRK1E8']
]

// base32 encode with no options
testCases.forEach(function (testCase) {
assert.equal(base32Encode(hexToArrayBuffer(testCase[1]), testCase[0]), testCase[2])
})

// base32 encode with disabled padding option
testCases.forEach(function (testCase) {
assert.equal(base32Encode(hexToArrayBuffer(testCase[1]), testCase[0], { padding: false }), testCase[2].replace(/=/g, ''))
})

0 comments on commit 06e521d

Please sign in to comment.