Git Product home page Git Product logo

formatting's Introduction

Formatting

Type-safe, functional string formatting in Swift.

Inspired by Chris Done's excellent Haskell library.

import Formatting

format("Hello, " % string % "!", "world")
// "Hello, world!"

Introduction

Traditional string formatting methods (interpolation, printf, and template strings) can lead to subtle (and not so subtle) runtime bugs.

print("Hello, \(thing)!")              // Hello, nil!
print("Hello, \(thing)!")              // Hello, Optional("world")!
NSLog(@"Hello, %@!", thing);           // Hello, (null)!

Formatting brings compile-time checks.

print("Hello, " % string % "!", thing) // Value of optional type String? not unwrapped

And composability.

let greet =
  format("Hello, " % string % "!")

greet("world")                         // Hello, world!

Composing formatters

Use % to build a formatter with strings and other formatters.

format(string % " is " % int % "years old.", "Alice", 25)
// "Alice is 25 years old."

Use <> to pass the previous formatter argument to the next formatter.

format(yyyy % "-" <> MM % "-" <> dd, Date())
// "2016-06-28"

Use .% to feed the result of one formatter into another.

format(left(2, "0") .% hex, 10)
// "0a"

Call format without arguments to return a curried formatter function.

let log =
  format(right(5) % " -- [" % iso8601 % "] " % string)

let infoLog = log("INFO")
let debugLog = log("DEBUG")

infoLog(Date())("Logging in...")
// "INFO  -- [2016-06-28T12:34:56Z] Logging in..."
debugLog(Date())("Logged in successfully!")
// "DEBUG -- [2016-06-28T12:34:56Z] Logged in successfully!"

License

Formatting is available under the MIT license. See the LICENSE file for more information.

formatting's People

Contributors

stephencelis 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  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  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  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

formatting's Issues

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.