pretty

Something attentive, conservative and pretty stuff utility backward-compatibility
0.5.3 released
maiha/pretty.cr
16
maiha

pretty.cr Build Status

Something pretty stuff for Crystal.

Pretty.bytes(123456789)                # => "123 MB"
Pretty.number(123456789)               # => "123,456,789"
Pretty.time("2000-01-02 03:04:05.678") # => 2000-01-02 03:04:05 UTC
Pretty.camelize("http_request")        # => "httpRequest"
Pretty.classify("http_request")        # => "HttpRequest"
Pretty.diff(1,2).to_s                  # => "Expected '1', but got '2'"
  • crystal: 0.23.1

API

Pretty.bytes(value : Int, block = 1000, suffix = "B")
Pretty.camelize(str : String)
Pretty.classify(str : String)
Pretty.error(err : Exception)
Pretty.json(json : String, color : Bool = false)
Pretty.lines(lines : Array(Array(String)), indent : String = "", delimiter : String = "")
Pretty.number(n : Int)
Pretty::Time.parse(value : String)

Installation

Add this to your application's shard.yml:

dependencies:
  pretty:
    github: maiha/pretty.cr
    version: 0.5.3

Then require it in your app.

require "pretty"

Usage

Pretty.bytes(args) : String

Pretty.bytes(416)                   # => "416 B"
Pretty.bytes(12255736)              # => "12.3 MB"
Pretty.bytes(12255736, block: 1024) # => "11.7 MiB"

Pretty.camelize(str) : String

Pretty.camelize("http_request")     # => "httpRequest"

Pretty.classify(str) : String

Pretty.classify("http_request")     # => "HttpRequest"

Pretty.diff(a, b) : Pretty::Diff

data1 = [[112433,15,0],[112465,7293,273]]
data2 = [[112433,15,0],[112465,1307,273]]

diff = Pretty.diff(data1, data2)
diff.size # => 1
diff.to_s # => "Expected '7293', but got '1307'"

Pretty.error(err) : Pretty::Error

err.backtrace            # => ["0x48df77: *CallStack::unwind:Array(Pointer(Void))
Pretty.error(err).where  # => "mysql at src/commands/db.cr 20:3"
IMPORTANT
  • We must call app with path for where, otherwise the app will be killed.
    • :x: foo # would kill your app in printing backtraces
    • :o: /usr/local/bin/foo # will work

Pretty.json(args) : String

str = %({"id": "123", "name": "maiha"})
Pretty.json(str)
{
  "id": "123",
  "name": "maiha"
}

Pretty.lines(args) : String

array = [
  ["user", "maiha"],
  ["password", "123"],
]
Pretty.lines(array, delimiter: " = ")
user     = maiha
password = 123

Pretty.number(n) : String

Pretty.number(1000000)  # => "1,000,000"

Pretty::Time.parse(args) : Time (aka. Pretty.time)

parses time without format!

Pretty.time("2000-01-02")                    # => 2000-01-02 00:00:00 UTC
Pretty.time("2000-01-02 03:04:05")           # => 2000-01-02 03:04:05 UTC
Pretty.time("2000-01-02 03:04:05.678")       # => 2000-01-02 03:04:05 UTC
Pretty.time("2000-01-02T03:04:05.678+09:00") # => 2000-01-02 03:04:05 +0900

Development

make

Contributing

  1. Fork it ( https://github.com/maiha/pretty.cr/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request

Contributors

  • maiha maiha - creator, maintainer
pretty:
  github: maiha/pretty.cr
  version: ~> 0.5.3
License MIT
Crystal none

Authors

Dependencies 0

Development Dependencies 0

Dependents 9

Last synced .
search fire star recently