import { Conversion } from '@fgv/ts-utils'; import { Converter } from '@fgv/ts-utils'; import { FileTree } from '@fgv/ts-json-base'; import { Hash as Hash_2 } from '@fgv/ts-utils'; import { JsonValue } from '@fgv/ts-json-base'; import { Result } from '@fgv/ts-utils'; import { Validator } from '@fgv/ts-utils'; declare namespace Converters { export { templateString, extendedArrayOf, rangeTypeOf, rangeOf, isoDate } } export { Converters } declare namespace Csv { export { parseCsvString, CsvOptions, readCsvFileSync, readCsvFromTree } } export { Csv } /** * Options for {@link Csv.readCsvFileSync | readCsvFileSync} * @beta */ declare interface CsvOptions { delimiter?: string; } /** * Default {@link Experimental.RangeOfFormats | formats} to use for both * open-ended and complete {@link Experimental.RangeOf | RangeOf}. * @public */ declare const DEFAULT_RANGEOF_FORMATS: RangeOfFormats; declare namespace Experimental { export { ExtendedArray, formatList, FormatTargets, Formattable, FormattableBase, Formatter, FormattersByExtendedTarget, FormattersByTarget, RangeOfProperties, RangeOfFormats, DEFAULT_RANGEOF_FORMATS, RangeOf } } export { Experimental } /** * An experimental array template which extend built-in `Array` to include a handful * of predicates which return `Result`. * @beta */ declare class ExtendedArray extends Array { readonly itemDescription: string; /** * Constructs an {@link Experimental.ExtendedArray | ExtendedArray}. * @param itemDescription - Brief description of the type of each item in this array. * @param items - The initial contents of the array. */ constructor(itemDescription: string, ...items: T[]); /** * Type guard to determine if some arbitrary array is an * {@link Experimental.ExtendedArray} * @param a - The `Array` to be tested. * @returns Returns `true` if `a` is an {@link Experimental.ExtendedArray | ExtendedArray}, * `false` otherwise. */ static isExtendedArray(a?: T[]): a is ExtendedArray; /** * Determines if this array contains exactly one element which matches * a supplied predicate. * @param predicate - The predicate function to be applied. * @returns Returns `Success` with the single matching * result if exactly one item matches `predicate`. Returns `Failure` * with an error message if there are no matches or more than one match. */ single(predicate?: (item: T) => boolean): Result; /** * Returns the first element of an {@link Experimental.ExtendedArray | ExtendedArray}. Fails with an * error message if the array is empty. * @param failMessage - Optional message to be displayed in the event of failure. * @returns Returns `Success` with the value of the first element * in the array, or `Failure` with an error message if the array is empty. */ first(failMessage?: string): Result; /** * Returns an array containing all elements of an {@link Experimental.ExtendedArray | ExtendedArray}. * Fails with an error message if the array is empty. * @param failMessage - Optional message to be displayed in the event of failure. * @returns Returns `Success` with a new (non-extended) `Array` * containing the elements of this array, or `Failure` with an error message * if the array is empty. */ atLeastOne(failMessage?: string): Result; /** * Gets a new (non-extended) `Array` containing all of the elements from this * {@link Experimental.ExtendedArray | ExtendedArray}. * @returns A new (non-extended) `Array`. */ all(): T[]; } /** * A helper function to create a `Converter` which converts `unknown` to {@link Experimental.ExtendedArray | ExtendedArray}. * @remarks * If `onError` is `'failOnError'` (default), then the entire conversion fails if any element cannot * be converted. If `onError` is `'ignoreErrors'`, then failing elements are silently ignored. * @param converter - `Converter` used to convert each item in the array * @param ignoreErrors - Specifies treatment of unconvertible elements * @beta */ declare function extendedArrayOf(label: string, converter: Converter, onError?: Conversion.OnError): Converter, TC>; /** * Formats a list of items using the supplied template and formatter, one result * per output line. * @param format - A mustache template used to format each item. * @param items - The items to be formatted. * @param itemFormatter - The {@link Experimental.Formatter | Formatter} used to format each item. * @returns The resulting string. * @beta */ declare function formatList(format: string, items: T[], itemFormatter: Formatter): Result; /** * Interface for an object that can be formatted. * @beta */ declare interface Formattable { /** * Formats an object using the supplied mustache template. * @param format - A mustache template used to format the object. * @returns `Success` with the resulting string, or `Failure` * with an error message if an error occurs. */ format(format: string): Result; } /** * Base class which adds common formatting. * @beta */ declare class FormattableBase { /** * Helper enables derived classes to add named details to a formatted presentation. * @param details - An array of detail description strings. * @param label - Label to use for the new detail. * @param value - Value to use for the new detail. * @internal */ protected static _tryAddDetail(details: string[], label: string, value: string | undefined): void; /** * {@inheritdoc Experimental.Formattable.format} */ format(template: string): Result; } /** * Destination format for some formatted string. * @beta */ declare type FormatTargets = 'text' | 'markdown' | 'embed'; /** * Type definition for a formatting function, which takes a `string` and an * item and returns `Result`. * @beta */ declare type Formatter = (format: string, item: T) => Result; /** * A collection of {@link Experimental.Formatter | formatters} indexed by target name, to enable * different format methods per output target. * @beta */ declare type FormattersByExtendedTarget = Record>; /** * A collection of {@link Experimental.Formatter | formatters} indexed by the * {@link Experimental.FormatTargets | default supported target formats}. * @beta */ declare type FormattersByTarget = FormattersByExtendedTarget; declare namespace Hash { export { Md5Normalizer } } export { Hash } /** * A `Converter` which converts an iso formatted string, a number or a `Date` object to * a `Date` object. * @public */ declare const isoDate: Converter; /** * @public */ declare type JarFieldPicker = (record: T) => (keyof T)[]; /** * Represents a single record in a JAR file * @public */ declare type JarRecord = Record; /** * Options for a JAR record parser. * @public */ declare interface JarRecordParserOptions { readonly arrayFields?: string[] | JarFieldPicker; readonly fixedContinuationSize?: number; } /** * A hashing normalizer which computes object * hash using the MD5 algorithm. * @public */ declare class Md5Normalizer extends Hash_2.HashingNormalizer { constructor(); static md5Hash(parts: string[]): string; } /** * Parses CSV data from a string. * @param body - The CSV string to parse. * @param options - optional parameters to control the processing * @returns The parsed CSV data. * @beta */ declare function parseCsvString(body: string, options?: CsvOptions): Result; /** * Reads a record-jar from an array of strings, each of which represents one * line in the source file. * @param lines - the array of strings to be parsed * @param options - Optional parser configuration * @returns a corresponding array of `Record` * @public */ declare function parseRecordJarLines(lines: string[], options?: JarRecordParserOptions): Result; /** * Simple implementation of a possibly open-ended range of some comparable * type `` with test and formatting. * @public */ declare class RangeOf implements RangeOfProperties { /** * Minimum extent of the range. */ readonly min?: T; /** * Maximum extent of the range. */ readonly max?: T; /** * Creates a new {@link Experimental.RangeOf | RangeOf}. * @param min - Optional minimum extent of the range. * @param max - Optional maximum extent of the range. */ constructor(min?: T, max?: T); /** * Static constructor for a {@link Experimental.RangeOf | RangeOf}. * @param init - {@link Experimental.RangeOfProperties | Range initializer}. * @returns A new {@link Experimental.RangeOf | RangeOf}. */ static createRange(init?: RangeOfProperties): Result>; /** * Gets a formatted description of a {@link Experimental.RangeOfProperties | RangeOfProperties} given an * optional set of formats and 'empty' value to use. * @param range - The {@link Experimental.RangeOfProperties | RangeOfProperties} to be formatted. * @param formats - Optional {@link Experimental.RangeOfFormats | formats} to use. Default is * {@link Experimental.DEFAULT_RANGEOF_FORMATS | DEFAULT_RANGEOF_FORMATS}. * @param emptyValue - Value which represents unbounded minimum or maximum for this range. Default is `undefined`. * @returns A string representation of the range. */ static propertiesToString(range: RangeOfProperties, formats?: RangeOfFormats, emptyValue?: T): string | undefined; /** * Default comparison uses javascript built-in comparison. * @param t1 - First value to be compared. * @param t2 - Second value to be compared. * @returns `'less'` if `t1` is less than `t2`, `'greater'` if `t1` is larger * and `'equal'` if `t1` and `t2` are equal. * @internal */ protected static _defaultCompare(t1: T, t2: T): 'less' | 'equal' | 'greater'; /** * Checks if a supplied value is within this range. * @param t - The value to be tested. * @returns `'included'` if `t` falls within the range, `'less'` if `t` falls * below the minimum extent of the range and `'greater'` if `t` is above the * maximum extent. */ check(t: T): 'less' | 'included' | 'greater'; /** * Determines if a supplied value is within this range. * @param t - The value to be tested. * @returns Returns `true` if `t` falls within the range, `false` otherwise. */ includes(t: T): boolean; /** * Finds the transition value that would bring a supplied value `t` into * range. * @param t - The value to be tested. * @returns The minimum extent of the range if `t` is below the range or * the maximum extent of the range if `t` is above the range. Returns * `undefined` if `t` already falls within the range. */ findTransition(t: T): T | undefined; /** * Formats the minimum and maximum values of this range. * @param format - A format function used to format the values. * @returns A {@link Experimental.RangeOfProperties | RangeOfProperties} containing the * formatted representation of the {@link Experimental.RangeOf.min | minimum} and * {@link Experimental.RangeOf.max | maximum} * extent of the range, or `undefined` for an extent that is not present. */ toFormattedProperties(format: (value: T) => string | undefined): RangeOfProperties; /** * Formats this range using the supplied format function. * @param format - Format function used to format minimum and maximum extent values. * @param formats - The {@link Experimental.RangeOfFormats | format strings} used to format the range * (default {@link Experimental.DEFAULT_RANGEOF_FORMATS}). * @returns Returns a formatted representation of this range. */ format(format: (value: T) => string | undefined, formats?: RangeOfFormats): string | undefined; /** * Inner compare method can be overridden by a derived class. * @param t1 - First value to compare. * @param t2 - Second value to compare. * @returns `'less'` if `t1` is less than `t2`, `'greater'` if `t1` is larger * and `'equal'` if `t1` and `t2` are equal. * @internal */ protected _compare(t1: T, t2: T): 'less' | 'equal' | 'greater'; } /** * A helper wrapper to construct a `Converter` which converts to {@link Experimental.RangeOf | RangeOf} * where `` is some comparable type. * @param converter - `Converter` used to convert `min` and `max` extent of the range. * @public */ declare function rangeOf(converter: Converter): Converter, TC>; /** * Format strings (in mustache format) to * use for both open-ended and complete * {@link Experimental.RangeOf | RangeOf}. * @public */ declare interface RangeOfFormats { minOnly: string; maxOnly: string; minMax: string; } /** * Represents a generic range of some comparable type ``. * @public */ declare interface RangeOfProperties { readonly min?: T; readonly max?: T; } /** * A helper wrapper to construct a `Converter` which converts to an arbitrary strongly-typed * range of some comparable type. * @param converter - `Converter` used to convert `min` and `max` extent of the range. * @param constructor - Static constructor to instantiate the object. * @public */ declare function rangeTypeOf, TC = unknown>(converter: Converter, constructor: (init: RangeOfProperties) => Result): Converter; /** * Reads a CSV file from a supplied path. * @param srcPath - Source path from which the file is read. * @param options - optional parameters to control the processing * @returns The contents of the file. * @beta */ declare function readCsvFileSync(srcPath: string, options?: CsvOptions): Result; /** * Reads a CSV file from a FileTree. * @param fileTree - The FileTree to read from. * @param filePath - Path of the file within the tree. * @param options - optional parameters to control the processing * @returns The parsed CSV data. * @beta */ declare function readCsvFromTree(fileTree: FileTree.FileTree, filePath: string, options?: CsvOptions): Result; /** * Reads a record-jar file from a supplied path. * @param srcPath - Source path from which the file is read. * @param options - Optional parser configuration * @returns The contents of the file as an array of `Record` * @see https://datatracker.ietf.org/doc/html/draft-phillips-record-jar-01 * @public */ declare function readRecordJarFileSync(srcPath: string, options?: JarRecordParserOptions): Result; /** * Reads a record-jar file from a FileTree. * @param fileTree - The FileTree to read from. * @param filePath - Path of the file within the tree. * @param options - Optional parser configuration * @returns The contents of the file as an array of `Record` * @see https://datatracker.ietf.org/doc/html/draft-phillips-record-jar-01 * @public */ declare function readRecordJarFromTree(fileTree: FileTree.FileTree, filePath: string, options?: JarRecordParserOptions): Result; declare namespace RecordJar { export { parseRecordJarLines, JarRecord, JarFieldPicker, JarRecordParserOptions, readRecordJarFileSync, readRecordJarFromTree } } export { RecordJar } /** * Helper function to create a `StringConverter` which converts * `unknown` to `string`, applying template conversions supplied at construction time or at * runtime as context. * @remarks * Template conversions are applied using `mustache` syntax. * @param defaultContext - Optional default context to use for template values. * @returns A new `Converter` returning `string`. * @public */ declare function templateString(defaultContext?: unknown): Conversion.StringConverter; /** * Implementation of `IFileTreeDirectoryItem` for directories in a ZIP archive. * @public */ declare class ZipDirectoryItem implements FileTree.IFileTreeDirectoryItem { /** * Indicates that this `FileTree.FileTreeItem` is a directory. */ readonly type: 'directory'; /** * The absolute path of the directory within the ZIP archive. */ readonly absolutePath: string; /** * The name of the directory */ readonly name: string; /** * The ZIP file tree accessors that created this item. */ private readonly _accessors; /** * Constructor for ZipDirectoryItem. * @param directoryPath - The path of the directory within the ZIP. * @param accessors - The ZIP file tree accessors. */ constructor(directoryPath: string, accessors: ZipFileTreeAccessors); /** * Gets the children of the directory. */ getChildren(): Result>>; } /** * Implementation of `FileTree.IFileTreeFileItem` for files in a ZIP archive. * @public */ declare class ZipFileItem implements FileTree.IFileTreeFileItem { /** * Indicates that this `FileTree.FileTreeItem` is a file. */ readonly type: 'file'; /** * The absolute path of the file within the ZIP archive. */ readonly absolutePath: string; /** * The name of the file */ readonly name: string; /** * The base name of the file (without extension) */ readonly baseName: string; /** * The extension of the file */ readonly extension: string; /** * The content type of the file. */ get contentType(): TCT | undefined; /** * The pre-loaded contents of the file. */ private readonly _contents; /** * The ZIP file tree accessors that created this item. */ private readonly _accessors; /** * Mutable content type of the file. */ private _contentType; /** * Flag to track if content type should be inferred on first access. */ private _shouldInferContentType; /** * Constructor for ZipFileItem. * @param zipFilePath - The path of the file within the ZIP. * @param contents - The pre-loaded contents of the file. * @param accessors - The ZIP file tree accessors. */ constructor(zipFilePath: string, contents: string, accessors: ZipFileTreeAccessors); /** * Sets the content type of the file. * @param contentType - The content type of the file. */ setContentType(contentType: TCT | undefined): void; /** * Gets the contents of the file as parsed JSON. */ getContents(): Result; getContents(converter: Validator | Converter): Result; /** * Gets the raw contents of the file as a string. */ getRawContents(): Result; } declare namespace ZipFileTree { export { ZipFileTreeAccessors, ZipFileItem, ZipDirectoryItem } } export { ZipFileTree } /** * File tree accessors for ZIP archives. * @public */ declare class ZipFileTreeAccessors implements FileTree.IFileTreeAccessors { /** * The unzipped file data. */ private readonly _files; /** * Optional prefix to prepend to paths. */ private readonly _prefix; /** * Content type inference function. */ private readonly _inferContentType; /** * Cache of all items in the ZIP for efficient lookups. */ private readonly _itemCache; /** * Constructor for ZipFileTreeAccessors. * @param files - The unzipped file data from fflate. * @param params - Optional initialization parameters. */ private constructor(); /** * Default function to infer the content type of a file. * @param filePath - The path of the file. * @param provided - Optional supplied content type. * @returns `Success` with the content type of the file if successful, or * `Failure` with an error message otherwise. * @remarks This default implementation always returns `Success` with `undefined`. * @public */ static defaultInferContentType(__filePath: string, __provided?: string): Result; /** * Creates a new ZipFileTreeAccessors instance from a ZIP file buffer (synchronous). * @param zipBuffer - The ZIP file as an ArrayBuffer or Uint8Array. * @param prefix - Optional prefix to prepend to paths. * @returns Result containing the ZipFileTreeAccessors instance. */ static fromBuffer(zipBuffer: ArrayBuffer | Uint8Array, prefix?: string): Result>; /** * Creates a new ZipFileTreeAccessors instance from a ZIP file buffer (synchronous). * @param zipBuffer - The ZIP file as an ArrayBuffer or Uint8Array. * @param params - Optional initialization parameters. * @returns Result containing the ZipFileTreeAccessors instance. */ static fromBuffer(zipBuffer: ArrayBuffer | Uint8Array, params?: FileTree.IFileTreeInitParams): Result>; /** * Creates a new ZipFileTreeAccessors instance from a ZIP file buffer (asynchronous). * @param zipBuffer - The ZIP file as an ArrayBuffer or Uint8Array. * @param prefix - Optional prefix to prepend to paths. * @returns Promise containing Result with the ZipFileTreeAccessors instance. */ static fromBufferAsync(zipBuffer: ArrayBuffer | Uint8Array, prefix?: string): Promise>>; /** * Creates a new ZipFileTreeAccessors instance from a ZIP file buffer (asynchronous). * @param zipBuffer - The ZIP file as an ArrayBuffer or Uint8Array. * @param params - Optional initialization parameters. * @returns Promise containing Result with the ZipFileTreeAccessors instance. */ static fromBufferAsync(zipBuffer: ArrayBuffer | Uint8Array, params?: FileTree.IFileTreeInitParams): Promise>>; /** * Creates a new ZipFileTreeAccessors instance from a File object (browser environment). * @param file - The File object containing ZIP data. * @param params - Optional initialization parameters. * @returns Result containing the ZipFileTreeAccessors instance. */ static fromFile(file: File, params?: FileTree.IFileTreeInitParams): Promise>>; /** * Builds the cache of all items in the ZIP archive. */ private _buildItemCache; /** * Resolves paths to an absolute path. */ resolveAbsolutePath(...paths: string[]): string; /** * Gets the extension of a path. */ getExtension(path: string): string; /** * Gets the base name of a path. */ getBaseName(path: string, suffix?: string): string; /** * Joins paths together. */ joinPaths(...paths: string[]): string; /** * Gets an item from the file tree. */ getItem(path: string): Result>; /** * Gets the contents of a file in the file tree. */ getFileContents(path: string): Result; /** * Gets the content type of a file in the file tree. */ getFileContentType(path: string, provided?: string): Result; /** * Gets the children of a directory in the file tree. */ getChildren(path: string): Result>>; /** * Checks if childPath is a direct child of parentPath. */ private _isDirectChild; } export { }