@trenskow/combinations 0.1.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/.vscode/launch.json +15 -0
- package/.vscode/settings.json +6 -0
- package/LICENSE +9 -0
- package/README.md +39 -0
- package/eslint.config.js +54 -0
- package/index.d.ts +13 -0
- package/index.js +9 -0
- package/lib/index.js +40 -0
- package/package.json +35 -0
- package/test/index.js +36 -0
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
{
|
|
2
|
+
// Use IntelliSense to learn about possible attributes.
|
|
3
|
+
// Hover to view descriptions of existing attributes.
|
|
4
|
+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
|
|
5
|
+
"version": "0.2.0",
|
|
6
|
+
"configurations": [
|
|
7
|
+
{
|
|
8
|
+
"type":"node",
|
|
9
|
+
"request":"launch",
|
|
10
|
+
"name":"Run tests",
|
|
11
|
+
"runtimeExecutable":"_mocha",
|
|
12
|
+
"cwd":"${workspaceFolder}/test",
|
|
13
|
+
"args":["index.js"]}
|
|
14
|
+
]
|
|
15
|
+
}
|
package/LICENSE
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
Copyright 2026 Kristian Trenskow
|
|
2
|
+
|
|
3
|
+
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
|
|
4
|
+
|
|
5
|
+
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
|
|
6
|
+
|
|
7
|
+
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
|
|
8
|
+
|
|
9
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
package/README.md
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
@trenskow/combinations
|
|
2
|
+
----
|
|
3
|
+
|
|
4
|
+
Small package that iterates an array and returns all ordering combinations.
|
|
5
|
+
|
|
6
|
+
# Usage
|
|
7
|
+
|
|
8
|
+
````javascript
|
|
9
|
+
|
|
10
|
+
import combinations from '@trenskow/combinations';
|
|
11
|
+
|
|
12
|
+
combinations([1, 2, 3])
|
|
13
|
+
|
|
14
|
+
/*
|
|
15
|
+
-> [
|
|
16
|
+
[],
|
|
17
|
+
[ 1 ],
|
|
18
|
+
[ 1, 2 ],
|
|
19
|
+
[ 1, 2, 3 ],
|
|
20
|
+
[ 1, 3 ],
|
|
21
|
+
[ 1, 3, 2 ],
|
|
22
|
+
[ 2 ],
|
|
23
|
+
[ 2, 1 ],
|
|
24
|
+
[ 2, 1, 3 ],
|
|
25
|
+
[ 2, 3 ],
|
|
26
|
+
[ 2, 3, 1 ],
|
|
27
|
+
[ 3 ],
|
|
28
|
+
[ 3, 1 ],
|
|
29
|
+
[ 3, 1, 2 ],
|
|
30
|
+
[ 3, 2 ],
|
|
31
|
+
[ 3, 2, 1 ]
|
|
32
|
+
]
|
|
33
|
+
*/
|
|
34
|
+
|
|
35
|
+
````
|
|
36
|
+
|
|
37
|
+
# License
|
|
38
|
+
|
|
39
|
+
See license in LICENSE
|
package/eslint.config.js
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import globals from 'globals';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { fileURLToPath } from 'node:url';
|
|
4
|
+
import js from '@eslint/js';
|
|
5
|
+
import { FlatCompat } from '@eslint/eslintrc';
|
|
6
|
+
|
|
7
|
+
const __filename = fileURLToPath(import.meta.url);
|
|
8
|
+
const __dirname = path.dirname(__filename);
|
|
9
|
+
const compat = new FlatCompat({
|
|
10
|
+
baseDirectory: __dirname,
|
|
11
|
+
recommendedConfig: js.configs.recommended,
|
|
12
|
+
allConfig: js.configs.all
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
export default [...compat.extends('eslint:recommended'), {
|
|
16
|
+
languageOptions: {
|
|
17
|
+
globals: {
|
|
18
|
+
...globals.node,
|
|
19
|
+
...globals.mocha,
|
|
20
|
+
},
|
|
21
|
+
|
|
22
|
+
ecmaVersion: 'latest',
|
|
23
|
+
sourceType: 'module',
|
|
24
|
+
},
|
|
25
|
+
|
|
26
|
+
rules: {
|
|
27
|
+
indent: ['error', 'tab', {
|
|
28
|
+
SwitchCase: 1,
|
|
29
|
+
}],
|
|
30
|
+
|
|
31
|
+
'linebreak-style': ['error', 'unix'],
|
|
32
|
+
quotes: ['error', 'single'],
|
|
33
|
+
semi: ['error', 'always'],
|
|
34
|
+
|
|
35
|
+
'no-console': ['error', {
|
|
36
|
+
allow: ['warn', 'error', 'info'],
|
|
37
|
+
}],
|
|
38
|
+
|
|
39
|
+
'no-unused-vars': ['error', {
|
|
40
|
+
argsIgnorePattern: '^_',
|
|
41
|
+
}],
|
|
42
|
+
|
|
43
|
+
'no-empty': ['error', {
|
|
44
|
+
allowEmptyCatch: true,
|
|
45
|
+
}],
|
|
46
|
+
|
|
47
|
+
'no-trailing-spaces': ['error', {
|
|
48
|
+
ignoreComments: true,
|
|
49
|
+
}],
|
|
50
|
+
|
|
51
|
+
'require-atomic-updates': 'off',
|
|
52
|
+
'eol-last': ['error', 'always'],
|
|
53
|
+
},
|
|
54
|
+
}];
|
package/index.d.ts
ADDED
package/index.js
ADDED
package/lib/index.js
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
//
|
|
2
|
+
// index.js
|
|
3
|
+
// @trenskow/combinations
|
|
4
|
+
//
|
|
5
|
+
// Created by Kristian Trenskow on 2026/03/18
|
|
6
|
+
// For license see LICENSE.
|
|
7
|
+
//
|
|
8
|
+
|
|
9
|
+
export default (array) => {
|
|
10
|
+
|
|
11
|
+
const result = [[]];
|
|
12
|
+
|
|
13
|
+
function backtrack(
|
|
14
|
+
current,
|
|
15
|
+
remaining
|
|
16
|
+
) {
|
|
17
|
+
|
|
18
|
+
remaining
|
|
19
|
+
.forEach((item, index) => {
|
|
20
|
+
|
|
21
|
+
const next = [...current, item];
|
|
22
|
+
|
|
23
|
+
result.push(next);
|
|
24
|
+
|
|
25
|
+
backtrack(
|
|
26
|
+
next,
|
|
27
|
+
[
|
|
28
|
+
...remaining.slice(0, index),
|
|
29
|
+
...remaining.slice(index + 1)
|
|
30
|
+
]);
|
|
31
|
+
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
backtrack([], array);
|
|
37
|
+
|
|
38
|
+
return result;
|
|
39
|
+
|
|
40
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@trenskow/combinations",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Small package that iterates an array and returns all ordering combinations.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"array",
|
|
7
|
+
"combine",
|
|
8
|
+
"sort"
|
|
9
|
+
],
|
|
10
|
+
"homepage": "https://github.com/trenskow/combinations#readme",
|
|
11
|
+
"bugs": {
|
|
12
|
+
"url": "https://github.com/trenskow/combinations/issues"
|
|
13
|
+
},
|
|
14
|
+
"repository": {
|
|
15
|
+
"type": "git",
|
|
16
|
+
"url": "git+https://github.com/trenskow/combinations.git"
|
|
17
|
+
},
|
|
18
|
+
"license": "BSD-2-Clause",
|
|
19
|
+
"author": "Kristian Trenskow <this.is@kristians.email>",
|
|
20
|
+
"type": "module",
|
|
21
|
+
"main": "index.js",
|
|
22
|
+
"types": "index.d.ts",
|
|
23
|
+
"scripts": {
|
|
24
|
+
"test": "node ./node_modules/mocha/bin/mocha.js ./test/index.js"
|
|
25
|
+
},
|
|
26
|
+
"dependencies": {
|
|
27
|
+
"@eslint/compat": "^2.0.3",
|
|
28
|
+
"@eslint/eslintrc": "^3.3.5",
|
|
29
|
+
"@eslint/js": "^10.0.1",
|
|
30
|
+
"chai": "^6.2.2",
|
|
31
|
+
"eslint": "^10.0.3",
|
|
32
|
+
"globals": "^17.4.0",
|
|
33
|
+
"mocha": "^11.7.5"
|
|
34
|
+
}
|
|
35
|
+
}
|
package/test/index.js
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
//
|
|
2
|
+
// index.js
|
|
3
|
+
// @trenskow/combinations
|
|
4
|
+
//
|
|
5
|
+
// Created by Kristian Trenskow on 2026/03/18
|
|
6
|
+
// For license see LICENSE.
|
|
7
|
+
//
|
|
8
|
+
|
|
9
|
+
import { expect } from 'chai';
|
|
10
|
+
|
|
11
|
+
import combinations from '../index.js';
|
|
12
|
+
|
|
13
|
+
describe('@trenskow/combinations', () => {
|
|
14
|
+
|
|
15
|
+
it('should come back with all combinations.', () => {
|
|
16
|
+
expect(combinations([1, 2, 3])).to.deep.equal([
|
|
17
|
+
[],
|
|
18
|
+
[ 1 ],
|
|
19
|
+
[ 1, 2 ],
|
|
20
|
+
[ 1, 2, 3 ],
|
|
21
|
+
[ 1, 3 ],
|
|
22
|
+
[ 1, 3, 2 ],
|
|
23
|
+
[ 2 ],
|
|
24
|
+
[ 2, 1 ],
|
|
25
|
+
[ 2, 1, 3 ],
|
|
26
|
+
[ 2, 3 ],
|
|
27
|
+
[ 2, 3, 1 ],
|
|
28
|
+
[ 3 ],
|
|
29
|
+
[ 3, 1 ],
|
|
30
|
+
[ 3, 1, 2 ],
|
|
31
|
+
[ 3, 2 ],
|
|
32
|
+
[ 3, 2, 1 ]
|
|
33
|
+
]);
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
});
|