Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
var assert = require("test/assert");
var Binary = require("binary").Binary,
ByteString = require("binary").ByteString,
ByteArray = require("binary").ByteArray;
exports.testByteArrayConstructorEncodings = function() {
// ByteString(string, charset)
// Convert a string. The ByteString will contain string encoded with charset.
var testString = "hello world";
var b = new ByteArray(testString, "US-ASCII");
assert.isEqual(testString.length, b.length);
b.length = 678;
assert.isEqual(678, b.length);
assert.isEqual(testString.charCodeAt(0), b.get(0));
assert.isEqual(testString.charCodeAt(testString.length-1), b.get(testString.length-1));
assert.isEqual(0, b.get(677));
};
var assert = require("test/assert");
var Binary = require("binary").Binary,
ByteString = require("binary").ByteString,
ByteArray = require("binary").ByteArray;
exports.testByteStringConstructor = function() {
var testArray = [1,2,3,4];
// ByteString()
// Construct an empty byte string.
var b1 = new ByteString();
//assert.isTrue(b1 instanceof Binary, "not instanceof Binary");
assert.isTrue(b1 instanceof ByteString, "not instanceof ByteString");
assert.isEqual(0, b1.length);
b1.length = 123;
assert.isEqual(0, b1.length);
// ByteString(byteString)
var assert = require("test/assert");
var Binary = require("binary").Binary,
ByteString = require("binary").ByteString,
ByteArray = require("binary").ByteArray;
exports.testByteStringConstructorEncodings = function() {
// ByteString(string, charset)
// Convert a string. The ByteString will contain string encoded with charset.
var testString = "hello world";
var b4 = new ByteString(testString, "ASCII");
assert.isEqual(testString.length, b4.length, "6543");
b4.length = 123;
assert.isEqual(testString.length, b4.length, "asdf");
assert.isEqual(testString.charCodeAt(0), b4.get(0), "234");
assert.isEqual(testString.charCodeAt(testString.length-1), b4.get(testString.length-1), "zxcv");
};
exports.testToByteStringConstructorUnicode = function() {
var assert = require("test/assert");
var Binary = require("binary").Binary,
ByteString = require("binary").ByteString,
ByteArray = require("binary").ByteArray;
exports.testByteArrayConstructor = function() {
var testArray = [1,2,3,4],
b;
// ByteArray()
// New, empty ByteArray.
b = new ByteArray();
//assert.isTrue(b instanceof Binary, "not instanceof Binary");
assert.isTrue(b instanceof ByteArray, "not instanceof ByteArray");
assert.isEqual(0, b.length);
b.length = 123;
assert.isEqual(123, b.length);
assert.isEqual(0, b.get(4));
// normalize to node encodings
var encodings = {
"utf8": "utf-8",
"utf16": "utf-16"
};
var normalizeEncoding = exports.normalizeEncoding = function(encoding) {
encoding = encoding || "utf-8";
if (typeof encoding !== "string") return encoding;
encoding = encoding.toLowerCase();
return encodings[encoding] || encoding;
};
var binaryModule = require("binary");
var Binary = exports.Binary = binaryModule.Binary;
var ByteArray = binaryModule.ByteArray;
var ByteString = binaryModule.ByteString;
exports.shim = function () {
// Binary
if (!String.prototype.getBytes) {
String.prototype.getBytes = function(encoding) {
return NativeBinary(this.toString(), normalizeEncoding(encoding));
};
}
// TODO Array.prototype.getBytes
exports.isBinary = function(target) {
return target instanceof binaryModule.Binary;
};
var binary = require("binary");
var Binary = binary.Binary, ByteString = binary.ByteString;
/**
* Returns true if string starts with the given substring
*
* @param {String}
* string the string to search in
* @param {String}
* substring pattern to search for
* @returns {Boolean} true in case it matches the beginning of the string, false
* otherwise
*/
var startsWith = exports.startsWith = function(string, substring) {
return string.indexOf(substring) == 0;
};
/**
function toDatagramPacket(arg) {
if (arg instanceof binary.Binary) {
return new net.DatagramPacket(arg, arg.length);
} else if (typeof arg === "string") {
return new net.DatagramPacket(arg.toByteString(), arg.length);
} else {
throw new Error("Unsupported argument to send: " + arg);
}
}
}