From 3e571b4ce8a0146c14472e7c3d423db550eeb9d4 Mon Sep 17 00:00:00 2001 From: Raul Ochoa Date: Thu, 25 Sep 2014 12:17:32 +0200 Subject: [PATCH] Use object.keys to iterate over objects --- lib/cartodb/template_maps.js | 36 ++++++++++++++++++++---------------- 1 file changed, 20 insertions(+), 16 deletions(-) diff --git a/lib/cartodb/template_maps.js b/lib/cartodb/template_maps.js index 04cac79e..15565e2b 100644 --- a/lib/cartodb/template_maps.js +++ b/lib/cartodb/template_maps.js @@ -120,7 +120,7 @@ o._releaseTemplateLock = function(owner, tpl_id, callback) { this._redisCmd('HDEL', [this.key_usr_tpl_lck({owner:owner}), tpl_id], callback); }; -o._reValidIdentifier = /^[a-zA-Z][0-9a-zA-Z_]*$/; +var _reValidIdentifier = /^[a-zA-Z][0-9a-zA-Z_]*$/; o._checkInvalidTemplate = function(template) { if ( template.version != '0.0.1' ) { return new Error("Unsupported template version " + template.version); @@ -129,21 +129,25 @@ o._checkInvalidTemplate = function(template) { if ( ! tplname ) { return new Error("Missing template name"); } - if ( ! tplname.match(this._reValidIdentifier) ) { + if ( ! tplname.match(_reValidIdentifier) ) { return new Error("Invalid characters in template name '" + tplname + "'"); } - var phold = template.placeholders; - for (var k in phold) { - if ( ! k.match(this._reValidIdentifier) ) { - return new Error("Invalid characters in placeholder name '" + k + "'"); - } - if ( ! phold[k].hasOwnProperty('default') ) { - return new Error("Missing default for placeholder '" + k + "'"); - } - if ( ! phold[k].hasOwnProperty('type') ) { - return new Error("Missing type for placeholder '" + k + "'"); - } + var placeholders = template.placeholders || {}; + + var placeholderKeys = Object.keys(placeholders); + for (var i = 0, len = placeholderKeys.length; i < len; i++) { + var placeholderKey = placeholderKeys[i]; + + if (!placeholderKey.match(_reValidIdentifier)) { + return new Error("Invalid characters in placeholder name '" + placeholderKey + "'"); + } + if ( ! placeholders[placeholderKey].hasOwnProperty('default') ) { + return new Error("Missing default for placeholder '" + placeholderKey + "'"); + } + if ( ! placeholders[placeholderKey].hasOwnProperty('type') ) { + return new Error("Missing type for placeholder '" + placeholderKey + "'"); + } } // Check certificate validity @@ -545,8 +549,8 @@ _replaceVars = function(str, params) { }; o.instance = function(template, params) { var all_params = {}; - var phold = template.placeholders; - for (var k in phold) { + var phold = template.placeholders || {}; + Object.keys(phold).forEach(function(k) { var val = params.hasOwnProperty(k) ? params[k] : phold[k].default; var type = phold[k].type; // properly escape @@ -578,7 +582,7 @@ o.instance = function(template, params) { throw new Error("Invalid placeholder type '" + type + "'"); } all_params[k] = val; - } + }); // NOTE: we're deep-cloning the layergroup here var layergroup = JSON.parse(JSON.stringify(template.layergroup));