Spaces:
Sleeping
Sleeping
File size: 1,946 Bytes
4a288a7 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
import refProperties from './util/ref_properties';
function stringify(obj) {
const type = typeof obj;
if (type === 'number' || type === 'boolean' || type === 'string' || obj === undefined || obj === null)
return JSON.stringify(obj);
if (Array.isArray(obj)) {
let str = '[';
for (const val of obj) {
str += `${stringify(val)},`;
}
return `${str}]`;
}
const keys = Object.keys(obj).sort();
let str = '{';
for (let i = 0; i < keys.length; i++) {
str += `${JSON.stringify(keys[i])}:${stringify(obj[keys[i]])},`;
}
return `${str}}`;
}
function getKey(layer) {
let key = '';
for (const k of refProperties) {
key += `/${stringify(layer[k])}`;
}
return key;
}
export default groupByLayout;
/**
* Given an array of layers, return an array of arrays of layers where all
* layers in each group have identical layout-affecting properties. These
* are the properties that were formerly used by explicit `ref` mechanism
* for layers: 'type', 'source', 'source-layer', 'minzoom', 'maxzoom',
* 'filter', and 'layout'.
*
* The input is not modified. The output layers are references to the
* input layers.
*
* @private
* @param {Array<Layer>} layers
* @param {Object} [cachedKeys] - an object to keep already calculated keys.
* @returns {Array<Array<Layer>>}
*/
function groupByLayout(layers, cachedKeys) {
const groups = {};
for (let i = 0; i < layers.length; i++) {
const k = (cachedKeys && cachedKeys[layers[i].id]) || getKey(layers[i]);
// update the cache if there is one
if (cachedKeys)
cachedKeys[layers[i].id] = k;
let group = groups[k];
if (!group) {
group = groups[k] = [];
}
group.push(layers[i]);
}
const result = [];
for (const k in groups) {
result.push(groups[k]);
}
return result;
}
|