utils.js (1076B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 5 "use strict"; 6 7 /** 8 * Find Placeholders in the template and save them along with their 9 * paths. 10 */ 11 function findPlaceholders(template, constructor, path = [], placeholders = []) { 12 if (!template || typeof template != "object") { 13 return placeholders; 14 } 15 16 if (template instanceof constructor) { 17 placeholders.push({ placeholder: template, path: [...path] }); 18 return placeholders; 19 } 20 21 for (const name in template) { 22 path.push(name); 23 findPlaceholders(template[name], constructor, path, placeholders); 24 path.pop(); 25 } 26 27 return placeholders; 28 } 29 30 exports.findPlaceholders = findPlaceholders; 31 32 /** 33 * Get the value at a given path, or undefined if not found. 34 */ 35 function getPath(obj, path) { 36 for (const name of path) { 37 if (!(name in obj)) { 38 return undefined; 39 } 40 obj = obj[name]; 41 } 42 return obj; 43 } 44 exports.getPath = getPath;