@cyclonedx/cyclonedx-library
Version:
Core functionality of CycloneDX for JavaScript (Node.js or WebBrowser).
64 lines (60 loc) • 2.35 kB
JavaScript
/*!
This file is part of CycloneDX JavaScript Library.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
SPDX-License-Identifier: Apache-2.0
Copyright (c) OWASP Foundation. All Rights Reserved.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.SortableNumbers = exports.SortableStringables = exports.SortableComparables = exports.SortableSet = exports.compareObjectsSymbol = void 0;
exports.compareObjectsSymbol = Symbol('internal compare function');
class SortableSet extends Set {
sorted() {
return Array.from(this).sort(this[exports.compareObjectsSymbol]);
}
compare(other) {
const sortedOther = other.sorted();
const sortedSelf = this.sorted();
if (sortedSelf.length !== sortedOther.length) {
return sortedSelf.length - sortedOther.length;
}
for (let i = sortedSelf.length - 1; i >= 0; --i) {
const iCompared = this[exports.compareObjectsSymbol](sortedSelf[i], sortedOther[i]);
if (iCompared !== 0) {
return iCompared;
}
}
return 0;
}
}
exports.SortableSet = SortableSet;
class SortableComparables extends SortableSet {
[exports.compareObjectsSymbol](a, b) {
if (a.constructor === b.constructor) {
return a.compare(b);
}
return a.constructor.name.localeCompare(b.constructor.name);
}
}
exports.SortableComparables = SortableComparables;
class SortableStringables extends SortableSet {
[exports.compareObjectsSymbol](a, b) {
return a.toString().localeCompare(b.toString());
}
}
exports.SortableStringables = SortableStringables;
class SortableNumbers extends SortableSet {
[exports.compareObjectsSymbol](a, b) {
return a - b;
}
}
exports.SortableNumbers = SortableNumbers;
//# sourceMappingURL=sortable.js.map
;