Skip to content

Commit fe74e7e

Browse files
committed
Adds getInt and getFloat to get value as Integer or Float. Adds default value to get method
1 parent a95f282 commit fe74e7e

4 files changed

Lines changed: 68 additions & 11 deletions

File tree

‎README.md‎

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,34 @@ Install the module with: `npm install java-properties`
1111
var values = properties.of('values.properties');
1212
values.get('a.key'); //returns value of a.key
1313
...
14-
values.getKeys(); // returns all the keys
14+
// returns the value of a.key of 'defaultValue' if key is not found
15+
values.get('a.key', 'defaultValue');
1516
...
16-
values.addFile('anotherFile.properties'); // adds another file the properties list
17+
// returns the value of the a.int.key as an int or 18
18+
values.getInt('a.int.key', 18);
1719
...
18-
values.reset(); // empty the keys previously loaded
20+
// returns the value of the a.float.key as a float or 18.23
21+
values.getFloat('a.float.key', 18.23);
22+
...
23+
// returns all the keys
24+
values.getKeys();
25+
...
26+
// adds another file the properties list
27+
values.addFile('anotherFile.properties');
28+
...
29+
// empty the keys previously loaded
30+
values.reset();
1931
...
2032
[ -- .properties file
2133
an.array.key=value1
2234
an.array.key=value2
2335
]
2436
values.get('an.array.key'); // returns [value1, value2]
2537

26-
// Since 0.2.0 : Multiple contexts
27-
var myFile = new PropertiesFile('example.properties', 'arrayExample.properties');
38+
// Multiple contexts
39+
var myFile = new PropertiesFile(
40+
'example.properties',
41+
'arrayExample.properties');
2842
myFile.get('arrayKey');
2943

3044
var myOtherFile = new PropertiesFile();
@@ -43,5 +57,7 @@ In lieu of a formal styleguide, take care to maintain the existing coding style.
4357

4458
0.2.0 Wrap features into a class to be able to have multiple running contexts
4559

60+
0.2.1 Add default value to get method. Add getInt and getFloat to get an integer or float value
61+
4662
## License
4763
Licensed under the MIT license.

‎lib/properties.js‎

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ PropertiesFile.prototype.of = function () {
5757
}
5858
};
5959

60-
PropertiesFile.prototype.get = function (key) {
60+
PropertiesFile.prototype.get = function (key, defaultValue) {
6161
if (this.objs.hasOwnProperty(key)) {
6262
if (Array.isArray(this.objs[key])) {
6363
var ret = [];
@@ -69,19 +69,31 @@ PropertiesFile.prototype.get = function (key) {
6969
return typeof this.objs[key] === 'undefined' ? '' : this.interpolate(this.objs[key]);
7070
}
7171
}
72-
return undefined;
72+
return defaultValue;
73+
};
74+
75+
PropertiesFile.prototype.getInt = function (key, defaultIntValue) {
76+
var val = this.get(key);
77+
if (!val) { return defaultIntValue; }
78+
else { return parseInt(val); }
79+
};
80+
81+
PropertiesFile.prototype.getFloat = function (key, defaultFloatValue) {
82+
var val = this.get(key);
83+
if (!val) { return defaultFloatValue; }
84+
else { return parseFloat(val); }
7385
};
7486

7587
PropertiesFile.prototype.set = function (key, value) {
7688
this.objs[key] = value;
7789
};
7890

7991
PropertiesFile.prototype.interpolate = function (s) {
80-
var moi=this;
92+
var me=this;
8193
return s
8294
.replace(/\\\\/g, '\\')
8395
.replace(/\$\{([A-Za-z0-9\.]*)\}/g, function (match) {
84-
return moi.get(match.substring(2, match.length - 1));
96+
return me.get(match.substring(2, match.length - 1));
8597
});
8698
};
8799

‎package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "java-properties",
33
"description": "Reads and interpolates Java .properties files",
4-
"version": "0.2.0",
4+
"version": "0.2.1",
55
"homepage": "http://github.com/mattdsteele/java-properties",
66
"author": {
77
"name": "Matt Steele",

‎test/properties_test.js‎

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,5 +193,34 @@ exports['properties'] = {
193193
test.equal('some=value', myOtherFile.get('property.with.equals'));
194194
test.equal('7', myOtherFile.get('referenced.property'));
195195
test.done();
196-
}
196+
},
197+
'Using defaut value for get': function(test) {
198+
test.expect(4);
199+
var myFile = new PropertiesFile('test/fixtures/example.properties', 'test/fixtures/arrayExample.properties');
200+
test.equal(undefined, myFile.get('referenced.property'));
201+
test.equal('defaultValue', myFile.get('referenced.property', 'defaultValue'));
202+
test.equal('hello', myFile.get('ricola.withSpaces'));
203+
test.equal('hello', myFile.get('ricola.withSpaces', 'defaultValue'));
204+
test.done();
205+
},
206+
'Using int value with getInt': function(test) {
207+
test.expect(5);
208+
var myFile = new PropertiesFile('test/fixtures/example.properties', 'test/fixtures/arrayExample.properties');
209+
test.equal('7', myFile.get('ricola.version.minor'));
210+
test.equal(7, myFile.getInt('ricola.version.minor'));
211+
test.equal(undefined, myFile.getInt('dont.exists'));
212+
test.equal(12, myFile.getInt('dont.exists', 12));
213+
test.equal(true, isNaN(myFile.getInt('ricola.withSpaces')));
214+
test.done();
215+
},
216+
'Using float value with getFloat': function(test) {
217+
test.expect(5);
218+
var myFile = new PropertiesFile('test/fixtures/example.properties', 'test/fixtures/arrayExample.properties');
219+
test.equal('2.5', myFile.get('ricola.version.major'));
220+
test.equal(2.5, myFile.getFloat('ricola.version.major'));
221+
test.equal(undefined, myFile.getFloat('dont.exists'));
222+
test.equal(12.23, myFile.getFloat('dont.exists', 12.23));
223+
test.equal(true, isNaN(myFile.getFloat('ricola.withSpaces')));
224+
test.done();
225+
},
197226
};

0 commit comments

Comments
 (0)