component.data = [['someKey', 0, 3]];
expect(() => component.ngOnInit()).toThrowError('Wrong array format: [string, any][]');
component.data = [{ somekey: 939, somethingElse: 'test' }];
- expect(() => component.ngOnInit()).toThrowError(
- 'Wrong object array format: {key: string, value: any}[]'
- );
+ });
+
+ describe('Class objects equal plain objects', () => {
+ class Example {
+ sth = 'something';
+ deep?: Example;
+ constructor(deep: boolean) {
+ if (deep) {
+ this.deep = new Example(false);
+ }
+ }
+ }
+
+ const classExample = new Example(true);
+ const objectExample = {
+ sth: 'something',
+ deep: {
+ sth: 'something'
+ }
+ };
+
+ const getTableData = (data) => {
+ component.data = data;
+ expect(() => component.ngOnInit()).not.toThrow();
+ return component.tableData;
+ };
+
+ const doesClassEqualsObject = (classData, objectData, dataLength) => {
+ const classTableData = getTableData(classData);
+ expect(classTableData).toEqual(getTableData(objectData));
+ expect(classTableData.length).toBe(dataLength);
+ };
+
+ it('should convert class objects the same way as plain objects', () => {
+ doesClassEqualsObject(classExample, objectExample, 1);
+ doesClassEqualsObject([classExample], [objectExample], 1);
+ component.renderObjects = true;
+ doesClassEqualsObject(classExample, objectExample, 2);
+ doesClassEqualsObject([classExample], [objectExample], 2);
+ });
});
it('tests _makePairs', () => {
return; // Wait for data
} else if (_.isArray(data)) {
temp = this._makePairsFromArray(data);
- } else if (_.isPlainObject(data)) {
+ } else if (_.isObject(data)) {
temp = this._makePairsFromObject(data);
} else {
throw new Error('Wrong data format');
_makePairsFromArray(data: any[]) {
let temp = [];
const first = data[0];
- if (_.isPlainObject(first)) {
+ if (_.isArray(first)) {
+ if (first.length === 2) {
+ temp = data.map((a) => ({
+ key: a[0],
+ value: a[1]
+ }));
+ } else {
+ throw new Error('Wrong array format: [string, any][]');
+ }
+ } else if (_.isObject(first)) {
if (_.has(first, 'key') && _.has(first, 'value')) {
temp = [...data];
} else {
- throw new Error('Wrong object array format: {key: string, value: any}[]');
- }
- } else {
- if (_.isArray(first)) {
- if (first.length === 2) {
- temp = data.map((a) => ({
- key: a[0],
- value: a[1]
- }));
- } else {
- throw new Error('Wrong array format: [string, any][]');
- }
+ temp = data.reduce(
+ (previous: any[], item) => previous.concat(this._makePairsFromObject(item)),
+ temp
+ );
}
}
return temp;
_insertFlattenObjects(temp: any[]) {
temp.forEach((v, i) => {
- if (_.isPlainObject(v.value)) {
+ if (_.isObject(v.value)) {
temp.splice(i, 1);
this._makePairs(v.value).forEach((item) => {
if (this.appendParentKey) {
_convertValue(v: any) {
if (_.isArray(v.value)) {
- v.value = v.value
- .map((item) => (_.isPlainObject(item) ? JSON.stringify(item) : item))
- .join(', ');
- } else if (_.isPlainObject(v.value) && !this.renderObjects) {
+ v.value = v.value.map((item) => (_.isObject(item) ? JSON.stringify(item) : item)).join(', ');
+ } else if (_.isObject(v.value) && !this.renderObjects) {
return;
}
return v;