summaryrefslogtreecommitdiffstats
path: root/src/pybind/mgr/dashboard/frontend/src/app/shared/pipes/array.pipe.ts
blob: f82e35316ab924b6a796a720ca6469eb4b784954 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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;
  }
}