@vbarbarosh/node-helpers 3.65.0 → 3.66.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/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "author": "Vladimir Barbarosh <vladimir.barbarosh@gmail.com>",
3
3
  "license": "MIT",
4
4
  "name": "@vbarbarosh/node-helpers",
5
- "version": "3.65.0",
5
+ "version": "3.66.0",
6
6
  "description": "A set of helpers for JavaScript/Node.js",
7
7
  "unpkg": "dist/browser.js",
8
8
  "jsdelivr": "dist/browser.js",
@@ -1,17 +1,21 @@
1
1
  // https://stackoverflow.com/a/37580979/23502239
2
- function array_permutations(array)
2
+ function array_permutations(array, k = array.length)
3
3
  {
4
- var length = array.length,
5
- out = [array.slice()],
6
- c = new Array(length).fill(0),
7
- i = 1, k, p;
4
+ if (k !== array.length) {
5
+ return array_permutations_k(array, k);
6
+ }
7
+
8
+ let length = array.length;
9
+ let out = [array.slice()];
10
+ let c = new Array(length).fill(0);
11
+ let i = 1, kk, p;
8
12
 
9
13
  while (i < length) {
10
14
  if (c[i] < i) {
11
- k = i % 2 && c[i];
15
+ kk = i % 2 && c[i];
12
16
  p = array[i];
13
- array[i] = array[k];
14
- array[k] = p;
17
+ array[i] = array[kk];
18
+ array[kk] = p;
15
19
  ++c[i];
16
20
  i = 1;
17
21
  out.push(array.slice());
@@ -24,4 +28,25 @@ function array_permutations(array)
24
28
  return out;
25
29
  }
26
30
 
31
+ function array_permutations_k(array, k)
32
+ {
33
+ if (k === 0) {
34
+ return [
35
+ []
36
+ ];
37
+ }
38
+ if (array.length < k) {
39
+ return [];
40
+ }
41
+
42
+ const out = [];
43
+ for (let i = 0; i < array.length; i++) {
44
+ const rest = array.slice(0, i).concat(array.slice(i + 1));
45
+ for (const perm of array_permutations(rest, k - 1)) {
46
+ out.push([array[i], ...perm]);
47
+ }
48
+ }
49
+ return out;
50
+ }
51
+
27
52
  module.exports = array_permutations;
@@ -7,4 +7,14 @@ describe('array_permutations', function () {
7
7
  assert.deepStrictEqual(array_permutations([1,2]), [[1,2],[2,1]]);
8
8
  assert.deepStrictEqual(array_permutations([1,2,3]), [[1,2,3],[2,1,3],[3,1,2],[1,3,2],[2,3,1],[3,2,1]]);
9
9
  });
10
+ it('should handle k', function () {
11
+ assert.deepStrictEqual(array_permutations([1,2], 1), [[1],[2]]);
12
+ assert.deepStrictEqual(array_permutations([1,2,3], 2), [[1,2],[1,3],[2,1],[2,3],[3,1],[3,2]]);
13
+ assert.deepStrictEqual(array_permutations([1,2,3,4], 3), [
14
+ [1,2,3],[1,2,4],[1,3,2],[1,3,4],[1,4,2],[1,4,3],
15
+ [2,1,3],[2,1,4],[2,3,1],[2,3,4],[2,4,1],[2,4,3],
16
+ [3,1,2],[3,1,4],[3,2,1],[3,2,4],[3,4,1],[3,4,2],
17
+ [4,1,2],[4,1,3],[4,2,1],[4,2,3],[4,3,1],[4,3,2]
18
+ ]);
19
+ });
10
20
  });
@@ -0,0 +1,7 @@
1
+ function date_add_hours(date, hours)
2
+ {
3
+ date.setHours(date.getHours() + hours);
4
+ return date;
5
+ }
6
+
7
+ module.exports = date_add_hours;
@@ -0,0 +1,7 @@
1
+ function date_add_milliseconds(date, milliseconds)
2
+ {
3
+ date.setMilliseconds(date.getMilliseconds() + milliseconds);
4
+ return date;
5
+ }
6
+
7
+ module.exports = date_add_milliseconds;
@@ -0,0 +1,7 @@
1
+ function date_add_minutes(date, minutes)
2
+ {
3
+ date.setMinutes(date.getMinutes() + minutes);
4
+ return date;
5
+ }
6
+
7
+ module.exports = date_add_minutes;
@@ -0,0 +1,7 @@
1
+ function date_add_seconds(date, seconds)
2
+ {
3
+ date.setSeconds(date.getSeconds() + seconds);
4
+ return date;
5
+ }
6
+
7
+ module.exports = date_add_seconds;
@@ -0,0 +1,6 @@
1
+ function date_diff_seconds(a, b)
2
+ {
3
+ return Math.floor((a.getTime() - b.getTime())/1000);
4
+ }
5
+
6
+ module.exports = date_diff_seconds;