Coder Social home page Coder Social logo

burrito's Introduction

🌯 Burrito

Type-Safe unwraps with default value.

Implicitly unwrap optional values and use default values whenever needed.

Installation

Carthage

Add the following to your Cartfile.

github "kimar/Burrito"

Quickstart

Burrito helps you to get your 🌯 together and unwrap concisely and in a more readable fashion.

Given you're about to instantiate the following struct:

struct Person {
	let name: String
}

With pure Swift you're likely to see this code in you App:

let person = Person(
	name: dict["name"] as? String ?? "Johnny Appleseed"
)

Using Burrito the required type will be inferredyou can make your code as readable as this:

let person = Person(
	name: dict.either("name") <~> "Johnny Appleseed"
)

What doesn't look like saving a lot of code at first clearly brings a few advantages:

  1. No more maintaining the type you'd like to unwrap to, the type will automatically be inferred.
  2. Easier to read code provides better maintainability
  3. A closure can be provided to compute the default value:
let person = Person(
	name: dict.either("name") <~> {
		return computedName
	}
)

Please note that <~> is just syntactic sugar and can effortlessly be omitted.

Unwrapping dictionaries shouldn't be painful, let Burrito help you unwrap:

let json: [String: Any?] = [
	"name": Optional("Marcus"),
	"age": 31,
	"blog": URL(string: "https://blog.kida.io")!
]

let person = Person(
	name: json.unwrap("name") { "Unknown" }, // -> Marcus
	age: json.unwrap("age") { -1 }, // -> 31
	github: json.unwrap("github") {
		URL(string: "https://github.com/kimar")!
	} // -> https://github.com/kimar
)

As you can see, because github is non-existent in the payload, when trying to unwrap it a default value will be used. This value can be returned in the closure.

Advanced Usage

By telling Burrito what type you'd like to unwrap and specify a default closure returning the value you'd expect if the object can't be unwrapped or casted:

Trying to cast an Int to a String won't work e.g.:

let value = Burrito<String>.unwrap(0) {
	return "This is not the String you're looking for!"
}

print(value) // -> "This is not the String you're looking for!"

But when providing the correct type you'll get it's value back e.g.:

let myString = "This is the String you're looking for…"
let value = Burrito<String>.unwrap(myString) {
	return "This is not the String you're looking for!"
}

print(value) // -> "This is the String you're looking for…"

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.