@atlaspack/utils 2.16.2-noselfbuild-3f2849b52.0 → 2.16.2-noselfbuild-8c516162e.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/DefaultMap.js +46 -0
- package/lib/Deferred.js +30 -0
- package/lib/PromiseQueue.js +112 -0
- package/lib/TapStream.js +34 -0
- package/lib/alternatives.js +116 -0
- package/lib/ansi-html.js +18 -0
- package/lib/blob.js +40 -0
- package/lib/bundle-url.js +34 -0
- package/lib/collection.js +111 -0
- package/lib/config.js +172 -0
- package/lib/countLines.js +15 -0
- package/lib/debounce.js +18 -0
- package/lib/debug-tools.js +36 -0
- package/lib/dependency-location.js +21 -0
- package/lib/escape-html.js +22 -0
- package/lib/generateBuildMetrics.js +121 -0
- package/lib/generateCertificate.js +129 -0
- package/lib/getCertificate.js +18 -0
- package/lib/getExisting.js +25 -0
- package/lib/getModuleParts.js +30 -0
- package/lib/getRootDir.js +52 -0
- package/lib/glob.js +110 -0
- package/lib/hash.js +50 -0
- package/lib/http-server.js +85 -0
- package/lib/index.js +478 -37357
- package/lib/is-url.js +22 -0
- package/lib/isDirectoryInside.js +18 -0
- package/lib/objectHash.js +27 -0
- package/lib/openInBrowser.js +74 -0
- package/lib/parseCSSImport.js +15 -0
- package/lib/path.js +39 -0
- package/lib/prettifyTime.js +9 -0
- package/lib/prettyDiagnostic.js +136 -0
- package/lib/progress-message.js +27 -0
- package/lib/relativeBundlePath.js +22 -0
- package/lib/relativeUrl.js +24 -0
- package/lib/replaceBundleReferences.js +199 -0
- package/lib/schema.js +336 -0
- package/lib/shared-buffer.js +27 -0
- package/lib/sourcemap.js +127 -0
- package/lib/stream.js +76 -0
- package/lib/throttle.js +15 -0
- package/lib/urlJoin.js +35 -0
- package/package.json +15 -16
- package/lib/index.js.map +0 -1
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.DefaultWeakMap = exports.DefaultMap = void 0;
|
|
7
|
+
class DefaultMap extends Map {
|
|
8
|
+
constructor(getDefault, entries) {
|
|
9
|
+
super(entries);
|
|
10
|
+
this._getDefault = getDefault;
|
|
11
|
+
}
|
|
12
|
+
get(key) {
|
|
13
|
+
let ret;
|
|
14
|
+
if (this.has(key)) {
|
|
15
|
+
ret = super.get(key);
|
|
16
|
+
} else {
|
|
17
|
+
ret = this._getDefault(key);
|
|
18
|
+
this.set(key, ret);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
// $FlowFixMe
|
|
22
|
+
return ret;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
exports.DefaultMap = DefaultMap;
|
|
26
|
+
// Duplicated from DefaultMap implementation for Flow
|
|
27
|
+
// Roughly mirrors https://github.com/facebook/flow/blob/2eb5a78d92c167117ba9caae070afd2b9f598599/lib/core.js#L617
|
|
28
|
+
class DefaultWeakMap extends WeakMap {
|
|
29
|
+
constructor(getDefault, entries) {
|
|
30
|
+
super(entries);
|
|
31
|
+
this._getDefault = getDefault;
|
|
32
|
+
}
|
|
33
|
+
get(key) {
|
|
34
|
+
let ret;
|
|
35
|
+
if (this.has(key)) {
|
|
36
|
+
ret = super.get(key);
|
|
37
|
+
} else {
|
|
38
|
+
ret = this._getDefault(key);
|
|
39
|
+
this.set(key, ret);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// $FlowFixMe
|
|
43
|
+
return ret;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
exports.DefaultWeakMap = DefaultWeakMap;
|
package/lib/Deferred.js
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.makeDeferredWithPromise = makeDeferredWithPromise;
|
|
7
|
+
function _assert() {
|
|
8
|
+
const data = _interopRequireDefault(require("assert"));
|
|
9
|
+
_assert = function () {
|
|
10
|
+
return data;
|
|
11
|
+
};
|
|
12
|
+
return data;
|
|
13
|
+
}
|
|
14
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
15
|
+
function makeDeferredWithPromise() {
|
|
16
|
+
let deferred;
|
|
17
|
+
let promise = new Promise((resolve, reject) => {
|
|
18
|
+
deferred = {
|
|
19
|
+
resolve,
|
|
20
|
+
reject
|
|
21
|
+
};
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
// Promise constructor callback executes synchronously, so this is defined
|
|
25
|
+
(0, _assert().default)(deferred != null);
|
|
26
|
+
return {
|
|
27
|
+
deferred,
|
|
28
|
+
promise
|
|
29
|
+
};
|
|
30
|
+
}
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = void 0;
|
|
7
|
+
var _Deferred = require("./Deferred");
|
|
8
|
+
class PromiseQueue {
|
|
9
|
+
_numRunning = 0;
|
|
10
|
+
_queue = [];
|
|
11
|
+
_runPromise = null;
|
|
12
|
+
_count = 0;
|
|
13
|
+
_results = [];
|
|
14
|
+
_addSubscriptions = new Set();
|
|
15
|
+
constructor(opts = {
|
|
16
|
+
maxConcurrent: Infinity
|
|
17
|
+
}) {
|
|
18
|
+
if (opts.maxConcurrent <= 0) {
|
|
19
|
+
throw new TypeError('maxConcurrent must be a positive, non-zero value');
|
|
20
|
+
}
|
|
21
|
+
this._maxConcurrent = opts.maxConcurrent;
|
|
22
|
+
}
|
|
23
|
+
getNumWaiting() {
|
|
24
|
+
return this._queue.length;
|
|
25
|
+
}
|
|
26
|
+
add(fn) {
|
|
27
|
+
new Promise((resolve, reject) => {
|
|
28
|
+
let i = this._count++;
|
|
29
|
+
let wrapped = () => fn().then(result => {
|
|
30
|
+
this._results[i] = result;
|
|
31
|
+
resolve(result);
|
|
32
|
+
}, err => {
|
|
33
|
+
reject(err);
|
|
34
|
+
throw err;
|
|
35
|
+
});
|
|
36
|
+
this._queue.push(wrapped);
|
|
37
|
+
for (const addFn of this._addSubscriptions) {
|
|
38
|
+
addFn();
|
|
39
|
+
}
|
|
40
|
+
if (this._numRunning > 0 && this._numRunning < this._maxConcurrent) {
|
|
41
|
+
this._next();
|
|
42
|
+
}
|
|
43
|
+
}).catch(() => {});
|
|
44
|
+
}
|
|
45
|
+
subscribeToAdd(fn) {
|
|
46
|
+
this._addSubscriptions.add(fn);
|
|
47
|
+
return () => {
|
|
48
|
+
this._addSubscriptions.delete(fn);
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
run() {
|
|
52
|
+
if (this._runPromise != null) {
|
|
53
|
+
return this._runPromise;
|
|
54
|
+
}
|
|
55
|
+
if (this._queue.length === 0) {
|
|
56
|
+
return Promise.resolve([]);
|
|
57
|
+
}
|
|
58
|
+
let {
|
|
59
|
+
deferred,
|
|
60
|
+
promise
|
|
61
|
+
} = (0, _Deferred.makeDeferredWithPromise)();
|
|
62
|
+
this._deferred = deferred;
|
|
63
|
+
this._runPromise = promise;
|
|
64
|
+
while (this._queue.length && this._numRunning < this._maxConcurrent) {
|
|
65
|
+
this._next();
|
|
66
|
+
}
|
|
67
|
+
return promise;
|
|
68
|
+
}
|
|
69
|
+
async _next() {
|
|
70
|
+
let fn = this._queue.shift();
|
|
71
|
+
await this._runFn(fn);
|
|
72
|
+
if (this._queue.length) {
|
|
73
|
+
this._next();
|
|
74
|
+
} else if (this._numRunning === 0) {
|
|
75
|
+
this._done();
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
async _runFn(fn) {
|
|
79
|
+
this._numRunning++;
|
|
80
|
+
try {
|
|
81
|
+
await fn();
|
|
82
|
+
} catch (e) {
|
|
83
|
+
// Only store the first error that occurs.
|
|
84
|
+
// We don't reject immediately so that any other concurrent
|
|
85
|
+
// requests have time to complete.
|
|
86
|
+
if (this._error == null) {
|
|
87
|
+
this._error = e;
|
|
88
|
+
}
|
|
89
|
+
} finally {
|
|
90
|
+
this._numRunning--;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
_resetState() {
|
|
94
|
+
this._queue = [];
|
|
95
|
+
this._count = 0;
|
|
96
|
+
this._results = [];
|
|
97
|
+
this._runPromise = null;
|
|
98
|
+
this._numRunning = 0;
|
|
99
|
+
this._deferred = null;
|
|
100
|
+
}
|
|
101
|
+
_done() {
|
|
102
|
+
if (this._deferred != null) {
|
|
103
|
+
if (this._error != null) {
|
|
104
|
+
this._deferred.reject(this._error);
|
|
105
|
+
} else {
|
|
106
|
+
this._deferred.resolve(this._results);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
this._resetState();
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
exports.default = PromiseQueue;
|
package/lib/TapStream.js
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = void 0;
|
|
7
|
+
function _stream() {
|
|
8
|
+
const data = require("stream");
|
|
9
|
+
_stream = function () {
|
|
10
|
+
return data;
|
|
11
|
+
};
|
|
12
|
+
return data;
|
|
13
|
+
}
|
|
14
|
+
/*
|
|
15
|
+
* "Taps" into the contents of a flowing stream, yielding chunks to the passed
|
|
16
|
+
* callback. Continues to pass data chunks down the stream.
|
|
17
|
+
*/
|
|
18
|
+
class TapStream extends _stream().Transform {
|
|
19
|
+
constructor(tap, options) {
|
|
20
|
+
super({
|
|
21
|
+
...options
|
|
22
|
+
});
|
|
23
|
+
this._tap = tap;
|
|
24
|
+
}
|
|
25
|
+
_transform(chunk, encoding, callback) {
|
|
26
|
+
try {
|
|
27
|
+
this._tap(Buffer.from(chunk));
|
|
28
|
+
callback(null, chunk);
|
|
29
|
+
} catch (err) {
|
|
30
|
+
callback(err);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
exports.default = TapStream;
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.findAlternativeFiles = findAlternativeFiles;
|
|
7
|
+
exports.findAlternativeNodeModules = findAlternativeNodeModules;
|
|
8
|
+
function _path() {
|
|
9
|
+
const data = _interopRequireDefault(require("path"));
|
|
10
|
+
_path = function () {
|
|
11
|
+
return data;
|
|
12
|
+
};
|
|
13
|
+
return data;
|
|
14
|
+
}
|
|
15
|
+
var _schema = require("./schema");
|
|
16
|
+
var _path2 = require("./path");
|
|
17
|
+
var _config = require("./config");
|
|
18
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
19
|
+
async function findAlternativeNodeModules(fs, moduleName, dir) {
|
|
20
|
+
let potentialModules = [];
|
|
21
|
+
let root = _path().default.parse(dir).root;
|
|
22
|
+
let isOrganisationModule = moduleName.startsWith('@');
|
|
23
|
+
while (dir !== root) {
|
|
24
|
+
// Skip node_modules directories
|
|
25
|
+
if (_path().default.basename(dir) === 'node_modules') {
|
|
26
|
+
dir = _path().default.dirname(dir);
|
|
27
|
+
}
|
|
28
|
+
try {
|
|
29
|
+
let modulesDir = _path().default.join(dir, 'node_modules');
|
|
30
|
+
let stats = await fs.stat(modulesDir);
|
|
31
|
+
if (stats.isDirectory()) {
|
|
32
|
+
let dirContent = (await fs.readdir(modulesDir)).sort();
|
|
33
|
+
|
|
34
|
+
// Filter out the modules that interest us
|
|
35
|
+
let modules = dirContent.filter(i => isOrganisationModule ? i.startsWith('@') : !i.startsWith('@'));
|
|
36
|
+
|
|
37
|
+
// If it's an organisation module, loop through all the modules of that organisation
|
|
38
|
+
if (isOrganisationModule) {
|
|
39
|
+
await Promise.all(modules.map(async item => {
|
|
40
|
+
let orgDirPath = _path().default.join(modulesDir, item);
|
|
41
|
+
let orgDirContent = (await fs.readdir(orgDirPath)).sort();
|
|
42
|
+
|
|
43
|
+
// Add all org packages
|
|
44
|
+
potentialModules.push(...orgDirContent.map(i => `${item}/${i}`));
|
|
45
|
+
}));
|
|
46
|
+
} else {
|
|
47
|
+
potentialModules.push(...modules);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
} catch (err) {
|
|
51
|
+
// ignore
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
// Move up a directory
|
|
55
|
+
dir = _path().default.dirname(dir);
|
|
56
|
+
}
|
|
57
|
+
return (0, _schema.fuzzySearch)(potentialModules.sort(), moduleName).slice(0, 2);
|
|
58
|
+
}
|
|
59
|
+
async function findAllFilesUp({
|
|
60
|
+
fs,
|
|
61
|
+
dir,
|
|
62
|
+
root,
|
|
63
|
+
basedir,
|
|
64
|
+
maxlength,
|
|
65
|
+
collected,
|
|
66
|
+
leadingDotSlash = true,
|
|
67
|
+
includeDirectories = true
|
|
68
|
+
}) {
|
|
69
|
+
let dirContent = (await fs.readdir(dir)).sort();
|
|
70
|
+
return Promise.all(dirContent.map(async item => {
|
|
71
|
+
let fullPath = _path().default.join(dir, item);
|
|
72
|
+
let relativeFilePath = (0, _path2.relativePath)(basedir, fullPath, leadingDotSlash);
|
|
73
|
+
if (relativeFilePath.length < maxlength) {
|
|
74
|
+
let stats = await fs.stat(fullPath);
|
|
75
|
+
let isDir = stats.isDirectory();
|
|
76
|
+
if (isDir && includeDirectories || stats.isFile()) {
|
|
77
|
+
collected.push(relativeFilePath);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
// If it's a directory, run over each item within said directory...
|
|
81
|
+
if (isDir) {
|
|
82
|
+
return findAllFilesUp({
|
|
83
|
+
fs,
|
|
84
|
+
dir: fullPath,
|
|
85
|
+
root,
|
|
86
|
+
basedir,
|
|
87
|
+
maxlength,
|
|
88
|
+
collected
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
}));
|
|
93
|
+
}
|
|
94
|
+
async function findAlternativeFiles(fs, fileSpecifier, dir, projectRoot, leadingDotSlash = true, includeDirectories = true, includeExtension = false) {
|
|
95
|
+
let potentialFiles = [];
|
|
96
|
+
// Find our root, we won't recommend files above the package root as that's bad practise
|
|
97
|
+
let pkg = await (0, _config.resolveConfig)(fs, _path().default.join(dir, 'index'), ['package.json'], projectRoot);
|
|
98
|
+
let pkgRoot = pkg ? _path().default.dirname(pkg) : projectRoot;
|
|
99
|
+
await findAllFilesUp({
|
|
100
|
+
fs,
|
|
101
|
+
dir: pkgRoot,
|
|
102
|
+
root: pkgRoot,
|
|
103
|
+
basedir: dir,
|
|
104
|
+
maxlength: fileSpecifier.length + 10,
|
|
105
|
+
collected: potentialFiles,
|
|
106
|
+
leadingDotSlash,
|
|
107
|
+
includeDirectories
|
|
108
|
+
});
|
|
109
|
+
if (_path().default.extname(fileSpecifier) === '' && !includeExtension) {
|
|
110
|
+
potentialFiles = potentialFiles.map(p => {
|
|
111
|
+
let ext = _path().default.extname(p);
|
|
112
|
+
return ext.length > 0 ? p.slice(0, -ext.length) : p;
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
return (0, _schema.fuzzySearch)(potentialFiles.sort(), fileSpecifier).slice(0, 2);
|
|
116
|
+
}
|
package/lib/ansi-html.js
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.ansiHtml = ansiHtml;
|
|
7
|
+
function _ansiHtmlCommunity() {
|
|
8
|
+
const data = _interopRequireDefault(require("ansi-html-community"));
|
|
9
|
+
_ansiHtmlCommunity = function () {
|
|
10
|
+
return data;
|
|
11
|
+
};
|
|
12
|
+
return data;
|
|
13
|
+
}
|
|
14
|
+
var _escapeHtml = require("./escape-html");
|
|
15
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
16
|
+
function ansiHtml(ansi) {
|
|
17
|
+
return (0, _ansiHtmlCommunity().default)((0, _escapeHtml.escapeHTML)(ansi));
|
|
18
|
+
}
|
package/lib/blob.js
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.blobToBuffer = blobToBuffer;
|
|
7
|
+
exports.blobToString = blobToString;
|
|
8
|
+
function _buffer() {
|
|
9
|
+
const data = require("buffer");
|
|
10
|
+
_buffer = function () {
|
|
11
|
+
return data;
|
|
12
|
+
};
|
|
13
|
+
return data;
|
|
14
|
+
}
|
|
15
|
+
var _ = require("./");
|
|
16
|
+
function _stream() {
|
|
17
|
+
const data = require("stream");
|
|
18
|
+
_stream = function () {
|
|
19
|
+
return data;
|
|
20
|
+
};
|
|
21
|
+
return data;
|
|
22
|
+
}
|
|
23
|
+
function blobToBuffer(blob) {
|
|
24
|
+
if (blob instanceof _stream().Readable) {
|
|
25
|
+
return (0, _.bufferStream)(blob);
|
|
26
|
+
} else if (blob instanceof _buffer().Buffer) {
|
|
27
|
+
return Promise.resolve(_buffer().Buffer.from(blob));
|
|
28
|
+
} else {
|
|
29
|
+
return Promise.resolve(_buffer().Buffer.from(blob, 'utf8'));
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
async function blobToString(blob) {
|
|
33
|
+
if (blob instanceof _stream().Readable) {
|
|
34
|
+
return (await (0, _.bufferStream)(blob)).toString();
|
|
35
|
+
} else if (blob instanceof _buffer().Buffer) {
|
|
36
|
+
return blob.toString();
|
|
37
|
+
} else {
|
|
38
|
+
return blob;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.getBaseURL = getBaseURL;
|
|
7
|
+
exports.getBundleURL = void 0;
|
|
8
|
+
let bundleURL = null;
|
|
9
|
+
function getBundleURLCached() {
|
|
10
|
+
if (bundleURL == null) {
|
|
11
|
+
bundleURL = _getBundleURL();
|
|
12
|
+
}
|
|
13
|
+
return bundleURL;
|
|
14
|
+
}
|
|
15
|
+
function _getBundleURL() {
|
|
16
|
+
// Attempt to find the URL of the current script and use that as the base URL
|
|
17
|
+
try {
|
|
18
|
+
throw new Error();
|
|
19
|
+
} catch (err) {
|
|
20
|
+
let stack = typeof err.stack === 'string' ? err.stack : '';
|
|
21
|
+
let matches = stack.match(/(https?|file|ftp):\/\/[^)\n]+/g);
|
|
22
|
+
if (matches) {
|
|
23
|
+
return getBaseURL(matches[0]);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
return '/';
|
|
27
|
+
}
|
|
28
|
+
function getBaseURL(url) {
|
|
29
|
+
if (url == null) {
|
|
30
|
+
return '/';
|
|
31
|
+
}
|
|
32
|
+
return url.replace(/^((?:https?|file|ftp):\/\/.+)\/[^/]+$/, '$1') + '/';
|
|
33
|
+
}
|
|
34
|
+
const getBundleURL = exports.getBundleURL = getBundleURLCached;
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.objectSortedEntries = objectSortedEntries;
|
|
7
|
+
exports.objectSortedEntriesDeep = objectSortedEntriesDeep;
|
|
8
|
+
exports.setDifference = setDifference;
|
|
9
|
+
exports.setEqual = setEqual;
|
|
10
|
+
exports.setIntersect = setIntersect;
|
|
11
|
+
exports.setIntersectStatic = setIntersectStatic;
|
|
12
|
+
exports.setSymmetricDifference = setSymmetricDifference;
|
|
13
|
+
exports.setUnion = setUnion;
|
|
14
|
+
exports.unique = unique;
|
|
15
|
+
function unique(array) {
|
|
16
|
+
return [...new Set(array)];
|
|
17
|
+
}
|
|
18
|
+
function objectSortedEntries(obj) {
|
|
19
|
+
return Object.entries(obj).sort(([keyA], [keyB]) => keyA.localeCompare(keyB));
|
|
20
|
+
}
|
|
21
|
+
function objectSortedEntriesDeep(object) {
|
|
22
|
+
let sortedEntries = objectSortedEntries(object);
|
|
23
|
+
for (let i = 0; i < sortedEntries.length; i++) {
|
|
24
|
+
sortedEntries[i][1] = sortEntry(sortedEntries[i][1]);
|
|
25
|
+
}
|
|
26
|
+
return sortedEntries;
|
|
27
|
+
}
|
|
28
|
+
function sortEntry(entry) {
|
|
29
|
+
if (Array.isArray(entry)) {
|
|
30
|
+
return entry.map(sortEntry);
|
|
31
|
+
}
|
|
32
|
+
if (typeof entry === 'object' && entry != null) {
|
|
33
|
+
return objectSortedEntriesDeep(entry);
|
|
34
|
+
}
|
|
35
|
+
return entry;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Get the difference of A and B sets
|
|
40
|
+
*
|
|
41
|
+
* This is the set of elements which are in A but not in B
|
|
42
|
+
* For example, the difference of the sets {1,2,3} and {3,4} is {1,2}
|
|
43
|
+
*
|
|
44
|
+
* @param {*} a Set A
|
|
45
|
+
* @param {*} b Set B
|
|
46
|
+
* @returns A \ B
|
|
47
|
+
*/
|
|
48
|
+
function setDifference(a, b) {
|
|
49
|
+
let difference = new Set();
|
|
50
|
+
for (let e of a) {
|
|
51
|
+
if (!b.has(e)) {
|
|
52
|
+
difference.add(e);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
return difference;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Get the symmetric difference of A and B sets
|
|
60
|
+
*
|
|
61
|
+
* This is the set of elements which are in either of the sets, but not in their intersection.
|
|
62
|
+
* For example, the symmetric difference of the sets {1,2,3} and {3,4} is {1,2,4}
|
|
63
|
+
*
|
|
64
|
+
* @param {*} a Set A
|
|
65
|
+
* @param {*} b Set B
|
|
66
|
+
* @returns A Δ B
|
|
67
|
+
*/
|
|
68
|
+
function setSymmetricDifference(a, b) {
|
|
69
|
+
let difference = new Set();
|
|
70
|
+
for (let e of a) {
|
|
71
|
+
if (!b.has(e)) {
|
|
72
|
+
difference.add(e);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
for (let d of b) {
|
|
76
|
+
if (!a.has(d)) {
|
|
77
|
+
difference.add(d);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
return difference;
|
|
81
|
+
}
|
|
82
|
+
function setIntersect(a, b) {
|
|
83
|
+
for (let entry of a) {
|
|
84
|
+
if (!b.has(entry)) {
|
|
85
|
+
a.delete(entry);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
function setIntersectStatic(a, b) {
|
|
90
|
+
let intersection = new Set();
|
|
91
|
+
for (let entry of a) {
|
|
92
|
+
if (b.has(entry)) {
|
|
93
|
+
intersection.add(entry);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
return intersection;
|
|
97
|
+
}
|
|
98
|
+
function setUnion(a, b) {
|
|
99
|
+
return new Set([...a, ...b]);
|
|
100
|
+
}
|
|
101
|
+
function setEqual(a, b) {
|
|
102
|
+
if (a.size != b.size) {
|
|
103
|
+
return false;
|
|
104
|
+
}
|
|
105
|
+
for (let entry of a) {
|
|
106
|
+
if (!b.has(entry)) {
|
|
107
|
+
return false;
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
return true;
|
|
111
|
+
}
|