thingsboard/ui-ngx/src/app/shared/pipe/key-value-not-empty.pipe.ts

45 lines
1.8 KiB
TypeScript
Raw Normal View History

2024-06-14 12:55:27 +03:00
///
/// Copyright © 2016-2024 The Thingsboard Authors
///
/// Licensed under the Apache License, Version 2.0 (the "License");
/// you may not use this file except in compliance with the License.
/// You may obtain a copy of the License at
///
/// http://www.apache.org/licenses/LICENSE-2.0
///
/// Unless required by applicable law or agreed to in writing, software
/// distributed under the License is distributed on an "AS IS" BASIS,
/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
/// See the License for the specific language governing permissions and
/// limitations under the License.
///
2024-06-14 17:44:29 +03:00
import { KeyValueChanges, KeyValueDiffer, KeyValueDiffers, Pipe, PipeTransform } from '@angular/core';
import { KeyValue, KeyValuePipe } from "@angular/common";
2024-06-14 12:48:47 +03:00
@Pipe({
2024-06-14 17:44:29 +03:00
name: 'keyValueIsNotEmpty',
2024-06-14 12:48:47 +03:00
})
2024-06-14 17:44:29 +03:00
export class KeyValueIsNotEmptyPipe extends KeyValuePipe implements PipeTransform {
private difference!: KeyValueDiffer<any, any>;
constructor(private readonly keyValueDiffers: KeyValueDiffers) {
super(keyValueDiffers);
}
transform<ValueType>(
input: Record<string, ValueType>,
compareFn?: (a: KeyValue<string, ValueType>, b: KeyValue<string, ValueType>) => number
): KeyValue<string, ValueType>[] & null {
super.transform(input, compareFn);
this.difference ??= this.keyValueDiffers.find(input).create();
const differChanges: KeyValueChanges<string, ValueType> | null = this.difference.diff(input as any);
if (differChanges) {
console.log(differChanges)
const filteredEntries = [...Object.entries(input)]
.filter(([_, value]) => value !== null && value !== undefined);
return super.transform(new Map<string, ValueType>(filteredEntries), compareFn) as KeyValue<string, ValueType>[] & null;
}
2024-06-14 12:48:47 +03:00
}
}