Skip to content

Commit

Permalink
Simplify setting default values
Browse files Browse the repository at this point in the history
  • Loading branch information
Ajnasz committed Jul 23, 2011
1 parent 618816f commit bcb7c67
Showing 1 changed file with 22 additions and 51 deletions.
73 changes: 22 additions & 51 deletions index.js
Expand Up @@ -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
Expand Down Expand Up @@ -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/;

Expand Down Expand Up @@ -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];
}
}
}
Expand Down

2 comments on commit bcb7c67

@pivaldi
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
Copy link
Owner Author

@Ajnasz Ajnasz commented on bcb7c67 Jul 24, 2011

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.