popcorn-logo

Popcorn

Language Tag Build Status

Easy and Safe popping from one type to another. Popcorn icon by HLD from the Noun Project.

Build in Crystal version >= v0.31.0.

Installation

Add this to your application's shard.yml:

dependencies:
  popcorn:
    github: icyleaf/popcorn

Usage

Popcorn is easy and safe from one type to another tool.

Popcorn proviedes a handful of to_xxx/to_xxx? methods and xxx is full type name, This is distinguish between built-in methods in Crystal.

require "popcorn"

Popcorn.to_sting("foobar")                                                    # => "foobar"
Popcorn.to_string(8.31)                                                       # => "8.31"
Popcorn.to_int("1")                                                           # => 1
Popcorn.to_int("123abc")                                                      # => 123
Popcorn.to_bool("no")                                                         # => false
Popcorn.to_bool("unkown")                                                     # => raise a `TypeCastError` exception
Popcorn.to_bool?("unkown")                                                    # => nil
Popcorn.to_time("2018-07-23T10:11:22")                                        # => Time.local(2018, 7, 23, 10, 11, 22, location: Time::Location::UTC)
Popcorn.to_time("2018-07-23T10:11:22Z", Time::Location.load("Asia/Shanghai")) # => Time.local(2018, 7, 23, 10, 11, 22, location: Time::Location.load("Asia/Shanghai"))
Popcorn.to_time("Tue, 20 Jan 2018", formatter: ["%a, %d %b %Y"])              # => Time.local(2018, 7, 23, 10, 11, 22, location: Time::Location::UTC)

Also these methods had been inject to Crystal literals, you can call it directly, Monkey Patching list:

require "popcorn"
require "popcorn/injection" # => make sure require it!

"foobar".to_string                                                   # => "foobar"
8.31.to_string                                                       # => "8.31"
"1".to_int                                                           # => 1
"123abc".to_int                                                      # => 123
"no".to_bool                                                         # => false
"unkown".to_bool                                                     # => raise a `TypeCastError` exception
"unkown".to_bool?                                                    # => nil
"2018-07-23T10:11:22".to_time                                        # => Time.local(2018, 7, 23, 10, 11, 22, location: Time::Location::UTC)
"2018-07-23T10:11:22Z".to_time(Time::Location.load("Asia/Shanghai")) # => Time.local(2018, 7, 23, 10, 11, 22, location: Time::Location.load("Asia/Shanghai"))
"Tue, 20 Jan 2018".to_time(formatter: ["%a, %d %b %Y"])              # => Time.local(2018, 7, 23, 10, 11, 22, location: Time::Location::UTC)

Casting unmatch class

By desgin in Crystal, we can not match type with T(no override by alone type) and no time to match each shard type. so you can write your own.

Reference at totem.

Contributing

  1. Fork it (https://github.com/icyleaf/popcorn/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

You may also like