diff options
Diffstat (limited to 'src/pybind/mgr/dashboard/frontend/src/app/shared/pipes/array.pipe.ts')
-rwxr-xr-x | src/pybind/mgr/dashboard/frontend/src/app/shared/pipes/array.pipe.ts | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/src/pybind/mgr/dashboard/frontend/src/app/shared/pipes/array.pipe.ts b/src/pybind/mgr/dashboard/frontend/src/app/shared/pipes/array.pipe.ts new file mode 100755 index 000000000..f82e35316 --- /dev/null +++ b/src/pybind/mgr/dashboard/frontend/src/app/shared/pipes/array.pipe.ts @@ -0,0 +1,26 @@ +import { Pipe, PipeTransform } from '@angular/core'; + +import _ from 'lodash'; + +/** + * Convert the given value to an array. + */ +@Pipe({ + name: 'array' +}) +export class ArrayPipe implements PipeTransform { + /** + * Convert the given value into an array. If the value is already an + * array, then nothing happens, except the `force` flag is set. + * @param value The value to process. + * @param force Convert the specified value to an array, either it is + * already an array. + */ + transform(value: any, force = false): any[] { + let result = value; + if (!_.isArray(value) || (_.isArray(value) && force)) { + result = [value]; + } + return result; + } +} |