java-properties
Advanced tools
Comparing version 0.1.8 to 0.2.0
@@ -8,91 +8,102 @@ /* | ||
'use strict'; | ||
var fs = require('fs'); | ||
'use strict'; | ||
var fs = require('fs'); | ||
// var utils = require('util'); | ||
exports.of = function() { | ||
var objs = {}; | ||
var makeKeys = function(j) { | ||
if(j && j.indexOf('#') !== 0) { | ||
var splitIndex = j.indexOf('='); | ||
var key = j.substring(0, splitIndex).trim(); | ||
var value = j.substring(splitIndex + 1).trim(); | ||
// Construct a PropertiesFile class to handle all properties of a specifice file or group of file | ||
var PropertiesFile = function () { | ||
this.objs = {}; | ||
if (arguments) { | ||
this.of.apply(this, arguments); | ||
} | ||
}; | ||
PropertiesFile.prototype.makeKeys = function (line) { | ||
// console.log('Into makeKeys this is '+utils.inspect(this)); | ||
if (line && line.indexOf('#') !== 0) { | ||
var splitIndex = line.indexOf('='); | ||
var key = line.substring(0, splitIndex).trim(); | ||
var value = line.substring(splitIndex + 1).trim(); | ||
// if keys already exists ... | ||
if (objs.hasOwnProperty(key)) { | ||
if (this.objs.hasOwnProperty(key)) { | ||
// if it is already an Array | ||
if (Array.isArray(objs[key])) { | ||
if (Array.isArray(this.objs[key])) { | ||
// just push the new value | ||
objs[key].push(value); | ||
} | ||
else { | ||
this.objs[key].push(value); | ||
} else { | ||
// transform the value into Array | ||
var oldValue = objs[key]; | ||
objs[key] = [ oldValue, value ]; | ||
var oldValue = this.objs[key]; | ||
this.objs[key] = [oldValue, value]; | ||
} | ||
} | ||
else { | ||
} else { | ||
// the key does not exists | ||
objs[key] = value; | ||
this.objs[key] = value; | ||
} | ||
} | ||
}; | ||
/* adds a file to the properties */ | ||
var addFile = function(file) { | ||
}; | ||
PropertiesFile.prototype.addFile = function (file) { | ||
// console.log('Into addFile this is '+utils.inspect(this)); | ||
var data = fs.readFileSync(file, 'utf-8'); | ||
var items = data.split(/\r?\n/); | ||
items.forEach(makeKeys); | ||
}; | ||
for (var i=0; i < arguments.length; i++) { | ||
addFile(arguments[i]); | ||
} | ||
var get = function(key) { | ||
if (objs.hasOwnProperty(key)) { | ||
if (Array.isArray(objs[key])) { | ||
var ret=[]; | ||
for (var i=0; i < objs[key].length; i++) { | ||
ret[i] = interpolate(objs[key][i]); | ||
} | ||
return ret; | ||
} | ||
else { | ||
return typeof objs[key] === 'undefined' ? '' : interpolate(objs[key]); | ||
} | ||
} | ||
return undefined; | ||
}; | ||
var set = function(key, value) { | ||
objs[key] = value; | ||
}; | ||
var interpolate = function(s) { | ||
var me = this; | ||
items.forEach(function(line) { | ||
me.makeKeys(line); | ||
}); | ||
}; | ||
PropertiesFile.prototype.of = function () { | ||
for (var i = 0; i < arguments.length; i++) { | ||
this.addFile(arguments[i]); | ||
} | ||
}; | ||
PropertiesFile.prototype.get = function (key) { | ||
if (this.objs.hasOwnProperty(key)) { | ||
if (Array.isArray(this.objs[key])) { | ||
var ret = []; | ||
for (var i = 0; i < this.objs[key].length; i++) { | ||
ret[i] = this.interpolate(this.objs[key][i]); | ||
} | ||
return ret; | ||
} else { | ||
return typeof this.objs[key] === 'undefined' ? '' : this.interpolate(this.objs[key]); | ||
} | ||
} | ||
return undefined; | ||
}; | ||
PropertiesFile.prototype.set = function (key, value) { | ||
this.objs[key] = value; | ||
}; | ||
PropertiesFile.prototype.interpolate = function (s) { | ||
var moi=this; | ||
return s | ||
.replace(/\\\\/g, '\\') | ||
.replace(/\$\{([A-Za-z0-9\.]*)\}/g, function(match) { | ||
return get(match.substring(2, match.length - 1)); | ||
}); | ||
}; | ||
/* gets all the keys of the property file */ | ||
var getKeys = function () { | ||
.replace(/\\\\/g, '\\') | ||
.replace(/\$\{([A-Za-z0-9\.]*)\}/g, function (match) { | ||
return moi.get(match.substring(2, match.length - 1)); | ||
}); | ||
}; | ||
PropertiesFile.prototype.getKeys = function () { | ||
var keys = []; | ||
for (var key in objs) { | ||
keys.push(key); | ||
for (var key in this.objs) { | ||
keys.push(key); | ||
} | ||
return keys; | ||
}; | ||
/* reset the properties */ | ||
var reset = function() { | ||
objs = {}; | ||
}; | ||
return { | ||
get: get, | ||
set: set, | ||
interpolate: interpolate, | ||
getKeys : getKeys, | ||
reset : reset, | ||
addFile : addFile | ||
}; | ||
}; | ||
}; | ||
PropertiesFile.prototype.reset = function () { | ||
this.objs = {}; | ||
}; | ||
// Keeped from v1 for backward compatibility | ||
exports.of = function () { | ||
var globalFile = new PropertiesFile(); | ||
globalFile.of.apply(globalFile, arguments); | ||
return globalFile; | ||
}; | ||
exports.PropertiesFile = PropertiesFile; |
{ | ||
"name": "java-properties", | ||
"description": "Reads and interpolates Java .properties files", | ||
"version": "0.1.8", | ||
"version": "0.2.0", | ||
"homepage": "http://github.com/mattdsteele/java-properties", | ||
@@ -6,0 +6,0 @@ "author": { |
@@ -34,2 +34,10 @@ # java-properties | ||
values.get('an.array.key'); // returns [value1, value2] | ||
// Since 0.2.0 : Multiple contexts | ||
var myFile = new PropertiesFile('example.properties', 'arrayExample.properties'); | ||
myFile.get('arrayKey'); | ||
var myOtherFile = new PropertiesFile(); | ||
myOtherFile.addFile('example.properties'); | ||
myOtherFile.addFile('example2.properties'); | ||
``` | ||
@@ -46,3 +54,5 @@ ## Contributing | ||
0.2.0 Wrap features into a class to be able to have multiple running contexts | ||
## License | ||
Licensed under the MIT license. |
'use strict'; | ||
var properties = require('../lib/properties.js'); | ||
var properties = require('../lib/properties.js'), | ||
PropertiesFile = properties.PropertiesFile; | ||
var props; | ||
@@ -170,3 +171,28 @@ | ||
test.done(); | ||
}, | ||
'Using PropertiesFile with files provided': function(test) { | ||
test.expect(2); | ||
props.reset(); | ||
var myFile = new PropertiesFile('test/fixtures/example.properties', 'test/fixtures/arrayExample.properties'); | ||
test.equal(3, myFile.get('arrayKey').length); | ||
myFile.reset(); | ||
test.equal(undefined, myFile.get('arrayKey')); | ||
test.done(); | ||
}, | ||
'Using PropertiesFile with 2 different contexts': function(test) { | ||
test.expect(4); | ||
props.reset(); | ||
var myFile = new PropertiesFile(); | ||
myFile.of('test/fixtures/example.properties', 'test/fixtures/arrayExample.properties'); | ||
var myOtherFile = new PropertiesFile(); | ||
myOtherFile.addFile('test/fixtures/example.properties'); | ||
myOtherFile.addFile('test/fixtures/example2.properties'); | ||
test.equal(3, myFile.get('arrayKey').length); | ||
test.equal(undefined, myFile.get('referenced.property')); | ||
test.equal('some=value', myOtherFile.get('property.with.equals')); | ||
test.equal('7', myOtherFile.get('referenced.property')); | ||
test.done(); | ||
} | ||
}; |
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
16038
327
57