refactor(cli): re-use source constants

This commit is contained in:
Johann Schopplich
2025-10-30 21:33:08 +01:00
parent 7317b869b1
commit 66212285e5
2 changed files with 12 additions and 12 deletions

View File

@@ -40,10 +40,10 @@ users[2]{id,name,role}:
## Key Features ## Key Features
- 💸 **Token-efficient:** typically 3060% fewer tokens than JSON - 💸 **Token-efficient:** typically 3060% fewer tokens than JSON
- 🤿 **LLM-friendly guardrails:** explicit lengths and field lists help models validate output - 🤿 **LLM-friendly guardrails:** explicit lengths and fields enable validation
- 🍱 **Minimal syntax:** removes redundant punctuation (braces, brackets, most quotes) - 🍱 **Minimal syntax:** removes redundant punctuation (braces, brackets, most quotes)
- 📐 **Indentation-based structure:** replaces braces with whitespace for better readability - 📐 **Indentation-based structure:** like YAML, uses whitespace instead of braces
- 🧺 **Tabular arrays:** declare keys once, then stream rows without repetition - 🧺 **Tabular arrays:** declare keys once, stream data as rows
## Benchmarks ## Benchmarks

View File

@@ -1,15 +1,15 @@
import type { DecodeOptions, EncodeOptions } from '../../src' import type { DecodeOptions, Delimiter, EncodeOptions } from '../../src'
import * as fsp from 'node:fs/promises' import * as fsp from 'node:fs/promises'
import * as path from 'node:path' import * as path from 'node:path'
import process from 'node:process' import process from 'node:process'
import { defineCommand, runMain } from 'citty' import { defineCommand, runMain } from 'citty'
import { consola } from 'consola' import { consola } from 'consola'
import { version } from '../../package.json' with { type: 'json' } import { name, version } from '../../package.json' with { type: 'json' }
import { decode, DELIMITERS, encode } from '../../src' import { decode, DEFAULT_DELIMITER, DELIMITERS, encode } from '../../src'
const main = defineCommand({ const main = defineCommand({
meta: { meta: {
name: 'toon', name,
description: 'TOON CLI — Convert between JSON and TOON formats', description: 'TOON CLI — Convert between JSON and TOON formats',
version, version,
}, },
@@ -71,8 +71,8 @@ const main = defineCommand({
} }
// Validate delimiter // Validate delimiter
const delimiter = args.delimiter || ',' const delimiter = args.delimiter || DEFAULT_DELIMITER
if (!Object.values(DELIMITERS).includes(delimiter as any)) { if (!(Object.values(DELIMITERS)).includes(delimiter as Delimiter)) {
throw new Error(`Invalid delimiter "${delimiter}". Valid delimiters are: comma (,), tab (\\t), pipe (|)`) throw new Error(`Invalid delimiter "${delimiter}". Valid delimiters are: comma (,), tab (\\t), pipe (|)`)
} }
@@ -83,7 +83,7 @@ const main = defineCommand({
await encodeToToon({ await encodeToToon({
input: inputPath, input: inputPath,
output: outputPath, output: outputPath,
delimiter: delimiter as ',' | '\t' | '|', delimiter: delimiter as Delimiter,
indent, indent,
lengthMarker: args.lengthMarker === true ? '#' : false, lengthMarker: args.lengthMarker === true ? '#' : false,
}) })
@@ -128,9 +128,9 @@ function detectMode(
async function encodeToToon(config: { async function encodeToToon(config: {
input: string input: string
output?: string output?: string
delimiter: ',' | '\t' | '|' delimiter: Delimiter
indent: number indent: number
lengthMarker: '#' | false lengthMarker: NonNullable<EncodeOptions['lengthMarker']>
}) { }) {
const jsonContent = await fsp.readFile(config.input, 'utf-8') const jsonContent = await fsp.readFile(config.input, 'utf-8')