@ckb-lumos/toolkit
Advanced tools
Comparing version 0.18.0-rc5 to 0.18.0-rc6
@@ -6,2 +6,4 @@ import JSBI from "jsbi"; | ||
static fromRawString(s: string): Reader; | ||
static isReader(x: unknown): x is Reader; | ||
static from(x: string | ArrayBuffer | Reader | Uint8Array): Reader; | ||
@@ -8,0 +10,0 @@ length(): number; |
@@ -8,4 +8,29 @@ "use strict"; | ||
class ArrayBufferReader { | ||
class BaseReader { | ||
constructor() { | ||
/** | ||
* instanceof would be nice here, but when a user use multi version of Reader that may cause problem | ||
* @example | ||
* const { Reader } = require('ckb-js-toolkit'); // ckb-js-toolkit@0.100.1 | ||
* const { readSomething } = require('other-serializer-lib'); // dependent on ckb-js-toolkit@0.100.0 | ||
* | ||
* readSomething() instanceof Reader; // false | ||
* | ||
* @type {boolean} | ||
* @protected | ||
*/ | ||
this.__isByteLikeReader__ = true; | ||
} | ||
static isReader(x) { | ||
if (x == null) return false; | ||
if (x instanceof BaseReader) return true; | ||
return x.__isByteLikeReader__ === true; | ||
} | ||
} | ||
class ArrayBufferReader extends BaseReader { | ||
constructor(buffer) { | ||
super(); | ||
this.view = new DataView(buffer); | ||
@@ -32,4 +57,5 @@ } | ||
class HexStringReader { | ||
class HexStringReader extends BaseReader { | ||
constructor(string) { | ||
super(); | ||
this.string = string; | ||
@@ -63,4 +89,6 @@ } | ||
class Reader { | ||
class Reader extends BaseReader { | ||
constructor(input) { | ||
super(); | ||
if (input instanceof HexStringReader || input instanceof ArrayBufferReader) { | ||
@@ -71,3 +99,3 @@ return input; | ||
if (typeof input === "string") { | ||
if (!input.startsWith("0x") || input.length % 2 != 0) { | ||
if (!input.startsWith("0x") || input.length % 2 !== 0) { | ||
throw new Error("Hex string must start with 0x, and has even numbered length!"); | ||
@@ -83,5 +111,13 @@ } | ||
if (input instanceof Uint8Array) { | ||
return new ArrayBufferReader(Uint8Array.from(input).buffer); | ||
} | ||
throw new Error("Reader can only accept hex string or ArrayBuffer!"); | ||
} | ||
static from(x) { | ||
return new Reader(x); | ||
} | ||
static fromRawString(string) { | ||
@@ -88,0 +124,0 @@ const buffer = new ArrayBuffer(string.length); |
{ | ||
"name": "@ckb-lumos/toolkit", | ||
"version": "0.18.0-rc5", | ||
"version": "0.18.0-rc6", | ||
"description": "JavaScript toolkits used to build dapps for Nervos CKB", | ||
@@ -38,3 +38,3 @@ "main": "lib/index.js", | ||
}, | ||
"gitHead": "842d5853f8ac8ae6ae74b9c9fa1d99b8aa161b66" | ||
"gitHead": "fd33f15dcd7e5d5d69d9ebd49290e9f90ab688fb" | ||
} |
Sorry, the diff of this file is not supported yet
164190
1401