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