import { parse as parseYAML, stringify as stringifyYAML } from "https://deno.land/std/yaml/mod.ts";
import csv from "npm:csvtojson";
import { parse as parseTOML, stringify as stringifyTOML } from "https://deno.land/std/toml/mod.ts";
type DataFormat = 'object' | 'json' | 'yaml' | 'csv' | 'toml';
type SelectorType = 'property' | 'index' | 'search' | 'dynamic' | 'append' | 'function';
interface Selector {
type: SelectorType;
value: string | number;
conditions?: Condition[];
args?: string[];
}
interface Condition {
property: string;
operator: string;
value: any;
}
class SelectorParser {
parse(selectorString: string): Selector[] {
const selectors: Selector[] = [];
const parts = this.splitSelector(selectorString);
for (const part of parts) {
if (part.startsWith('.')) {
selectors.push(this.parseProperty(part));
} else if (part.startsWith('[') && part.endsWith(']')) {
selectors.push(this.parseIndex(part));
} else if (part.startsWith('(') && part.endsWith(')')) {
selectors.push(this.parseDynamic(part));
} else if (part === '[]') {
selectors.push(this.parseAppend());
} else if (part.includes('(')) {
selectors.push(this.parseFunction(part));
} else {
selectors.push(this.parseProperty(`.${part}`));
}
}
return selectors;
}
private splitSelector(selector: string): string[] {
const parts: string[] = [];
let current = '';
let depth = 0;
let inQuotes = false;
for (const char of selector) {
if (char === '"' && depth === 0) {
inQuotes = !inQuotes;
}
if (!inQuotes) {
if (char === '(' || char === '[') depth++;
if (char === ')' || char === ']') depth--;
}
if (char === '.' && depth === 0 && !inQuotes && current) {
parts.push(current);
current = '';
} else {
current += char;
}
}
if (current) parts.push(current);
if (selector[0] !== '.') {
return parts;
}
return parts.map(part => part.startsWith('.') ? part : `.${part}`);
}
private parseProperty(part: string): Selector {
return { type: 'property', value: part.slice(1) };
}
private parseIndex(part: string): Selector {
const inner = part.slice(1, -1);
if (inner === '*') {
return { type: 'index', value: 'all' };
}
const index = parseInt(inner, 10);
if (isNaN(index)) {
throw new Error(`Invalid index: ${inner}`);
}
return { type: 'index', value: index };
}
private parseDynamic(part: string): Selector {
const inner = part.slice(1, -1);