ms_list_utils 0.5.0 copy "ms_list_utils: ^0.5.0" to clipboard
ms_list_utils: ^0.5.0 copied to clipboard

Novas funções para lidar com listas. Com suporte an extensions

Overview #

Pub Pub GitHub stars Buy Me A Coffee donate button

Some help functions for using Lists

Whats MS_List_Utils do #

Add useful functions to map:

  • addAround Returns new list with items generated around list.
  • addBetween Returns new list with items generated between list.
  • containsAll Returns new list with items generated between list.
  • containsAny Returns new list with items generated between list.
  • containsAtLeast Returns new list with items generated between list.
  • containsHits Returns new list with items generated between list.
  • diff Elements that you have in listA and do not have in listB.
  • firstOrNull Returns first element in array, if it's empty returns null.
  • firstWhereOrAdd Returns the first element that satisfies the test if there isn't one add a new one and return it.
  • flat Returns a flatted a [list] that has other lists inside other lists inside other lists ... recursive
  • intersection Common elements between listA and listB
  • isFirst Returns a true value if element is the first in the list.
  • isLast Returns a true value if element is the last in the list.
  • joinLast It joins all elements of the list with one separator and for the last iteration a different separator.
  • lastOrNull Returns last element in array, if it's empty returns null.
  • lastWhereOrAdd Returns the last element that satisfies the test if there isn't one add a new one and return it.
  • toMap Returns a Map with keys that's generated by generator.
  • toStream Creates a stream with the items in the list.

Usage #

Just import lib and use extensions, call the functions to starts work:

// Don't forget to import
import 'package:ms_list_utils/ms_list_utils.dart';

List list = ['😁','😒','😊'];
final newList = addAround(list, (index,previous,next) => '🍨');
print(newList);//['🍨','😁','🍨','😒','🍨','😊','🍨']
// Or use extensions
final newList = list.addAround((index,previous,next) => '🍨');
print(newList);//['🍨','😁','🍨','😒','🍨','😊','🍨']

addAround #

The function addAround returns new list with items generated around list.

  test('tests the function add_around.dart', () {
    final initialValue = [1, 2, 3, 4];
    final result = addAround(initialValue,
        (int index, int? previousValue, int? nextValue) {
      return 5;
    });
    expect(result, [5, 1, 5, 2, 5, 3, 5, 4, 5]);
    expect(initialValue.addAround((index, previous, next) => 5),
        [5, 1, 5, 2, 5, 3, 5, 4, 5]);
  });

see more in test file.

addBetween #

The function addBetween returns new list with items generated between list.

  test('add values between in list', () {
    final initialValue = <int>[1, 2, 3];
    final result = addBetween<int>(
        initialValue, (int index, previousValue, nextValue) => 5);
    expect(result, [1, 5, 2, 5, 3]);
    expect(
        initialValue.addBetween((index, previous, next) => 5), [1, 5, 2, 5, 3]);
  });

see more in test file.

addBetween #

The function diff returns elements that you have in listA and do not have in listB.

  test('differences between two lists', () {
    final listA = [1, 2, 3, 4, 5];
    final listB = [2, 4, 6];
    expect(diff(listA, listB), orderedEquals([1, 3, 5]));
    expect(diff(listB, listA), orderedEquals([6]));
    expect(listB - listA, orderedEquals([6]));
    expect(listB.diff(listA), orderedEquals([6]));
  });

see more in test file.

firstWhereOrAdd #

The function firstWhereOrAdd returns the first element that satisfies the test if there isn't one add a new one and return it.

  test('tests the function first_where_or_add.dart', () {
    final list = [1, 2, 3];
    expect(firstWhereOrAdd(list, (value) => value == 4, () => 4), 4);
    expect(list, [1, 2, 3, 4]);
    final listE = [1, 2, 3];
    expect(listE.firstWhereOrAdd((value) => value == 4, () => 4), 4);
    expect(listE, [1, 2, 3, 4]);
  });

flat #

The function flat returns a flatted a [list] that has other lists inside other lists inside other lists ... recursive return it.

  test('flat dynamic', () {
    final multiList = [
      1,
      2,
      3,
      [
        "4",
        "5",
        [
          6,
          7,
          [true, true, false]
        ]
      ]
    ];
    final result = flat(multiList);
    expect(result, [1, 2, 3, "4", "5", 6, 7, true, true, false]);
    expect(multiList.flat(), [1, 2, 3, "4", "5", 6, 7, true, true, false]);
  });

