Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modules modernization #412

Merged
merged 52 commits into from
Dec 11, 2020
Merged
Show file tree
Hide file tree
Changes from 42 commits
Commits
Show all changes
52 commits
Select commit Hold shift + click to select a range
2254fd0
var => const/let + other modernizations
Oct 29, 2020
fa23f47
replaced var with const in examples
Oct 29, 2020
088e53b
code modernization
Oct 29, 2020
caff111
code modernization, removed obsolete require of Binary, ByteArray and…
Oct 29, 2020
88eaa88
code modernization, added tests for net module
Oct 29, 2020
efbc42b
code modernization
Oct 29, 2020
9213031
code modernization
Oct 29, 2020
3fdd099
code modernization, added test for help() method
Oct 31, 2020
26207a8
code modernization
Oct 31, 2020
b0bac7e
code modernization
Oct 31, 2020
dca4046
use require instead of include in code example to make things explicit
Nov 2, 2020
cc17fbd
avoid redeclaration of consts
Nov 2, 2020
5ca27db
modernized/simplified code, added test for Semaphore
Nov 2, 2020
3fc56f2
code modernization
Nov 2, 2020
f7dabd3
code modernization (ie. var -> const/let)
Nov 2, 2020
ae0def5
code modernization (ie. var -> const/let)
Nov 2, 2020
8a54199
code modernization (ie. var -> const/let)
Nov 2, 2020
52eb27a
code modernization (ie. var -> const/let)
Nov 2, 2020
da34dbd
code modernization
Nov 2, 2020
8f4f2c3
code modernization (ie. var -> const/let)
Nov 2, 2020
27640b5
bugfix: arrow functions have no arguments object
Nov 2, 2020
6399f07
code modernization
Nov 2, 2020
3630d6f
code modernization (ie. var -> const/let)
Nov 2, 2020
305f511
code modernization (ie. var -> const/let)
Nov 2, 2020
24ff785
code modernization (ie. var -> const/let)
Nov 2, 2020
f9d3aa2
replaced include() in code example with more explicit require
Nov 2, 2020
9800eb2
code modernization (ie. var -> const/let), ZipIterator is now a Gener…
Nov 2, 2020
9179851
added missing tests
Nov 2, 2020
a278580
code modernization (ie. var -> const/let)
Nov 2, 2020
09184bc
code modernization (ie. var -> const/let)
Nov 2, 2020
1f47930
code modernization (ie. var -> const/let)
Nov 2, 2020
1e92bbe
code modernization (ie. var -> const/let)
Nov 2, 2020
0362ea2
code modernization (ie. var -> const/let)
Nov 2, 2020
1268b7a
use strict comparisons and 0o1234 for octals, minor code formatting
Nov 2, 2020
87f41a4
code modernization (ie. var -> const/let), fixed an error in parseFil…
Nov 2, 2020
c8fde04
code modernization (ie. var -> const/let)
Nov 2, 2020
07046a8
code modernization (ie. var -> const/let)
Nov 2, 2020
fcbb093
code modernization (ie. var -> const/let)
Nov 2, 2020
d406b5f
stack traces now exclude the test module too, code modernization
Nov 3, 2020
7d999ba
code modernization
Nov 3, 2020
81ed270
#409 implemented binary.toByteString() as replacement of the
Nov 3, 2020
d5547a4
minor: simplified named exports
Nov 3, 2020
d43a229
regression fix: short option can be null/undefined
Nov 3, 2020
bc00d84
modified prototype construction of AssertionError and
Nov 3, 2020
de3e2e4
regression fix: printResult and printError must be declared as local …
Nov 4, 2020
556bcc7
regression fix: objects.merge() must handle null/undefined arguments
Nov 4, 2020
73a093a
fixed variable declaration
Nov 4, 2020
7b34b34
added worker test
Nov 4, 2020
2da80dc
fixed write/writeln method binding
Nov 4, 2020
18bb889
reverted change to arrow functions to maintain correct scope
Nov 4, 2020
b8f35ce
fixed & modernized examples
Nov 4, 2020
2965bf1
#409 binary module no longer modifies the String prototype
Nov 16, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
220 changes: 89 additions & 131 deletions modules/assert.js

Large diffs are not rendered by default.

