Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 7 additions & 9 deletions src/utils/entity.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,25 @@
var split = require('./split').split;

/**
* Split a delimited component property string (e.g., `material.color`) to an object
* containing `component` name and `property` name. If there is no delimiter, just return the
* string back.
*
* Cache arrays from splitting strings via delimiter to save on memory.
* Uses the caching split implementation `AFRAME.utils.split`
*
* @param {string} str - e.g., `material.opacity`.
* @param {string} delimiter - e.g., `.`.
* @returns {array} e.g., `['material', 'opacity']`.
*/
var propertyPathCache = {};
function getComponentPropertyPath (str, delimiter) {
delimiter = delimiter || '.';
if (!propertyPathCache[delimiter]) { propertyPathCache[delimiter] = {}; }
if (str.indexOf(delimiter) !== -1) {
propertyPathCache[delimiter][str] = str.split(delimiter);
} else {
propertyPathCache[delimiter][str] = str;
var parts = split(str, delimiter);
if (parts.length === 1) {
return parts[0];
}
return propertyPathCache[delimiter][str];
return parts;
}
module.exports.getComponentPropertyPath = getComponentPropertyPath;
module.exports.propertyPathCache = propertyPathCache;

/**
* Get component property using encoded component name + component property name with a
Expand Down
4 changes: 0 additions & 4 deletions tests/utils/entity.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,12 @@ suite('utils.entity', function () {
var el = this.el;
el.setAttribute('material', {color: 'red'});
assert.equal(getComponentProperty(el, 'material.color'), 'red');
assert.equal(entity.propertyPathCache['.']['material.color'][0], 'material');
assert.equal(entity.propertyPathCache['.']['material.color'][1], 'color');
});

test('can get custom-delimited attribute', function () {
var el = this.el;
el.setAttribute('material', {color: 'red'});
assert.equal(getComponentProperty(el, 'material|color', '|'), 'red');
assert.equal(entity.propertyPathCache['|']['material|color'][0], 'material');
assert.equal(entity.propertyPathCache['|']['material|color'][1], 'color');
});
});

Expand Down