mirror of
https://github.com/voson-wang/toon.git
synced 2026-01-29 23:34:10 +08:00
feat: add default delimiter map
This commit is contained in:
@@ -40,3 +40,18 @@ export const CARRIAGE_RETURN = '\r'
|
|||||||
export const TAB = '\t'
|
export const TAB = '\t'
|
||||||
|
|
||||||
// #endregion
|
// #endregion
|
||||||
|
|
||||||
|
// #region Delimiters
|
||||||
|
|
||||||
|
export const DELIMITERS = {
|
||||||
|
comma: COMMA as ',',
|
||||||
|
tab: TAB as '\t',
|
||||||
|
pipe: PIPE as '|',
|
||||||
|
} as const
|
||||||
|
|
||||||
|
export type DelimiterKey = keyof typeof DELIMITERS
|
||||||
|
export type Delimiter = typeof DELIMITERS[DelimiterKey]
|
||||||
|
|
||||||
|
export const DEFAULT_DELIMITER: Delimiter = DELIMITERS.comma
|
||||||
|
|
||||||
|
// #endregion
|
||||||
|
|||||||
@@ -2,10 +2,14 @@ import type {
|
|||||||
EncodeOptions,
|
EncodeOptions,
|
||||||
ResolvedEncodeOptions,
|
ResolvedEncodeOptions,
|
||||||
} from './types'
|
} from './types'
|
||||||
|
import { DEFAULT_DELIMITER } from './constants'
|
||||||
import { encodeValue } from './encoders'
|
import { encodeValue } from './encoders'
|
||||||
import { normalizeValue } from './normalize'
|
import { normalizeValue } from './normalize'
|
||||||
|
|
||||||
|
export { DEFAULT_DELIMITER, DELIMITERS } from './constants'
|
||||||
export type {
|
export type {
|
||||||
|
Delimiter,
|
||||||
|
DelimiterKey,
|
||||||
EncodeOptions,
|
EncodeOptions,
|
||||||
JsonArray,
|
JsonArray,
|
||||||
JsonObject,
|
JsonObject,
|
||||||
@@ -23,6 +27,6 @@ export function encode(input: unknown, options?: EncodeOptions): string {
|
|||||||
function resolveOptions(options?: EncodeOptions): ResolvedEncodeOptions {
|
function resolveOptions(options?: EncodeOptions): ResolvedEncodeOptions {
|
||||||
return {
|
return {
|
||||||
indent: options?.indent ?? 2,
|
indent: options?.indent ?? 2,
|
||||||
delimiter: options?.delimiter ?? ',',
|
delimiter: options?.delimiter ?? DEFAULT_DELIMITER,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
// #region JSON types
|
// #region JSON types
|
||||||
|
|
||||||
|
import type { Delimiter, DelimiterKey } from './constants'
|
||||||
|
|
||||||
export type JsonPrimitive = string | number | boolean | null
|
export type JsonPrimitive = string | number | boolean | null
|
||||||
export type JsonObject = { [Key in string]: JsonValue } & { [Key in string]?: JsonValue | undefined }
|
export type JsonObject = { [Key in string]: JsonValue } & { [Key in string]?: JsonValue | undefined }
|
||||||
export type JsonArray = JsonValue[] | readonly JsonValue[]
|
export type JsonArray = JsonValue[] | readonly JsonValue[]
|
||||||
@@ -9,6 +11,8 @@ export type JsonValue = JsonPrimitive | JsonObject | JsonArray
|
|||||||
|
|
||||||
// #region Encoder options
|
// #region Encoder options
|
||||||
|
|
||||||
|
export type { Delimiter, DelimiterKey }
|
||||||
|
|
||||||
export interface EncodeOptions {
|
export interface EncodeOptions {
|
||||||
/**
|
/**
|
||||||
* Number of spaces per indentation level.
|
* Number of spaces per indentation level.
|
||||||
@@ -17,9 +21,9 @@ export interface EncodeOptions {
|
|||||||
indent?: number
|
indent?: number
|
||||||
/**
|
/**
|
||||||
* Delimiter to use for tabular array rows and inline primitive arrays.
|
* Delimiter to use for tabular array rows and inline primitive arrays.
|
||||||
* @default ','
|
* @default DELIMITERS.comma
|
||||||
*/
|
*/
|
||||||
delimiter?: ',' | '\t' | '|'
|
delimiter?: Delimiter
|
||||||
}
|
}
|
||||||
|
|
||||||
export type ResolvedEncodeOptions = Readonly<Required<EncodeOptions>>
|
export type ResolvedEncodeOptions = Readonly<Required<EncodeOptions>>
|
||||||
|
|||||||
Reference in New Issue
Block a user