it('should create', () => {
expect(component).toBeTruthy();
- expect(component._convertValue).toBeTruthy();
- expect(component._makePairs).toBeTruthy();
- expect(component._makePairsFromObject).toBeTruthy();
- expect(component._makePairsFromArray).toBeTruthy();
- expect(component._insertFlattenObjects).toBeTruthy();
});
it('should make key value object pairs out of arrays with length two', () => {
expect(component._convertValue(v(value)).value).toBe(result);
it('should leave a string as it is', () => {
- testConvertValue('something', 'something')
+ testConvertValue('something', 'something');
});
it('should leave an int as it is', () => {
- testConvertValue(29, 29)
+ testConvertValue(29, 29);
});
it('should convert arrays with any type to string', () => {
- testConvertValue([1, 2, 3], '1, 2, 3')
- testConvertValue([{ sth: 'something' }], '{"sth":"something"}')
- testConvertValue([1, 'two', { 3: 'three' }], '1, two, {"3":"three"}')
+ testConvertValue([1, 2, 3], '1, 2, 3');
+ testConvertValue([{ sth: 'something' }], '{"sth":"something"}');
+ testConvertValue([1, 'two', { 3: 'three' }], '1, two, {"3":"three"}');
});
it('should convert only allow objects if renderObjects is set to true', () => {
styleUrls: ['./table-key-value.component.scss']
})
export class TableKeyValueComponent implements OnInit, OnChanges {
-
columns: Array<CdTableColumn> = [];
@Input() data: any;
@Input() appendParentKey = true;
tableData: {
- key: string,
- value: any
+ key: string;
+ value: any;
}[];
/**
*/
@Output() fetchData = new EventEmitter();
- constructor() { }
+ constructor() {}
ngOnInit() {
this.columns = [
} else {
throw new Error('Wrong data format');
}
- temp = temp.map((v) => this._convertValue(v)).filter(o => o); // Filters out undefined
+ temp = temp.map((v) => this._convertValue(v)).filter((o) => o); // Filters out undefined
return this.renderObjects ? this._insertFlattenObjects(temp) : temp;
}
} else {
if (_.isArray(first)) {
if (first.length === 2) {
- temp = data.map(a => ({
+ temp = data.map((a) => ({
key: a[0],
value: a[1]
}));
}
_makePairsFromObject(data: object) {
- return Object.keys(data).map(k => ({
+ return Object.keys(data).map((k) => ({
key: k,
value: data[k]
}));
temp.forEach((v, i) => {
if (_.isPlainObject(v.value)) {
temp.splice(i, 1);
- this._makePairs(v.value).forEach(item => {
+ this._makePairs(v.value).forEach((item) => {
if (this.appendParentKey) {
item.key = v.key + ' ' + item.key;
}
_convertValue(v: any) {
if (_.isArray(v.value)) {
- v.value = v.value.map((v) => (_.isPlainObject(v) ? JSON.stringify(v) : v)).join(', ');
+ v.value = v.value
+ .map((item) => (_.isPlainObject(item) ? JSON.stringify(item) : item))
+ .join(', ');
} else if (_.isPlainObject(v.value) && !this.renderObjects) {
return;
}