mirror of
https://github.com/marko-js/marko.git
synced 2025-12-08 19:26:05 +00:00
52 lines
1.2 KiB
JavaScript
52 lines
1.2 KiB
JavaScript
'use strict';
|
|
|
|
var safeVarName = require('./safeVarName');
|
|
|
|
class UniqueVars {
|
|
constructor() {
|
|
this.vars = {};
|
|
}
|
|
|
|
addVar(name, value) {
|
|
if (typeof value !== 'string') {
|
|
// Convert the non-string value into a string for easy comparison
|
|
value = JSON.stringify(value);
|
|
}
|
|
|
|
name = safeVarName(name);
|
|
|
|
var entry = this.vars[name];
|
|
if (entry) {
|
|
var vars = entry.vars;
|
|
|
|
// See if there is already a variable with the requested value
|
|
for (var i=0; i<vars.length; i++) {
|
|
var curVar = vars[i];
|
|
if (curVar.value === value) {
|
|
return curVar.name;
|
|
}
|
|
}
|
|
|
|
entry.vars.push({
|
|
name: name + (++entry.counter),
|
|
value: value
|
|
});
|
|
} else {
|
|
entry = {
|
|
vars: [
|
|
{
|
|
name: name,
|
|
value: value
|
|
}
|
|
],
|
|
counter: 1
|
|
};
|
|
|
|
this.vars[name] = entry;
|
|
}
|
|
|
|
return name;
|
|
}
|
|
}
|
|
|
|
module.exports = UniqueVars; |