From 7d4bb889f594cb829c84e400d22cfbc1cfbac340 Mon Sep 17 00:00:00 2001 From: Johann Schopplich Date: Sat, 25 Oct 2025 09:58:11 +0200 Subject: [PATCH] feat: add default delimiter map --- src/constants.ts | 15 +++++++++++++++ src/index.ts | 6 +++++- src/types.ts | 8 ++++++-- 3 files changed, 26 insertions(+), 3 deletions(-) diff --git a/src/constants.ts b/src/constants.ts index 4682897..91a4264 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -40,3 +40,18 @@ export const CARRIAGE_RETURN = '\r' export const TAB = '\t' // #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 diff --git a/src/index.ts b/src/index.ts index 09fac44..43c936c 100644 --- a/src/index.ts +++ b/src/index.ts @@ -2,10 +2,14 @@ import type { EncodeOptions, ResolvedEncodeOptions, } from './types' +import { DEFAULT_DELIMITER } from './constants' import { encodeValue } from './encoders' import { normalizeValue } from './normalize' +export { DEFAULT_DELIMITER, DELIMITERS } from './constants' export type { + Delimiter, + DelimiterKey, EncodeOptions, JsonArray, JsonObject, @@ -23,6 +27,6 @@ export function encode(input: unknown, options?: EncodeOptions): string { function resolveOptions(options?: EncodeOptions): ResolvedEncodeOptions { return { indent: options?.indent ?? 2, - delimiter: options?.delimiter ?? ',', + delimiter: options?.delimiter ?? DEFAULT_DELIMITER, } } diff --git a/src/types.ts b/src/types.ts index a3f4cb6..c6ff65e 100644 --- a/src/types.ts +++ b/src/types.ts @@ -1,5 +1,7 @@ // #region JSON types +import type { Delimiter, DelimiterKey } from './constants' + export type JsonPrimitive = string | number | boolean | null export type JsonObject = { [Key in string]: JsonValue } & { [Key in string]?: JsonValue | undefined } export type JsonArray = JsonValue[] | readonly JsonValue[] @@ -9,6 +11,8 @@ export type JsonValue = JsonPrimitive | JsonObject | JsonArray // #region Encoder options +export type { Delimiter, DelimiterKey } + export interface EncodeOptions { /** * Number of spaces per indentation level. @@ -17,9 +21,9 @@ export interface EncodeOptions { indent?: number /** * Delimiter to use for tabular array rows and inline primitive arrays. - * @default ',' + * @default DELIMITERS.comma */ - delimiter?: ',' | '\t' | '|' + delimiter?: Delimiter } export type ResolvedEncodeOptions = Readonly>