vault/ui/app/helpers/add-to-array.js
Vault Automation 0c6c13dd38
license: update headers to IBM Corp. (#10229) (#10233)
* license: update headers to IBM Corp.
* `make proto`
* update offset because source file changed

Signed-off-by: Ryan Cragun <me@ryan.ec>
Co-authored-by: Ryan Cragun <me@ryan.ec>
2025-10-21 15:20:20 -06:00

34 lines
886 B
JavaScript

/**
* Copyright IBM Corp. 2016, 2025
* SPDX-License-Identifier: BUSL-1.1
*/
import { helper as buildHelper } from '@ember/component/helper';
import { assert } from '@ember/debug';
function dedupe(items) {
return items.filter((v, i) => items.indexOf(v) === i);
}
export function addManyToArray(array, otherArray) {
assert(`Both values must be an array`, Array.isArray(array) && Array.isArray(otherArray));
const newArray = [...array].concat(otherArray);
return dedupe(newArray);
}
export function addToArray(array, string) {
if (!Array.isArray(array)) {
assert(`Value provided is not an array`, false);
}
const newArray = [...array];
newArray.push(string);
return dedupe(newArray);
}
export default buildHelper(function ([array, string]) {
if (Array.isArray(string)) {
return addManyToArray(array, string);
}
return addToArray(array, string);
});