GithubHelp home page GithubHelp logo

icyleaf / popcorn Goto Github PK

View Code? Open in Web Editor NEW
26.0 3.0 1.0 736 KB

Easy and Safe popping from one type to another.

Home Page: http://icyleaf.github.io/popcorn

License: MIT License

Crystal 100.00%
crystal type-casting

popcorn's Introduction

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.

  • 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_array(target : T.class = String)
  • to_hash(target : T.class = String)
  • 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.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:

  • String
  • Int
  • Float
  • Bool
  • Time
  • Symbol
  • NamedTupe
  • Array
  • Hash
  • Nil
  • 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.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

  • icyleaf icyleaf - creator, maintainer

You may also like

  • halite - Crystal HTTP Requests Client with a chainable REST API, built-in sessions and middlewares.
  • totem - Load and parse a configuration file or string in JSON, YAML, dotenv formats.
  • poncho - A .env parser/loader improved for performance.
  • fast-crystal - ๐Ÿ’จ Writing Fast Crystal ๐Ÿ˜ -- Collect Common Crystal idioms.

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.