UNPKG

promisified-properties

Version:

Handle .properties file via promisified and typed API

60 lines (59 loc) 2.22 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.parseFile = parseFile; exports.parse = parse; exports.stringify = stringify; exports.write = write; const fs_1 = require("fs"); const escape_1 = require("./escape"); const parser_1 = require("./parser"); /** * Parse the file pointed by the given path as [the Properties defined by Java](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/Properties.html). * * @param path - Path to the target file to parse * @returns Promise which returns parsed properties */ async function parseFile(path) { return fs_1.promises.readFile(path, { encoding: "utf8" }).then((s) => { return (0, parser_1.parse)(s); }); } /** * Parse given text as [the Properties defined by Java](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/Properties.html). * * @param data - Text to parse * @returns Promise which returns parsed properties */ function parse(data) { return (0, parser_1.parse)(data); } /** * Convert properties data to text, based on [the spec defined by Java](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/Properties.html). * @param data - Properties data to convert into text format * @returns Converted text which represents the given properties */ function stringify(data) { let result = ""; data.forEach((line) => { if ("text" in line) { result += line.text + "\n"; } else { result += (0, escape_1.escape)((0, escape_1.escapeKey)(line.key)) + " = " + (0, escape_1.escape)(line.value ?? "") + "\n"; } }); return result; } /** * Convert properties data to text, based on [the spec defined by Java](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/Properties.html), * and write it to the file pointed by the given path. * @param data - Properties data to convert into text format * @param path - Path to the target file to write * @returns Promise which is resolved when file is successfully written */ function write(data, path) { return fs_1.promises.writeFile(path, stringify(data), { encoding: "utf8", }); }