Windshaft-cartodb/lib/utils/substitution-tokens.js

32 lines
819 B
JavaScript
Raw Normal View History

'use strict';
var SUBSTITUTION_TOKENS = {
bbox: /!bbox!/g,
scale_denominator: /!scale_denominator!/g,
pixel_width: /!pixel_width!/g,
pixel_height: /!pixel_height!/g
};
var SubstitutionTokens = {
2019-10-22 01:07:24 +08:00
tokens: function (sql) {
return Object.keys(SUBSTITUTION_TOKENS).filter(function (tokenName) {
return !!sql.match(SUBSTITUTION_TOKENS[tokenName]);
});
},
2019-10-22 01:07:24 +08:00
hasTokens: function (sql) {
return this.tokens(sql).length > 0;
},
2019-10-22 01:07:24 +08:00
replace: function (sql, replaceValues) {
Object.keys(replaceValues).forEach(function (token) {
if (SUBSTITUTION_TOKENS[token]) {
sql = sql.replace(SUBSTITUTION_TOKENS[token], replaceValues[token]);
}
});
return sql;
}
};
module.exports = SubstitutionTokens;