GithubHelp home page GithubHelp logo

ashtanko / nullx Goto Github PK

View Code? Open in Web Editor NEW
26.0 1.0 0.0 102 KB

nullx is a Dart toolkit that enhances handling of nullable types

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

License: Apache License 2.0

Makefile 1.96% Dart 96.53% Shell 1.51%
dart dartlang null-safety

nullx's Introduction

nullx is a Dart toolkit that enhances handling of nullable types, providing utilities for null-checking, navigating nullable structures, and robust error handling, for cleaner and more resilient code.

Coverage Dart CI

nullx

CodeFactor Codacy Badge codecov Codacy Badge

Features

  • Provides utilities for null-checking
  • Helps in navigating nullable structures
  • Offers robust error handling

Getting started ๐ŸŽ‰

To use this package, add nullx as a dependency in your pubspec.yaml file.

dependencies:
  nullx: ^latest_version

Here is a simple example of how to use nullx:

Collections

import 'package:nullx/nullx.dart';

void main() {
  final list = [1, null, 3, null];
  list.mapNonNull((item) => item * 2); // prints: [2, 6]
}
import 'package:nullx/nullx.dart';

void main() {
  final list = [1, null, 3, null];
  list.mapNonNullIndexed((item, index) => item * index); // prints: [0, 6]
}
import 'package:nullx/nullx.dart';

void main() {
  final List<int>? nullableList = [1, 2, 3];
  nullableList.whatIfNotNullOrEmpty(
    (list) => print(list), // whatIf
    () => print('List is null or empty'), // whatIfNot
  );
  // prints: [1, 2, 3]
}
import 'package:nullx/nullx.dart';

void main() {
  List<int>? nullableList = [1, 2, 3];
  print(nullableList.isNullOrEmpty); // prints: false

  nullableList = null;
  print(nullableList.isNullOrEmpty); // prints: true

  nullableList = [];
  print(nullableList.isNullOrEmpty); // prints: true
}

Types

import 'package:nullx/nullx.dart';

void main() {
  const String? nullableString = 'example';
  unwrapped(nullableString, (value) {});

  final List<String?> strings = [null, 'one', null, 'two', null];
  strings.map((s) => s.letNonNull((s) => s.length)).whereType<int>().toList();
}
import 'package:nullx/nullx.dart';

void main() {
    const int? nullableInt = 10;
    final resultInt = nullableInt.letNonNull((value) => value * 2); // prints: 2
}
import 'package:nullx/nullx.dart';

void main() {
  const int userAge = 20;
  executeIf(
    () => userAge >= 18,
    onConditionMet: () {
      // prints 'You are an adult.'
    },
    onConditionNotMet: () {
      // prints 'You are not an adult.'
    },
  );
}
import 'package:nullx/nullx.dart';

void main() {
  var userAge = 20;
  final result = executeIfAs<String>(
    () => userAge >= 18,
    onConditionMet: () => 'You are an adult.',
    onConditionNotMet: () => 'You are not an adult.',
  );
}
import 'package:nullx/nullx.dart';

void main() {
    void printValue(String value) {
      // prints 'The value is: $value'
    }

    notEmpty(nullableString, printValue);
}
import 'package:nullx/nullx.dart';

void main() {
    const String? nullableString = 'example';
    unwrapped(nullableString, (value) {}); // prints: 'example'
}
import 'package:nullx/nullx.dart';

void main() {
    var nullableString = 'Hello';
    nullableString.notEmpty((item) => print(item)); // prints: Hello
}
import 'package:nullx/nullx.dart';

void main() {
  const int userAge = 20;
  // Unwraps the nullable string and performs an operation on it
  callWhen(
    condition: () => userAge >= 18,
    onMet: () {
      // prints 'You are an adult.'
    },
    onNotMet: () {
      // prints 'You are not an adult.'
    },
  );
}
import 'package:nullx/nullx.dart';

void main() {
    const int? nullableInt = null;
    nullableInt.orZero; // Outputs: 0
    nullableInt.or(defaultValue: 5); // Outputs: 5

    const double? nullableDouble = null;
    nullableDouble.orZero; // Outputs: 0.0
    nullableDouble.or(defaultValue: 5.5); // Outputs: 5.5

    const bool? nullableBool = null;
    nullableBool.orFalse; // Outputs: false
    nullableBool.or(defaultValue: true); // Outputs: true
}

Contributing

Contributions are welcome! Please read the contributing guide to learn how to contribute to the project and set up a development environment.

License

Copyright 2024 Oleksii Shtanko

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.

nullx's People

Contributors

ashtanko avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

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.