Add @cosmjs/encoding and @cosmjs/math

This commit is contained in:
Simon Warta
2020-06-09 17:46:03 +02:00
parent 4cca97651b
commit 3cfad7a541
53 changed files with 2176 additions and 0 deletions
+1
View File
@@ -1,2 +1,3 @@
export { assert } from "./assert";
export { sleep } from "./sleep";
export { isNonNullObject, isUint8Array } from "./typechecks";
+58
View File
@@ -0,0 +1,58 @@
import { isNonNullObject, isUint8Array } from "./typechecks";
describe("typechecks", () => {
describe("isNonNullObject", () => {
it("returns true for objects", () => {
expect(isNonNullObject({})).toEqual(true);
expect(isNonNullObject({ foo: 123 })).toEqual(true);
expect(isNonNullObject(new Uint8Array([]))).toEqual(true);
});
it("returns true for arrays", () => {
// > object is a type that represents the non-primitive type, i.e.
// > anything that is not number, string, boolean, symbol, null, or undefined.
// https://www.typescriptlang.org/docs/handbook/basic-types.html#object
expect(isNonNullObject([])).toEqual(true);
});
it("returns false for null", () => {
expect(isNonNullObject(null)).toEqual(false);
});
it("returns false for other kind of data", () => {
expect(isNonNullObject(undefined)).toEqual(false);
expect(isNonNullObject("abc")).toEqual(false);
expect(isNonNullObject(123)).toEqual(false);
expect(isNonNullObject(true)).toEqual(false);
});
});
describe("isUint8Array", () => {
it("returns true for Uint8Arrays", () => {
expect(isUint8Array(new Uint8Array())).toEqual(true);
expect(isUint8Array(new Uint8Array([1, 2, 3]))).toEqual(true);
});
it("returns false for Buffer", () => {
// One could start a big debate about whether or not a Buffer is a Uint8Array, which
// required a definition of "is a" in a languages that has no proper object oriented
// programming support.
//
// In all our software we use Uint8Array for storing binary data and copy Buffers into
// new Uint8Array to make deep equality checks work and to ensure our code works the same
// way in browsers and Node.js. So our expectation is: _a Buffer is not an Uint8Array_.
expect(isUint8Array(Buffer.from(""))).toEqual(false);
});
it("returns false for other kind of data", () => {
expect(isUint8Array(undefined)).toEqual(false);
expect(isUint8Array("abc")).toEqual(false);
expect(isUint8Array(123)).toEqual(false);
expect(isUint8Array(true)).toEqual(false);
expect(isUint8Array([])).toEqual(false);
expect(isUint8Array(new Int8Array())).toEqual(false);
expect(isUint8Array(new Uint16Array())).toEqual(false);
});
});
});
+26
View File
@@ -0,0 +1,26 @@
/**
* Checks if data is a non-null object (i.e. matches the TypeScript object type)
*/
export function isNonNullObject(data: unknown): data is object {
return typeof data === "object" && data !== null;
}
/**
* Checks if data is an Uint8Array. Note: Buffer is treated as not a Uint8Array
*/
export function isUint8Array(data: unknown): data is Uint8Array {
if (!isNonNullObject(data)) return false;
// Avoid instanceof check which is unreliable in some JS environments
// https://medium.com/@simonwarta/limitations-of-the-instanceof-operator-f4bcdbe7a400
// Use check that was discussed in https://github.com/crypto-browserify/pbkdf2/pull/81
if (Object.prototype.toString.call(data) !== "[object Uint8Array]") return false;
if (typeof Buffer !== "undefined" && typeof Buffer.isBuffer !== "undefined") {
// Buffer.isBuffer is available at runtime
if (Buffer.isBuffer(data)) return false;
}
return true;
}