Giter Site home page Giter Site logo

dmora42 / tiny_invariant Goto Github PK

View Code? Open in Web Editor NEW

This project forked from zagmi/tiny_invariant

0.0 0.0 0.0 18 KB

A tiny implementation of the invariant function in Dart

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

License: BSD 2-Clause "Simplified" License

Dart 100.00%

tiny_invariant's Introduction

Tiny Invariant

Build Status npm dependencies types minzip Downloads per month

A tiny invariant alternative in Dart.

What is invariant?

An invariant function takes a value, and if the value is falsy then the invariant function will throw. If the value is truthy, then the function will not throw.

import 'package:tiny_invariant/tiny_invariant.dart';

invariant(truthyValue, 'This should not throw!');

invariant(falsyValue, 'This will throw!');
// Error('Invariant violation: This will throw!');

You can also provide a function to generate your message, for when your message is expensive to create

import 'package:tiny_invariant/tiny_invariant.dart';

invariant(value, () => getExpensiveMessage());

Why tiny_invariant?

The initial implementation from where I got the idea library: invariant supports passing in arguments to the invariant function in a sprintf style (condition, format, a, b, c, d, e, f). It has internal logic to execute the sprintf substitutions. The sprintf logic is not removed in production builds. tiny_invariant has dropped all of the sprintf logic. tiny_invariant allows you to pass a single string message.

invariant(condition, `Hello, ${name} - how are you today?`);

API: (condition: T, message: String?)

  • condition is required and can be anything
  • message optional string or a function that returns a string (() => string)

Installation

dart pub add tiny_invariant

Dropping your message for kb savings!

Big idea: you will want your compiler to convert this code:

invariant(condition, 'My cool message that takes up a lot of kbs');

Into this:

if (!condition) {
  if ('production' !== process.env.NODE_ENV) {
    invariant(false, 'My cool message that takes up a lot of kbs');
  } else {
    invariant(false);
  }
}

Original implementation

tiny_invariant's People

Contributors

santiirepair avatar dmora42 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.