Comparing version 0.2.0 to 0.3.0
@@ -7,6 +7,3 @@ export declare function encodeDate(self: Date): string; | ||
export declare function decodeBase64(str: string): Uint8Array; | ||
export declare function encodeNumner(self: number): string; | ||
export declare function decodeNumber(str: string): number; | ||
export declare function encodeBigInt(self: BigInt): string; | ||
export declare function decodeBigInt(str: string): BigInt; | ||
export declare function encodeBoolean(self: boolean): string; |
@@ -21,16 +21,7 @@ import Base93 from './base93'; | ||
} | ||
export function encodeNumner(self) { | ||
return '͢Number:' + self.toString(); | ||
} | ||
export function decodeNumber(str) { | ||
return Number.parseFloat(str.substring(8)); | ||
} | ||
export function encodeBigInt(self) { | ||
return '͢BigInt:' + self.toString(); | ||
return '͢n:' + self.toString(); | ||
} | ||
export function decodeBigInt(str) { | ||
return BigInt(str.substring(8)); | ||
return BigInt(str.substring(3)); | ||
} | ||
export function encodeBoolean(self) { | ||
return '͢' + self.toString(); | ||
} |
@@ -5,3 +5,2 @@ interface Options { | ||
encodeBigInt?: boolean; | ||
encodePrimitive?: boolean; | ||
} | ||
@@ -11,3 +10,2 @@ export default class Arrow { | ||
private _decodeTable; | ||
encodePrimitive: boolean; | ||
constructor(options?: Options); | ||
@@ -14,0 +12,0 @@ registerRaw(key: string, type: object, encoder: (self: object) => string, decoder: (str: string) => any): void; |
104
es/index.js
@@ -1,2 +0,2 @@ | ||
import { decodeBase64, decodeBigInt, decodeDate, decodeNumber, decodeUint8Array, encodeBase64, encodeBigInt, encodeBoolean, encodeDate, encodeNumner, encodeUint8Array } from "./codec"; | ||
import { decodeBase64, decodeBigInt, decodeDate, decodeUint8Array, encodeBase64, encodeBigInt, encodeDate, encodeUint8Array } from "./codec"; | ||
const UNDEFINED_JSON = '"͢"'; | ||
@@ -11,4 +11,3 @@ const UNDEFINED = '͢'; | ||
this._decodeTable = {}; | ||
this.encodePrimitive = false; | ||
if ((options === null || options === void 0 ? void 0 : options.encodeBinary) === 'base64') { | ||
if (options?.encodeBinary === 'base64') { | ||
this.registerRaw('B64', Uint8Array, encodeBase64, decodeBase64); | ||
@@ -18,3 +17,3 @@ // only register base93 decoder, not encoder | ||
} | ||
else if ((options === null || options === void 0 ? void 0 : options.encodeBinary) !== false) { | ||
else if (options?.encodeBinary !== false) { | ||
this.registerRaw('Bin', Uint8Array, encodeUint8Array, decodeUint8Array); | ||
@@ -24,12 +23,8 @@ // only register base64 decoder, not encoder | ||
} | ||
if ((options === null || options === void 0 ? void 0 : options.encodeDate) !== false) { | ||
if (options?.encodeDate !== false) { | ||
this.registerRaw('Date', Date, encodeDate, decodeDate); | ||
} | ||
if ((options === null || options === void 0 ? void 0 : options.encodeBigInt) !== false) { | ||
this.registerRaw('BigInt', BigInt, encodeBigInt, decodeBigInt); | ||
if (options?.encodeBigInt !== false) { | ||
this.registerRaw('n', BigInt, encodeBigInt, decodeBigInt); | ||
} | ||
if (options === null || options === void 0 ? void 0 : options.encodePrimitive) { | ||
this.encodePrimitive = true; | ||
this.registerRaw('Number', Number, null, decodeNumber); | ||
} | ||
} | ||
@@ -52,3 +47,3 @@ registerRaw(key, type, encoder, decoder) { | ||
if (typeof value === 'string' && value.charCodeAt(0) === 0x362) { | ||
if (value.length < 7) { | ||
if (value.length < 6) { | ||
switch (value) { | ||
@@ -61,8 +56,2 @@ case '͢NaN': | ||
return -Infinity; | ||
case '͢true': | ||
return true; | ||
case '͢false': | ||
return false; | ||
case '͢null': | ||
return null; | ||
} | ||
@@ -232,23 +221,23 @@ } | ||
encode(input) { | ||
if (typeof input === 'string' && input.charCodeAt(0) === 0x362) { | ||
// double arrow encode this string | ||
return `͢${input}`; | ||
} | ||
const result = this.replacer(null, input, null); | ||
if (this.encodePrimitive) { | ||
switch (typeof result) { | ||
case "string": | ||
return result; | ||
case "number": | ||
return encodeNumner(input); | ||
case "boolean": | ||
return encodeBoolean(input); | ||
case 'undefined': | ||
return UNDEFINED; | ||
default: | ||
if (result === null) { | ||
return '͢null'; | ||
} | ||
} | ||
switch (typeof result) { | ||
case "string": | ||
return result; | ||
case "number": | ||
return `͢${input}`; | ||
case "boolean": | ||
return `͢${input}`; | ||
case 'undefined': | ||
return UNDEFINED; | ||
case "bigint": | ||
return encodeBigInt(input); | ||
} | ||
else if (typeof result === 'string') { | ||
return result; | ||
if (result === null) { | ||
return '͢null'; | ||
} | ||
if (Array.isArray(input) || (input === null || input === void 0 ? void 0 : input.constructor) === Object) { | ||
if (Array.isArray(input) || input?.constructor === Object) { | ||
return `͢${this.encodeJSON(input)}`; | ||
@@ -259,6 +248,43 @@ } | ||
decode(str) { | ||
if (str.startsWith('͢[') || str.startsWith('͢{')) { | ||
return this.decodeJSON(str.substring(1)); | ||
if (typeof str === 'string' && str.charCodeAt(0) === 0x362) { | ||
if (str.length < 7) { | ||
switch (str) { | ||
case '͢NaN': | ||
return NaN; | ||
case '͢Inf': | ||
return Infinity; | ||
case '͢-Inf': | ||
return -Infinity; | ||
case '͢true': | ||
return true; | ||
case '͢false': | ||
return false; | ||
case '͢null': | ||
return null; | ||
case '͢': | ||
return undefined; | ||
} | ||
} | ||
let nextChar = str.charAt(1); | ||
if (nextChar === '[' || nextChar === '{') { | ||
return this.decodeJSON(str.substring(1)); | ||
} | ||
if ((nextChar >= '0' && nextChar <= '9') || nextChar === '-') { | ||
return Number.parseFloat(str.substring(1)); | ||
} | ||
if (nextChar === '͢') { | ||
// double encoded string, remove first arrow | ||
return str.substring(1); | ||
} | ||
let colonPos = str.indexOf(':'); | ||
if (colonPos > -1) { | ||
let key = str.substring(1, colonPos); | ||
let decoder = this._decodeTable[key]; | ||
if (decoder) { | ||
return decoder(str); | ||
} | ||
} | ||
return undefined; | ||
} | ||
return this.reviver(null, str); | ||
return str; | ||
} | ||
@@ -265,0 +291,0 @@ encodeJSON(input, space, sortKeys = false) { |
@@ -7,6 +7,3 @@ export declare function encodeDate(self: Date): string; | ||
export declare function decodeBase64(str: string): Uint8Array; | ||
export declare function encodeNumner(self: number): string; | ||
export declare function decodeNumber(str: string): number; | ||
export declare function encodeBigInt(self: BigInt): string; | ||
export declare function decodeBigInt(str: string): BigInt; | ||
export declare function encodeBoolean(self: boolean): string; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.encodeBoolean = exports.decodeBigInt = exports.encodeBigInt = exports.decodeNumber = exports.encodeNumner = exports.decodeBase64 = exports.encodeBase64 = exports.decodeUint8Array = exports.encodeUint8Array = exports.decodeDate = exports.encodeDate = void 0; | ||
exports.decodeBigInt = exports.encodeBigInt = exports.decodeBase64 = exports.encodeBase64 = exports.decodeUint8Array = exports.encodeUint8Array = exports.decodeDate = exports.encodeDate = void 0; | ||
const base93_1 = require("./base93"); | ||
@@ -30,21 +30,9 @@ const Base64 = require("base64-js"); | ||
exports.decodeBase64 = decodeBase64; | ||
function encodeNumner(self) { | ||
return '͢Number:' + self.toString(); | ||
} | ||
exports.encodeNumner = encodeNumner; | ||
function decodeNumber(str) { | ||
return Number.parseFloat(str.substring(8)); | ||
} | ||
exports.decodeNumber = decodeNumber; | ||
function encodeBigInt(self) { | ||
return '͢BigInt:' + self.toString(); | ||
return '͢n:' + self.toString(); | ||
} | ||
exports.encodeBigInt = encodeBigInt; | ||
function decodeBigInt(str) { | ||
return BigInt(str.substring(8)); | ||
return BigInt(str.substring(3)); | ||
} | ||
exports.decodeBigInt = decodeBigInt; | ||
function encodeBoolean(self) { | ||
return '͢' + self.toString(); | ||
} | ||
exports.encodeBoolean = encodeBoolean; |
@@ -5,3 +5,2 @@ interface Options { | ||
encodeBigInt?: boolean; | ||
encodePrimitive?: boolean; | ||
} | ||
@@ -11,3 +10,2 @@ export default class Arrow { | ||
private _decodeTable; | ||
encodePrimitive: boolean; | ||
constructor(options?: Options); | ||
@@ -14,0 +12,0 @@ registerRaw(key: string, type: object, encoder: (self: object) => string, decoder: (str: string) => any): void; |
102
lib/index.js
@@ -13,4 +13,3 @@ "use strict"; | ||
this._decodeTable = {}; | ||
this.encodePrimitive = false; | ||
if ((options === null || options === void 0 ? void 0 : options.encodeBinary) === 'base64') { | ||
if (options?.encodeBinary === 'base64') { | ||
this.registerRaw('B64', Uint8Array, codec_1.encodeBase64, codec_1.decodeBase64); | ||
@@ -20,3 +19,3 @@ // only register base93 decoder, not encoder | ||
} | ||
else if ((options === null || options === void 0 ? void 0 : options.encodeBinary) !== false) { | ||
else if (options?.encodeBinary !== false) { | ||
this.registerRaw('Bin', Uint8Array, codec_1.encodeUint8Array, codec_1.decodeUint8Array); | ||
@@ -26,12 +25,8 @@ // only register base64 decoder, not encoder | ||
} | ||
if ((options === null || options === void 0 ? void 0 : options.encodeDate) !== false) { | ||
if (options?.encodeDate !== false) { | ||
this.registerRaw('Date', Date, codec_1.encodeDate, codec_1.decodeDate); | ||
} | ||
if ((options === null || options === void 0 ? void 0 : options.encodeBigInt) !== false) { | ||
this.registerRaw('BigInt', BigInt, codec_1.encodeBigInt, codec_1.decodeBigInt); | ||
if (options?.encodeBigInt !== false) { | ||
this.registerRaw('n', BigInt, codec_1.encodeBigInt, codec_1.decodeBigInt); | ||
} | ||
if (options === null || options === void 0 ? void 0 : options.encodePrimitive) { | ||
this.encodePrimitive = true; | ||
this.registerRaw('Number', Number, null, codec_1.decodeNumber); | ||
} | ||
} | ||
@@ -54,3 +49,3 @@ registerRaw(key, type, encoder, decoder) { | ||
if (typeof value === 'string' && value.charCodeAt(0) === 0x362) { | ||
if (value.length < 7) { | ||
if (value.length < 6) { | ||
switch (value) { | ||
@@ -63,8 +58,2 @@ case '͢NaN': | ||
return -Infinity; | ||
case '͢true': | ||
return true; | ||
case '͢false': | ||
return false; | ||
case '͢null': | ||
return null; | ||
} | ||
@@ -234,23 +223,23 @@ } | ||
encode(input) { | ||
if (typeof input === 'string' && input.charCodeAt(0) === 0x362) { | ||
// double arrow encode this string | ||
return `͢${input}`; | ||
} | ||
const result = this.replacer(null, input, null); | ||
if (this.encodePrimitive) { | ||
switch (typeof result) { | ||
case "string": | ||
return result; | ||
case "number": | ||
return (0, codec_1.encodeNumner)(input); | ||
case "boolean": | ||
return (0, codec_1.encodeBoolean)(input); | ||
case 'undefined': | ||
return UNDEFINED; | ||
default: | ||
if (result === null) { | ||
return '͢null'; | ||
} | ||
} | ||
switch (typeof result) { | ||
case "string": | ||
return result; | ||
case "number": | ||
return `͢${input}`; | ||
case "boolean": | ||
return `͢${input}`; | ||
case 'undefined': | ||
return UNDEFINED; | ||
case "bigint": | ||
return (0, codec_1.encodeBigInt)(input); | ||
} | ||
else if (typeof result === 'string') { | ||
return result; | ||
if (result === null) { | ||
return '͢null'; | ||
} | ||
if (Array.isArray(input) || (input === null || input === void 0 ? void 0 : input.constructor) === Object) { | ||
if (Array.isArray(input) || input?.constructor === Object) { | ||
return `͢${this.encodeJSON(input)}`; | ||
@@ -261,6 +250,43 @@ } | ||
decode(str) { | ||
if (str.startsWith('͢[') || str.startsWith('͢{')) { | ||
return this.decodeJSON(str.substring(1)); | ||
if (typeof str === 'string' && str.charCodeAt(0) === 0x362) { | ||
if (str.length < 7) { | ||
switch (str) { | ||
case '͢NaN': | ||
return NaN; | ||
case '͢Inf': | ||
return Infinity; | ||
case '͢-Inf': | ||
return -Infinity; | ||
case '͢true': | ||
return true; | ||
case '͢false': | ||
return false; | ||
case '͢null': | ||
return null; | ||
case '͢': | ||
return undefined; | ||
} | ||
} | ||
let nextChar = str.charAt(1); | ||
if (nextChar === '[' || nextChar === '{') { | ||
return this.decodeJSON(str.substring(1)); | ||
} | ||
if ((nextChar >= '0' && nextChar <= '9') || nextChar === '-') { | ||
return Number.parseFloat(str.substring(1)); | ||
} | ||
if (nextChar === '͢') { | ||
// double encoded string, remove first arrow | ||
return str.substring(1); | ||
} | ||
let colonPos = str.indexOf(':'); | ||
if (colonPos > -1) { | ||
let key = str.substring(1, colonPos); | ||
let decoder = this._decodeTable[key]; | ||
if (decoder) { | ||
return decoder(str); | ||
} | ||
} | ||
return undefined; | ||
} | ||
return this.reviver(null, str); | ||
return str; | ||
} | ||
@@ -267,0 +293,0 @@ encodeJSON(input, space, sortKeys = false) { |
{ | ||
"name": "arrow-code", | ||
"version": "0.2.0", | ||
"version": "0.3.0", | ||
"description": "Arrow Code", | ||
@@ -5,0 +5,0 @@ "main": "lib/index", |
@@ -14,5 +14,7 @@ # Arrow Code | ||
NaN, | ||
-Infinity, | ||
-Infinity, | ||
9007199254740997n, | ||
new Date(), | ||
new Uint8Array([1,2,3,4]) | ||
new Uint8Array([1,2,3,4]), | ||
undefined | ||
], 1); | ||
@@ -23,4 +25,6 @@ // returns: | ||
"͢-Inf", | ||
"͢Date:2018-02-07T19:07:18.207Z", | ||
"͢Bin:wFg{A" | ||
"͢n:9007199254740997", // BigInt | ||
"͢Date:2018-02-07T19:07:18.207Z", // Date | ||
"͢Bin:wFg{A", // Uint8Array | ||
"͢" // undefined | ||
] | ||
@@ -53,3 +57,3 @@ ``` | ||
new Arrow({ | ||
// whether to encode Binary (Ui, default true | ||
// whether to encode Binary ( Uint8Array ), default true, which uses Base93 encoding | ||
encodeBinary?: boolean | 'base64', | ||
@@ -62,6 +66,2 @@ | ||
encodeBigInt?: boolean, | ||
// whether to encode number, boolean and null, default false | ||
// no effect in JSON mode | ||
encodePrimitive?: boolean, | ||
}) | ||
@@ -99,3 +99,3 @@ ``` | ||
myJson.stringify(new MyClass("hello")); | ||
myJson.encodeJSON(new MyClass("hello")); | ||
// "͢My:hello" | ||
@@ -116,4 +116,4 @@ ``` | ||
arrow.stringify({binary: new Uint8Array([1,2,3,4])}); | ||
arrow.encodeJSON({binary: new Uint8Array([1,2,3,4])}); | ||
// {"binary":"͢B64:AQIDBA=="} | ||
``` |
51269
953