-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add binascii functions for zero dependencies
- Loading branch information
1 parent
0316412
commit 8b3f22e
Showing
10 changed files
with
155 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/** | ||
* @license MIT | ||
* @copyright Copyright (c) 2014 Michał Budzyński (@michalbe) | ||
* @see https://github.com/michalbe/binascii | ||
* @see https://docs.python.org/2/library/binascii.html | ||
*/ | ||
|
||
/** | ||
* @param {string} str | ||
* @returns {string} | ||
*/ | ||
export function hexlify(str) { | ||
let result = '' | ||
for (let i = 0, l = str.length; i < l; i++) { | ||
const digit = str.charCodeAt(i).toString(16) | ||
const padded = ('00' + digit).slice(-2) | ||
result += padded | ||
} | ||
return result | ||
} | ||
|
||
/** | ||
* @param {string} str | ||
* @returns {string} | ||
*/ | ||
export function unhexlify(str) { | ||
let result = '' | ||
for (var i = 0, l = str.length; i < l; i += 2) { | ||
result += String.fromCharCode(parseInt(str.slice(i, i + 2), 16)) | ||
} | ||
return result | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import assert from 'node:assert' | ||
import { hexlify, unhexlify } from '../src/binascii.js' | ||
|
||
describe('binascii', function () { | ||
it('shall return a string', function () { | ||
assert.equal(hexlify(''), '') | ||
}) | ||
|
||
it('shall hexlify char', function () { | ||
assert.equal(hexlify('A'), '41') | ||
}) | ||
|
||
it('shall hexlify string', function () { | ||
assert.equal(hexlify('Pamietamy 44'), '50616d696574616d79203434') | ||
}) | ||
|
||
it('shall hexlify binary data', function () { | ||
assert.equal(hexlify("7z¼¯'\u001c"), '377abcaf271c') | ||
}) | ||
|
||
it('shall ensure that single-digit codes are correctly padded', function () { | ||
assert.equal(hexlify('\n'), '0a') | ||
}) | ||
|
||
it('shall unhexlify char', function () { | ||
assert.equal(unhexlify(hexlify('A')), 'A') | ||
}) | ||
|
||
it('shall unhexlify string', function () { | ||
assert.equal(unhexlify('50616d696574616d79203434'), 'Pamietamy 44') | ||
}) | ||
|
||
it('shall unhexlify binary data', function () { | ||
assert.equal(unhexlify('377abcaf271c'), "7z¼¯'\u001c") | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
/** | ||
* @license MIT | ||
* @copyright Copyright (c) 2014 Michał Budzyński (@michalbe) | ||
* @see https://github.com/michalbe/binascii | ||
* @see https://docs.python.org/2/library/binascii.html | ||
*/ | ||
/** | ||
* @param {string} str | ||
* @returns {string} | ||
*/ | ||
export function hexlify(str: string): string; | ||
/** | ||
* @param {string} str | ||
* @returns {string} | ||
*/ | ||
export function unhexlify(str: string): string; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters