Giter Site home page Giter Site logo

letyletylety / deepcopy Goto Github PK

View Code? Open in Web Editor NEW
2.0 2.0 2.0 368 KB

recursive deep copy for nested collections

Home Page: https://pub.dev/packages/deepcopy

License: BSD 2-Clause "Simplified" License

Dart 89.71% C++ 1.09% C 2.02% CMake 4.96% Swift 0.49% Shell 1.73%
collection copy dart deep-copy deepcopy flutter recursive-copy

deepcopy's Introduction

For the better world

You can call me lety. πŸ€ͺ

πŸ“« How to reach me:

image image image

πŸ”₯ recently working with ...

image image image

image image

image image

🌟 main experienced Tech Stack ...

image

image image image image image

image image(These two lang was used during National ServiceπŸ™‚)

image image

image

✍️ editor (from 1st to 3rd)

image image

image

❓

image

image

image

Top Langs

deepcopy's People

Contributors

letyletylety avatar

Stargazers

 avatar  avatar

Watchers

 avatar

Forkers

sun-jiao

deepcopy's Issues

Support generics

I would like to see generic support added to the library. Here is an example implementation:

class ClonerService {
  static List<T> deepcopyList<T>(List<T> list) {
    List<T> copy = [];
    for (final item in list) {
      if (item is Map) {
        copy.add(deepcopyMap(item) as T);
      } else if (item is List) {
        copy.add(deepcopyList(item) as T);
      } else if (item is Set) {
        copy.add(deepcopySet(item) as T);
      } else {
        copy.add(item);
      }
    }
    return copy;
  }

  static Map<TKey, TValue> deepcopyMap<TKey, TValue>(Map<TKey, TValue> map) {
    Map<TKey, TValue> copy = {};
    for (final entry in map.entries) {
      final key = entry.key;
      final value = entry.value;

      if (value is Map) {
        copy[key] = deepcopyMap(value) as TValue;
      } else if (value is List) {
        copy[key] = deepcopyList(value) as TValue;
      } else if (value is Set) {
        copy[key] = deepcopySet(value) as TValue;
      } else {
        copy[key] = value;
      }
    }
    return copy;
  }

  static Set<T> deepcopySet<T>(Set<T> set) {
    Set<T> copy = {};
    for (final item in set) {
      if (item is Map) {
        copy.add(deepcopyMap(item) as T);
      } else if (item is List) {
        copy.add(deepcopyList(item) as T);
      } else if (item is Set) {
        copy.add(deepcopySet(item) as T);
      } else {
        copy.add(item);
      }
    }
    return copy;
  }
}


extension MapExtensions<TKey, TValue> on Map<TKey, TValue> {
  Map<TKey, TValue> deepcopy() {
    return ClonerService.deepcopyMap<TKey, TValue>(this);
  }
}

extension ListExtensions<TKey, T> on List<T> {
  List<T> deepcopy() {
    return ClonerService.deepcopyList<T>(this);
  }
}

extension SetExtensions<T> on Set<T> {
  Set<T> deepcopy() {
    return ClonerService.deepcopySet<T>(this);
  }
}

Tests:

void main() {
  group('cloner', () {
    testWidgets('Map<int, Map>', (tester) async {
      final Map<int, Map> original = {
        1: {
          'abc': {'abc': 'abc'}
        }
      };
      final copy = original.deepcopy();
      expect(original.values.first.entries.first != copy.values.first.entries.first,
          true);
    });

    testWidgets('List<Map>', (tester) async {
      final List<Map> original = [
        {
          'abc': {'abc': 'abc'}
        }
      ];
      final copy = original.deepcopy();
      expect(original.first.entries.first != copy.first.entries.first, true);
    });

    testWidgets('Set<Map>', (tester) async {
      final Set<Map> original = {
        {
          'abc': {'abc': 'abc'}
        }
      };
      final copy = original.deepcopy();
      expect(original.first.entries.first != copy.first.entries.first, true);
    });
  });
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.