Comparing version 0.1.0 to 0.2.0
@@ -7,1 +7,6 @@ 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,1 +21,16 @@ 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(); | ||
} | ||
export function decodeBigInt(str) { | ||
return BigInt(str.substring(8)); | ||
} | ||
export function encodeBoolean(self) { | ||
return '͢' + self.toString(); | ||
} |
interface Options { | ||
binaryFormat: 'base93' | 'base64'; | ||
encodeBinary?: boolean | 'base64'; | ||
encodeDate?: boolean; | ||
encodeBigInt?: boolean; | ||
encodePrimitive?: boolean; | ||
} | ||
export default class JsonEsc { | ||
export default class Arrow { | ||
private _encodeTable; | ||
private _decodeTable; | ||
encodePrimitive: boolean; | ||
constructor(options?: Options); | ||
registerDate(): void; | ||
registerRaw(key: string, type: object, encoder: (self: object) => string, decoder: (str: string) => object): void; | ||
registerRaw(key: string, type: object, encoder: (self: object) => string, decoder: (str: string) => any): void; | ||
register(key: string, type: object, encoder: (self: object) => string, decoder: (str: string) => object): void; | ||
reviver(key: string, value: any): any; | ||
replacer(key: string, value: any, parent: any): any; | ||
parse(str: string): any; | ||
stringify(input: any, space?: number): string; | ||
stringifySorted(input: any, space?: number): string; | ||
encode(input: any): string; | ||
decode(str: string): any; | ||
reviver(key: string, value: unknown): unknown; | ||
replacer(key: string, value: unknown, parent: unknown): unknown; | ||
parse(str: string): unknown; | ||
stringify(input: unknown, space?: number): string; | ||
stringifySorted(input: unknown, space?: number): string; | ||
encode(input: unknown): string; | ||
decode(str: string): unknown; | ||
encodeJSON(input: unknown, space?: number, sortKeys?: boolean): string; | ||
decodeJSON(str: string): unknown; | ||
private static defaultEncoder; | ||
static parse(str: string): any; | ||
static stringify(input: any, space?: number, sortKeys?: boolean): string; | ||
static decodeJSON(str: string): unknown; | ||
static encodeJSON(input: unknown, space?: number, sortKeys?: boolean): string; | ||
static encode(input: unknown): string; | ||
static decode(str: string): unknown; | ||
} | ||
export {}; |
113
es/index.js
@@ -1,22 +0,33 @@ | ||
import * as Codec from './codec'; | ||
const UNDEFINED_ENCODED = '"͢"'; | ||
import { decodeBase64, decodeBigInt, decodeDate, decodeNumber, decodeUint8Array, encodeBase64, encodeBigInt, encodeBoolean, encodeDate, encodeNumner, encodeUint8Array } from "./codec"; | ||
const UNDEFINED_JSON = '"͢"'; | ||
const UNDEFINED = '͢'; | ||
export default class JsonEsc { | ||
function hasToArrow(obj) { | ||
return 'toArrow' in obj; | ||
} | ||
export default class Arrow { | ||
constructor(options) { | ||
this._encodeTable = new Map(); | ||
this._decodeTable = {}; | ||
if ((options === null || options === void 0 ? void 0 : options.binaryFormat) === 'base64') { | ||
this.registerRaw('B64', Uint8Array, Codec.encodeBase64, Codec.decodeBase64); | ||
this.encodePrimitive = false; | ||
if ((options === null || options === void 0 ? void 0 : options.encodeBinary) === 'base64') { | ||
this.registerRaw('B64', Uint8Array, encodeBase64, decodeBase64); | ||
// only register base93 decoder, not encoder | ||
this.registerRaw('Bin', null, null, Codec.decodeUint8Array); | ||
this.registerRaw('Bin', null, null, decodeUint8Array); | ||
} | ||
else { | ||
this.registerRaw('Bin', Uint8Array, Codec.encodeUint8Array, Codec.decodeUint8Array); | ||
else if ((options === null || options === void 0 ? void 0 : options.encodeBinary) !== false) { | ||
this.registerRaw('Bin', Uint8Array, encodeUint8Array, decodeUint8Array); | ||
// only register base64 decoder, not encoder | ||
this.registerRaw('B64', null, null, Codec.decodeBase64); | ||
this.registerRaw('B64', null, null, decodeBase64); | ||
} | ||
if ((options === null || options === void 0 ? void 0 : 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 === null || options === void 0 ? void 0 : options.encodePrimitive) { | ||
this.encodePrimitive = true; | ||
this.registerRaw('Number', Number, null, decodeNumber); | ||
} | ||
} | ||
registerDate() { | ||
this.registerRaw('Date', Date, Codec.encodeDate, Codec.decodeDate); | ||
} | ||
registerRaw(key, type, encoder, decoder) { | ||
@@ -34,7 +45,7 @@ if (type && encoder) { | ||
this._encodeTable.set(type, (self) => `${prefix}${encoder(self)}`); | ||
this._decodeTable[key] = (str) => decoder(str.substr(prefixLen)); | ||
this._decodeTable[key] = (str) => decoder(str.substring(prefixLen)); | ||
} | ||
reviver(key, value) { | ||
if (typeof value === 'string' && value.charCodeAt(0) === 0x362) { | ||
if (value.length < 6) { | ||
if (value.length < 7) { | ||
switch (value) { | ||
@@ -87,4 +98,4 @@ case '͢NaN': | ||
} | ||
else if ('toJsonEsc' in value) { | ||
return value.toJsonEsc(); | ||
else if (hasToArrow(value)) { | ||
return value.toArrow(); | ||
} | ||
@@ -95,4 +106,4 @@ } | ||
case "function": { | ||
if ('toJsonEsc' in value) { | ||
return value.toJsonEsc(); | ||
if (hasToArrow(value)) { | ||
return value.toArrow(); | ||
} | ||
@@ -114,3 +125,3 @@ return undefined; | ||
if (input === undefined) { | ||
return UNDEFINED_ENCODED; | ||
return UNDEFINED_JSON; | ||
} | ||
@@ -136,3 +147,3 @@ let toJSONCache = new Map(); | ||
if (input === undefined) { | ||
return UNDEFINED_ENCODED; | ||
return UNDEFINED_JSON; | ||
} | ||
@@ -190,3 +201,3 @@ let spacesCached = 0; | ||
if (val === undefined) { | ||
items.push(UNDEFINED_ENCODED); | ||
items.push(UNDEFINED_JSON); | ||
} | ||
@@ -222,23 +233,57 @@ else { | ||
encode(input) { | ||
return this.reviver(null, 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'; | ||
} | ||
} | ||
} | ||
else if (typeof result === 'string') { | ||
return result; | ||
} | ||
if (Array.isArray(input) || (input === null || input === void 0 ? void 0 : input.constructor) === Object) { | ||
return `͢${this.encodeJSON(input)}`; | ||
} | ||
return null; | ||
} | ||
decode(str) { | ||
return this.replacer(null, str, null); | ||
if (str.startsWith('͢[') || str.startsWith('͢{')) { | ||
return this.decodeJSON(str.substring(1)); | ||
} | ||
return this.reviver(null, str); | ||
} | ||
static parse(str) { | ||
return JsonEsc.defaultEncoder.parse(str); | ||
} | ||
static stringify(input, space, sortKeys = false) { | ||
encodeJSON(input, space, sortKeys = false) { | ||
if (sortKeys === true) { | ||
return JsonEsc.defaultEncoder.stringifySorted(input, space); | ||
return this.stringifySorted(input, space); | ||
} | ||
else { | ||
return JsonEsc.defaultEncoder.stringify(input, space); | ||
return this.stringify(input, space); | ||
} | ||
} | ||
decodeJSON(str) { | ||
return this.parse(str); | ||
} | ||
static decodeJSON(str) { | ||
return Arrow.defaultEncoder.parse(str); | ||
} | ||
static encodeJSON(input, space, sortKeys = false) { | ||
return Arrow.defaultEncoder.encodeJSON(input, space, sortKeys); | ||
} | ||
static encode(input) { | ||
return Arrow.defaultEncoder.encode(input); | ||
} | ||
static decode(str) { | ||
return Arrow.defaultEncoder.decode(str); | ||
} | ||
} | ||
JsonEsc.defaultEncoder = (() => { | ||
let defaultEncoder = new JsonEsc(); | ||
defaultEncoder.registerDate(); | ||
return defaultEncoder; | ||
})(); | ||
Arrow.defaultEncoder = (() => new Arrow())(); |
@@ -7,1 +7,6 @@ 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.decodeBase64 = exports.encodeBase64 = exports.decodeUint8Array = exports.encodeUint8Array = exports.decodeDate = exports.encodeDate = void 0; | ||
exports.encodeBoolean = exports.decodeBigInt = exports.encodeBigInt = exports.decodeNumber = exports.encodeNumner = exports.decodeBase64 = exports.encodeBase64 = exports.decodeUint8Array = exports.encodeUint8Array = exports.decodeDate = exports.encodeDate = void 0; | ||
const base93_1 = require("./base93"); | ||
@@ -30,1 +30,21 @@ 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(); | ||
} | ||
exports.encodeBigInt = encodeBigInt; | ||
function decodeBigInt(str) { | ||
return BigInt(str.substring(8)); | ||
} | ||
exports.decodeBigInt = decodeBigInt; | ||
function encodeBoolean(self) { | ||
return '͢' + self.toString(); | ||
} | ||
exports.encodeBoolean = encodeBoolean; |
interface Options { | ||
binaryFormat: 'base93' | 'base64'; | ||
encodeBinary?: boolean | 'base64'; | ||
encodeDate?: boolean; | ||
encodeBigInt?: boolean; | ||
encodePrimitive?: boolean; | ||
} | ||
export default class JsonEsc { | ||
export default class Arrow { | ||
private _encodeTable; | ||
private _decodeTable; | ||
encodePrimitive: boolean; | ||
constructor(options?: Options); | ||
registerDate(): void; | ||
registerRaw(key: string, type: object, encoder: (self: object) => string, decoder: (str: string) => object): void; | ||
registerRaw(key: string, type: object, encoder: (self: object) => string, decoder: (str: string) => any): void; | ||
register(key: string, type: object, encoder: (self: object) => string, decoder: (str: string) => object): void; | ||
reviver(key: string, value: any): any; | ||
replacer(key: string, value: any, parent: any): any; | ||
parse(str: string): any; | ||
stringify(input: any, space?: number): string; | ||
stringifySorted(input: any, space?: number): string; | ||
encode(input: any): string; | ||
decode(str: string): any; | ||
reviver(key: string, value: unknown): unknown; | ||
replacer(key: string, value: unknown, parent: unknown): unknown; | ||
parse(str: string): unknown; | ||
stringify(input: unknown, space?: number): string; | ||
stringifySorted(input: unknown, space?: number): string; | ||
encode(input: unknown): string; | ||
decode(str: string): unknown; | ||
encodeJSON(input: unknown, space?: number, sortKeys?: boolean): string; | ||
decodeJSON(str: string): unknown; | ||
private static defaultEncoder; | ||
static parse(str: string): any; | ||
static stringify(input: any, space?: number, sortKeys?: boolean): string; | ||
static decodeJSON(str: string): unknown; | ||
static encodeJSON(input: unknown, space?: number, sortKeys?: boolean): string; | ||
static encode(input: unknown): string; | ||
static decode(str: string): unknown; | ||
} | ||
export {}; |
115
lib/index.js
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const Codec = require("./codec"); | ||
const UNDEFINED_ENCODED = '"͢"'; | ||
const codec_1 = require("./codec"); | ||
const UNDEFINED_JSON = '"͢"'; | ||
const UNDEFINED = '͢'; | ||
class JsonEsc { | ||
function hasToArrow(obj) { | ||
return 'toArrow' in obj; | ||
} | ||
class Arrow { | ||
constructor(options) { | ||
this._encodeTable = new Map(); | ||
this._decodeTable = {}; | ||
if ((options === null || options === void 0 ? void 0 : options.binaryFormat) === 'base64') { | ||
this.registerRaw('B64', Uint8Array, Codec.encodeBase64, Codec.decodeBase64); | ||
this.encodePrimitive = false; | ||
if ((options === null || options === void 0 ? void 0 : options.encodeBinary) === 'base64') { | ||
this.registerRaw('B64', Uint8Array, codec_1.encodeBase64, codec_1.decodeBase64); | ||
// only register base93 decoder, not encoder | ||
this.registerRaw('Bin', null, null, Codec.decodeUint8Array); | ||
this.registerRaw('Bin', null, null, codec_1.decodeUint8Array); | ||
} | ||
else { | ||
this.registerRaw('Bin', Uint8Array, Codec.encodeUint8Array, Codec.decodeUint8Array); | ||
else if ((options === null || options === void 0 ? void 0 : options.encodeBinary) !== false) { | ||
this.registerRaw('Bin', Uint8Array, codec_1.encodeUint8Array, codec_1.decodeUint8Array); | ||
// only register base64 decoder, not encoder | ||
this.registerRaw('B64', null, null, Codec.decodeBase64); | ||
this.registerRaw('B64', null, null, codec_1.decodeBase64); | ||
} | ||
if ((options === null || options === void 0 ? void 0 : 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 === null || options === void 0 ? void 0 : options.encodePrimitive) { | ||
this.encodePrimitive = true; | ||
this.registerRaw('Number', Number, null, codec_1.decodeNumber); | ||
} | ||
} | ||
registerDate() { | ||
this.registerRaw('Date', Date, Codec.encodeDate, Codec.decodeDate); | ||
} | ||
registerRaw(key, type, encoder, decoder) { | ||
@@ -36,7 +47,7 @@ if (type && encoder) { | ||
this._encodeTable.set(type, (self) => `${prefix}${encoder(self)}`); | ||
this._decodeTable[key] = (str) => decoder(str.substr(prefixLen)); | ||
this._decodeTable[key] = (str) => decoder(str.substring(prefixLen)); | ||
} | ||
reviver(key, value) { | ||
if (typeof value === 'string' && value.charCodeAt(0) === 0x362) { | ||
if (value.length < 6) { | ||
if (value.length < 7) { | ||
switch (value) { | ||
@@ -89,4 +100,4 @@ case '͢NaN': | ||
} | ||
else if ('toJsonEsc' in value) { | ||
return value.toJsonEsc(); | ||
else if (hasToArrow(value)) { | ||
return value.toArrow(); | ||
} | ||
@@ -97,4 +108,4 @@ } | ||
case "function": { | ||
if ('toJsonEsc' in value) { | ||
return value.toJsonEsc(); | ||
if (hasToArrow(value)) { | ||
return value.toArrow(); | ||
} | ||
@@ -116,3 +127,3 @@ return undefined; | ||
if (input === undefined) { | ||
return UNDEFINED_ENCODED; | ||
return UNDEFINED_JSON; | ||
} | ||
@@ -138,3 +149,3 @@ let toJSONCache = new Map(); | ||
if (input === undefined) { | ||
return UNDEFINED_ENCODED; | ||
return UNDEFINED_JSON; | ||
} | ||
@@ -192,3 +203,3 @@ let spacesCached = 0; | ||
if (val === undefined) { | ||
items.push(UNDEFINED_ENCODED); | ||
items.push(UNDEFINED_JSON); | ||
} | ||
@@ -224,24 +235,58 @@ else { | ||
encode(input) { | ||
return this.reviver(null, 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'; | ||
} | ||
} | ||
} | ||
else if (typeof result === 'string') { | ||
return result; | ||
} | ||
if (Array.isArray(input) || (input === null || input === void 0 ? void 0 : input.constructor) === Object) { | ||
return `͢${this.encodeJSON(input)}`; | ||
} | ||
return null; | ||
} | ||
decode(str) { | ||
return this.replacer(null, str, null); | ||
if (str.startsWith('͢[') || str.startsWith('͢{')) { | ||
return this.decodeJSON(str.substring(1)); | ||
} | ||
return this.reviver(null, str); | ||
} | ||
static parse(str) { | ||
return JsonEsc.defaultEncoder.parse(str); | ||
} | ||
static stringify(input, space, sortKeys = false) { | ||
encodeJSON(input, space, sortKeys = false) { | ||
if (sortKeys === true) { | ||
return JsonEsc.defaultEncoder.stringifySorted(input, space); | ||
return this.stringifySorted(input, space); | ||
} | ||
else { | ||
return JsonEsc.defaultEncoder.stringify(input, space); | ||
return this.stringify(input, space); | ||
} | ||
} | ||
decodeJSON(str) { | ||
return this.parse(str); | ||
} | ||
static decodeJSON(str) { | ||
return Arrow.defaultEncoder.parse(str); | ||
} | ||
static encodeJSON(input, space, sortKeys = false) { | ||
return Arrow.defaultEncoder.encodeJSON(input, space, sortKeys); | ||
} | ||
static encode(input) { | ||
return Arrow.defaultEncoder.encode(input); | ||
} | ||
static decode(str) { | ||
return Arrow.defaultEncoder.decode(str); | ||
} | ||
} | ||
exports.default = JsonEsc; | ||
JsonEsc.defaultEncoder = (() => { | ||
let defaultEncoder = new JsonEsc(); | ||
defaultEncoder.registerDate(); | ||
return defaultEncoder; | ||
})(); | ||
exports.default = Arrow; | ||
Arrow.defaultEncoder = (() => new Arrow())(); |
{ | ||
"name": "arrow-code", | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
"description": "Arrow Code", | ||
@@ -5,0 +5,0 @@ "main": "lib/index", |
# Arrow Code | ||
Arrow Code use unicode combining character "͢ " in string to store types that normally not allowed in JSON | ||
Arrow Code use unicode [combining character "͢ "](https://www.compart.com/en/unicode/U+0362) in string to store types that normally not allowed in JSON | ||
[unicode character 0x362](https://www.compart.com/en/unicode/U+0362) is used for string escaping | ||
#### examples | ||
@@ -14,3 +12,3 @@ | ||
// encode as JSON | ||
Arrow.stringify( [ | ||
Arrow.encodeJSON( [ | ||
NaN, | ||
@@ -31,2 +29,3 @@ -Infinity, | ||
## Advantage | ||
(Arrow.encodeJSON vs MsgPack and BSON) | ||
@@ -51,9 +50,29 @@ Arrow Code allows additional data types to be serialized in JSON, such as binary date (Uint8Array) and Date, while still keeps the verbose nature of JSON.<br> | ||
## API | ||
## Constructor | ||
```typescript | ||
new Arrow({ | ||
// whether to encode Binary (Ui, default true | ||
encodeBinary?: boolean | 'base64', | ||
```typescript | ||
Arrow.stringify(inpt:any, space?:number, sortKeys?:boolean = false); | ||
Arrow.parse(inpt:string); | ||
// whether to encode Date, default true | ||
encodeDate?: boolean, | ||
// whether to encode BigInt, default true | ||
encodeBigInt?: boolean, | ||
// whether to encode number, boolean and null, default false | ||
// no effect in JSON mode | ||
encodePrimitive?: boolean, | ||
}) | ||
``` | ||
## API | ||
| API | comments | | ||
|:--------------------------------------------------------------------------------|:--------------------------------------------| | ||
| **encodeJSON**( **inpt**: unknown, **space**?: number, **sortKeys** = false) | encode as JSON, can be used as static api | | ||
| **decodeJSON**( **inpt**: string) | decode JSON, can be used as static api | | ||
| **encode**( **inpt**: unknown) | encode to String, can be used as static api | | ||
| **decode**( **inpt**: string) | decode String, can be used as static api | | ||
| **register**( **key**: string, **type**: Constructor, **encoder**, **decoder**) | register a custom type | | ||
## Custom Types | ||
@@ -60,0 +79,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
51204
932
114