flat #

The function intersection returns common items between lists

  test('intersection between two lists', () {
    final listA = [1, 2, 3, 4, 5];
    final listB = [2, 4, 6];
    expect(intersection(listA, listB), orderedEquals([2, 4]));
    expect(listA.intersection(listB), orderedEquals([2, 4]));
  });

see more in test file.

isFirst #

The function isFirst returns a true value if element is the first in the list.

  test('tests the function is_first.dart', () {
    final list = [1, 2, 3];
    expect(isFirst(list, 3), isFalse);
    expect(isFirst(list, 1), isTrue);
    expect(isFirstIndex(list, 3), isFalse);
    expect(isFirstIndex(list, 0), isTrue);
    expect(list.isFirst(3), isFalse);
    expect(list.isFirst(1), isTrue);
    expect(list.isFirstIndex(3), isFalse);
    expect(list.isFirstIndex(0), isTrue);
  });

see more in test file.

isLast #

The function isLast returns a true value if element is the last in the list.

  test('tests the function is_last.dart', () {
    final list = [1, 2, 3];
    expect(isLast(list, 1), isFalse);
    expect(isLast(list, 3), isTrue);
    expect(isLastIndex(list, 1), isFalse);
    expect(isLastIndex(list, 2), isTrue);
    expect(list.isLast(1), isFalse);
    expect(list.isLast(3), isTrue);
    expect(list.isLastIndex(1), isFalse);
    expect(list.isLastIndex(2), isTrue);
  });

see more in test file.

joinLast #

The function joinLast it joins all elements of the list with one separator and for the last iteration a different separator.

  test('tests the function join.dart', () {
    final list = [1, 2, 3];
    expect(join(list, ', ', ' and '), '1, 2 and 3');
    expect(join(list), '123');
    expect(list.joinLast(', ', ' and '), '1, 2 and 3');
    expect(list.joinLast(), '123');
  });

see more in test file.

lastOrNull #

The function lastOrNull returns last element in array, if it's empty returns null.

  test('tests the function last_or_null.dart', () {
    final emptyList = [];
    final list = [1, 2, 3];
    expect(lastOrNull(emptyList), null);
    expect(lastOrNull(list), 3);
    expect(emptyList.lastOrNull, null);
    expect(list.lastOrNull, 3);
  });

see more in test file.

lastWhereOrAdd #

The function lastWhereOrAdd returns the last element that satisfies the test if there isn't one add a new one and return it.

  test('tests the function last_where_or_add.dart', () {
    final list = [1, 2, 3];
    expect(lastWhereOrAdd(list, (value) => value == 4, () => 4), 4);
    expect(list, [1, 2, 3, 4]);
    final listE = [1, 2, 3];
    expect(listE.lastWhereOrAdd((value) => value == 4, () => 4), 4);
    expect(listE, [1, 2, 3, 4]);
  });

see more in test file.

toMap #

The function toMap returns a Map with keys that's generated by generator.

    final list = [1, 2, 3];
    expect(toMap<String, int>(list, (value) => value.toString()),
        {'1': 1, '2': 2, '3': 3});
    expect(list.toMap((value) => "$value"), {'1': 1, '2': 2, '3': 3});
  });

see more in test file.

toStream #

The function toStream returns a stream with the items in the list.

  test('tests the function to_stream.dart', () async {
    await expectLater([1, 2, 3].toStream(), emitsInOrder([1, 2, 3]));
    await expectLater([1, 2, 3].toStream(const Duration(milliseconds: 85)),
        emitsInOrder([1, 2, 3]));
    await expectLater(toStream([1, 2, 3]), emitsInOrder([1, 2, 3]));
  });

see more in test file.

See another libs here

5
likes
150
points
42
downloads

Publisher

verified publishermarcussoftware.info

Weekly Downloads

Novas funções para lidar com listas. Com suporte an extensions

Repository (GitHub)
View/report issues

Documentation

API reference

License

MIT (license)

More

Packages that depend on ms_list_utils