ranges-sort 3.11.0 → 3.11.4
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/README.md +164 -167
- package/dist/ranges-sort.cjs.js +52 -3
- package/dist/ranges-sort.dev.umd.js +169 -0
- package/dist/ranges-sort.esm.js +4 -4
- package/dist/ranges-sort.umd.js +2 -2
- package/package.json +24 -19
package/README.md
CHANGED
|
@@ -1,167 +1,164 @@
|
|
|
1
|
-
# ranges-sort
|
|
2
|
-
|
|
3
|
-
> Sort natural number index ranges [ [5, 6], [1, 3] ] => [ [1, 3], [5, 6] ]
|
|
4
|
-
|
|
5
|
-
[![
|
|
6
|
-
[![
|
|
7
|
-
[![
|
|
8
|
-
[![
|
|
9
|
-
[![
|
|
10
|
-
[![
|
|
11
|
-
[![
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
|
48
|
-
|
|
|
49
|
-
|
|
|
50
|
-
| **
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
- [
|
|
58
|
-
- [
|
|
59
|
-
- [
|
|
60
|
-
- [
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
[ [5, 6], [
|
|
73
|
-
[
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
[[
|
|
77
|
-
[[
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
|
97
|
-
|
|
|
98
|
-
| `
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
|
114
|
-
|
|
|
115
|
-
|
|
|
116
|
-
| `
|
|
117
|
-
|
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
- If you
|
|
136
|
-
- If you
|
|
137
|
-
- If you
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
[
|
|
153
|
-
[
|
|
154
|
-
[
|
|
155
|
-
[
|
|
156
|
-
[
|
|
157
|
-
[
|
|
158
|
-
[
|
|
159
|
-
[
|
|
160
|
-
[
|
|
161
|
-
[
|
|
162
|
-
[
|
|
163
|
-
[
|
|
164
|
-
[
|
|
165
|
-
[prettier-url]: https://prettier.io
|
|
166
|
-
[license-img]: https://img.shields.io/badge/licence-MIT-51c838.svg?style=flat-square
|
|
167
|
-
[license-url]: https://gitlab.com/codsen/codsen/blob/master/LICENSE
|
|
1
|
+
# ranges-sort
|
|
2
|
+
|
|
3
|
+
> Sort natural number index ranges [ [5, 6], [1, 3] ] => [ [1, 3], [5, 6] ]
|
|
4
|
+
|
|
5
|
+
[![Repository is on GitLab][gitlab-img]][gitlab-url]
|
|
6
|
+
[![Coverage][cov-img]][cov-url]
|
|
7
|
+
[![no dependencies][no-deps-img]][no-deps-url]
|
|
8
|
+
[![Downloads/Month][downloads-img]][downloads-url]
|
|
9
|
+
[![Test in browser][runkit-img]][runkit-url]
|
|
10
|
+
[![Code style: prettier][prettier-img]][prettier-url]
|
|
11
|
+
[![MIT License][license-img]][license-url]
|
|
12
|
+
|
|
13
|
+
## Install
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
npm i ranges-sort
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
The [_default_](https://exploringjs.com/es6/ch_modules.html#_default-exports-one-per-module) is exported, so instead of "`rsort`" below, you can name the consumed function however you want.
|
|
20
|
+
|
|
21
|
+
Consume via a `require()`:
|
|
22
|
+
|
|
23
|
+
```js
|
|
24
|
+
const rsort = require("ranges-sort");
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
or as an ES Module:
|
|
28
|
+
|
|
29
|
+
```js
|
|
30
|
+
import rsort from "ranges-sort";
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
or for web pages, as a production-ready minified script file (so-called "UMD build"), straight from CDN:
|
|
34
|
+
|
|
35
|
+
```html
|
|
36
|
+
<script src="https://cdn.jsdelivr.net/npm/ranges-sort/dist/ranges-sort.umd.js"></script>
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
```js
|
|
40
|
+
// in which case you get a global variable "rangesSort" which you consume like this:
|
|
41
|
+
const rsort = rangesSort;
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
This package has three builds in `dist/` folder:
|
|
45
|
+
|
|
46
|
+
| Type | Key in `package.json` | Path | Size |
|
|
47
|
+
| ------------------------------------------------------------------------------------------------------- | --------------------- | ------------------------- | ---- |
|
|
48
|
+
| Main export - **CommonJS version**, transpiled to ES5, contains `require` and `module.exports` | `main` | `dist/ranges-sort.cjs.js` | 4 KB |
|
|
49
|
+
| **ES module** build that Webpack/Rollup understands. Untranspiled ES6 code with `import`/`export`. | `module` | `dist/ranges-sort.esm.js` | 3 KB |
|
|
50
|
+
| **UMD build** for browsers, transpiled, minified, containing `iife`'s and has all dependencies baked-in | `browser` | `dist/ranges-sort.umd.js` | 3 KB |
|
|
51
|
+
|
|
52
|
+
**[⬆ back to top](#)**
|
|
53
|
+
|
|
54
|
+
## Table of Contents
|
|
55
|
+
|
|
56
|
+
- [Install](#install)
|
|
57
|
+
- [What it does](#what-it-does)
|
|
58
|
+
- [API](#api)
|
|
59
|
+
- [Contributing](#contributing)
|
|
60
|
+
- [Licence](#licence)
|
|
61
|
+
|
|
62
|
+
## What it does
|
|
63
|
+
|
|
64
|
+
Background: strings in JavaScript consist of characters. For example, `abc` is a string. Each character has an index number and the numbering starts from zero. For example, in "abc", "a" has index `0`, "b" has index `1` and so on.
|
|
65
|
+
|
|
66
|
+
We use arrays to **mark what to delete** from a string, from example, deletion from index `0` to index `5` would be `[0, 5]`. If we added a third element, that would mean we want to **insert it**, replacing the given index range. For example, if a string is `abc` and we want to delete "b", that would be range `[1, 2]`. If we wanted to replace "b" with "x", that would be range `[1, 2, "x"]`.
|
|
67
|
+
|
|
68
|
+
Now, if you have _an array_ of such ranges (that's an _array of arrays_), this library can **sort them**. For example:
|
|
69
|
+
|
|
70
|
+
```js
|
|
71
|
+
[ [5, 6], [1, 3] ] => [ [1, 3], [5, 6] ]
|
|
72
|
+
[ [5, 6], [5, 3], [5, 0] ] => [ [5, 0], [5, 3], [5, 6] ]
|
|
73
|
+
[] => []
|
|
74
|
+
|
|
75
|
+
[[1, 2], []] => // throws, because there's at least one empty range
|
|
76
|
+
[['a']] => // throws, because range is not a range but a string
|
|
77
|
+
[[1], [2]] => // throws, because one index is not a range
|
|
78
|
+
|
|
79
|
+
// 3rd argument and onwards are ignored:
|
|
80
|
+
[[3, 4, 'aaa', 'bbb'], [1, 2, 'zzz']] => [[1, 2, 'zzz'], [3, 4, 'aaa', 'bbb']]
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
This is a specialised library to sort these types of arrays, not any type of arrays. We expect first two elements in each array to be a natural number and there can be optional third argument (of any type).
|
|
84
|
+
|
|
85
|
+
The purpose of string index ranges is to avoid changing a string many times but instead, track the changes (ranges of indexes), compiling them in an array, and later perform all changes in one go. This guarantees the original characters in the string retain the original positions throughout the whole operation. It's easier and faster to process strings this way.
|
|
86
|
+
|
|
87
|
+
The purpose of _range sorting_ is to make life easier for other range-processing libraries.
|
|
88
|
+
|
|
89
|
+
**[⬆ back to top](#)**
|
|
90
|
+
|
|
91
|
+
## API
|
|
92
|
+
|
|
93
|
+
**rsort(arr[, opts])** — in other words, this library gives you a _function_ and you must feed an array into its first argument and also if you wish, you can feed a second argument, the _Optional Options Object_ (bracket in `[, opts]` means "optional").
|
|
94
|
+
|
|
95
|
+
| Input argument | Type | Obligatory? | Description |
|
|
96
|
+
| -------------- | ------------ | ----------- | ---------------------------------------------------------------------------- |
|
|
97
|
+
| `arrOfRanges` | Array | yes | Array of zero or more arrays meaning natural number ranges (2 elements each) |
|
|
98
|
+
| `opts` | Plain object | no | Optional options go here. |
|
|
99
|
+
|
|
100
|
+
For example,
|
|
101
|
+
|
|
102
|
+
```js
|
|
103
|
+
[ [5, 9], [5, 3] ] => [ [5, 3], [5, 9] ]
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
This library does not mutate the inputs. In theory, a function in JavaScript could mutate its arguments, but only if they are on an "object" primitive type (an array or a plain object, for example).
|
|
107
|
+
|
|
108
|
+
**[⬆ back to top](#)**
|
|
109
|
+
|
|
110
|
+
### Options object
|
|
111
|
+
|
|
112
|
+
| `options` object's key | Type | Obligatory? | Default | Description |
|
|
113
|
+
| ---------------------------------- | -------- | ----------- | ------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
114
|
+
| { | | | |
|
|
115
|
+
| `strictlyTwoElementsInRangeArrays` | Boolean | no | `false` | If set to `true`, all ranges must have two and only elements, otherwise error is thrown. For example, input being `[ [1, 2, 'zzz'] ]` would throw (3 elements), as well as `[ ['a'] ]` (1 element). |
|
|
116
|
+
| `progressFn` | Function | no | `null` | If a function is given, it will be called with natural number meaning percentage of the total work done. It's approximate and used in worker setups. |
|
|
117
|
+
| } | | | |
|
|
118
|
+
|
|
119
|
+
**Output:** Sorted input array. First, we sort by the first argument of each child range array, then by second.
|
|
120
|
+
|
|
121
|
+
Here is whole Optional Options Object in one place, with all defaults, in case you want to copy it:
|
|
122
|
+
|
|
123
|
+
```js
|
|
124
|
+
{
|
|
125
|
+
strictlyTwoElementsInRangeArrays: false,
|
|
126
|
+
progressFn: null
|
|
127
|
+
}
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
**[⬆ back to top](#)**
|
|
131
|
+
|
|
132
|
+
## Contributing
|
|
133
|
+
|
|
134
|
+
- If you see an error, [raise an issue](<https://gitlab.com/codsen/codsen/issues/new?issue[title]=ranges-sort%20package%20-%20put%20title%20here&issue[description]=**Which%20package%20is%20this%20issue%20for**%3A%20%0Aranges-sort%0A%0A**Describe%20the%20issue%20(if%20necessary)**%3A%20%0A%0A%0A%2Fassign%20%40revelt>).
|
|
135
|
+
- If you want a new feature but can't code it up yourself, also [raise an issue](<https://gitlab.com/codsen/codsen/issues/new?issue[title]=ranges-sort%20package%20-%20put%20title%20here&issue[description]=**Which%20package%20is%20this%20issue%20for**%3A%20%0Aranges-sort%0A%0A**Describe%20the%20issue%20(if%20necessary)**%3A%20%0A%0A%0A%2Fassign%20%40revelt>). Let's discuss it.
|
|
136
|
+
- If you tried to use this package, but something didn't work out, also [raise an issue](<https://gitlab.com/codsen/codsen/issues/new?issue[title]=ranges-sort%20package%20-%20put%20title%20here&issue[description]=**Which%20package%20is%20this%20issue%20for**%3A%20%0Aranges-sort%0A%0A**Describe%20the%20issue%20(if%20necessary)**%3A%20%0A%0A%0A%2Fassign%20%40revelt>). We'll try to help.
|
|
137
|
+
- If you want to contribute some code, fork the [monorepo](https://gitlab.com/codsen/codsen/) via GitLab, then write code, then file a pull request on GitLab. We'll merge it in and release.
|
|
138
|
+
|
|
139
|
+
In monorepo, npm libraries are located in `packages/` folder. Inside, the source code is located either in `src/` folder (normal npm library) or in the root, `cli.js` (if it's a command-line application).
|
|
140
|
+
|
|
141
|
+
The npm script "`dev`", the `"dev": "rollup -c --dev"` builds the development version retaining all `console.log`s with row numbers. It's handy to have [js-row-num-cli](https://www.npmjs.com/package/js-row-num-cli) installed globally so you can automatically update the row numbers on all `console.log`s.
|
|
142
|
+
|
|
143
|
+
**[⬆ back to top](#)**
|
|
144
|
+
|
|
145
|
+
## Licence
|
|
146
|
+
|
|
147
|
+
MIT License
|
|
148
|
+
|
|
149
|
+
Copyright (c) 2015-2020 Roy Revelt and other contributors
|
|
150
|
+
|
|
151
|
+
[gitlab-img]: https://img.shields.io/badge/repo-on%20GitLab-brightgreen.svg?style=flat-square
|
|
152
|
+
[gitlab-url]: https://gitlab.com/codsen/codsen/tree/master/packages/ranges-sort
|
|
153
|
+
[cov-img]: https://img.shields.io/badge/coverage-100%25-brightgreen.svg?style=flat-square
|
|
154
|
+
[cov-url]: https://gitlab.com/codsen/codsen/tree/master/packages/ranges-sort
|
|
155
|
+
[no-deps-img]: https://img.shields.io/badge/-no%20dependencies-brightgreen?style=flat-square
|
|
156
|
+
[no-deps-url]: https://www.npmjs.com/package/ranges-sort?activeTab=dependencies
|
|
157
|
+
[downloads-img]: https://img.shields.io/npm/dm/ranges-sort.svg?style=flat-square
|
|
158
|
+
[downloads-url]: https://npmcharts.com/compare/ranges-sort
|
|
159
|
+
[runkit-img]: https://img.shields.io/badge/runkit-test_in_browser-a853ff.svg?style=flat-square
|
|
160
|
+
[runkit-url]: https://npm.runkit.com/ranges-sort
|
|
161
|
+
[prettier-img]: https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat-square
|
|
162
|
+
[prettier-url]: https://prettier.io
|
|
163
|
+
[license-img]: https://img.shields.io/badge/licence-MIT-51c838.svg?style=flat-square
|
|
164
|
+
[license-url]: https://gitlab.com/codsen/codsen/blob/master/LICENSE
|
package/dist/ranges-sort.cjs.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* ranges-sort
|
|
3
3
|
* Sort natural number index ranges [ [5, 6], [1, 3] ] => [ [1, 3], [5, 6] ]
|
|
4
|
-
* Version: 3.11.
|
|
4
|
+
* Version: 3.11.4
|
|
5
5
|
* Author: Roy Revelt, Codsen Ltd
|
|
6
6
|
* License: MIT
|
|
7
7
|
* Homepage: https://gitlab.com/codsen/codsen/tree/master/packages/ranges-sort
|
|
@@ -25,6 +25,55 @@ function _typeof(obj) {
|
|
|
25
25
|
return _typeof(obj);
|
|
26
26
|
}
|
|
27
27
|
|
|
28
|
+
function _defineProperty(obj, key, value) {
|
|
29
|
+
if (key in obj) {
|
|
30
|
+
Object.defineProperty(obj, key, {
|
|
31
|
+
value: value,
|
|
32
|
+
enumerable: true,
|
|
33
|
+
configurable: true,
|
|
34
|
+
writable: true
|
|
35
|
+
});
|
|
36
|
+
} else {
|
|
37
|
+
obj[key] = value;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
return obj;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function ownKeys(object, enumerableOnly) {
|
|
44
|
+
var keys = Object.keys(object);
|
|
45
|
+
|
|
46
|
+
if (Object.getOwnPropertySymbols) {
|
|
47
|
+
var symbols = Object.getOwnPropertySymbols(object);
|
|
48
|
+
if (enumerableOnly) symbols = symbols.filter(function (sym) {
|
|
49
|
+
return Object.getOwnPropertyDescriptor(object, sym).enumerable;
|
|
50
|
+
});
|
|
51
|
+
keys.push.apply(keys, symbols);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
return keys;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function _objectSpread2(target) {
|
|
58
|
+
for (var i = 1; i < arguments.length; i++) {
|
|
59
|
+
var source = arguments[i] != null ? arguments[i] : {};
|
|
60
|
+
|
|
61
|
+
if (i % 2) {
|
|
62
|
+
ownKeys(Object(source), true).forEach(function (key) {
|
|
63
|
+
_defineProperty(target, key, source[key]);
|
|
64
|
+
});
|
|
65
|
+
} else if (Object.getOwnPropertyDescriptors) {
|
|
66
|
+
Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));
|
|
67
|
+
} else {
|
|
68
|
+
ownKeys(Object(source)).forEach(function (key) {
|
|
69
|
+
Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
return target;
|
|
75
|
+
}
|
|
76
|
+
|
|
28
77
|
function rangesSort(arrOfRanges, originalOptions) {
|
|
29
78
|
if (!Array.isArray(arrOfRanges)) {
|
|
30
79
|
throw new TypeError("ranges-sort: [THROW_ID_01] Input must be an array, consisting of range arrays! Currently its type is: ".concat(_typeof(arrOfRanges), ", equal to: ").concat(JSON.stringify(arrOfRanges, null, 4)));
|
|
@@ -36,7 +85,7 @@ function rangesSort(arrOfRanges, originalOptions) {
|
|
|
36
85
|
strictlyTwoElementsInRangeArrays: false,
|
|
37
86
|
progressFn: null
|
|
38
87
|
};
|
|
39
|
-
var opts =
|
|
88
|
+
var opts = _objectSpread2(_objectSpread2({}, defaults), originalOptions);
|
|
40
89
|
var culpritsIndex;
|
|
41
90
|
var culpritsLen;
|
|
42
91
|
if (opts.strictlyTwoElementsInRangeArrays && !arrOfRanges.every(function (rangeArr, indx) {
|
|
@@ -62,7 +111,7 @@ function rangesSort(arrOfRanges, originalOptions) {
|
|
|
62
111
|
var counter = 0;
|
|
63
112
|
return Array.from(arrOfRanges).sort(function (range1, range2) {
|
|
64
113
|
if (opts.progressFn) {
|
|
65
|
-
counter
|
|
114
|
+
counter += 1;
|
|
66
115
|
opts.progressFn(Math.floor(counter * 100 / maxPossibleIterations));
|
|
67
116
|
}
|
|
68
117
|
if (range1[0] === range2[0]) {
|
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ranges-sort
|
|
3
|
+
* Sort natural number index ranges [ [5, 6], [1, 3] ] => [ [1, 3], [5, 6] ]
|
|
4
|
+
* Version: 3.11.4
|
|
5
|
+
* Author: Roy Revelt, Codsen Ltd
|
|
6
|
+
* License: MIT
|
|
7
|
+
* Homepage: https://gitlab.com/codsen/codsen/tree/master/packages/ranges-sort
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
(function (global, factory) {
|
|
11
|
+
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
|
|
12
|
+
typeof define === 'function' && define.amd ? define(factory) :
|
|
13
|
+
(global = global || self, global.rangesSort = factory());
|
|
14
|
+
}(this, (function () { 'use strict';
|
|
15
|
+
|
|
16
|
+
function _typeof(obj) {
|
|
17
|
+
"@babel/helpers - typeof";
|
|
18
|
+
|
|
19
|
+
if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") {
|
|
20
|
+
_typeof = function (obj) {
|
|
21
|
+
return typeof obj;
|
|
22
|
+
};
|
|
23
|
+
} else {
|
|
24
|
+
_typeof = function (obj) {
|
|
25
|
+
return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj;
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
return _typeof(obj);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function _defineProperty(obj, key, value) {
|
|
33
|
+
if (key in obj) {
|
|
34
|
+
Object.defineProperty(obj, key, {
|
|
35
|
+
value: value,
|
|
36
|
+
enumerable: true,
|
|
37
|
+
configurable: true,
|
|
38
|
+
writable: true
|
|
39
|
+
});
|
|
40
|
+
} else {
|
|
41
|
+
obj[key] = value;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
return obj;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function ownKeys(object, enumerableOnly) {
|
|
48
|
+
var keys = Object.keys(object);
|
|
49
|
+
|
|
50
|
+
if (Object.getOwnPropertySymbols) {
|
|
51
|
+
var symbols = Object.getOwnPropertySymbols(object);
|
|
52
|
+
if (enumerableOnly) symbols = symbols.filter(function (sym) {
|
|
53
|
+
return Object.getOwnPropertyDescriptor(object, sym).enumerable;
|
|
54
|
+
});
|
|
55
|
+
keys.push.apply(keys, symbols);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
return keys;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function _objectSpread2(target) {
|
|
62
|
+
for (var i = 1; i < arguments.length; i++) {
|
|
63
|
+
var source = arguments[i] != null ? arguments[i] : {};
|
|
64
|
+
|
|
65
|
+
if (i % 2) {
|
|
66
|
+
ownKeys(Object(source), true).forEach(function (key) {
|
|
67
|
+
_defineProperty(target, key, source[key]);
|
|
68
|
+
});
|
|
69
|
+
} else if (Object.getOwnPropertyDescriptors) {
|
|
70
|
+
Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));
|
|
71
|
+
} else {
|
|
72
|
+
ownKeys(Object(source)).forEach(function (key) {
|
|
73
|
+
Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
return target;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
//
|
|
82
|
+
// /\___/\
|
|
83
|
+
// ( o o )
|
|
84
|
+
// ====Y====
|
|
85
|
+
// ( )
|
|
86
|
+
// ( )
|
|
87
|
+
// ( )))))))))))
|
|
88
|
+
//
|
|
89
|
+
// does this: [ [2, 5], [1, 6] ] => [ [1, 6], [2, 5] ]
|
|
90
|
+
// sorts first by first element, then by second. Retains possible third element.
|
|
91
|
+
function rangesSort(arrOfRanges, originalOptions) {
|
|
92
|
+
// arrOfRanges validation
|
|
93
|
+
if (!Array.isArray(arrOfRanges)) {
|
|
94
|
+
throw new TypeError("ranges-sort: [THROW_ID_01] Input must be an array, consisting of range arrays! Currently its type is: ".concat(_typeof(arrOfRanges), ", equal to: ").concat(JSON.stringify(arrOfRanges, null, 4)));
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
if (arrOfRanges.length === 0) {
|
|
98
|
+
return arrOfRanges;
|
|
99
|
+
} // opts validation
|
|
100
|
+
// declare defaults, so we can enforce types later:
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
var defaults = {
|
|
104
|
+
strictlyTwoElementsInRangeArrays: false,
|
|
105
|
+
progressFn: null
|
|
106
|
+
}; // fill any settings with defaults if missing:
|
|
107
|
+
|
|
108
|
+
var opts = _objectSpread2(_objectSpread2({}, defaults), originalOptions); // arrOfRanges validation
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
var culpritsIndex;
|
|
112
|
+
var culpritsLen; // validate does every range consist of exactly two indexes:
|
|
113
|
+
|
|
114
|
+
if (opts.strictlyTwoElementsInRangeArrays && !arrOfRanges.every(function (rangeArr, indx) {
|
|
115
|
+
if (rangeArr.length !== 2) {
|
|
116
|
+
culpritsIndex = indx;
|
|
117
|
+
culpritsLen = rangeArr.length;
|
|
118
|
+
return false;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
return true;
|
|
122
|
+
})) {
|
|
123
|
+
throw new TypeError("ranges-sort: [THROW_ID_03] The first argument should be an array and must consist of arrays which are natural number indexes representing TWO string index ranges. However, ".concat(culpritsIndex, "th range (").concat(JSON.stringify(arrOfRanges[culpritsIndex], null, 4), ") has not two but ").concat(culpritsLen, " elements!"));
|
|
124
|
+
} // validate are range indexes natural numbers:
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
if (!arrOfRanges.every(function (rangeArr, indx) {
|
|
128
|
+
if (!Number.isInteger(rangeArr[0]) || rangeArr[0] < 0 || !Number.isInteger(rangeArr[1]) || rangeArr[1] < 0) {
|
|
129
|
+
culpritsIndex = indx;
|
|
130
|
+
return false;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
return true;
|
|
134
|
+
})) {
|
|
135
|
+
throw new TypeError("ranges-sort: [THROW_ID_04] The first argument should be an array and must consist of arrays which are natural number indexes representing string index ranges. However, ".concat(culpritsIndex, "th range (").concat(JSON.stringify(arrOfRanges[culpritsIndex], null, 4), ") does not consist of only natural numbers!"));
|
|
136
|
+
} // let's assume worst case scenario is N x N.
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+
var maxPossibleIterations = arrOfRanges.length * arrOfRanges.length;
|
|
140
|
+
var counter = 0;
|
|
141
|
+
return Array.from(arrOfRanges).sort(function (range1, range2) {
|
|
142
|
+
if (opts.progressFn) {
|
|
143
|
+
counter += 1;
|
|
144
|
+
opts.progressFn(Math.floor(counter * 100 / maxPossibleIterations));
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
if (range1[0] === range2[0]) {
|
|
148
|
+
if (range1[1] < range2[1]) {
|
|
149
|
+
return -1;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
if (range1[1] > range2[1]) {
|
|
153
|
+
return 1;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
return 0;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
if (range1[0] < range2[0]) {
|
|
160
|
+
return -1;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
return 1;
|
|
164
|
+
});
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
return rangesSort;
|
|
168
|
+
|
|
169
|
+
})));
|
package/dist/ranges-sort.esm.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* ranges-sort
|
|
3
3
|
* Sort natural number index ranges [ [5, 6], [1, 3] ] => [ [1, 3], [5, 6] ]
|
|
4
|
-
* Version: 3.11.
|
|
4
|
+
* Version: 3.11.4
|
|
5
5
|
* Author: Roy Revelt, Codsen Ltd
|
|
6
6
|
* License: MIT
|
|
7
7
|
* Homepage: https://gitlab.com/codsen/codsen/tree/master/packages/ranges-sort
|
|
@@ -22,9 +22,9 @@ function rangesSort(arrOfRanges, originalOptions) {
|
|
|
22
22
|
}
|
|
23
23
|
const defaults = {
|
|
24
24
|
strictlyTwoElementsInRangeArrays: false,
|
|
25
|
-
progressFn: null
|
|
25
|
+
progressFn: null,
|
|
26
26
|
};
|
|
27
|
-
const opts =
|
|
27
|
+
const opts = { ...defaults, ...originalOptions };
|
|
28
28
|
let culpritsIndex;
|
|
29
29
|
let culpritsLen;
|
|
30
30
|
if (
|
|
@@ -72,7 +72,7 @@ function rangesSort(arrOfRanges, originalOptions) {
|
|
|
72
72
|
let counter = 0;
|
|
73
73
|
return Array.from(arrOfRanges).sort((range1, range2) => {
|
|
74
74
|
if (opts.progressFn) {
|
|
75
|
-
counter
|
|
75
|
+
counter += 1;
|
|
76
76
|
opts.progressFn(Math.floor((counter * 100) / maxPossibleIterations));
|
|
77
77
|
}
|
|
78
78
|
if (range1[0] === range2[0]) {
|
package/dist/ranges-sort.umd.js
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* ranges-sort
|
|
3
3
|
* Sort natural number index ranges [ [5, 6], [1, 3] ] => [ [1, 3], [5, 6] ]
|
|
4
|
-
* Version: 3.11.
|
|
4
|
+
* Version: 3.11.4
|
|
5
5
|
* Author: Roy Revelt, Codsen Ltd
|
|
6
6
|
* License: MIT
|
|
7
7
|
* Homepage: https://gitlab.com/codsen/codsen/tree/master/packages/ranges-sort
|
|
8
8
|
*/
|
|
9
9
|
|
|
10
|
-
!function(r,
|
|
10
|
+
!function(r,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):(r=r||self).rangesSort=e()}(this,(function(){"use strict";function r(e){return(r="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(r){return typeof r}:function(r){return r&&"function"==typeof Symbol&&r.constructor===Symbol&&r!==Symbol.prototype?"symbol":typeof r})(e)}function e(r,e,t){return e in r?Object.defineProperty(r,e,{value:t,enumerable:!0,configurable:!0,writable:!0}):r[e]=t,r}function t(r,e){var t=Object.keys(r);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(r);e&&(n=n.filter((function(e){return Object.getOwnPropertyDescriptor(r,e).enumerable}))),t.push.apply(t,n)}return t}function n(r){for(var n=1;n<arguments.length;n++){var o=null!=arguments[n]?arguments[n]:{};n%2?t(Object(o),!0).forEach((function(t){e(r,t,o[t])})):Object.getOwnPropertyDescriptors?Object.defineProperties(r,Object.getOwnPropertyDescriptors(o)):t(Object(o)).forEach((function(e){Object.defineProperty(r,e,Object.getOwnPropertyDescriptor(o,e))}))}return r}return function(e,t){if(!Array.isArray(e))throw new TypeError("ranges-sort: [THROW_ID_01] Input must be an array, consisting of range arrays! Currently its type is: ".concat(r(e),", equal to: ").concat(JSON.stringify(e,null,4)));if(0===e.length)return e;var o,s,i=n(n({},{strictlyTwoElementsInRangeArrays:!1,progressFn:null}),t);if(i.strictlyTwoElementsInRangeArrays&&!e.every((function(r,e){return 2===r.length||(o=e,s=r.length,!1)})))throw new TypeError("ranges-sort: [THROW_ID_03] The first argument should be an array and must consist of arrays which are natural number indexes representing TWO string index ranges. However, ".concat(o,"th range (").concat(JSON.stringify(e[o],null,4),") has not two but ").concat(s," elements!"));if(!e.every((function(r,e){return!(!Number.isInteger(r[0])||r[0]<0||!Number.isInteger(r[1])||r[1]<0)||(o=e,!1)})))throw new TypeError("ranges-sort: [THROW_ID_04] The first argument should be an array and must consist of arrays which are natural number indexes representing string index ranges. However, ".concat(o,"th range (").concat(JSON.stringify(e[o],null,4),") does not consist of only natural numbers!"));var a=e.length*e.length,u=0;return Array.from(e).sort((function(r,e){return i.progressFn&&(u+=1,i.progressFn(Math.floor(100*u/a))),r[0]===e[0]?r[1]<e[1]?-1:r[1]>e[1]?1:0:r[0]<e[0]?-1:1}))}}));
|
package/package.json
CHANGED
|
@@ -1,11 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ranges-sort",
|
|
3
|
-
"version": "3.11.
|
|
3
|
+
"version": "3.11.4",
|
|
4
4
|
"description": "Sort natural number index ranges [ [5, 6], [1, 3] ] => [ [1, 3], [5, 6] ]",
|
|
5
5
|
"license": "MIT",
|
|
6
|
-
"engines": {
|
|
7
|
-
"node": ">=8.9"
|
|
8
|
-
},
|
|
9
6
|
"repository": "https://gitlab.com/codsen/codsen/",
|
|
10
7
|
"homepage": "https://gitlab.com/codsen/codsen/tree/master/packages/ranges-sort",
|
|
11
8
|
"author": {
|
|
@@ -25,18 +22,20 @@
|
|
|
25
22
|
"browser": "dist/ranges-sort.umd.js",
|
|
26
23
|
"scripts": {
|
|
27
24
|
"build": "rollup -c",
|
|
25
|
+
"ci_test": "npm run build && npm run lint && tap --no-only --reporter=silent --output-file=testStats.md && npm run format",
|
|
28
26
|
"dev": "rollup -c --dev",
|
|
29
|
-
"devunittest": "npm run dev && ./node_modules/.bin/tap --only",
|
|
27
|
+
"devunittest": "npm run dev && ./node_modules/.bin/tap --only -R 'base'",
|
|
30
28
|
"format": "npm run lect && npm run prettier && npm run lint",
|
|
31
29
|
"lect": "lect",
|
|
32
|
-
"lint": "../../node_modules/eslint/bin/eslint.js \"**/*.js\" --fix --config \"../../.eslintrc.json\"",
|
|
30
|
+
"lint": "../../node_modules/eslint/bin/eslint.js \"**/*.js\" --fix --config \"../../.eslintrc.json\" --quiet",
|
|
33
31
|
"perf": "node perf/check",
|
|
34
32
|
"prepare": "npm run build",
|
|
35
|
-
"prettier": "../../node_modules/prettier/bin-prettier.js '*.{js,css,scss,vue,md}' --write",
|
|
33
|
+
"prettier": "../../node_modules/prettier/bin-prettier.js '*.{js,css,scss,vue,md}' --write --loglevel silent",
|
|
34
|
+
"republish": "npm publish || :",
|
|
36
35
|
"tap": "tap",
|
|
37
36
|
"pretest": "npm run build",
|
|
38
37
|
"test": "npm run lint && npm run unittest && npm run format",
|
|
39
|
-
"unittest": "./node_modules/.bin/tap --no-only -
|
|
38
|
+
"unittest": "./node_modules/.bin/tap --no-only --output-file=testStats.md && npm run perf",
|
|
40
39
|
"version": "npm run build && git add ."
|
|
41
40
|
},
|
|
42
41
|
"husky": {
|
|
@@ -108,23 +107,29 @@
|
|
|
108
107
|
}
|
|
109
108
|
},
|
|
110
109
|
"tap": {
|
|
111
|
-
"coverage-report":
|
|
110
|
+
"coverage-report": [
|
|
111
|
+
"json-summary",
|
|
112
|
+
"text"
|
|
113
|
+
],
|
|
114
|
+
"nyc-arg": [
|
|
115
|
+
"--exclude=**/*.umd.js"
|
|
116
|
+
],
|
|
112
117
|
"timeout": 0
|
|
113
118
|
},
|
|
114
119
|
"devDependencies": {
|
|
115
|
-
"@babel/core": "^7.
|
|
116
|
-
"@babel/preset-env": "^7.
|
|
117
|
-
"@rollup/plugin-
|
|
118
|
-
"@rollup/plugin-
|
|
119
|
-
"@rollup/plugin-
|
|
120
|
+
"@babel/core": "^7.9.6",
|
|
121
|
+
"@babel/preset-env": "^7.9.6",
|
|
122
|
+
"@rollup/plugin-babel": "^5.0.0",
|
|
123
|
+
"@rollup/plugin-commonjs": "^11.1.0",
|
|
124
|
+
"@rollup/plugin-node-resolve": "^7.1.3",
|
|
125
|
+
"@rollup/plugin-strip": "^1.3.2",
|
|
120
126
|
"benchmark": "^2.1.4",
|
|
121
|
-
"lect": "^0.
|
|
122
|
-
"rollup": "^
|
|
127
|
+
"lect": "^0.13.0",
|
|
128
|
+
"rollup": "^2.8.0",
|
|
123
129
|
"rollup-plugin-ascii": "^0.0.3",
|
|
124
|
-
"rollup-plugin-babel": "^4.3.3",
|
|
125
130
|
"rollup-plugin-banner": "^0.2.1",
|
|
126
131
|
"rollup-plugin-cleanup": "^3.1.1",
|
|
127
|
-
"rollup-plugin-terser": "^5.
|
|
128
|
-
"tap": "^14.10.
|
|
132
|
+
"rollup-plugin-terser": "^5.3.0",
|
|
133
|
+
"tap": "^14.10.7"
|
|
129
134
|
}
|
|
130
135
|
}
|