@ce1pers/array-helpers
Version:
Included simple array helpers.
124 lines (120 loc) • 5.33 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getPermutationsWithSelf = exports.getPermutations = exports.getCombinations = exports.objectArraySorter = void 0;
/**
* Sorts an object array based on the provided 'sortBy' and 'sortByType'.
*
* @throws {Error} Throws an type error if the 'array' argument type is not array.
* @throws {Error} Throws an type error if the 'sortByType' is not supported.
*/
const objectArraySorter = (input) => {
try {
const { array, sortBy, sortByType, reversed = false } = input;
if (!Array.isArray(array))
throw new TypeError(`'array' argument must be array type.`);
// Cloning array.
const cloned = JSON.parse(JSON.stringify(array));
// Not passed key parameter?
if (!sortBy) {
cloned.sort();
}
else {
// Has key?
switch (sortByType) {
case "string":
cloned.sort((a, b) => {
const aKey = a[sortBy].toUpperCase();
const bKey = b[sortBy].toUpperCase();
return aKey.localeCompare(bKey);
});
break;
case "number":
cloned.sort((a, b) => a[sortBy] - b[sortBy]);
break;
default:
throw new TypeError(`Unsupported 'sortByType'.`);
}
}
// Use reversed?
if (reversed)
cloned.reverse();
return cloned;
}
catch (err) {
console.warn(err.message);
return input.array;
}
};
exports.objectArraySorter = objectArraySorter;
/**
* 중복되지 않는 조합에 대한 모든 경우의 수를 가져옵니다.
* 조합은 서로 다른 n개의 원소를 가지고 순서에 상관없이 r개의 원소를 선택하는 것이다.
* Input: [1, 2, 3]
* Output: [ [1, 2], [1, 3], [2, 3] ]
*/
const getCombinations = (arr, num) => {
const results = [];
// nC1 이며, 1이면 의미 없기때문에 바로 반환한다.
if (num === 1)
return arr.map((v) => [v]);
arr.forEach((fixed, index, origin) => {
// 조합에서는 값 순서에 상관없이 중복이 되면 안되기 때문에 현재값 이후의 배열들만 추출한다.
const rest = origin.slice(index + 1);
// 나머지 배열을 기준으로 다시 조합을 실시한다.
// 기준값(fixed)이 있기 때문에 선택하려는 개수에서 - 1 을 해준다.
const combinations = (0, exports.getCombinations)(rest, num - 1);
// 기준값(fixed)에 돌아온 조합(combinations)을 붙인다.
const attached = combinations.map((v) => [fixed, ...v]);
// 붙인 값을 결과 값에 넣어준다.
results.push(...attached);
});
return results;
};
exports.getCombinations = getCombinations;
/**
* 중복을 허용하는 순열에 대한 모든 경우의 수를 가져옵니다.
* 순열은 서로 다른 n개의 원소를 가지고 중복 없이 순서에 상관있게 r개의 원소를 선택 혹은 나열 하는것이다.
* Input: [1, 2, 3]
* Output: [ [1, 2], [1, 3], [2, 1], [2, 3], [3, 1], [3, 2] ]
*/
const getPermutations = (arr, num) => {
const results = [];
// nP1 이며, 1이면 의미 없기때문에 바로 반환한다.
if (num === 1)
return arr.map((v) => [v]);
arr.forEach((fixed, index, origin) => {
// 순열에서는 조합과 달리 순서만 바뀌면 중복이 아니기때문에 기준값을 제외한 나머지 배열을 넣어준다.
const rest = [...origin.slice(0, index), ...origin.slice(index + 1)];
// 나머지 배열을 기준으로 다시 순열을 구한다.
// 기준값(fixed)이 있기 때문에 선택하려는 개수에서 - 1 을 해준다.
const permutations = (0, exports.getPermutations)(rest, num - 1);
// 기준값(fixed)에 순열(permutations)을 붙인다.
const attached = permutations.map((v) => [fixed, ...v]);
// 붙인 값을 결과 값에 넣어준다.
results.push(...attached);
});
return results;
};
exports.getPermutations = getPermutations;
/**
* 중복순열에 대한 모든 경우의 수를 가져옵니다.
* 중복순열은 서로 다른 n개의 원소를 가지고 중복을 허용하여 r개의 원소를 선택 혹은 나열 하는것이다.
* (중복을 허용한다는건 본인 숫자의 중복을 의미한다.)
* Input: [1, 2, 3]
* Output: [ [1, 1], [1, 2], [1, 3], [2, 1], [2, 2], [2, 3], [3, 1], [3, 2], [3, 3] ]
*/
const getPermutationsWithSelf = (arr, num) => {
const results = [];
if (num === 1)
return arr.map((v) => [v]);
arr.forEach((fixed, index, origin) => {
// 기준값(fixed)이 있기 때문에 선택하려는 개수에서 - 1 을 해준다.
const permutations = (0, exports.getPermutations)(origin, num - 1);
// 기준값(fixed)에 순열(permutations)을 붙인다.
const attached = permutations.map((v) => [fixed, ...v]);
// 붙인 값을 결과 값에 넣어준다.
results.push(...attached);
});
return results;
};
exports.getPermutationsWithSelf = getPermutationsWithSelf;