Sha256: a1cdc7edcb628c08e8a0d03da46c01f332b1847330836039c41482d522ae094b

Contents?: true

Size: 923 Bytes

Versions: 7

Compression:

Stored size: 923 Bytes

Contents

"use strict";

module.exports = class SortableSet extends Set {

	constructor(initialIterable, defaultSort) {
		super(initialIterable);
		this._sortFn = defaultSort;
		this._lastActiveSortFn = null;
	}

	/**
	 * @param {any} value - value to add to set
	 * @returns {SortableSet} - returns itself
	 */
	add(value) {
		this._lastActiveSortFn = null;
		super.add(value);
		return this;
	}

	/**
	 * @param {Function} sortFn - function to sort the set
	 * @returns {void}
	 */
	sortWith(sortFn) {
		if(this.size === 0 || sortFn === this._lastActiveSortFn) {
			// already sorted - nothing to do
			return;
		}

		const sortedArray = Array.from(this).sort(sortFn);
		super.clear();
		for(let i = 0; i < sortedArray.length; i += 1) {
			this.add(sortedArray[i]);
		}
		this._lastActiveSortFn = sortFn;
	}

	/**
	 * @returns {void}
	 */
	sort() {
		this.sortWith(this._sortFn);
	}
};

Version data entries

7 entries across 3 versions & 2 rubygems

Version Path
optimacms-0.4.3 spec/dummy/node_modules/webpack/lib/util/SortableSet.js
optimacms-0.4.3 spec/dummy/node_modules/@rails/webpacker/node_modules/webpack/lib/util/SortableSet.js
optimacms-0.4.3 spec/dummy/node_modules/@rails/webpacker/node_modules/webpack/node_modules/webpack/lib/util/SortableSet.js
optimacms-0.4.2 spec/dummy/node_modules/webpack/lib/util/SortableSet.js
optimacms-0.4.2 spec/dummy/node_modules/@rails/webpacker/node_modules/webpack/lib/util/SortableSet.js
optimacms-0.4.2 spec/dummy/node_modules/@rails/webpacker/node_modules/webpack/node_modules/webpack/lib/util/SortableSet.js
cortex-0.1.3 spec/dummy/node_modules/webpack/lib/util/SortableSet.js