Skip to content

Commit 6ba6fcf

Browse files
committed
Added typescript definitions and jsdocs
1 parent 684477e commit 6ba6fcf

File tree

1 file changed

+253
-0
lines changed

1 file changed

+253
-0
lines changed

lib/index.d.ts

Lines changed: 253 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,253 @@
1+
declare module "properties" {
2+
export interface ParseOptions {
3+
/**
4+
* By default `parse()` reads a String.
5+
* If you want to read a file, set this option to true.
6+
* If this option is used, the callback is mandatory.
7+
* It gets 2 parameters, a possible error and the object with all the properties.
8+
*/
9+
path?: boolean;
10+
11+
/**
12+
* Allows you to add additional comment tokens.
13+
* The token must be a single printable non-whitespae ascii character.
14+
* If the `strict` option is not set, the tokens `#` and `!` are parsed as comment tokens.
15+
*
16+
* @example
17+
* comments: ";"
18+
* @example
19+
* comments: [";", "@"]
20+
*/
21+
comments?: string | string[];
22+
23+
/**
24+
* Allows you to add additional separator tokens.
25+
* The token must be a single printable non-whitespae ascii character.
26+
* If the `strict` option is not set, the tokens `=` and `:` are parsed as comment tokens.
27+
*
28+
* @example
29+
* separators: "-"
30+
* @example
31+
* separators: ["-", ">"]
32+
*/
33+
separators?: string | string[];
34+
35+
/**
36+
* This option can be used with the `comments` and `separators` options.
37+
* If true, **only** the tokens specified in these options are used to parse comments and separators.
38+
*/
39+
strict?: boolean;
40+
41+
/**
42+
* Parses INI sections.
43+
* Read the INI section for further details.
44+
*
45+
* @link https://www.npmjs.com/package/properties#ini
46+
*/
47+
sections?: boolean;
48+
49+
/**
50+
* Parses dot separated keys as JavaScript objects.
51+
*
52+
* Look at the namespaces section for further details.
53+
* @link https://www.npmjs.com/package/properties#namespaces
54+
*/
55+
namespaces?: boolean;
56+
57+
/**
58+
* Allows you to read the value of a key while the file is being parsed.
59+
*
60+
* Look at the variables section for further details.
61+
* @link https://www.npmjs.com/package/properties#variables
62+
*/
63+
variables?: boolean;
64+
65+
/**
66+
* External variables can be passed to the file if the variables option is enabled.
67+
*
68+
* Look at the variables section for further details.
69+
* @link https://www.npmjs.com/package/properties#variables
70+
*/
71+
vars?: boolean;
72+
73+
/**
74+
* Files can be linked and imported with the include key.
75+
* If this option is used, the callback is mandatory.
76+
*
77+
* Look at the include section for further details.
78+
* @link https://www.npmjs.com/package/properties#include
79+
*/
80+
include?: boolean;
81+
82+
/**
83+
* Each property or section can be removed or modified from the final object.
84+
* It's similar to the reviver of the `JSON.parse()` function.
85+
*
86+
* The reviver it's exactly the same as the replacer from `stringify()`.
87+
* The same function can be reused.
88+
*
89+
* The callback gets 3 parameters: key, value and section.
90+
*
91+
* A property has a key and a value and can belong to a section.
92+
* If it's a global property, the section is set to null.
93+
* If **undefined** is returned, the property will be removed from the final object, otherwise the returned value will be used as the property value.
94+
*
95+
* If the key and the value are set to null, then it's a section line.
96+
* If it returns a falsy value, it won't be added to the final object, the entire section _including all the properties_ will be discarded.
97+
* If it returns a truthy value, the section is parsed.
98+
*
99+
* For your convenience, to know whether the line is a property or section you can access to `this.isProperty` and `this.isSection` from inside the replacer function.
100+
* Also, `this.assert()` can be used to return the default value, the unmodified value that will be used to parse the line.
101+
*
102+
* Look at the reviver example for further details.
103+
* @link https://github.com/gagle/node-properties/blob/master/examples/reviver/reviver.js
104+
*/
105+
reviver?: (this: Context, key: any, value: any) => any;
106+
}
107+
108+
export interface Context {
109+
assert(): any;
110+
}
111+
112+
/**
113+
* Parses a .properties string.
114+
*
115+
* @param data
116+
* @param options
117+
*/
118+
export function parse(data: string, options?: ParseOptions): object;
119+
120+
/**
121+
* Parses a .properties string.
122+
*
123+
* If a callback is given, the result is returned as the second parameter. Some options will require a callback.
124+
*
125+
* @param data
126+
* @param options
127+
* @param cb
128+
*/
129+
export function parse(data: string, options: ParseOptions | undefined, cb: (err: any, result: { [key: string]: unknown } | undefined) => void): void;
130+
131+
export interface StringifyOptions {
132+
/**
133+
* By default `stringify()` returns a string.
134+
* If you want to write it to a file, use this option and pass the path of a file.
135+
* If this option is used, the callback is mandatory.
136+
* It gets two parameters, a possible error and the string.
137+
*/
138+
path?: string;
139+
140+
/**
141+
* The token to use to write comments.
142+
* It must be a single printable non-whitespace ascii character.
143+
* @default `#`
144+
*/
145+
comment?: string;
146+
147+
/**
148+
* The token to use to separate keys from values.
149+
* It must be a single printable non-whitespace ascii character.
150+
* @default `=`
151+
*/
152+
separator?: string;
153+
154+
/**
155+
* The .properties specification uses iso 8859-1 (latin-1) as a default encoding.
156+
* In the other hand, Node.js has a utf8 default encoding.
157+
* This means that if you want a full compatibility with Java, that is, you are generating a .properties file that is going to be read by a Java program, then set this option to true.
158+
* This will encode all ascii extended and multibyte characters to their unicode string representation (`\uXXXX`).
159+
*
160+
* Non-printable control codes (control sets 0 and 1) are always encoded as unicode strings except `\t`, `\n`, `\f` and `\r`.
161+
*
162+
* If you are in a platform that can handle utf8 strings, e.g. Node.js, you don't need to use this option.
163+
*/
164+
unicode?: boolean;
165+
166+
/**
167+
* Each property or section can be removed or modified from the final string.
168+
* It's similar to the replacer of the `JSON.stringify()` function.
169+
*
170+
* The replacer it's exatcly the same as the reviver from `parse()`.
171+
* The same function can be reused.
172+
*
173+
* The callback gets three parameters: key, value and section.
174+
*
175+
* A property has a key and a value and can belong to a section.
176+
* If it's a global property, the section is set to null.
177+
* If **undefined** is returned, the property won't be stringified, otherwise the returned value will be used as the property value.
178+
*
179+
* If the key and the value are set to null, then it's a section line.
180+
* If it returns a falsy value, it won't be added to the final string, the entire section _including all the properties_ will be discarded.
181+
* If it returns a truthy value, the section is stringified.
182+
*
183+
* For your convenience, to know whether the line is a property or section you can access to `this.isProperty` and `this.isSection` from inside the replacer function.
184+
* Also, `this.assert()` can be used to return the default value, the unmodified value that will be used to stringify the line.
185+
*
186+
* Look at the replacer example for further details.
187+
* @link https://github.com/gagle/node-properties/blob/master/examples/replacer.js
188+
*/
189+
replacer?: (this: Context, key: string, value: any) => any;
190+
}
191+
192+
/**
193+
* Stringifies an `object` or a `Stringifier`.
194+
*
195+
* If you don't need to add sections or comments simply pass an object, otherwise use a `Stringifier`.
196+
*
197+
* @param obj
198+
* @param options
199+
*/
200+
export function stringify(obj: object, options?: StringifyOptions): string;
201+
202+
/**
203+
* Stringifies an `object` or a `Stringifier`.
204+
*
205+
* If you don't need to add sections or comments simply pass an object, otherwise use a `Stringifier`.
206+
*
207+
* The callback is only necessary when the `path` option is used.
208+
*
209+
* @param obj
210+
* @param options
211+
* @param cb
212+
*/
213+
export function stringify(obj: object, options: StringifyOptions | undefined, cb: (err: any, result: string) => void): void;
214+
215+
/**
216+
* This class is used when you want to add sections or comments to the final string.
217+
*
218+
* To create a Stringifier use the `createStringifier()` function.
219+
*
220+
* Look at the stringify-ini example for further details.
221+
* @link https://github.com/gagle/node-properties/blob/master/examples/ini/stringify-ini.js
222+
*/
223+
export interface Stringifier {
224+
/**
225+
* Writes a header comment.
226+
* It will be written to the top of the final string.
227+
* Returns the Stringifier being used.
228+
*/
229+
header(comment: string): this;
230+
231+
/**
232+
* Writes a property line.
233+
* It takes an object with three options: `key`, `value` and comment.
234+
* Both the key and the value are converted into a string automatically.
235+
* Returns the Stringifier being used.
236+
*/
237+
property(obj: { key?: any; value?: any; comment?: string }): this;
238+
239+
/**
240+
* Writes a section line.
241+
* It gets an object with two options: `name` and `comment`.
242+
* The name is converted into a string.
243+
* If you don't need to write a comment, you can pass the name instead of an object.
244+
* Returns the stringifier being used.
245+
*/
246+
section(obj: string | { name: string; comment?: string }): this;
247+
}
248+
249+
/**
250+
* Returns a new `Stringifier` instance.
251+
*/
252+
export function createStringifier(): Stringifier;
253+
}

0 commit comments

Comments
 (0)