popcorn

Easy and Safe popping from one type to another type-casting
0.1.1 released
icyleaf/popcorn
25 1
icyleaf

Popcorn

Language Tag Build Status

Easy and Safe popping from one type to another.

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.

  • to_int8
  • to_int16
  • to_int (alias to to_int32)
  • to_int64
  • to_uint8
  • to_uint16
  • to_uint (alias to to_uint32)
  • to_uint64
  • to_float32
  • to_float64
  • to_bool
  • to_time(location : Time::Location? = nil, formatters : Array(String)? = nil)
  • to_string (alias to to_s 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.new(2018, 7, 23, 10, 11, 22, location: Time::Location::UTC)
Popcorn.to_time("2018-07-23T10:11:22Z", Time::Location.load("Asia/Shanghai")) # => Time.new(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.new(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:

  • String
  • Int8/Int16/Int32/Int64/UInt8/UInt16/UInt32/UInt64
  • Float32/Float64
  • Bool
  • Time
  • JSON::Any
  • YAML::Any
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.new(2018, 7, 23, 10, 11, 22, location: Time::Location::UTC)
"2018-07-23T10:11:22Z".to_time(Time::Location.load("Asia/Shanghai")) # => Time.new(2018, 7, 23, 10, 11, 22, location: Time::Location.load("Asia/Shanghai"))
"Tue, 20 Jan 2018".to_time(formatter: ["%a, %d %b %Y"])              # => Time.new(2018, 7, 23, 10, 11, 22, location: Time::Location::UTC)

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

  • icyleaf icyleaf - creator, maintainer
popcorn:
  github: icyleaf/popcorn
  version: ~> 0.1.1
License MIT
Crystal 0.25.1

Authors

Dependencies 0

Development Dependencies 0

Dependents 1

Last synced .
search fire star recently