From b04655e76dddefab6722908c7c826fedda1ec6e6 Mon Sep 17 00:00:00 2001 From: Jean-Martin Thibault Date: Sat, 31 Jan 2015 17:55:30 -0500 Subject: [PATCH] added support for custom nconf.formats and added missing format in IFileOptions --- nconf/nconf-tests.ts | 25 ++++++++++++++++++++++++- nconf/nconf.d.ts | 14 ++++++++++---- 2 files changed, 34 insertions(+), 5 deletions(-) diff --git a/nconf/nconf-tests.ts b/nconf/nconf-tests.ts index 13b314af8e..7037abfb60 100644 --- a/nconf/nconf-tests.ts +++ b/nconf/nconf-tests.ts @@ -13,6 +13,7 @@ var opts: nconf.IOptions; var fopts: nconf.IFileOptions; var store: nconf.IStore; var callback: (err: Error) => void; +var fmt: nconf.IFormat; value = nconf.clear(str, callback); value = nconf.get (str, callback); @@ -37,7 +38,10 @@ p = nconf.env(opts); p = nconf.file(str); p = nconf.file(str, fopts); p = nconf.file(fopts); - +p = nconf.file({ + format: fmt, + file: 'jsonFile.custom' +}); p = nconf.use(str); p = nconf.use(str, opts); @@ -92,6 +96,18 @@ p = p.env(opts); p = p.file(str); p = p.file(str, fopts); p = p.file(fopts); +p = p.file({ + file: 'iniFile.ini', + format: nconf.formats.ini +}); +p = p.file({ + file: 'jsonFile.json', + format: nconf.formats.json +}); +p = p.file({ + file: 'jsonFile.custom', + format: fmt +}); p = p.use(str, opts); p = p.defaults(opts); @@ -99,3 +115,10 @@ p.init(opts); p = p.overrides(opts); p.remove(str); store = p.create(str, opts); + +// - - - - - - - - - - - - - - - - - - - - - - - - - + +class CustomFmt implements nconf.IFormat { + stringify(obj: any, replacer: any, spacing?: any): string { return ""; } + parse(str: string): any { return {} } +}; diff --git a/nconf/nconf.d.ts b/nconf/nconf.d.ts index 2ebd18a150..ee59591fd8 100644 --- a/nconf/nconf.d.ts +++ b/nconf/nconf.d.ts @@ -1,6 +1,6 @@ // Type definitions for nconf // Project: https://github.com/flatiron/nconf -// Definitions by: Jeff Goddard +// Definitions by: Jeff Goddard , Jean-Martin Thibault // Definitions: https://github.com/borisyankov/DefinitelyTyped // Imported from: https://github.com/soywiz/typescript-node-definitions/nconf.d.ts @@ -38,9 +38,14 @@ declare module "nconf" { export function loadFiles(files: any, callback?: ICallbackFunction): void; export function loadFilesSync(files: any, callback?: ICallbackFunction): void; - export enum formats { - json, - ini + export var formats: { + json: IFormat; + ini: IFormat; + }; + + export interface IFormat { + stringify: (obj: any, replacer: any, spacing?: any) => string; + parse: (str: string) => any; } export interface IOptions { @@ -51,6 +56,7 @@ declare module "nconf" { file?: string; dir?: string; search?: boolean; + format?: IFormat; json_spacing?: number; }