vault/ui/app/helpers/remove-from-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

35 lines
950 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 removeManyFromArray(array, toRemove) {
assert(`Both values must be an array`, Array.isArray(array) && Array.isArray(toRemove));
const a = [...(array || [])];
return a.filter((v) => !toRemove.includes(v));
}
export function removeFromArray(array, string) {
assert(`Value provided is not an array`, Array.isArray(array));
const newArray = [...array];
const idx = newArray.indexOf(string);
if (idx >= 0) {
newArray.splice(idx, 1);
}
return dedupe(newArray);
}
export default buildHelper(function ([array, string]) {
if (Array.isArray(string)) {
return removeManyFromArray(array, string);
}
return removeFromArray(array, string);
});