Permalink
Browse files

Simplify setting default values

  • Loading branch information...
Ajnasz committed Jul 23, 2011
1 parent 618816f commit bcb7c67da360b5bd52ad19d783f02395adf7a347
Showing with 22 additions and 51 deletions.
  1. +22 −51 index.js
View
@@ -56,34 +56,21 @@ var fixQuoted = function (str) {
};
-var inheritDefault = function(block, _default, key) {
- var out, _inheritDefault, _key;
+var inheritDefault = function (block, _default) {
+ var _inheritDefault, _key;
_default = typeof _default === 'object' ? _default : {};
- if (typeof block !== 'object') {
- return undefined;
- }
-
- _inheritDefault = function(key) {
- return block.hasOwnProperty(key) ? block[key] : _default[key];
- };
-
- if (typeof key === 'string') {
- out = _inheritDefault(key);
- } else {
- out = {};
- for (_key in block) {
- out[_key] = block[_key];
- };
+ if (typeof block === 'object') {
for (_key in _default) {
- out[_key] = _inheritDefault(_key);
+ if (!(_key in block)) {
+ block[_key] = _default[_key];
+ }
}
}
- return out;
+ return block;
};
-
/**
* Parses a .ini file and convert's it's content to a JS object
* Parser regexps are from the Config::Simple Perl module
@@ -176,7 +163,7 @@ IniReader.prototype.parseFile = function () {
var output, lines, groupName, keyVal, line, currentSection, lineNumber;
// regular expressions to clear, validate and get the values
- const skipLineRex = /^\s*(\n|\#|;)/,
+ var skipLineRex = /^\s*(\n|\#|;)/,
chompRex = /(?:\n|\r)$/,
nonWhitespaceRex = /\S/;
@@ -252,42 +239,26 @@ IniReader.prototype.getValue = function (block, key) {
*/
IniReader.prototype.getParam = function (param) {
var output = this.values,
- block, key,
- useDefault = this.inheritDefault && this.values.hasOwnProperty('DEFAULT');
+ block, key, _block;
+
+ if (this.inheritDefault && output.hasOwnProperty('DEFAULT')) {
+ for (_block in output) {
+ if (output.hasOwnProperty(_block)) {
+ output[_block] = inheritDefault(output[_block], output.DEFAULT);
+ }
+ }
+ }
if (param) {
param = param.split('.');
block = param[0];
key = param[1];
- if (typeof block === 'string') {
- if (typeof key === 'string' && typeof output[block] !== 'undefined') {
- if (useDefault) {
- output = inheritDefault(output[block], this.values.DEFAULT, key);
- } else {
- output = output[block][key];
- }
- } else {
- if (useDefault) {
- output = inheritDefault(output[block], this.values.DEFAULT);
- } else
- output = output[block];
- }
- }
- } else if (useDefault) {
- output = {};
- for (var section in this.values) {
- output[section] = {};
- for (key in this.values[section]) {
- output[section][key] = this.values[section][key];
- };
-
- if (section != 'DEFAULT') {
- for (key in this.values.DEFAULT) {
- if (!output[section].hasOwnProperty(key)) {
- output[section][key] = output.DEFAULT[key];
- }
- }
+ if (block) {
+ output = output[block];
+
+ if (key) {
+ output = output[key];
}
}
}

2 comments on commit bcb7c67

@pivaldi

This comment has been minimized.

Show comment Hide comment
@pivaldi

pivaldi Jul 23, 2011

Contributor

Because of this simplification, the routine getParam is to inherit all the blocks by the block "DEFAULT" even if only a specific block or block.key is requested.
Don't you think that may be a problem on huge file ?

Contributor

pivaldi replied Jul 23, 2011

Because of this simplification, the routine getParam is to inherit all the blocks by the block "DEFAULT" even if only a specific block or block.key is requested.
Don't you think that may be a problem on huge file ?

@Ajnasz

This comment has been minimized.

Show comment Hide comment
@Ajnasz

Ajnasz Jul 24, 2011

Owner

I did some tests.
The DEFAULT section had more than 1000 keys and with 100 sections. It took around 30 ms to extend the sections with the 1000 keys.
If a file is huge with lots of keys and sections the bigger problem would be the parseFile method which took almost a second.

Owner

Ajnasz replied Jul 24, 2011

I did some tests.
The DEFAULT section had more than 1000 keys and with 100 sections. It took around 30 ms to extend the sections with the 1000 keys.
If a file is huge with lots of keys and sections the bigger problem would be the parseFile method which took almost a second.

Please sign in to comment.