mirror of
https://github.com/google/blockly.git
synced 2026-01-07 17:10:11 +01:00
Enable extraction of names from name DB.
Also gets rid of hacky name_realm contatination.
This commit is contained in:
@@ -114,20 +114,35 @@ Blockly.Names.prototype.getName = function(name, realm) {
|
||||
name = varName;
|
||||
}
|
||||
}
|
||||
var normalized = name.toLowerCase() + '_' + realm;
|
||||
var normalizedName = name.toLowerCase();
|
||||
|
||||
var isVar = realm == Blockly.VARIABLE_CATEGORY_NAME ||
|
||||
realm == Blockly.Names.DEVELOPER_VARIABLE_TYPE;
|
||||
|
||||
var prefix = isVar ? this.variablePrefix_ : '';
|
||||
if (normalized in this.db_) {
|
||||
return prefix + this.db_[normalized];
|
||||
if (!(realm in this.db_)) {
|
||||
this.db_[realm] = Object.create(null);
|
||||
}
|
||||
var realmDb = this.db_[realm];
|
||||
if (normalizedName in realmDb) {
|
||||
return prefix + realmDb[normalizedName];
|
||||
}
|
||||
var safeName = this.getDistinctName(name, realm);
|
||||
this.db_[normalized] = safeName.substr(prefix.length);
|
||||
realmDb[normalizedName] = safeName.substr(prefix.length);
|
||||
return safeName;
|
||||
};
|
||||
|
||||
/**
|
||||
* Return a list of all known user-created names in a specified realm.
|
||||
* @param {string} realm The realm of entity in Blockly
|
||||
* ('VARIABLE', 'PROCEDURE', 'DEVELOPER_VARIABLE', etc...).
|
||||
* @return {!Array.<string>} A list of Blockly entity names (no constraints).
|
||||
*/
|
||||
Blockly.Names.prototype.getUserNames = function(realm) {
|
||||
var realmDb = this.db_[realm] || {};
|
||||
return Object.keys(realmDb);
|
||||
};
|
||||
|
||||
/**
|
||||
* Convert a Blockly entity name to a legal exportable entity name.
|
||||
* Ensure that this is a new name not overlapping any previously defined name.
|
||||
|
||||
@@ -21,7 +21,7 @@ suite('Names', function() {
|
||||
test('Safe name', function() {
|
||||
var varDB = new Blockly.Names('window,door');
|
||||
chai.assert.equal(varDB.safeName_(''), 'unnamed','SafeName empty.');
|
||||
chai.assert.equal( varDB.safeName_('foobar'), 'foobar','SafeName ok.');
|
||||
chai.assert.equal(varDB.safeName_('foobar'), 'foobar','SafeName ok.');
|
||||
chai.assert.equal(varDB.safeName_('9lives'), 'my_9lives', 'SafeName number start.');
|
||||
chai.assert.equal(varDB.safeName_('lives9'), 'lives9', 'SafeName number end.');
|
||||
chai.assert.equal(varDB.safeName_('!@#$'), '____', 'SafeName special chars.');
|
||||
@@ -38,6 +38,9 @@ suite('Names', function() {
|
||||
chai.assert.equal(varDB.getName('Foo.bar', 'proc'), 'Foo_bar3', 'Name add #4.');
|
||||
chai.assert.equal(varDB.getName('Foo.bar', 'var'), 'Foo_bar', 'Name get #1b.');
|
||||
chai.assert.equal(varDB.getName('Foo.bar', 'proc'), 'Foo_bar3', 'Name get #4.');
|
||||
|
||||
chai.assert.equal(String(varDB.getUserNames('var')), 'foo.bar,foo bar,door', 'Get var names.');
|
||||
chai.assert.equal(String(varDB.getUserNames('proc')), 'foo.bar', 'Get proc names.');
|
||||
});
|
||||
|
||||
test('Get distinct name', function() {
|
||||
|
||||
Reference in New Issue
Block a user