@yellicode/elements
Version:
The meta model API for Yellicode - an extensible code generator.
76 lines (75 loc) • 2.95 kB
JavaScript
/*
* Copyright (c) 2020 Yellicode
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
import * as Interfaces from "./interfaces";
import { isFeature } from './utils';
var NO_ORDER = 0;
var ElementComparerImpl = /** @class */ (function () {
function ElementComparerImpl() {
}
ElementComparerImpl.getInstance = function () {
if (!ElementComparerImpl.instance) {
ElementComparerImpl.instance = new ElementComparerImpl();
}
return ElementComparerImpl.instance;
};
ElementComparerImpl.prototype.compareOrderedElements = function (x, y) {
// We use a value of 0 (NO_ORDER) for elements having no order at all.
// An order of NO_ORDER comes after a set order
var xOrder = x.order || NO_ORDER;
var yOrder = y.order || NO_ORDER;
if (xOrder === NO_ORDER && yOrder !== NO_ORDER) {
return 1;
}
if (yOrder === NO_ORDER && xOrder !== NO_ORDER) {
return -1;
}
// Both have an order
var result = xOrder - yOrder;
if (result !== 0)
return result;
// Both have the same order. Fallback by name for features (operations and properties), this is what we also do
// in the modeler. For enum members and parameters: keep the order as is.
return isFeature(x) ? x.name.localeCompare(y.name) : 0;
};
ElementComparerImpl.prototype.comparePackageableElements = function (x, y) {
// Packages first
if (x.elementType === Interfaces.ElementType.package) {
if (y.elementType !== Interfaces.ElementType.package) {
// x is a package, y isn't
return -1;
}
}
else if (y.elementType === Interfaces.ElementType.package) {
// x is not a package, y is
return 1;
}
// Then compare by name
return x.name.localeCompare(y.name);
};
ElementComparerImpl.haveEqualSignatures = function (x, y) {
if (x.name !== y.name)
return false;
// Same name
if (x.ownedParameters.length !== y.ownedParameters.length) {
return false;
}
// Same number of parameters
if (x.ownedParameters.length === 0)
return true;
// Same number of parameters (> 0)
for (var i = 0, len = x.ownedParameters.length; i < len; i++) {
var typeOfParamX = x.ownedParameters[i].type;
var typeOfParamY = y.ownedParameters[i].type;
if (typeOfParamX !== typeOfParamY)
return false;
}
return true;
};
return ElementComparerImpl;
}());
export { ElementComparerImpl };