UNPKG

wle-aligner

Version:

Align two Wonderland Engine projects so that they can share, as much as possible, the same resources and structure

108 lines 4.59 kB
import { ArrayToken, JSONTokenType, KeyToken, ObjectToken } from "@playkostudios/jsonc-ast"; export class ParentChildTokenPair { parent; child; childKey; constructor(parent, child, childKey = null) { this.parent = parent; this.child = child; this.childKey = childKey; } } export function replaceParentTokenKey(oldKey, newKey, parentToken) { let found = false; for (const childToken of parentToken.children) { if (childToken.type === JSONTokenType.Key && childToken.getString() === oldKey) { parentToken.replaceChild(childToken, KeyToken.fromString(newKey)); found = true; break; } } return found; } export function getJSONTokensByKeyByTypeHierarchy(tokenKeyToFind, tokenTypeToFind, parentObjectToken) { return getJSONTokensHierarchy((tokenKey, tokenToCheck) => tokenKey == tokenKeyToFind && tokenToCheck.type === tokenTypeToFind, parentObjectToken); } export function getJSONTokensHierarchy(findCallback, parentObjectToken) { const jsonTokensByKey = []; const parentObjectTokens = [parentObjectToken]; while (parentObjectTokens.length > 0) { const currentParentObjectToken = parentObjectTokens.shift(); if (currentParentObjectToken) { for (const [tokenKey, tokenToCheck] of currentParentObjectToken.getTokenEntries()) { if (findCallback(tokenKey, tokenToCheck)) { jsonTokensByKey.push(new ParentChildTokenPair(currentParentObjectToken, tokenToCheck, tokenKey)); } if (tokenToCheck.type == JSONTokenType.Object) { parentObjectTokens.push(ObjectToken.assert(tokenToCheck)); } } } } return jsonTokensByKey; } export function getEqualJSONTokens(tokenToCheck, parentObjectToken, ignorePropertiesOrder = true) { const equalTokens = []; for (const [otherTokenKey, otherTokenToCheck] of parentObjectToken.getTokenEntries()) { if (areTokensEqual(tokenToCheck, otherTokenToCheck, ignorePropertiesOrder)) { equalTokens.push(new ParentChildTokenPair(parentObjectToken, otherTokenToCheck, otherTokenKey)); } } return equalTokens; } export function areTokensEqual(firstToken, secondToken, ignorePropertiesOrder = true) { if (firstToken == null || secondToken == null) { return firstToken == secondToken; } if (!ignorePropertiesOrder || (firstToken.type != JSONTokenType.Object && firstToken.type != JSONTokenType.Array)) { return firstToken.evaluate() == secondToken.evaluate(); } if (firstToken.type != secondToken.type) { return false; } if (firstToken.type == JSONTokenType.Object) { const firstObjectToken = ObjectToken.assert(firstToken); const secondObjectToken = ObjectToken.assert(secondToken); if (firstObjectToken.getTokenEntries().length != secondObjectToken.getTokenEntries().length) { return false; } for (const [firstTokenKey, firstTokenToCheck] of firstObjectToken.getTokenEntries()) { let tokenFound = false; for (const [secondTokenKey, secondTokenToCheck] of secondObjectToken.getTokenEntries()) { if (firstTokenKey == secondTokenKey) { tokenFound = true; if (!areTokensEqual(firstTokenToCheck, secondTokenToCheck, true)) { return false; } break; } } if (!tokenFound) { return false; } } } else if (firstToken.type == JSONTokenType.Array) { const firstObjectToken = ArrayToken.assert(firstToken); const secondObjectToken = ArrayToken.assert(secondToken); const firstTokenEntries = firstObjectToken.getTokenEntries(); const secondTokenEntries = secondObjectToken.getTokenEntries(); if (firstTokenEntries.length != secondTokenEntries.length) { return false; } for (let i = 0; i < firstTokenEntries.length; i++) { let tokenFound = false; for (let j = 0; j < secondTokenEntries.length; j++) { if (areTokensEqual(firstTokenEntries[i], secondTokenEntries[i], true)) { tokenFound = true; break; } } if (!tokenFound) { return false; } } } return true; } //# sourceMappingURL=jsonast_utils.js.map