Code Monkey home page Code Monkey logo

swiftyformat's Introduction

SwiftyFormat

[![CI Status](http://img.shields.io/travis/Igor Palaguta/SwiftyFormat.svg?style=flat)](https://travis-ci.org/Igor Palaguta/SwiftyFormat) Version License Platform

SwiftyFormat has simple syntax, inside string you can add #{{key|default value|prefix|suffix}}.

  1. key (required) is used for identifying parameter inside mapping
  2. default value (optional) is used when when mapping returns nil for key
  3. prefix (optional) is added before value. Not added for default value
  4. suffix (optional) is added after value. Not added for default value

Example

let format = "#{{user}} mentioned you in a comment. #{{comment}}"
let result = NSAttributedString(format: format, mapping: ["user": "Jack", "comment": "How are you Jill?"])

Closure can be passed instead of dictionary. It is useful, when format can be changed, and not all parameters required for every format.

let result = NSAttributedString(format: someFormat) { key in
   switch key {
   case "name":
      return NSAttributedString(string: "Jack", attributes: nameAttributes)
   case "cookies":
      return cookies
   default:
      return nil
   }
}

You can also specify default value, prefix and suffix

let format ="#{{name|Your friend}} mentioned you in a comment#{{comment|| \"|\"}"
let result1 = NSAttributedString(format: format, mapping: [:])
//result1 == Your friend mentioned you in a comment
let result2 = NSAttributedString(format: format, mapping: ["name": "Jack", comment: "How are you?"])
//result2 == Jack mentioned you in a comment "How are you?"

Same format syntax can be used for String

let format = "#{{user}} mentioned you in a comment. #{{comment}}"
let result = String(format: format, mapping: ["user": "Jack", "comment": "How are you Jill?"])

Requirements

SwiftyFormat supports Swift 2 and Swift 3. Use 'swift-2.3' branch for Swift 2

Installation

Cocoapods:

pod "SwiftyFormat"

`

Carthage:

github "Igor-Palaguta/SwiftyFormat"

Author

Igor Palaguta, [email protected]

License

SwiftyFormat is available under the MIT license. See the LICENSE file for more info.

swiftyformat's People

Contributors

igor-palaguta avatar

Watchers

Carabineiro 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.