import {isArray} from 'rxjs/util/isArray'; export class FilterOption { static populate(data, item?): FilterOption { if (!item) { item = new FilterOption(); } for (const key of ['code', 'name']) { if (key in data) { item[key] = data[key]; } } return item; } static populateNew(data): FilterOption { return FilterOption.populate(data); } constructor( public code = '', public name = '') { } } export class Filter { static populate(data, item?): Filter { if (!item) { item = new Filter(); } for (const key of ['filterCode']) { if (key in data) { item[key] = data[key]; } } if ('options' in data) { let i = 0; for (; i < data['options'].length; ++i) { // update array in place let option = data['options'][i]; if (!(option instanceof FilterOption)) { option = new FilterOption(option['code'], option['name']); } item.options[i] = option; } item.options.length = i; } return item; } static populateNew(data): Filter { return Filter.populate(data); } constructor( public filterCode = '', public options: FilterOption[] = [] ) { if (isArray(options)) { Filter.populate({options}, this); } } }