85 changes: 55 additions & 30 deletions modules/binary.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
*
* @example
* // raw network streams only accept Binary as input
* var stream = socket.getStream();
* const stream = socket.getStream();
* stream.write(new ByteArray([0xFA, 0xF0, 0x10, 0x58, 0xFF]));
*
* // network protocols like HTTP/1.1 require ASCII
Expand All @@ -34,8 +34,8 @@
* fs.write("id_dsa.pub", ByteArray.wrap(publicKey.getEncoded()));
*
* // Generates a salt for hashing
* var random = java.security.SecureRandom.getInstance("SHA1PRNG");
* var salt = new ByteArray(8);
* const random = java.security.SecureRandom.getInstance("SHA1PRNG");
* const salt = new ByteArray(8);
* random.nextBytes(salt); // fills up salt with random bytes
*
* @see http://wiki.commonjs.org/wiki/Binary/B
Expand Down Expand Up @@ -82,9 +82,34 @@ exports.ByteString = ByteString;

/**
* Converts the String to a mutable ByteArray using the specified encoding.
* @param {String} string The string to convert into a ByteArray
* @param {String} charset the name of the string encoding. Defaults to 'UTF-8'
* @returns {ByteArray} a ByteArray representing the string
* @example var ba = "hello world".toByteArray();
* @example const binary = require("binary");
* const ba = binary.toByteArray("hello world");
*/
exports.toByteArray = (string, charset) => {
return new ByteArray(String(string), charset || 'utf8');
};

/**
* Converts the String to an immutable ByteString using the specified encoding.
* @param {String} string The string to convert into a ByteString
* @param {String} charset the name of the string encoding. Defaults to 'UTF-8'
* @returns {ByteString} a ByteString representing the string
* @example const binary = require("binary");
* const bs = binary.toByteString("hello world");
*/
exports.toByteString = (string, charset) => {
return new ByteArray(String(string), charset || 'utf8');
};

