MCPcopy
hub / github.com/formatjs/formatjs / isStructurallySame

Function isStructurallySame

packages/icu-messageformat-parser/manipulator.ts:217–259  ·  view source on GitHub ↗
(
  a: MessageFormatElement[],
  b: MessageFormatElement[]
)

Source from the content-addressed store, hash-verified

215 * @returns
216 */
217export function isStructurallySame(
218 a: MessageFormatElement[],
219 b: MessageFormatElement[]
220): IsStructurallySameResult {
221 const aVars = new Map<string, TYPE>()
222 const bVars = new Map<string, TYPE>()
223 collectVariables(a, aVars)
224 collectVariables(b, bVars)
225
226 if (aVars.size !== bVars.size) {
227 return {
228 success: false,
229 error: new Error(
230 `Different number of variables: [${Array.from(aVars.keys()).join(', ')}] vs [${Array.from(bVars.keys()).join(', ')}]`
231 ),
232 }
233 }
234
235 return Array.from(aVars.entries()).reduce<IsStructurallySameResult>(
236 (result, [key, type]) => {
237 if (!result.success) {
238 return result
239 }
240 const bType = bVars.get(key)
241 if (bType == null) {
242 return {
243 success: false,
244 error: new Error(`Missing variable ${key} in message`),
245 }
246 }
247 if (bType !== type) {
248 return {
249 success: false,
250 error: new Error(
251 `Variable ${key} has conflicting types: ${TYPE[type]} vs ${TYPE[bType]}`
252 ),
253 }
254 }
255 return result
256 },
257 {success: true}
258 )
259}

Callers 2

checkStructuralEqualityFunction · 0.90

Calls 2

collectVariablesFunction · 0.85
getMethod · 0.65

Tested by

no test coverage detected