@logic-pad/core
Version:
73 lines (72 loc) • 2.72 kB
JavaScript
import { DIRECTIONS, ORIENTATIONS, } from './primitives.js';
import { normalizeShape, shapeEquals, tilesToShape } from './shapes.js';
export var ConfigType;
(function (ConfigType) {
ConfigType["Boolean"] = "boolean";
ConfigType["NullableBoolean"] = "nullableBoolean";
ConfigType["Number"] = "number";
ConfigType["NullableNumber"] = "nullableNumber";
ConfigType["String"] = "string";
ConfigType["Color"] = "color";
ConfigType["Comparison"] = "comparison";
ConfigType["Wrapping"] = "wrapping";
ConfigType["Direction"] = "direction";
ConfigType["DirectionToggle"] = "directionToggle";
ConfigType["Orientation"] = "orientation";
ConfigType["OrientationToggle"] = "orientationToggle";
ConfigType["Tile"] = "tile";
ConfigType["Shape"] = "shape";
ConfigType["Grid"] = "grid";
ConfigType["NullableGrid"] = "nullableGrid";
ConfigType["Icon"] = "icon";
ConfigType["ControlLines"] = "controlLines";
ConfigType["NullableNote"] = "nullableNote";
ConfigType["NullableInstrument"] = "nullableInstrument";
ConfigType["SolvePath"] = "solvePath";
})(ConfigType || (ConfigType = {}));
/**
* Compare two config values for equality, using an appropriate method for the config type.
*
* @param type The type of the config.
* @param a The first value to compare.
* @param b The second value to compare.
* @returns Whether the two values are equal.
*/
export function configEquals(type, a, b) {
if (type === ConfigType.ControlLines) {
const aLines = a;
const bLines = b;
if (aLines.length !== bLines.length)
return false;
return aLines.every((line, i) => line.equals(bLines[i]));
}
if (type === ConfigType.Tile || type === ConfigType.Grid) {
return a.equals(b);
}
if (type === ConfigType.Shape) {
const aShape = normalizeShape(tilesToShape(a.tiles));
const bShape = normalizeShape(tilesToShape(b.tiles));
return shapeEquals(aShape, bShape);
}
if (type === ConfigType.NullableGrid) {
if (a === null && b === null)
return true;
if (a === null || b === null)
return false;
return a.equals(b);
}
if (type === ConfigType.DirectionToggle) {
return DIRECTIONS.every(dir => a[dir] === b[dir]);
}
if (type === ConfigType.OrientationToggle) {
return ORIENTATIONS.every(dir => a[dir] === b[dir]);
}
if (type === ConfigType.SolvePath) {
const aPath = a;
const bPath = b;
if (aPath.length !== bPath.length)
return false;
return aPath.every((pos, i) => pos.x === bPath[i].x && pos.y === bPath[i].y);
}
return a === b;
}