UNPKG

@polygonjs/polygonjs

Version:

node-based WebGL 3D engine https://polygonjs.com

82 lines (81 loc) 2.55 kB
"use strict"; import { stringMatchMask } from "../String"; export const ROOT_NAME = "/"; const REGEX_PATH_SANITIZE = /\/+/g; export function findObjectByMask(mask, parent) { return findObjectByMaskInObject(mask, parent); } export function findObjectByMaskInObject(mask, object, objectPath2 = "") { for (const child of object.children) { const childName = sanitizeObjectPath(child.name); const path = sanitizeObjectPath(`${objectPath2}/${childName}`); if (stringMatchMask(path, mask)) { return child; } const grandChild = findObjectByMaskInObject(mask, child, path); if (grandChild) { return grandChild; } } } export function objectsByMask(mask, parent, invertMask = false) { const list = []; traverseObjectsWithMask( mask, (obj) => { list.push(obj); }, parent, invertMask ); return list; } export function objectsByMaskInObject(mask, object, list = [], objectPath2 = "") { traverseObjectsWithMask( mask, (obj) => { list.push(obj); }, object ); return list; } export function traverseObjectsWithMask(mask, callback, object, invertMask = false) { traverseObjectsWithMaskInObject(mask, object, callback, invertMask); } export function traverseObjectsWithMaskInObject(mask, object, callback, invertMask, objectPath2) { const objectName = sanitizeObjectPath(object.name); const path = sanitizeObjectPath(objectPath2 != null ? `${objectPath2}/${objectName}` : objectName); let match = stringMatchMask(path, mask); if (invertMask) { match = !match; } if (match) { callback(object); } for (const child of object.children) { traverseObjectsWithMaskInObject(mask, child, callback, invertMask, path); } } export function sanitizeObjectPath(path) { return path.replace(REGEX_PATH_SANITIZE, "/"); } export function objectPath(object, topParent) { const parent = object.parent; if (parent && object != topParent) { const parentPath = objectPath(parent, topParent); return sanitizeObjectPath(`${parentPath}/${object.name}`); } else { return object.name; } } export class CorePath { } CorePath.findObjectByMask = findObjectByMask; CorePath.findObjectByMaskInObject = findObjectByMaskInObject; CorePath.objectsByMask = objectsByMask; CorePath.objectsByMaskInObject = objectsByMaskInObject; CorePath.traverseObjectsWithMask = traverseObjectsWithMask; CorePath.traverseObjectsWithMaskInObject = traverseObjectsWithMaskInObject; CorePath.objectPath = objectPath; CorePath.sanitizePath = sanitizeObjectPath;