Adds hasTokens method to SubstitutionTokens
This commit is contained in:
parent
573932efba
commit
ba08745c23
@ -6,6 +6,16 @@ var SUBSTITUTION_TOKENS = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
var SubstitutionTokens = {
|
var SubstitutionTokens = {
|
||||||
|
tokens: function(sql) {
|
||||||
|
return Object.keys(SUBSTITUTION_TOKENS).filter(function(tokenName) {
|
||||||
|
return !!sql.match(SUBSTITUTION_TOKENS[tokenName]);
|
||||||
|
});
|
||||||
|
},
|
||||||
|
|
||||||
|
hasTokens: function(sql) {
|
||||||
|
return this.tokens(sql).length > 0;
|
||||||
|
},
|
||||||
|
|
||||||
replace: function(sql, replaceValues) {
|
replace: function(sql, replaceValues) {
|
||||||
Object.keys(replaceValues).forEach(function(token) {
|
Object.keys(replaceValues).forEach(function(token) {
|
||||||
if (SUBSTITUTION_TOKENS[token]) {
|
if (SUBSTITUTION_TOKENS[token]) {
|
||||||
|
40
test/unit/cartodb/substitution-tokens.test.js
Normal file
40
test/unit/cartodb/substitution-tokens.test.js
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
var assert = require('assert');
|
||||||
|
var SubstitutionTokens = require('../../../lib/cartodb/utils/substitution-tokens');
|
||||||
|
|
||||||
|
describe('SubstitutionTokens', function() {
|
||||||
|
|
||||||
|
var sql = [
|
||||||
|
'WITH hgrid AS (',
|
||||||
|
' SELECT CDB_HexagonGrid(',
|
||||||
|
' ST_Expand(!bbox!, greatest(!pixel_width!,!pixel_height!) * 100),',
|
||||||
|
' greatest(!pixel_width!,!pixel_height!) * 100',
|
||||||
|
' ) as cell',
|
||||||
|
')',
|
||||||
|
'SELECT',
|
||||||
|
' hgrid.cell as the_geom_webmercator,',
|
||||||
|
' count(1) as points_count,',
|
||||||
|
' count(1)/power(100 * CDB_XYZ_Resolution(CDB_ZoomFromScale(!scale_denominator!)), 2) as points_density,',
|
||||||
|
' 1 as cartodb_id',
|
||||||
|
'FROM hgrid, (select * from table) i',
|
||||||
|
'where ST_Intersects(i.the_geom_webmercator, hgrid.cell)',
|
||||||
|
'GROUP BY hgrid.cell'
|
||||||
|
].join('\n');
|
||||||
|
|
||||||
|
it('should return tokens present in sql', function() {
|
||||||
|
assert.deepEqual(SubstitutionTokens.tokens(sql), ['bbox', 'scale_denominator', 'pixel_width', 'pixel_height']);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should return just one token', function() {
|
||||||
|
assert.deepEqual(SubstitutionTokens.tokens('select !bbox! from wadus'), ['bbox']);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should not return other tokens', function() {
|
||||||
|
assert.deepEqual(SubstitutionTokens.tokens('select !wadus! from wadus'), []);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should report sql has tokens', function() {
|
||||||
|
assert.equal(SubstitutionTokens.hasTokens(sql), true);
|
||||||
|
assert.equal(SubstitutionTokens.hasTokens('select !bbox! from wadus'), true);
|
||||||
|
assert.equal(SubstitutionTokens.hasTokens('select !wadus! from wadus'), false);
|
||||||
|
});
|
||||||
|
});
|
Loading…
Reference in New Issue
Block a user