@fgv/ts-json-base
Advanced tools
Comparing version 3.0.1-alpha.2 to 3.0.1-alpha.3
@@ -29,7 +29,7 @@ import { Converter } from '@fgv/ts-utils'; | ||
* @param srcPath - The path of the folder to be read. | ||
* @param options - {@link JsonFile.IDirectoryConvertOptions | Options} to control | ||
* @param options - {@link JsonFile.IJsonFsDirectoryOptions | Options} to control | ||
* conversion and filtering | ||
* @public | ||
*/ | ||
declare function convertJsonDirectorySync<T>(srcPath: string, options: IDirectoryConvertOptions<T>): Result<IReadDirectoryItem<T>[]>; | ||
declare function convertJsonDirectorySync<T>(srcPath: string, options: IJsonFsDirectoryOptions<T>): Result<IReadDirectoryItem<T>[]>; | ||
@@ -41,7 +41,7 @@ /** | ||
* @param srcPath - The path of the folder to be read. | ||
* @param options - {@link JsonFile.IDirectoryToMapConvertOptions | Options} to control conversion, | ||
* @param options - {@link JsonFile.IJsonFsDirectoryToMapOptions | Options} to control conversion, | ||
* filtering and naming. | ||
* @public | ||
*/ | ||
declare function convertJsonDirectoryToMapSync<T, TC = unknown>(srcPath: string, options: IDirectoryToMapConvertOptions<T, TC>): Result<Map<string, T>>; | ||
declare function convertJsonDirectoryToMapSync<T, TC = unknown>(srcPath: string, options: IJsonFsDirectoryToMapOptions<T, TC>): Result<Map<string, T>>; | ||
@@ -74,2 +74,10 @@ /** | ||
/** | ||
* Conversion context for JSON converters. | ||
* @public | ||
*/ | ||
declare interface IJsonConverterContext { | ||
ignoreUndefinedProperties?: boolean; | ||
} | ||
/** | ||
* Options for directory conversion. | ||
@@ -79,8 +87,11 @@ * TODO: add filtering, allowed and excluded. | ||
*/ | ||
declare interface IDirectoryConvertOptions<T, TC = unknown> { | ||
declare interface IJsonFsDirectoryOptions<T, TC = unknown> { | ||
/** | ||
* The converter used to convert incoming JSON objects. | ||
*/ | ||
converter: Converter<T, TC>; | ||
validator?: undefined; | ||
converter: Converter<T, TC> | Validator<T, TC>; | ||
/** | ||
* Filter applied to items in the directory | ||
*/ | ||
files?: RegExp[]; | ||
} | ||
@@ -92,3 +103,3 @@ | ||
*/ | ||
declare interface IDirectoryToMapConvertOptions<T, TC = unknown> extends IDirectoryConvertOptions<T, TC> { | ||
declare interface IJsonFsDirectoryToMapOptions<T, TC = unknown> extends IJsonFsDirectoryOptions<T, TC> { | ||
transformName?: ItemNameTransformFunction<T>; | ||
@@ -98,30 +109,2 @@ } | ||
/** | ||
* Options controlling validation of a directory to a `Map`. | ||
* @public | ||
*/ | ||
declare interface IDirectoryToMapValidateOptions<T, TC = unknown> extends IDirectoryValidateOptions<T, TC> { | ||
transformName?: ItemNameTransformFunction<T>; | ||
} | ||
/** | ||
* Options for directory validation. | ||
* @public | ||
*/ | ||
declare interface IDirectoryValidateOptions<T, TC = unknown> { | ||
converter?: undefined; | ||
/** | ||
* The validator used to validate incoming JSON objects | ||
*/ | ||
validator: Validator<T, TC>; | ||
} | ||
/** | ||
* Conversion context for JSON converters. | ||
* @public | ||
*/ | ||
declare interface IJsonConverterContext { | ||
ignoreUndefinedProperties?: boolean; | ||
} | ||
/** | ||
* Configuration for {@link JsonFile.JsonFsHelper | JsonFsHelper}. | ||
@@ -133,2 +116,3 @@ * @public | ||
allowUndefinedWrite: boolean; | ||
defaultFiles: RegExp[]; | ||
} | ||
@@ -235,10 +219,6 @@ | ||
writeJsonFileSync, | ||
IDirectoryConvertOptions, | ||
IDirectoryValidateOptions, | ||
JsonFsDirectoryOptions, | ||
IJsonFsDirectoryOptions, | ||
IReadDirectoryItem, | ||
ItemNameTransformFunction, | ||
IDirectoryToMapConvertOptions, | ||
IDirectoryToMapValidateOptions, | ||
JsonFsDirectoryToMapOptions, | ||
IJsonFsDirectoryToMapOptions, | ||
IJsonFsHelperConfig, | ||
@@ -260,14 +240,2 @@ JsonFsHelperInitOptions, | ||
/** | ||
* Options for directory conversion or validation. | ||
* @public | ||
*/ | ||
declare type JsonFsDirectoryOptions<T, TC = unknown> = IDirectoryConvertOptions<T, TC> | IDirectoryValidateOptions<T, TC>; | ||
/** | ||
* Options controlling processing of a directory to a `Map`. | ||
* @public | ||
*/ | ||
declare type JsonFsDirectoryToMapOptions<T, TC = unknown> = IDirectoryToMapConvertOptions<T, TC> | IDirectoryToMapValidateOptions<T, TC>; | ||
/** | ||
* Helper class to simplify common filesystem operations involving JSON (or JSON-like) | ||
@@ -306,6 +274,6 @@ * files. | ||
* @param srcPath - The path of the folder to be read. | ||
* @param options - {@link JsonFile.JsonFsDirectoryOptions | Options} to control | ||
* @param options - {@link JsonFile.IJsonFsDirectoryOptions | Options} to control | ||
* conversion and filtering | ||
*/ | ||
convertJsonDirectorySync<T>(srcPath: string, options: JsonFsDirectoryOptions<T>): Result<IReadDirectoryItem<T>[]>; | ||
convertJsonDirectorySync<T>(srcPath: string, options: IJsonFsDirectoryOptions<T>): Result<IReadDirectoryItem<T>[]>; | ||
/** | ||
@@ -316,6 +284,6 @@ * Reads and converts or validates all JSON files from a directory, returning a | ||
* @param srcPath - The path of the folder to be read. | ||
* @param options - {@link JsonFile.JsonFsDirectoryToMapOptions | Options} to control conversion, | ||
* @param options - {@link JsonFile.IJsonFsDirectoryToMapOptions | Options} to control conversion, | ||
* filtering and naming. | ||
*/ | ||
convertJsonDirectoryToMapSync<T, TC = unknown>(srcPath: string, options: JsonFsDirectoryToMapOptions<T, TC>): Result<Map<string, T>>; | ||
convertJsonDirectoryToMapSync<T, TC = unknown>(srcPath: string, options: IJsonFsDirectoryToMapOptions<T, TC>): Result<Map<string, T>>; | ||
/** | ||
@@ -327,2 +295,3 @@ * Write type-safe JSON to a file. | ||
writeJsonFileSync(srcPath: string, value: JsonValue): Result<boolean>; | ||
protected _pathMatchesOptions<T, TC>(options: IJsonFsDirectoryOptions<T, TC>, path: string): boolean; | ||
} | ||
@@ -329,0 +298,0 @@ |
import { Converter, Result } from '@fgv/ts-utils'; | ||
import { JsonValue } from '../json'; | ||
import { IDirectoryConvertOptions, IDirectoryToMapConvertOptions, IReadDirectoryItem } from './jsonFsHelper'; | ||
import { IJsonFsDirectoryOptions, IJsonFsDirectoryToMapOptions, IReadDirectoryItem } from './jsonFsHelper'; | ||
/** | ||
@@ -20,7 +20,7 @@ * {@inheritdoc JsonFile.JsonFsHelper.readJsonFileSync} | ||
* @param srcPath - The path of the folder to be read. | ||
* @param options - {@link JsonFile.IDirectoryConvertOptions | Options} to control | ||
* @param options - {@link JsonFile.IJsonFsDirectoryOptions | Options} to control | ||
* conversion and filtering | ||
* @public | ||
*/ | ||
export declare function convertJsonDirectorySync<T>(srcPath: string, options: IDirectoryConvertOptions<T>): Result<IReadDirectoryItem<T>[]>; | ||
export declare function convertJsonDirectorySync<T>(srcPath: string, options: IJsonFsDirectoryOptions<T>): Result<IReadDirectoryItem<T>[]>; | ||
/** | ||
@@ -31,7 +31,7 @@ * Reads and converts all JSON files from a directory, returning a | ||
* @param srcPath - The path of the folder to be read. | ||
* @param options - {@link JsonFile.IDirectoryToMapConvertOptions | Options} to control conversion, | ||
* @param options - {@link JsonFile.IJsonFsDirectoryToMapOptions | Options} to control conversion, | ||
* filtering and naming. | ||
* @public | ||
*/ | ||
export declare function convertJsonDirectoryToMapSync<T, TC = unknown>(srcPath: string, options: IDirectoryToMapConvertOptions<T, TC>): Result<Map<string, T>>; | ||
export declare function convertJsonDirectoryToMapSync<T, TC = unknown>(srcPath: string, options: IJsonFsDirectoryToMapOptions<T, TC>): Result<Map<string, T>>; | ||
/** | ||
@@ -38,0 +38,0 @@ * {@inheritDoc JsonFile.JsonFsHelper.writeJsonFileSync} |
@@ -49,3 +49,3 @@ "use strict"; | ||
* @param srcPath - The path of the folder to be read. | ||
* @param options - {@link JsonFile.IDirectoryConvertOptions | Options} to control | ||
* @param options - {@link JsonFile.IJsonFsDirectoryOptions | Options} to control | ||
* conversion and filtering | ||
@@ -63,3 +63,3 @@ * @public | ||
* @param srcPath - The path of the folder to be read. | ||
* @param options - {@link JsonFile.IDirectoryToMapConvertOptions | Options} to control conversion, | ||
* @param options - {@link JsonFile.IJsonFsDirectoryToMapOptions | Options} to control conversion, | ||
* filtering and naming. | ||
@@ -66,0 +66,0 @@ * @public |
@@ -9,26 +9,13 @@ import { Converter, Result, Validator } from '@fgv/ts-utils'; | ||
*/ | ||
export interface IDirectoryConvertOptions<T, TC = unknown> { | ||
export interface IJsonFsDirectoryOptions<T, TC = unknown> { | ||
/** | ||
* The converter used to convert incoming JSON objects. | ||
*/ | ||
converter: Converter<T, TC>; | ||
validator?: undefined; | ||
} | ||
/** | ||
* Options for directory validation. | ||
* @public | ||
*/ | ||
export interface IDirectoryValidateOptions<T, TC = unknown> { | ||
converter?: undefined; | ||
converter: Converter<T, TC> | Validator<T, TC>; | ||
/** | ||
* The validator used to validate incoming JSON objects | ||
* Filter applied to items in the directory | ||
*/ | ||
validator: Validator<T, TC>; | ||
files?: RegExp[]; | ||
} | ||
/** | ||
* Options for directory conversion or validation. | ||
* @public | ||
*/ | ||
export type JsonFsDirectoryOptions<T, TC = unknown> = IDirectoryConvertOptions<T, TC> | IDirectoryValidateOptions<T, TC>; | ||
/** | ||
* Return value for one item in a directory conversion. | ||
@@ -57,18 +44,6 @@ * @public | ||
*/ | ||
export interface IDirectoryToMapConvertOptions<T, TC = unknown> extends IDirectoryConvertOptions<T, TC> { | ||
export interface IJsonFsDirectoryToMapOptions<T, TC = unknown> extends IJsonFsDirectoryOptions<T, TC> { | ||
transformName?: ItemNameTransformFunction<T>; | ||
} | ||
/** | ||
* Options controlling validation of a directory to a `Map`. | ||
* @public | ||
*/ | ||
export interface IDirectoryToMapValidateOptions<T, TC = unknown> extends IDirectoryValidateOptions<T, TC> { | ||
transformName?: ItemNameTransformFunction<T>; | ||
} | ||
/** | ||
* Options controlling processing of a directory to a `Map`. | ||
* @public | ||
*/ | ||
export type JsonFsDirectoryToMapOptions<T, TC = unknown> = IDirectoryToMapConvertOptions<T, TC> | IDirectoryToMapValidateOptions<T, TC>; | ||
/** | ||
* Configuration for {@link JsonFile.JsonFsHelper | JsonFsHelper}. | ||
@@ -80,2 +55,3 @@ * @public | ||
allowUndefinedWrite: boolean; | ||
defaultFiles: RegExp[]; | ||
} | ||
@@ -126,6 +102,6 @@ /** | ||
* @param srcPath - The path of the folder to be read. | ||
* @param options - {@link JsonFile.JsonFsDirectoryOptions | Options} to control | ||
* @param options - {@link JsonFile.IJsonFsDirectoryOptions | Options} to control | ||
* conversion and filtering | ||
*/ | ||
convertJsonDirectorySync<T>(srcPath: string, options: JsonFsDirectoryOptions<T>): Result<IReadDirectoryItem<T>[]>; | ||
convertJsonDirectorySync<T>(srcPath: string, options: IJsonFsDirectoryOptions<T>): Result<IReadDirectoryItem<T>[]>; | ||
/** | ||
@@ -136,6 +112,6 @@ * Reads and converts or validates all JSON files from a directory, returning a | ||
* @param srcPath - The path of the folder to be read. | ||
* @param options - {@link JsonFile.JsonFsDirectoryToMapOptions | Options} to control conversion, | ||
* @param options - {@link JsonFile.IJsonFsDirectoryToMapOptions | Options} to control conversion, | ||
* filtering and naming. | ||
*/ | ||
convertJsonDirectoryToMapSync<T, TC = unknown>(srcPath: string, options: JsonFsDirectoryToMapOptions<T, TC>): Result<Map<string, T>>; | ||
convertJsonDirectoryToMapSync<T, TC = unknown>(srcPath: string, options: IJsonFsDirectoryToMapOptions<T, TC>): Result<Map<string, T>>; | ||
/** | ||
@@ -147,2 +123,3 @@ * Write type-safe JSON to a file. | ||
writeJsonFileSync(srcPath: string, value: JsonValue): Result<boolean>; | ||
protected _pathMatchesOptions<T, TC>(options: IJsonFsDirectoryOptions<T, TC>, path: string): boolean; | ||
} | ||
@@ -149,0 +126,0 @@ /** |
@@ -66,3 +66,4 @@ "use strict"; | ||
json: jsonLike_1.DefaultJsonLike, | ||
allowUndefinedWrite: false | ||
allowUndefinedWrite: false, | ||
defaultFiles: [/.*.json/] | ||
}; | ||
@@ -111,3 +112,3 @@ /** | ||
* @param srcPath - The path of the folder to be read. | ||
* @param options - {@link JsonFile.JsonFsDirectoryOptions | Options} to control | ||
* @param options - {@link JsonFile.IJsonFsDirectoryOptions | Options} to control | ||
* conversion and filtering | ||
@@ -124,6 +125,5 @@ */ | ||
.map((fi) => { | ||
var _a; | ||
if (fi.isFile() && path.extname(fi.name) === '.json') { | ||
if (fi.isFile() && this._pathMatchesOptions(options, fi.name)) { | ||
const filePath = path.resolve(fullPath, fi.name); | ||
return this.convertJsonFileSync(filePath, (_a = options.converter) !== null && _a !== void 0 ? _a : options.validator) | ||
return this.convertJsonFileSync(filePath, options.converter) | ||
.onSuccess((payload) => { | ||
@@ -150,3 +150,3 @@ return (0, ts_utils_1.succeed)({ | ||
* @param srcPath - The path of the folder to be read. | ||
* @param options - {@link JsonFile.JsonFsDirectoryToMapOptions | Options} to control conversion, | ||
* @param options - {@link JsonFile.IJsonFsDirectoryToMapOptions | Options} to control conversion, | ||
* filtering and naming. | ||
@@ -184,2 +184,7 @@ */ | ||
} | ||
_pathMatchesOptions(options, path) { | ||
var _a; | ||
const match = (_a = options.files) !== null && _a !== void 0 ? _a : this.config.defaultFiles; | ||
return match.some((m) => m.exec(path)); | ||
} | ||
} | ||
@@ -186,0 +191,0 @@ exports.JsonFsHelper = JsonFsHelper; |
{ | ||
"name": "@fgv/ts-json-base", | ||
"version": "3.0.1-alpha.2", | ||
"version": "3.0.1-alpha.3", | ||
"description": "Typescript types and basic functions for working with json", | ||
@@ -19,4 +19,4 @@ "main": "lib/index.js", | ||
"devDependencies": { | ||
"@fgv/ts-utils": "3.0.1-alpha.2", | ||
"@fgv/ts-utils-jest": "3.0.1-alpha.2", | ||
"@fgv/ts-utils": "3.0.1-alpha.3", | ||
"@fgv/ts-utils-jest": "3.0.1-alpha.3", | ||
"@types/jest": "^29.5.12", | ||
@@ -47,3 +47,3 @@ "@types/mustache": "^4.2.5", | ||
"peerDependencies": { | ||
"@fgv/ts-utils": "3.0.1-alpha.2", | ||
"@fgv/ts-utils": "3.0.1-alpha.3", | ||
"mustache": "^4.2.0" | ||
@@ -50,0 +50,0 @@ }, |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
149373
1730