properties-reader
Advanced tools
Comparing version 2.0.0 to 2.1.0
{ | ||
"name": "properties-reader", | ||
"description": "Properties file reader for Node.js", | ||
"version": "2.0.0", | ||
"version": "2.1.0", | ||
"author": { | ||
@@ -48,5 +48,5 @@ "name": "Steve King", | ||
"engines": { | ||
"node": ">=8" | ||
"node": ">=10" | ||
}, | ||
"license": "MIT" | ||
} |
@@ -76,3 +76,3 @@ Properties-Reader | ||
const propertiesReader = require('properties-reader'); | ||
const props = propertiesReader(filePath); | ||
const props = propertiesReader(filePath, {writer: { saveSections: true }}); | ||
await props.save(filePath); | ||
@@ -87,2 +87,4 @@ | ||
To output the properties without any section headings, set the `saveSections` option to `false` | ||
Data Types | ||
@@ -89,0 +91,0 @@ ========== |
const PropertiesReader = require('./properties-reader'); | ||
module.exports = function propertiesReaderFactory (sourceFile, encoding, appender) { | ||
module.exports = function propertiesReaderFactory (sourceFile, encoding, options) { | ||
return new PropertiesReader(sourceFile, encoding, appender); | ||
return new PropertiesReader(sourceFile, encoding, options); | ||
}; | ||
const fs = require('fs'); | ||
const propertyAppender = require('./property-appender').propertyAppender; | ||
const propertyWriter = require('./property-writer').propertyWriter; | ||
/** | ||
* | ||
* @param {String} sourceFile Path to the file from which to read properties | ||
* @param {string} [encoding=utf-8] Default string encoding to use when reading files | ||
* @param {Function | Object} [propertiesAppender] A `propertiesAppender` function or configuration object to create one | ||
*/ | ||
function PropertiesReader (sourceFile, encoding, propertiesAppender) { | ||
function PropertiesReader (sourceFile, encoding, options = {}) { | ||
this._encoding = typeof encoding === 'string' && encoding || 'utf-8'; | ||
@@ -15,3 +10,4 @@ this._properties = {}; | ||
this.appender(propertiesAppender || {}); | ||
this.appender(options.appender || options); | ||
this.writer(options.writer || options); | ||
this.append(sourceFile, encoding); | ||
@@ -66,2 +62,27 @@ } | ||
/** | ||
* Define the property appending mechanism to be used by the instance. | ||
* | ||
* By default, duplicate sections will be collapsed when saving the properties. To disable this | ||
* feature, set the `allowDuplicateSections` appender configuration to `true`: | ||
* | ||
* ``` | ||
const properties = propertiesReader('file.ini', 'utf-8', { allowDuplicateSections: true }); | ||
const properties = propertiesReader('file.ini').appender({ allowDuplicateSections: true }); | ||
``` | ||
* | ||
* @param writer | ||
* @returns {PropertiesReader} | ||
*/ | ||
PropertiesReader.prototype.writer = function (writer) { | ||
if (typeof writer === 'function') { | ||
this._propertyWriter = writer; | ||
} | ||
else if (typeof writer === 'object') { | ||
this._propertyWriter = propertyWriter(writer); | ||
} | ||
return this; | ||
}; | ||
/** | ||
* Append a file to the properties into the PropertiesReader | ||
@@ -313,22 +334,3 @@ * | ||
*/ | ||
PropertiesReader.prototype._stringifyProperties = function () { | ||
var lines = []; | ||
var section = null; | ||
this.each(function (key, value) { | ||
var tokens = key.split('.'); | ||
if (tokens.length > 1) { | ||
if (section !== tokens[0]) { | ||
section = tokens[0]; | ||
lines.push('[' + section + ']'); | ||
} | ||
key = tokens.slice(1).join('.'); | ||
} | ||
else { | ||
section = null; | ||
} | ||
lines.push(key + '=' + value); | ||
}); | ||
return lines; | ||
}; | ||
@@ -339,28 +341,8 @@ /** | ||
* @param {String} destFile | ||
* @param {function} onComplete callback | ||
* @param {Function} onComplete callback | ||
*/ | ||
PropertiesReader.prototype.save = function (destFile, onComplete) { | ||
const content = this._stringifyProperties().join('\n'); | ||
const onDone = new Promise((done, fail) => { | ||
fs.writeFile(destFile, content, (err) => { | ||
if (err) { | ||
return fail(err); | ||
} | ||
done(content); | ||
}); | ||
}); | ||
if (typeof onComplete === 'function') { | ||
if (onComplete.length > 1) { | ||
onDone.then(onComplete.bind(null, null), onComplete.bind(null)); | ||
} | ||
else { | ||
onDone.then(onComplete) | ||
} | ||
} | ||
return onDone; | ||
return this._propertyWriter(this, destFile, onComplete); | ||
}; | ||
module.exports = PropertiesReader; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
19101
7
411
116
0
2