Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/toon/src/shared/validation.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { DEFAULT_DELIMITER, LIST_ITEM_MARKER } from '../constants'
import { isBooleanOrNullLiteral } from './literal-utils'

const NUMERIC_LIKE_PATTERN = /^-?\d+(?:\.\d+)?(?:e[+-]?\d+)?$/i
const NUMERIC_LIKE_PATTERN = /^[+-]?(\d+(?:\.\d*)?|\.\d+)(?:e[+-]?\d+)?$/i
const LEADING_ZERO_PATTERN = /^0\d+$/

/**
Expand Down
35 changes: 35 additions & 0 deletions packages/toon/test/issue-249.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { describe, expect, it } from 'vitest'
import { decode, encode } from '../src/index'

describe('Issue 249', () => {
it('should preserve strings starting with dot or plus followed by numbers', () => {
const indent: number = 1;

function encodeToon(data: any): string {
return encode(data, { indent });
}

function decodeToon<T>(toon: string): T {
return decode(toon, { indent }) as any;
}

const data = [
'.6226633103089010',
'+8613334445577',
]

// Expecting that it properly encodes these strings so that they decode back to strings
// If it encodes them as raw numbers in TOON format without quotes, they might be read back as numbers.
const encoded = encodeToon(data);
console.log('Encoded:', encoded);

// Encoded values MUST be quoted to avoid ambiguity with numbers
// This assertion will fail initially
expect(encoded).toContain('".6226633103089010"');
expect(encoded).toContain('"+8613334445577"');

const decoded = decodeToon(encoded);

expect(decoded).toEqual(data);
})
})