/**
* Converts the String to a mutable ByteArray using the specified encoding.
* @param {String} charset the name of the string encoding. Defaults to 'UTF-8'
* @returns {ByteArray} a ByteArray representing the string
* @example const ba = "hello world".toByteArray();
* @deprecated
*/
Object.defineProperty(String.prototype, 'toByteArray', {
value: function(charset) {
Expand All @@ -97,7 +122,8 @@ Object.defineProperty(String.prototype, 'toByteArray', {
* Converts the String to an immutable ByteString using the specified encoding.
* @param {String} charset the name of the string encoding. Defaults to 'UTF-8'
* @returns {ByteString} a ByteString representing the string
* @example var bs = "hello world".toByteString();
* @example const bs = "hello world".toByteString();
* @deprecated
*/
Object.defineProperty(String.prototype, 'toByteString', {
value: function(charset) {
Expand All @@ -109,7 +135,7 @@ Object.defineProperty(String.prototype, 'toByteString', {
/**
* Reverses the content of the ByteArray in-place.
* @returns {ByteArray} this ByteArray with its elements reversed
* @example var ba = new ByteArray([0,1,2,3,4,5,6,7,8]);
* @example const ba = new ByteArray([0,1,2,3,4,5,6,7,8]);
* ba.reverse();
* print(ba[0] == 8); // --> true
*/
Expand All @@ -123,22 +149,21 @@ Object.defineProperty(ByteArray.prototype, 'reverse', {
* Sorts the content of the ByteArray in-place.
* @param {Function} comparator the function to compare entries
* @returns {ByteArray} this ByteArray with its elements sorted
* @example var ba = "hello world".toByteArray();
* @example const ba = "hello world".toByteArray();
* ba.sort();
* ba.decodeToString() // --> "dehllloorw"
*/
Object.defineProperty(ByteArray.prototype, 'sort', {
value: function(fn) {
fn = fn || function(a, b) a - b;
return Array.prototype.sort.call(this, fn);
return Array.prototype.sort.call(this, fn || ((a, b) => a - b));
}, writable: true
});

/**
* Apply a function for each element in the ByteArray.
* @param {Function} fn the function to call for each element
* @param {Object} thisObj optional this-object for callback
* @example var ba = "hello world".toByteArray();
* @example const ba = "hello world".toByteArray();
* // prints 104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100
* ba.forEach(function(byte) { console.log(byte) });
*/
Expand All @@ -154,8 +179,8 @@ Object.defineProperty(ByteArray.prototype, 'forEach', {
* @param {Function} callback the filter function
* @param {Object} thisObj optional this-object for callback
* @returns {ByteArray} a new ByteArray
* @example var ba = "hello world".toByteArray();
* var bf = ba.filter(function(byte) { return byte > 110 });
* @example const ba = "hello world".toByteArray();
* const bf = ba.filter(function(byte) { return byte > 110 });
* bf.decodeToString(); // returns "owor"
*/
Object.defineProperty(ByteArray.prototype, 'filter', {
Expand All @@ -170,7 +195,7 @@ Object.defineProperty(ByteArray.prototype, 'filter', {
* @param {Function} callback the callback function
* @param {Object} thisObj optional this-object for callback
* @returns {Boolean} true if at least one invocation of callback returns true
* @example var ba = new ByteArray([0,1,2,3,4,5,6,7,8]);
* @example const ba = new ByteArray([0,1,2,3,4,5,6,7,8]);
* ba.some(function(byte) { return byte > 10; }); // --> false
* ba.some(function(byte) { return byte < 10; }); // --> true
*/
Expand All @@ -186,7 +211,7 @@ Object.defineProperty(ByteArray.prototype, 'some', {
* @param {Function} callback the callback function
* @param {Object} thisObj optional this-object for callback
* @returns {Boolean} true if every invocation of callback returns true
* @example var ba = new ByteArray([0,1,2,3,4,5,6,7,8]);
* @example const ba = new ByteArray([0,1,2,3,4,5,6,7,8]);
* ba.every(function(byte) { return byte > 5; }); // --> false
* ba.every(function(byte) { return byte < 10; }); // --> true
*/
Expand All @@ -202,8 +227,8 @@ Object.defineProperty(ByteArray.prototype, 'every', {
* @param {Function} callback the callback
* @param {Object} thisObj optional this-object for callback
* @returns {ByteArray} a new ByteArray
* @example var ba1 = new ByteArray([0,1,2,3,4,5,6,7,8]);
* var ba2 = ba1.map(function(byte) { return 2 * byte; });
* @example const ba1 = new ByteArray([0,1,2,3,4,5,6,7,8]);
* const ba2 = ba1.map(function(byte) { return 2 * byte; });
* console.log(ba2.toArray()); // prints [0, 2, 4, 6, 8, 10, 12, 14, 16]
*/
Object.defineProperty(ByteArray.prototype, 'map', {
Expand All @@ -218,7 +243,7 @@ Object.defineProperty(ByteArray.prototype, 'map', {
* @param {Object} initialValue optional argument to be used as the first argument to the first call to the callback
* @returns the return value of the last callback invocation
* @see <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce">Array.prototype.reduce()</a>
* @example var ba = new ByteArray([0,1,2,3,4,5,6,7,8]);
* @example const ba = new ByteArray([0,1,2,3,4,5,6,7,8]);
* ba.reduce(function(prev, curr) { return prev + curr; }); // --> 36
*/
Object.defineProperty(ByteArray.prototype, 'reduce', {
Expand Down Expand Up @@ -248,7 +273,7 @@ Object.defineProperty(ByteArray.prototype, 'reduceRight', {
/**
* Removes the last element from an array and returns that element.
* @returns {Number}
* @example var ba = new ByteArray([0,1,2,4,8]);
* @example const ba = new ByteArray([0,1,2,4,8]);
* ba.pop() === 8; // --> true
*/
Object.defineProperty(ByteArray.prototype, 'pop', {
Expand All @@ -261,7 +286,7 @@ Object.defineProperty(ByteArray.prototype, 'pop', {
* Appends the given elements and returns the new length of the array.
* @param {Number...} num... one or more numbers to append
* @returns {Number} the new length of the ByteArray
* @example var ba = new ByteArray([0,1,2,4,8]);
* @example const ba = new ByteArray([0,1,2,4,8]);
* ba.push(16);
* console.log(ba.toArray()); // [0, 1, 2, 4, 8, 16]
*/
Expand All @@ -275,7 +300,7 @@ Object.defineProperty(ByteArray.prototype, 'push', {
* Removes the first element from the ByteArray and returns that element.
* This method changes the length of the ByteArray
* @returns {Number} the removed first element
* @example var ba = new ByteArray([0,1,2,4,8]);
* @example const ba = new ByteArray([0,1,2,4,8]);
* ba.shift();
* console.log(ba.toArray()); // [1, 2, 4, 8]
*/
Expand All @@ -290,7 +315,7 @@ Object.defineProperty(ByteArray.prototype, 'shift', {
* new length.
* @param {Number...} num... one or more numbers to append
* @returns {Number} the new length of the ByteArray
* @example var ba = new ByteArray([0,1,2,4,8]);
* @example const ba = new ByteArray([0,1,2,4,8]);
* ba.unshift(-8, -4, -2, -1);
* console.log(ba.toArray()); // [248, 252, 254, 255, 0, 1, 2, 4, 8]
*/
Expand All @@ -307,7 +332,7 @@ Object.defineProperty(ByteArray.prototype, 'unshift', {
* @param {Number} howMany The number of elements to remove at the given
* position
* @param {Number...} elements... the new elements to add at the given position
* @example var ba = new ByteArray([0,1,2,4,8]);
* @example const ba = new ByteArray([0,1,2,4,8]);
* ba.splice(2,2);
* console.log(ba.toArray()); // [0, 1, 8]
*/
Expand All @@ -323,7 +348,7 @@ Object.defineProperty(ByteArray.prototype, 'splice', {
* @param {Number} offset
* @returns {ByteArray}
* @function
* @example var ba = new ByteArray([0,1,2,4,8]);
* @example const ba = new ByteArray([0,1,2,4,8]);
* ba.byteAt(0); // --> [ByteArray 1]
*/

Expand All @@ -333,7 +358,7 @@ Object.defineProperty(ByteArray.prototype, 'splice', {
* @param {Number} offset
* @returns {ByteArray}
* @function
* @example var ba = new ByteArray([0,1,2,4,8]);
* @example const ba = new ByteArray([0,1,2,4,8]);
* ba.charAt(0); // --> [ByteArray 1]
*/

Expand All @@ -343,7 +368,7 @@ Object.defineProperty(ByteArray.prototype, 'splice', {
* @param {Number} offset
* @returns {Number}
* @function
* @example var ba = new ByteArray([0,1,2,4,8]);
* @example const ba = new ByteArray([0,1,2,4,8]);
* ba.charCodeAt(0); // --> 0
*/

Expand Down Expand Up @@ -410,7 +435,7 @@ Object.defineProperty(ByteArray.prototype, 'splice', {
* Returns a String representation of the ByteArray.
* @name ByteArray.prototype.toString
* @function
* @example var ba = new ByteArray([0,1,2,4,8]);
* @example const ba = new ByteArray([0,1,2,4,8]);
* console.log(ba.toString()); // prints '[ByteArray 5]'
*/

Expand All @@ -419,7 +444,7 @@ Object.defineProperty(ByteArray.prototype, 'splice', {
* @param {String} encoding the name of the encoding to use
* @name ByteArray.prototype.decodeToString
* @function
* @example var ba = new ByteArray([240, 159, 152, 130]);
* @example const ba = new ByteArray([240, 159, 152, 130]);
* console.log(ba.decodeToString("UTF-8")); // prints &#128514;
*/

Expand Down Expand Up @@ -493,7 +518,7 @@ Object.defineProperty(ByteArray.prototype, 'splice', {
* @param {Number} offset
* @param {Number} value
* @function
* @example var ba = new ByteArray([0,255]);
* @example const ba = new ByteArray([0,255]);
* ba[0] = 64;
* print(ba[0]); // prints 64
*/
Expand All @@ -505,7 +530,7 @@ Object.defineProperty(ByteArray.prototype, 'splice', {
* @param {Number} offset
* @returns {Number}
* @function
* @example var ba = new ByteArray([0,255]);
* @example const ba = new ByteArray([0,255]);
* print(ba[0]); // prints 0
*/

Expand Down Expand Up @@ -675,4 +700,4 @@ Object.defineProperty(ByteArray.prototype, 'splice', {
* silently fails.
* @type Number
* @name ByteString.prototype.length
*/
*/
Loading