easy-configuration
Advanced tools
Comparing version 1.5.7 to 1.6.0
// Generated by CoffeeScript 1.6.3 | ||
(function() { | ||
var EasyConfiguration, Extension, Helpers, clone, merge, shims; | ||
var EasyConfiguration, Extension, Helpers, clone, merge; | ||
shims = require('./shims'); | ||
merge = require('recursive-merge'); | ||
@@ -24,2 +22,4 @@ | ||
EasyConfiguration.prototype._parameters = null; | ||
EasyConfiguration.prototype.parameters = null; | ||
@@ -33,2 +33,3 @@ | ||
this.files = []; | ||
this._parameters = {}; | ||
this.parameters = {}; | ||
@@ -42,3 +43,3 @@ } | ||
EasyConfiguration.prototype.addExtension = function(name, extension) { | ||
if (this.reserved.indexOf(name) !== -1) { | ||
if (Helpers.arrayIndexOf(this.reserved, name) !== -1) { | ||
throw new Error('Extension\'s name ' + name + ' is reserved.'); | ||
@@ -103,2 +104,3 @@ } | ||
if (typeof data.parameters !== 'undefined') { | ||
this._parameters = data.parameters; | ||
result.parameters = this.expandParameters(data.parameters); | ||
@@ -128,3 +130,3 @@ } | ||
section = this.extensions[name].loadConfiguration(); | ||
section = Helpers.expandWithParameters(section, result.parameters); | ||
section = this.expandParameters(section); | ||
section = this.extensions[name].afterCompile(section); | ||
@@ -138,8 +140,52 @@ result.sections[name] = section; | ||
EasyConfiguration.prototype.expandParameters = function(parameters) { | ||
parameters = Helpers.stringifyParameters(parameters); | ||
parameters = Helpers.expandParameters(parameters); | ||
parameters = Helpers.objectifyParameters(parameters); | ||
var name, param, parse, type, _i, _len, _type, | ||
_this = this; | ||
_type = Object.prototype.toString; | ||
parse = function(name, param) { | ||
switch (_type.call(param)) { | ||
case '[object String]': | ||
return parameters[name] = param.replace(/%([a-zA-Z.-_]+)%/g, function(match, variable) { | ||
return _this.getParameter(variable); | ||
}); | ||
case '[object Object]': | ||
case '[object Array]': | ||
return parameters[name] = _this.expandParameters(param); | ||
default: | ||
return parameters[name] = param; | ||
} | ||
}; | ||
type = _type.call(parameters); | ||
switch (type) { | ||
case '[object Object]': | ||
for (name in parameters) { | ||
param = parameters[name]; | ||
parse(name, param); | ||
} | ||
break; | ||
case '[object Array]': | ||
for (name = _i = 0, _len = parameters.length; _i < _len; name = ++_i) { | ||
param = parameters[name]; | ||
parse(name, param); | ||
} | ||
break; | ||
default: | ||
throw new Error("Can not parse " + type + " parameters."); | ||
} | ||
return parameters; | ||
}; | ||
EasyConfiguration.prototype.getParameter = function(parameter) { | ||
var actual, part, parts, _i, _len; | ||
parts = parameter.split('.'); | ||
actual = this._parameters; | ||
for (_i = 0, _len = parts.length; _i < _len; _i++) { | ||
part = parts[_i]; | ||
if (typeof actual[part] === 'undefined') { | ||
throw new Error("Parameter " + parameter + " is not defined."); | ||
} | ||
actual = actual[part]; | ||
} | ||
return actual; | ||
}; | ||
EasyConfiguration.prototype.merge = function(left, right) { | ||
@@ -146,0 +192,0 @@ right = clone(right, false); |
@@ -35,91 +35,19 @@ // Generated by CoffeeScript 1.6.3 | ||
Helpers.stringifyParameters = function(parameters, parent) { | ||
var name, result, type, value; | ||
if (parent == null) { | ||
parent = null; | ||
Helpers.arrayIndexOf = function(array, search) { | ||
var element, i, _i, _len; | ||
if (typeof Array.prototype.indexOf !== 'undefined') { | ||
return array.indexOf(search); | ||
} | ||
result = {}; | ||
for (name in parameters) { | ||
value = parameters[name]; | ||
if (parameters.hasOwnProperty(name)) { | ||
name = parent === null ? name : parent + '.' + name; | ||
type = Object.prototype.toString.call(value); | ||
if (type === '[object Object]') { | ||
result = merge(result, this.stringifyParameters(value, name)); | ||
} else { | ||
result[name] = value; | ||
} | ||
} | ||
if (array.length === 0) { | ||
return -1; | ||
} | ||
return result; | ||
}; | ||
Helpers.expandParameters = function(parameters) { | ||
var name, parse, result, value; | ||
parse = function(parameter) { | ||
var asString, i, param, _i, _len; | ||
asString = false; | ||
if (typeof parameter === 'string') { | ||
asString = true; | ||
parameter = [parameter]; | ||
for (i = _i = 0, _len = array.length; _i < _len; i = ++_i) { | ||
element = array[i]; | ||
if (element === search) { | ||
return i; | ||
} | ||
for (i = _i = 0, _len = parameter.length; _i < _len; i = ++_i) { | ||
param = parameter[i]; | ||
parameter[i] = param.replace(/%([a-zA-Z.-_]+)%/g, function(match, variable) { | ||
if (typeof parameters[variable] === 'undefined') { | ||
throw new Error('Parameter ' + variable + ' was not found'); | ||
} | ||
return parse(parameters[variable]); | ||
}); | ||
} | ||
if (asString === true) { | ||
parameter = parameter[0]; | ||
} | ||
return parameter; | ||
}; | ||
result = {}; | ||
for (name in parameters) { | ||
value = parameters[name]; | ||
if (parameters.hasOwnProperty(name)) { | ||
result[name] = parse(value); | ||
} | ||
} | ||
return result; | ||
return -1; | ||
}; | ||
Helpers.expandWithParameters = function(data, parameters) { | ||
var i, replace, value, _i, _len; | ||
replace = function(s) { | ||
return s.replace(/%([a-zA-Z.-_]+)%/g, function(match, variable) { | ||
if (typeof parameters[variable] === 'undefined') { | ||
throw new Error('Parameter ' + variable + ' was not found'); | ||
} | ||
return parameters[variable]; | ||
}); | ||
}; | ||
switch (Object.prototype.toString.call(data)) { | ||
case '[object String]': | ||
data = replace(data); | ||
break; | ||
case '[object Array]': | ||
for (i = _i = 0, _len = data.length; _i < _len; i = ++_i) { | ||
value = data[i]; | ||
data[i] = this.expandWithParameters(value, parameters); | ||
} | ||
break; | ||
case '[object Object]': | ||
for (i in data) { | ||
value = data[i]; | ||
if (data.hasOwnProperty(i)) { | ||
data[i] = this.expandWithParameters(value, parameters); | ||
} | ||
} | ||
} | ||
return data; | ||
}; | ||
Helpers.objectifyParameters = function(parameters) { | ||
return parameters; | ||
}; | ||
return Helpers; | ||
@@ -126,0 +54,0 @@ |
{ | ||
"name": "easy-configuration", | ||
"description": "Simply extensible loader for json config files", | ||
"version": "1.5.7", | ||
"version": "1.6.0", | ||
"author": { | ||
@@ -31,7 +31,8 @@ "name": "David Kudera", | ||
"devDependencies": { | ||
"chai": "~1.8.0" | ||
"chai": "~1.8.0", | ||
"mocha": "~1.14.0" | ||
}, | ||
"scripts": { | ||
"test": "cd ./test; mocha ./node/index.js --reporter spec;" | ||
"test": "cd ./test; echo \"Testing in node:\"; mocha ./node/index.js --reporter spec; cd ./browser; echo \"Testing in browser:\"; mocha-phantomjs ./index.html;" | ||
} | ||
} |
@@ -143,8 +143,8 @@ # Easy Configuration | ||
}, | ||
"items": [] | ||
items: [] | ||
}; | ||
var defaultsItems = { | ||
"name": "", | ||
"title": "", | ||
name: "", | ||
title: "", | ||
allowed: true | ||
@@ -190,2 +190,6 @@ }; | ||
var parameters = config.parameters; | ||
// or | ||
var parameter = config.getParameter('path.to.some.variable'); | ||
``` | ||
@@ -201,4 +205,9 @@ | ||
* 1.6.0 | ||
+ Added many tests + tests for browser | ||
+ Updated docs | ||
+ Rewritten parameters parsing (now much better) | ||
* 1.5.7 | ||
+ Support for Internet Explorer 8 | ||
+ Support for Internet Explorer 8 | ||
@@ -205,0 +214,0 @@ * 1.5.4 - 1.5.6 |
@@ -8,2 +8,3 @@ { | ||
"paths": { | ||
"cdn": "./cdn/data", | ||
"lang": "%base%/lang", | ||
@@ -10,0 +11,0 @@ "translator": "%paths.lang%/translator.js", |
// Generated by CoffeeScript 1.6.3 | ||
(function() { | ||
var EasyConfiguration, Extension, configuration, expect, file, path; | ||
var EasyConfiguration, Extension, configuration, dir, expect, path; | ||
@@ -13,3 +13,3 @@ expect = require('chai').expect; | ||
file = path.resolve(__dirname + '/../data/config.json'); | ||
dir = path.normalize(__dirname + '/../data'); | ||
@@ -20,3 +20,3 @@ configuration = null; | ||
beforeEach(function() { | ||
return configuration = new EasyConfiguration(file); | ||
return configuration = new EasyConfiguration("" + dir + "/config.json"); | ||
}); | ||
@@ -28,3 +28,3 @@ describe('#load()', function() { | ||
}); | ||
return describe('#addSection()', function() { | ||
describe('#addSection()', function() { | ||
it('should return instance of newly registered section', function() { | ||
@@ -40,4 +40,86 @@ return expect(configuration.addSection('newSection')).to.be.an["instanceof"](Extension); | ||
}); | ||
describe('#getParameter()', function() { | ||
beforeEach(function() { | ||
return configuration.load(); | ||
}); | ||
it('should throw an error for unknown parameter', function() { | ||
return expect(function() { | ||
return configuration.getParameter('unknown'); | ||
}).to["throw"](Error, 'Parameter unknown is not defined.'); | ||
}); | ||
it('should return parameter', function() { | ||
return expect(configuration.getParameter('base')).to.be.equal('./www'); | ||
}); | ||
it('should return parameter from not first depth', function() { | ||
return expect(configuration.getParameter('paths.cdn')).to.be.equal('./cdn/data'); | ||
}); | ||
it('should return parameter pointing to other parameter', function() { | ||
expect(configuration.getParameter('paths.lang')).to.be.equal('./www/lang'); | ||
return expect(configuration.getParameter('paths.translator')).to.be.equal('./www/lang/translator.js'); | ||
}); | ||
return it('should return parameter pointing to other parameter from included file', function() { | ||
return expect(configuration.getParameter('paths.videos')).to.be.equal('./www/videos'); | ||
}); | ||
}); | ||
return describe('sections', function() { | ||
it('should throw an error for unknown section', function() { | ||
configuration = new EasyConfiguration("" + dir + "/unknownSection"); | ||
return expect(function() { | ||
return configuration.load(); | ||
}).to["throw"](Error, 'Found section unknown but there is no coresponding extension.'); | ||
}); | ||
it('should load data of section', function() { | ||
configuration = new EasyConfiguration("" + dir + "/advanced"); | ||
configuration.addSection('application'); | ||
return expect(configuration.load()).to.be.eql({ | ||
application: { | ||
path: './www', | ||
data: ['./cdn/data', './www/lang', './www/lang/translator.js', './www/images', './www/videos'] | ||
} | ||
}); | ||
}); | ||
return it('should load data from section with defaults', function() { | ||
var section; | ||
configuration = new EasyConfiguration("" + dir + "/advanced"); | ||
section = configuration.addSection('application'); | ||
section.loadConfiguration = function() { | ||
var config, i, _i, _len, _path, _ref; | ||
config = this.getConfig({ | ||
data: [], | ||
run: true, | ||
cache: '%base%/temp/cache' | ||
}); | ||
_ref = config.data; | ||
for (i = _i = 0, _len = _ref.length; _i < _len; i = ++_i) { | ||
_path = _ref[i]; | ||
config.data[i] = { | ||
path: _path | ||
}; | ||
} | ||
return config; | ||
}; | ||
return expect(configuration.load()).to.be.eql({ | ||
application: { | ||
path: './www', | ||
data: [ | ||
{ | ||
path: './cdn/data' | ||
}, { | ||
path: './www/lang' | ||
}, { | ||
path: './www/lang/translator.js' | ||
}, { | ||
path: './www/images' | ||
}, { | ||
path: './www/videos' | ||
} | ||
], | ||
run: true, | ||
cache: './www/temp/cache' | ||
} | ||
}); | ||
}); | ||
}); | ||
}); | ||
}).call(this); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
Long strings
Supply chain riskContains long string literals, which may be a sign of obfuscated or packed code.
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
79883
27
1587
288
2
5
1