mituri

[JS] deep copy

Mar 29th, 2024
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.54 KB | None | 0 0
  1. import { catchError, of } from 'rxjs';
  2.  
  3. export function deepCopy(obj: any): any {
  4. if (Array.isArray(obj)) {
  5. return obj.map((item) => deepCopy(item));
  6. } else if (typeof obj === 'object' && obj !== null) {
  7. const newObj = {};
  8. for (const key in obj) {
  9. (newObj as any)[key] = deepCopy(obj[key]);
  10. }
  11. return newObj;
  12. } else {
  13. return obj;
  14. }
  15. }
  16.  
  17. export function handleHttpError(errorMessage: string) {
  18. return catchError((error) => {
  19. console.error(errorMessage, error);
  20. return of(null);
  21. });
  22. }
  23.  
Advertisement
Add Comment
Please, Sign In to add comment