w

Référence API

Documentation technique destinée aux développeurs qui souhaitent intégrer ou étendre la fonctionnalité de conversion JSON vers CSV.

Utilisation des API du navigateur

API Clipboard

L'outil utilise l'API Clipboard moderne pour les opérations de copie :

// Copy text to clipboard
async function copyToClipboard(text) {
  try {
    await navigator.clipboard.writeText(text);
    console.log("Text copied to clipboard");
  } catch (error) {
    console.error("Failed to copy text: ", error);
  }
}

Prise en charge des navigateurs :

  • Chrome 66+
  • Firefox 63+
  • Safari 13.1+
  • Edge 79+

API de téléchargement de fichiers

Les fichiers CSV sont téléchargés à l'aide de l'API Blob :

// Download CSV file
function downloadCSV(csvContent, filename) {
  const blob = new Blob([csvContent], {
    type: "text/csv;charset=utf-8",
  });
  const url = URL.createObjectURL(blob);
  const link = document.createElement("a");
  link.href = url;
  link.download = filename;
  document.body.appendChild(link);
  link.click();
  document.body.removeChild(link);
  URL.revokeObjectURL(url);
}

Fonctions de traitement des données

Validation JSON

// Validate JSON string
function validateJSON(jsonString) {
  try {
    const parsed = JSON.parse(jsonString);
    return { valid: true, data: parsed };
  } catch (error) {
    return { valid: false, error: error.message };
  }
}

Logique de conversion CSV

// Convert JSON array to CSV
function jsonToCSV(jsonData, options = {}) {
  const { delimiter = ',', includeHeaders = true, escapeQuotes = true } = options;

  if (!Array.isArray(jsonData) || jsonData.length === 0) {
    return '';
  }

  // Get all unique keys
  const allKeys = new Set();
  jsonData.forEach((item) => {
    if (typeof item === 'object' && item !== null) {
      Object.keys(item).forEach((key) => allKeys.add(key));
    }
  });

  const keys = Array.from(allKeys);
  let csv = '';

  // Add headers
  if (includeHeaders) {
    csv += keys.map((key) => escapeCSVValue(key, delimiter, escapeQuotes)).join(delimiter) + '
';
  }

  // Add data rows
  jsonData.forEach((item) => {
    if (typeof item === 'object' && item !== null) {
      const row = keys.map((key) => {
        const value = item[key];
        return escapeCSVValue(value !== undefined ? String(value) : '', delimiter, escapeQuotes);
      });
      csv += row.join(delimiter) + '
';
    }
  });

  return csv;
}

Échappement des valeurs CSV

