Skip to content

Commit c8cbe55

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

File tree

1 file changed

+259
-0
lines changed

1 file changed

+259
-0
lines changed

lib/index.d.ts

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

0 commit comments

Comments
 (0)