CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-lumino--coreutils

Essential utility functions and classes for TypeScript/JavaScript applications including JSON handling, MIME data management, promise delegation, secure tokens, and cross-platform random number generation.

Pending
Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

SecuritybySnyk

Pending

The risk profile of this skill

Overview
Eval results
Files

mime-data.mddocs/

MIME Data Management

Storage and management system for arbitrary data organized by MIME types. Designed for transferring arbitrary data and objects within the same application without enforcing MIME type "correctness".

Capabilities

MimeData Class

A class that stores data organized by MIME types, allowing applications to store and retrieve arbitrary data with type labels.

/**
 * An object which stores MIME data for general application use.
 * 
 * Notes:
 * This class does not attempt to enforce "correctness" of MIME types
 * and their associated data. Since this class is designed to transfer
 * arbitrary data and objects within the same application, it assumes
 * that the user provides correct and accurate data.
 */
class MimeData {
  /**
   * Get an array of the MIME types contained within the dataset
   * @returns A new array of the MIME types, in order of insertion
   */
  types(): string[];

  /**
   * Test whether the dataset has an entry for the given type
   * @param mime - The MIME type of interest
   * @returns true if the dataset contains a value for the given MIME type, false otherwise
   */
  hasData(mime: string): boolean;

  /**
   * Get the data value for the given MIME type
   * @param mime - The MIME type of interest
   * @returns The value for the given MIME type, or undefined if the dataset does not contain a value for the type
   */
  getData(mime: string): any | undefined;

  /**
   * Set the data value for the given MIME type
   * @param mime - The MIME type of interest
   * @param data - The data value for the given MIME type
   * 
   * Notes:
   * This will overwrite any previous entry for the MIME type
   */
  setData(mime: string, data: any): void;

  /**
   * Remove the data entry for the given MIME type
   * @param mime - The MIME type of interest
   * 
   * Notes:
   * This is a no-op if there is no entry for the given MIME type
   */
  clearData(mime: string): void;

  /**
   * Remove all data entries from the dataset
   */
  clear(): void;
}

Usage Examples:

import { MimeData } from "@lumino/coreutils";

// Create a new MIME data container
const mimeData = new MimeData();

// Store different types of data
mimeData.setData("text/plain", "Hello, World!");
mimeData.setData("application/json", JSON.stringify({ name: "Alice", age: 30 }));
mimeData.setData("text/html", "<p>Hello, <strong>World!</strong></p>");
mimeData.setData("application/x-custom", { custom: "data", array: [1, 2, 3] });

// Check what MIME types are available
const types = mimeData.types();
console.log(types); // ["text/plain", "application/json", "text/html", "application/x-custom"]

// Check if specific data exists
const hasText = mimeData.hasData("text/plain"); // true
const hasImage = mimeData.hasData("image/png"); // false

// Retrieve data
const plainText = mimeData.getData("text/plain"); // "Hello, World!"
const jsonData = mimeData.getData("application/json"); // '{"name":"Alice","age":30}'
const missingData = mimeData.getData("missing/type"); // undefined

// Parse retrieved JSON data
const parsedData = JSON.parse(jsonData);
console.log(parsedData.name); // "Alice"

// Update existing data (overwrites previous entry)
mimeData.setData("text/plain", "Updated text content");

// Remove specific data
mimeData.clearData("text/html");
console.log(mimeData.hasData("text/html")); // false

// Clear all data
mimeData.clear();
console.log(mimeData.types()); // []

Common Use Cases

Drag and Drop Operations:

import { MimeData } from "@lumino/coreutils";

// Create data for drag and drop
const dragData = new MimeData();
dragData.setData("text/plain", "Draggable text");
dragData.setData("text/uri-list", "https://example.com");
dragData.setData("application/x-custom-widget", { widgetId: "widget-123", config: {} });

// In drop handler, check available formats
if (dragData.hasData("application/x-custom-widget")) {
  const widgetData = dragData.getData("application/x-custom-widget");
  // Handle custom widget drop
} else if (dragData.hasData("text/uri-list")) {
  const url = dragData.getData("text/uri-list");
  // Handle URL drop
} else if (dragData.hasData("text/plain")) {
  const text = dragData.getData("text/plain");
  // Handle text drop
}

Clipboard Operations:

import { MimeData } from "@lumino/coreutils";

// Prepare clipboard data with multiple formats
const clipboardData = new MimeData();
clipboardData.setData("text/plain", "Plain text version");
clipboardData.setData("text/html", "<b>Rich text version</b>");
clipboardData.setData("application/json", JSON.stringify({ id: 1, title: "Item" }));

// Application can choose the best available format
const availableTypes = clipboardData.types();
if (availableTypes.includes("application/json")) {
  const jsonData = JSON.parse(clipboardData.getData("application/json"));
  // Use structured data
} else if (availableTypes.includes("text/html")) {
  const htmlData = clipboardData.getData("text/html");
  // Use rich text
} else {
  const plainData = clipboardData.getData("text/plain");
  // Fallback to plain text
}

Inter-Component Data Transfer:

import { MimeData } from "@lumino/coreutils";

// Transfer complex data between application components
const transferData = new MimeData();
transferData.setData("application/x-user-profile", {
  id: 123,
  name: "Alice Smith",
  preferences: { theme: "dark", language: "en" }
});
transferData.setData("text/csv", "id,name,theme\n123,Alice Smith,dark");
transferData.setData("text/plain", "Alice Smith (ID: 123)");

// Receiving component can choose appropriate format
function handleData(data: MimeData) {
  if (data.hasData("application/x-user-profile")) {
    // Use rich object data
    const profile = data.getData("application/x-user-profile");
    return profile;
  } else if (data.hasData("text/csv")) {
    // Parse CSV data
    const csv = data.getData("text/csv");
    return parseCsv(csv);
  } else {
    // Fallback to plain text
    return { name: data.getData("text/plain") };
  }
}

docs

index.md

json-utilities.md

mime-data.md

promise-delegation.md

random-generation.md

token-system.md

uuid-generation.md

tile.json