// Escape CSV values
function escapeCSVValue(value, delimiter, escapeQuotes) {
  if (!escapeQuotes) {
    return value;
  }

  // If value contains delimiter, newline, or quotes, wrap in quotes
  if (value.includes(delimiter) || value.includes('
') || value.includes('"')) {
    // Escape quotes: replace " with ""
    return `"${value.replace(/"/g, '""')}"`;
  }

  return value;
}

Options de configuration

Interface des options de conversion

interface ConversionOptions {
  delimiter: "," | ";" | "  " | "|";
  encoding: "utf-8" | "utf-16" | "gbk";
  includeHeaders: boolean;
  escapeQuotes: boolean;
}

Configuration par défaut

const defaultOptions = {
  delimiter: ",",
  encoding: "utf-8",
  includeHeaders: true,
  escapeQuotes: true,
};

Structures de données

Interface de l'historique

interface HistoryRecord {
  id: string;
  input: string;
  inputPreview: string;
  output: string;
  delimiter: string;
  encoding: string;
  includeHeaders: boolean;
  escapeQuotes: boolean;
  rows: number;
  columns: number;
  timestamp: string;
  timestampMs: number;
}

Interface des statistiques

interface ConversionStats {
  rows: number;
  columns: number;
  size: string; // Formatted file size
}

Gestion des erreurs

Types d'erreurs

enum ConversionError {
  INVALID_JSON = "INVALID_JSON",
  CONVERSION_FAILED = "CONVERSION_FAILED",
  EMPTY_DATA = "EMPTY_DATA",
  UNSUPPORTED_FORMAT = "UNSUPPORTED_FORMAT",
}

Fonction de gestion des erreurs

// Handle conversion errors
function handleConversionError(error, jsonInput) {
  switch (error.type) {
    case "INVALID_JSON":
      return {
        message: "Invalid JSON format. Please check your input.",
        field: "input",
        severity: "error",
      };
    case "CONVERSION_FAILED":
      return {
        message: "Failed to convert JSON to CSV. Please check your data.",
        field: "output",
        severity: "error",
      };
    default:
      return {
        message: "An unexpected error occurred.",
        field: "general",
        severity: "error",
      };
  }
}

Considérations de performance

Gestion des grands jeux de données

// Process large datasets in chunks
function processLargeDataset(jsonData, chunkSize = 1000) {
  const chunks = [];
  for (let i = 0; i < jsonData.length; i += chunkSize) {
    chunks.push(jsonData.slice(i, i + chunkSize));
  }

  return chunks.map((chunk) => jsonToCSV(chunk));
}

Gestion de la mémoire

// Monitor memory usage
function getMemoryUsage() {
  if (performance.memory) {
    return {
      used: performance.memory.usedJSHeapSize,
      total: performance.memory.totalJSHeapSize,
      limit: performance.memory.jsHeapSizeLimit,
    };
  }
  return null;
}

Compatibilité des navigateurs

Détection des fonctionnalités

// Check browser capabilities
function checkBrowserSupport() {
  return {
    clipboardAPI: !!navigator.clipboard,
    blobAPI: !!window.Blob,
    fileAPI: !!window.File,
    localStorage: !!window.localStorage,
    jsonSupport: !!JSON.parse,
  };
}

Polyfills

Pour les anciens navigateurs, envisagez ces polyfills :

// Clipboard API polyfill
if (!navigator.clipboard) {
  navigator.clipboard = {
    writeText: function (text) {
      return new Promise((resolve, reject) => {
        const textArea = document.createElement("textarea");
        textArea.value = text;
        document.body.appendChild(textArea);
        textArea.select();
        try {
          document.execCommand("copy");
          resolve();
        } catch (error) {
          reject(error);
        }
        document.body.removeChild(textArea);
      });
    },
  };
}

Exemples d'intégration

Intégration d'un composant React

import React, { useState, useCallback } from "react";

function JSONToCSVConverter() {
  const [jsonInput, setJsonInput] = useState("");
  const [csvOutput, setCsvOutput] = useState("");
  const [options, setOptions] = useState(defaultOptions);

  const convertToCSV = useCallback(() => {
    try {
      const jsonData = JSON.parse(jsonInput);
      const csv = jsonToCSV(jsonData, options);
      setCsvOutput(csv);
    } catch (error) {
      console.error("Conversion failed:", error);
    }
  }, [jsonInput, options]);

  return (
    <div>
      <textarea
        value={jsonInput}
        onChange={(e) => setJsonInput(e.target.value)}
        placeholder="Enter JSON data..."
      />
      <button onClick={convertToCSV}>Convert</button>
      <textarea value={csvOutput} readOnly placeholder="CSV output will appear here..." />
    </div>
  );
}

Intégration Vue.js

<template>
  <div class="json-to-csv-converter">
    <textarea v-model="jsonInput" placeholder="Enter JSON data..." @input="validateAndConvert" />
    <div class="options">
      <select v-model="options.delimiter">
        <option value=",">Comma (,)</option>
        <option value=";">Semicolon (;)</option>
        <option value=" ">Tab</option>
        <option value="|">Pipe (|)</option>
      </select>
    </div>
    <textarea :value="csvOutput" readonly placeholder="CSV output will appear here..." />
  </div>
</template>

<script>
export default {
  data() {
    return {
      jsonInput: "",
      csvOutput: "",
      options: {
        delimiter: ",",
        includeHeaders: true,
        escapeQuotes: true,
      },
    };
  },
  methods: {
    validateAndConvert() {
      try {
        const jsonData = JSON.parse(this.jsonInput);
        this.csvOutput = this.jsonToCSV(jsonData, this.options);
      } catch (error) {
        this.csvOutput = "";
      }
    },
    jsonToCSV(jsonData, options) {
      // Implementation here
    },
  },
};
</script>

Tests

Exemples de tests unitaires

// Jest test examples
describe('JSON to CSV Converter', () => {
  test('converts simple array to CSV', () => {
    const input = [
      { name: 'John', age: 30 },
      { name: 'Jane', age: 25 },
    ];
    const expected = 'name,age
John,30
Jane,25
';
    expect(jsonToCSV(input)).toBe(expected);
  });

  test('handles special characters', () => {
    const input = [{ name: 'Smith, John', quote: 'He said "Hello"' }];
    const expected = 'name,quote
"Smith, John","He said ""Hello"""
';
    expect(jsonToCSV(input)).toBe(expected);
  });

  test('validates JSON input', () => {
    const invalidJson = '{ name: "John" }'; // Missing quotes
    const result = validateJSON(invalidJson);
    expect(result.valid).toBe(false);
  });
});
Cette page vous a-t-elle été utile ?