@chubbyts/chubbyts-decode-encode 2.0.0 → 2.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -3
- package/dist/data.d.ts +11 -0
- package/dist/data.js +6 -0
- package/dist/decoder/decoder.d.ts +13 -0
- package/dist/decoder/decoder.js +19 -0
- package/dist/decoder/index.d.ts +5 -13
- package/dist/decoder/index.js +4 -19
- package/dist/decoder/json-type-decoder.d.ts +1 -1
- package/dist/decoder/json-type-decoder.js +1 -1
- package/dist/decoder/jsonx-type-decoder.d.ts +1 -1
- package/dist/decoder/jsonx-type-decoder.js +2 -2
- package/dist/decoder/url-encoded-type-decoder.d.ts +1 -1
- package/dist/decoder/url-encoded-type-decoder.js +1 -1
- package/dist/decoder/yaml-type-decoder.d.ts +1 -1
- package/dist/decoder/yaml-type-decoder.js +1 -1
- package/dist/encoder/encoder.d.ts +13 -0
- package/dist/encoder/encoder.js +19 -0
- package/dist/encoder/index.d.ts +5 -13
- package/dist/encoder/index.js +4 -19
- package/dist/encoder/json-type-encoder.d.ts +1 -1
- package/dist/encoder/jsonx-type-encoder.d.ts +1 -1
- package/dist/encoder/jsonx-type-encoder.js +3 -3
- package/dist/encoder/url-encoded-type-encoder.d.ts +1 -1
- package/dist/encoder/url-encoded-type-encoder.js +1 -1
- package/dist/encoder/yaml-type-encoder.d.ts +1 -1
- package/dist/index.d.ts +5 -11
- package/dist/index.js +4 -6
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -34,7 +34,7 @@ A simple decode/encode solution for json / jsonx / url-encoded / xml / yaml.
|
|
|
34
34
|
Through [NPM](https://www.npmjs.com) as [@chubbyts/chubbyts-decode-encode][1].
|
|
35
35
|
|
|
36
36
|
```ts
|
|
37
|
-
npm i @chubbyts/chubbyts-decode-encode@^2.
|
|
37
|
+
npm i @chubbyts/chubbyts-decode-encode@^2.1.0
|
|
38
38
|
```
|
|
39
39
|
|
|
40
40
|
## Usage
|
|
@@ -44,7 +44,7 @@ npm i @chubbyts/chubbyts-decode-encode@^2.0.0
|
|
|
44
44
|
#### createDecoder
|
|
45
45
|
|
|
46
46
|
```ts
|
|
47
|
-
import { createDecoder } from '@chubbyts/chubbyts-decode-encode/dist/decoder';
|
|
47
|
+
import { createDecoder } from '@chubbyts/chubbyts-decode-encode/dist/decoder/decoder';
|
|
48
48
|
import { createJsonTypeDecoder } from '@chubbyts/chubbyts-decode-encode/dist/decoder/json-type-decoder';
|
|
49
49
|
|
|
50
50
|
const decoder = createDecoder([createJsonTypeDecoder()]);
|
|
@@ -112,7 +112,7 @@ const contentType = yamlTypeDecoder.contentType;
|
|
|
112
112
|
#### createEncoder
|
|
113
113
|
|
|
114
114
|
```ts
|
|
115
|
-
import { createEncoder } from '@chubbyts/chubbyts-encode-encode/dist/encoder';
|
|
115
|
+
import { createEncoder } from '@chubbyts/chubbyts-encode-encode/dist/encoder/encoder';
|
|
116
116
|
import { createJsonTypeEncoder } from '@chubbyts/chubbyts-encode-encode/dist/encoder/json-type-encoder';
|
|
117
117
|
|
|
118
118
|
const encoder = createEncoder([createJsonTypeEncoder()]);
|
package/dist/data.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export type Data = {
|
|
2
|
+
[key: string]: Data;
|
|
3
|
+
} | Array<Data> | string | number | boolean | null;
|
|
4
|
+
export declare const isNull: (value: unknown) => value is null;
|
|
5
|
+
export declare const isBoolean: (value: unknown) => value is boolean;
|
|
6
|
+
export declare const isNumber: (value: unknown) => value is number;
|
|
7
|
+
export declare const isString: (value: unknown) => value is string;
|
|
8
|
+
export declare const isArray: (value: unknown) => value is Array<Data>;
|
|
9
|
+
export declare const isObject: (value: unknown) => value is {
|
|
10
|
+
[key: string]: Data;
|
|
11
|
+
};
|
package/dist/data.js
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export const isNull = (value) => value === null;
|
|
2
|
+
export const isBoolean = (value) => typeof value === 'boolean';
|
|
3
|
+
export const isNumber = (value) => typeof value === 'number';
|
|
4
|
+
export const isString = (value) => typeof value === 'string';
|
|
5
|
+
export const isArray = (value) => Array.isArray(value);
|
|
6
|
+
export const isObject = (value) => value !== null && typeof value === 'object' && value.constructor.name == 'Object';
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { Data } from '../data.js';
|
|
2
|
+
export declare class DecodeError extends Error {
|
|
3
|
+
constructor(message: string, stack?: string);
|
|
4
|
+
}
|
|
5
|
+
export type TypeDecoder = {
|
|
6
|
+
decode: (encodedData: string) => Data;
|
|
7
|
+
contentType: string;
|
|
8
|
+
};
|
|
9
|
+
export type Decoder = {
|
|
10
|
+
decode: (encodedData: string, contentType: string, context?: Record<string, unknown>) => Data;
|
|
11
|
+
contentTypes: Array<string>;
|
|
12
|
+
};
|
|
13
|
+
export declare const createDecoder: (decoderTypes: Array<TypeDecoder>) => Decoder;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
export class DecodeError extends Error {
|
|
2
|
+
constructor(message, stack) {
|
|
3
|
+
super(message);
|
|
4
|
+
this.stack = stack;
|
|
5
|
+
}
|
|
6
|
+
}
|
|
7
|
+
export const createDecoder = (decoderTypes) => {
|
|
8
|
+
const decoderTypeMap = new Map(decoderTypes.map((DecoderType) => [DecoderType.contentType, DecoderType]));
|
|
9
|
+
const contentTypes = Array.from(decoderTypeMap.keys());
|
|
10
|
+
return {
|
|
11
|
+
decode: (encodedData, contentType) => {
|
|
12
|
+
if (decoderTypeMap.has(contentType)) {
|
|
13
|
+
return decoderTypeMap.get(contentType).decode(encodedData);
|
|
14
|
+
}
|
|
15
|
+
throw new Error(`Unsupported contentType "${contentType}", supported contentTypes are "${contentTypes.join('", "')}".`);
|
|
16
|
+
},
|
|
17
|
+
contentTypes,
|
|
18
|
+
};
|
|
19
|
+
};
|
package/dist/decoder/index.d.ts
CHANGED
|
@@ -1,13 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
}
|
|
5
|
-
export type TypeDecoder
|
|
6
|
-
decode: (encodedData: string) => Data;
|
|
7
|
-
contentType: string;
|
|
8
|
-
};
|
|
9
|
-
export type Decoder = {
|
|
10
|
-
decode: (encodedData: string, contentType: string, context?: Record<string, unknown>) => Data;
|
|
11
|
-
contentTypes: Array<string>;
|
|
12
|
-
};
|
|
13
|
-
export declare const createDecoder: (decoderTypes: Array<TypeDecoder>) => Decoder;
|
|
1
|
+
/**
|
|
2
|
+
* @deprecated This file re-exports the old API. Use './decoder.ts' directly instead.
|
|
3
|
+
*/
|
|
4
|
+
export { DecodeError, createDecoder } from './decoder.js';
|
|
5
|
+
export type { TypeDecoder, Decoder } from './decoder.js';
|
package/dist/decoder/index.js
CHANGED
|
@@ -1,19 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
}
|
|
6
|
-
}
|
|
7
|
-
export const createDecoder = (decoderTypes) => {
|
|
8
|
-
const decoderTypeMap = new Map(decoderTypes.map((DecoderType) => [DecoderType.contentType, DecoderType]));
|
|
9
|
-
const contentTypes = Array.from(decoderTypeMap.keys());
|
|
10
|
-
return {
|
|
11
|
-
decode: (encodedData, contentType) => {
|
|
12
|
-
if (decoderTypeMap.has(contentType)) {
|
|
13
|
-
return decoderTypeMap.get(contentType).decode(encodedData);
|
|
14
|
-
}
|
|
15
|
-
throw new Error(`Unsupported contentType "${contentType}", supported contentTypes are "${contentTypes.join('", "')}".`);
|
|
16
|
-
},
|
|
17
|
-
contentTypes,
|
|
18
|
-
};
|
|
19
|
-
};
|
|
1
|
+
/**
|
|
2
|
+
* @deprecated This file re-exports the old API. Use './decoder.ts' directly instead.
|
|
3
|
+
*/
|
|
4
|
+
export { DecodeError, createDecoder } from './decoder.js';
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import type { TypeDecoder } from './
|
|
1
|
+
import type { TypeDecoder } from './decoder.js';
|
|
2
2
|
export declare const createJsonTypeDecoder: () => TypeDecoder;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { throwableToError } from '@chubbyts/chubbyts-throwable-to-error/dist/throwable-to-error';
|
|
2
|
-
import { DecodeError } from './
|
|
2
|
+
import { DecodeError } from './decoder.js';
|
|
3
3
|
export const createJsonTypeDecoder = () => {
|
|
4
4
|
return {
|
|
5
5
|
decode: (encodedData) => {
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import type { TypeDecoder } from './
|
|
1
|
+
import type { TypeDecoder } from './decoder.js';
|
|
2
2
|
export declare const createJsonxTypeDecoder: () => TypeDecoder;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { XMLParser } from 'fast-xml-parser';
|
|
2
2
|
import { throwableToError } from '@chubbyts/chubbyts-throwable-to-error/dist/throwable-to-error';
|
|
3
|
-
import { DATATYPE_ARRAY, DATATYPE_BOOLEAN, DATATYPE_NULL, DATATYPE_NUMBER, DATATYPE_OBJECT, DATATYPE_STRING, } from '../jsonx-datatypes';
|
|
4
|
-
import { DecodeError } from './
|
|
3
|
+
import { DATATYPE_ARRAY, DATATYPE_BOOLEAN, DATATYPE_NULL, DATATYPE_NUMBER, DATATYPE_OBJECT, DATATYPE_STRING, } from '../jsonx-datatypes.js';
|
|
4
|
+
import { DecodeError } from './decoder.js';
|
|
5
5
|
const decodeHtmlEntities = (string) => string.replace(/&#\d+;/gm, (code) => String.fromCharCode(code.match(/\d+/)[0]));
|
|
6
6
|
const isWithName = (node) => typeof node === 'object' &&
|
|
7
7
|
null !== node &&
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import type { TypeDecoder } from './
|
|
1
|
+
import type { TypeDecoder } from './decoder.js';
|
|
2
2
|
export declare const createUrlEncodedTypeDecoder: () => TypeDecoder;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { parse } from 'qs';
|
|
2
|
-
import { isArray, isObject } from '../
|
|
2
|
+
import { isArray, isObject } from '../data.js';
|
|
3
3
|
const decodeValue = (value) => {
|
|
4
4
|
if (isObject(value)) {
|
|
5
5
|
return Object.fromEntries(Object.entries(value).map(([subKey, subValue]) => [subKey, decodeValue(subValue)]));
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import type { TypeDecoder } from './
|
|
1
|
+
import type { TypeDecoder } from './decoder.js';
|
|
2
2
|
export declare const createYamlTypeDecoder: () => TypeDecoder;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { parse } from 'yaml';
|
|
2
2
|
import { throwableToError } from '@chubbyts/chubbyts-throwable-to-error/dist/throwable-to-error';
|
|
3
|
-
import { DecodeError } from './
|
|
3
|
+
import { DecodeError } from './decoder.js';
|
|
4
4
|
export const createYamlTypeDecoder = () => {
|
|
5
5
|
return {
|
|
6
6
|
decode: (encodedData) => {
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { Data } from '../data.js';
|
|
2
|
+
export declare class EncodeError extends Error {
|
|
3
|
+
constructor(message: string, stack?: string);
|
|
4
|
+
}
|
|
5
|
+
export type TypeEncoder = {
|
|
6
|
+
encode: (data: Data) => string;
|
|
7
|
+
contentType: string;
|
|
8
|
+
};
|
|
9
|
+
export type Encoder = {
|
|
10
|
+
encode: (data: Data, contentType: string, context?: Record<string, unknown>) => string;
|
|
11
|
+
contentTypes: Array<string>;
|
|
12
|
+
};
|
|
13
|
+
export declare const createEncoder: (encoderTypes: Array<TypeEncoder>) => Encoder;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
export class EncodeError extends Error {
|
|
2
|
+
constructor(message, stack) {
|
|
3
|
+
super(message);
|
|
4
|
+
this.stack = stack;
|
|
5
|
+
}
|
|
6
|
+
}
|
|
7
|
+
export const createEncoder = (encoderTypes) => {
|
|
8
|
+
const encoderTypeMap = new Map(encoderTypes.map((encoderType) => [encoderType.contentType, encoderType]));
|
|
9
|
+
const contentTypes = Array.from(encoderTypeMap.keys());
|
|
10
|
+
return {
|
|
11
|
+
encode: (data, contentType) => {
|
|
12
|
+
if (encoderTypeMap.has(contentType)) {
|
|
13
|
+
return encoderTypeMap.get(contentType).encode(data);
|
|
14
|
+
}
|
|
15
|
+
throw new Error(`Unsupported contentType "${contentType}", supported contentTypes are "${contentTypes.join('", "')}".`);
|
|
16
|
+
},
|
|
17
|
+
contentTypes,
|
|
18
|
+
};
|
|
19
|
+
};
|
package/dist/encoder/index.d.ts
CHANGED
|
@@ -1,13 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
}
|
|
5
|
-
export type TypeEncoder
|
|
6
|
-
encode: (data: Data) => string;
|
|
7
|
-
contentType: string;
|
|
8
|
-
};
|
|
9
|
-
export type Encoder = {
|
|
10
|
-
encode: (data: Data, contentType: string, context?: Record<string, unknown>) => string;
|
|
11
|
-
contentTypes: Array<string>;
|
|
12
|
-
};
|
|
13
|
-
export declare const createEncoder: (encoderTypes: Array<TypeEncoder>) => Encoder;
|
|
1
|
+
/**
|
|
2
|
+
* @deprecated This file re-exports the old API. Use './encoder.ts' directly instead.
|
|
3
|
+
*/
|
|
4
|
+
export { EncodeError, createEncoder } from './encoder.js';
|
|
5
|
+
export type { TypeEncoder, Encoder } from './encoder.js';
|
package/dist/encoder/index.js
CHANGED
|
@@ -1,19 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
}
|
|
6
|
-
}
|
|
7
|
-
export const createEncoder = (encoderTypes) => {
|
|
8
|
-
const encoderTypeMap = new Map(encoderTypes.map((encoderType) => [encoderType.contentType, encoderType]));
|
|
9
|
-
const contentTypes = Array.from(encoderTypeMap.keys());
|
|
10
|
-
return {
|
|
11
|
-
encode: (data, contentType) => {
|
|
12
|
-
if (encoderTypeMap.has(contentType)) {
|
|
13
|
-
return encoderTypeMap.get(contentType).encode(data);
|
|
14
|
-
}
|
|
15
|
-
throw new Error(`Unsupported contentType "${contentType}", supported contentTypes are "${contentTypes.join('", "')}".`);
|
|
16
|
-
},
|
|
17
|
-
contentTypes,
|
|
18
|
-
};
|
|
19
|
-
};
|
|
1
|
+
/**
|
|
2
|
+
* @deprecated This file re-exports the old API. Use './encoder.ts' directly instead.
|
|
3
|
+
*/
|
|
4
|
+
export { EncodeError, createEncoder } from './encoder.js';
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import type { TypeEncoder } from './
|
|
1
|
+
import type { TypeEncoder } from './encoder.js';
|
|
2
2
|
export declare const createJsonTypeEncoder: (prettyPrint?: boolean) => TypeEncoder;
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import type { TypeEncoder } from './
|
|
1
|
+
import type { TypeEncoder } from './encoder.js';
|
|
2
2
|
export declare const createJsonxTypeEncoder: (prettyPrint?: boolean) => TypeEncoder;
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { XMLBuilder } from 'fast-xml-parser';
|
|
2
2
|
import { throwableToError } from '@chubbyts/chubbyts-throwable-to-error/dist/throwable-to-error';
|
|
3
|
-
import { isArray, isBoolean, isNumber, isObject, isString, isNull } from '../
|
|
4
|
-
import { DATATYPE_ARRAY, DATATYPE_BOOLEAN, DATATYPE_NULL, DATATYPE_NUMBER, DATATYPE_OBJECT, DATATYPE_STRING, } from '../jsonx-datatypes';
|
|
5
|
-
import { EncodeError } from './
|
|
3
|
+
import { isArray, isBoolean, isNumber, isObject, isString, isNull } from '../data.js';
|
|
4
|
+
import { DATATYPE_ARRAY, DATATYPE_BOOLEAN, DATATYPE_NULL, DATATYPE_NUMBER, DATATYPE_OBJECT, DATATYPE_STRING, } from '../jsonx-datatypes.js';
|
|
5
|
+
import { EncodeError } from './encoder.js';
|
|
6
6
|
const encodeHtmlEntities = (string) => string.replace(/[\u00A0-\u9999<>&]/g, (i) => '&#' + i.charCodeAt(0) + ';');
|
|
7
7
|
const createXmlNode = () => {
|
|
8
8
|
return {
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import type { TypeEncoder } from './
|
|
1
|
+
import type { TypeEncoder } from './encoder.js';
|
|
2
2
|
export declare const createUrlEncodedTypeEncoder: () => TypeEncoder;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { stringify } from 'qs';
|
|
2
|
-
import { isArray, isBoolean, isNull, isNumber, isObject } from '../
|
|
2
|
+
import { isArray, isBoolean, isNull, isNumber, isObject } from '../data.js';
|
|
3
3
|
const encodeValue = (value) => {
|
|
4
4
|
if (isObject(value)) {
|
|
5
5
|
return Object.fromEntries(Object.entries(value).map(([subKey, subValue]) => [subKey, encodeValue(subValue)]));
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import type { TypeEncoder } from './
|
|
1
|
+
import type { TypeEncoder } from './encoder.js';
|
|
2
2
|
export declare const createYamlTypeEncoder: () => TypeEncoder;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,11 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
export
|
|
5
|
-
export
|
|
6
|
-
export declare const isNumber: (value: unknown) => value is number;
|
|
7
|
-
export declare const isString: (value: unknown) => value is string;
|
|
8
|
-
export declare const isArray: (value: unknown) => value is Array<Data>;
|
|
9
|
-
export declare const isObject: (value: unknown) => value is {
|
|
10
|
-
[key: string]: Data;
|
|
11
|
-
};
|
|
1
|
+
/**
|
|
2
|
+
* @deprecated This file re-exports the old API. Use './data.ts' directly instead.
|
|
3
|
+
*/
|
|
4
|
+
export { isNull, isBoolean, isNumber, isString, isArray, isObject } from './data.js';
|
|
5
|
+
export type { Data } from './data.js';
|
package/dist/index.js
CHANGED
|
@@ -1,6 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
export
|
|
5
|
-
export const isArray = (value) => Array.isArray(value);
|
|
6
|
-
export const isObject = (value) => value !== null && typeof value === 'object' && value.constructor.name == 'Object';
|
|
1
|
+
/**
|
|
2
|
+
* @deprecated This file re-exports the old API. Use './data.ts' directly instead.
|
|
3
|
+
*/
|
|
4
|
+
export { isNull, isBoolean, isNumber, isString, isArray, isObject } from './data.js';
|
package/package.json
CHANGED