# FlashExtensions [![Build Status](https://travis-ci.org/drexed/flash_extensions.svg?branch=master)](https://travis-ci.org/drexed/flash_extensions) [![Coverage Status](https://coveralls.io/repos/drexed/flash_extensions/badge.png)](https://coveralls.io/r/drexed/flash_extensions) [![Code Climate](https://codeclimate.com/github/drexed/flash_extensions.png)](https://codeclimate.com/github/drexed/flash_extensions) Flash Extensions is a collection of commonly used object helpers in a ruby based project. It currently includes extensions for: arrays, enumerables, hashes, numerics, objects, strings, and time. `Rails Safe` = methods extracted from rails but that do not override that rails method. Highly recommended extensions: * **Hash:** Hashie - https://github.com/intridea/hashie * **String:** Escape Utils - https://github.com/brianmario/escape_utils * **String:** Fast Blank - https://github.com/SamSaffron/fast_blank * **Translation:** Fast Gettext - https://github.com/grosser/fast_gettext ## Installation Add this line to your application's Gemfile: gem 'flash_extensions' And then execute: $ bundle Or install it yourself as: $ gem install flash_extensions ## Usage ### ArrayExtensions ####Remove Blanks:#### Use the `remove_blanks` method removes blank elements from an array. ```ruby ["this", "", "that", nil].remove_blanks #=> ["this", "that"] "this is a test".split(" ").remove_blanks #=> ["this", "is", "a", "test"] ``` ####Remove First Element:#### Use the `remove_first` method removes the first element from an array. Like Array.shift, but returns the array instead of removed the element. ```ruby ["1", "2", "3"].remove_first #=> ["2", "3"] ``` ####Remove Last Element:#### Use the `remove_last` method removes the last element from an array. Like Array.pop, but returns the array instead of removed the element. ```ruby ["1", "2", "3"].remove_last #=> ["1", "2"] ``` ### EnumerableExtensions ####Average:#### Use the `average` method to return the average of a collection of numbers. ```ruby [1,2,3].average #=> 2 [].average #=> 0 [].average(nil) #=> nil ``` ####Drop Last:#### Use the `drop_last` method to drops the last number of elements of a collection. ```ruby [1,2,3].drop_last(1) #=> [1,2] [].drop_last(3) #=> [] ``` ####Drop Last While:#### Use the `drop_last_while` method to drops the last number of elements of a collection while it meets a criteria. ```ruby [1,2,3].drop_last_while(&:odd?) #=> [1,2] [].drop_last_while(&:odd?) #=> [] ``` ####Exactly:#### Use the `excatly?` method to return if there are exactly the number of an element type. ```ruby [1,2,3].excatly?(3) #=> true [1,1,3,3].exactly?(2, &:even?) #=> false [].exactly?(1) #=> false ``` ####Several:#### Use the `several?` method to return if there are several types of an element. ```ruby [1,2,3].several? #=> true [1,1,3,3].several?(&:even?) #=> false [].several? #=> false ``` ####Sum:#### Use the `sum` method to to return the sum of a collection of numbers. ```ruby [1,2,3].sum #=> 2 [1,2,3,4].sum #=> 2.5 [].sum #=> 0 [].sum(nil) #=> nil ``` ####Take Last:#### Use the `take_last` method to return the last number of elements of a collection. ```ruby [1,2,3].take_last(2) #=> [2,3] [].take_last(3) #=> [] ``` ####Take Last While:#### Use the `take_last_while` method to return the last number of elements of a collection while it meets a criteria. ```ruby [1,2,3,5].take_last_while(&:odd?) #=> [5, 5] [].take_last_while(&:odd?) #=> [] ``` ### HashExtensions ####Except:#### Use the `except` method to return only key/value pairs not matching certain keys. ```ruby { foo: 'foo', baz: 'baz', bar: 'bar' }.except(:foo) #=> { baz: 'baz', bar: 'bar' } { :foo => 'foo', :baz => 'baz', :bar => 'bar' }.except(:baz, :bar) #=> { :foo => 'foo' } {}.except(:foo) #=> {} ``` ####Only:#### Use the `only` method to return only key/value pairs matching certain keys. ```ruby { foo: 'foo', baz: 'baz', bar: 'bar' }.only(:foo) #=> { foo: 'foo' } { :foo => 'foo', :baz => 'baz', :bar => 'bar' }.only(:baz, :bar) #=> { :baz => 'baz', :bar => 'bar' } {}.only(:foo) #=> {} ``` ####Rename Keys:#### Use the `rename_keys` and `rename_keys!` method to rename the keys of a hash. ```ruby { foo: 'foo', baz: 'baz' }.rename_keys(foo: :bar) #=> { bar: 'foo', baz: 'baz' } { foo: 'foo', 'baz' => 'baz' }.rename_keys(foo: :bar, 'baz' => 'tick') #=> { bar: 'foo', tick: 'baz' } ``` ####Stringify Keys:#### Use the `stringify_keys` and `stringify_keys!` method to convert the hash keys to strings. `Rails Safe` ```ruby { foo: 'foo', 'bar' => 'bar' }.stringify_keys #=> { 'foo' => 'foo', 'baz' => 'baz' } ``` ####Symbolize Keys:#### Use the `symbolize_keys` and `symbolize_keys!` method to convert the hash keys to symbols. `Rails Safe` ```ruby { foo: 'foo', 'bar' => 'bar' }.symbolize_keys #=> { foo: 'foo', baz: 'baz' } ``` ####Symbolize and Underscore Keys:#### Use the `symbolize_and_underscore_keys` and `symbolize_and_underscore_keys!` method to symbolize and underscore keys. ```ruby { 'firstName' => 'example', lastName: 'string' }.symbolize_and_underscore_keys #=> { first_name: 'foo', last_name: 'test' } ``` ### ObjectExtensions ####Blank:#### Use the `blank?` method on a object to determine if it is empty or nil. `Rails Safe` ```ruby "".blank? #=> true "Awesome Sting".blank? #=> false ``` ####Present:#### Use the `present?` method on a object to determine if it is not empty or nil. `Rails Safe` ```ruby "Awesome Sting".blank? #=> true "".present? #=> false ``` ####Numeric:#### Use the `numeric?` method to determine whether an object's to_s value is numeric. ```ruby "-32.50".numeric? #=> true "$2.55".numeric? #=> false ``` ####Palindrome:#### Use the `palindrome?` method to determine if an object is a palindrome. ```ruby "racecar".palindrome? #=> true 12321.palindrome? #=> true "example".palindrome? #=> false 12345.palindrome? #=> false ``` ####Try:#### Use the `try` method on a object to try that method with out raising an error. `Rails Safe` ```ruby "example".try(:upcase) #=> "EXAMPLE" "example".try(:fake_method) #=> nil ``` ### NumericExtensions ####Multiple Of:#### Use the `multiple_of?` method to check if a number is the multiple of another. `Rails Safe` ```ruby 9.multiple_of?(3) #=> true 7.multiple_of?(3) #=> false ``` ####Negative:#### Use the `negative?` method to check if a number is negative. ```ruby -1.negative? #=> true 1.negative? #=> false ``` ####Positive:#### Use the `positive?` method to check if a number is positive. ```ruby 1.positive? #=> true -1.positive? #=> false ``` ### StringExtensions ####Camelize:#### Use the `camelize` and `camelize!` method to transfrom a string to camelcase. `Rails Safe` ```ruby "example_string".camelize #=> "ExampleString" "example_string".camelize(:lower) #=> "exampleString" ``` ####Ends With:#### Use the `ends_with?` method to determine whether a string ends with a certain value. `Rails Safe` ```ruby "example string".ends_with?("g") #=> true "example string".ends_with?("ng") #=> true "example string".ends_with?("e") #=> false ``` ####Starts With:#### Use the `starts_with?` method to determine whether a string starts with a certain value. `Rails Safe` ```ruby "example string".starts_with?("e") #=> true "example string".starts_with?("ex") #=> true "example string".starts_with?("g") #=> false ``` ####Humanize:#### Use the `humanize` and `humanize!` method to transform a string to a human readable string. `Rails Safe` ```ruby "ExampleString".humanize #=> "Example string" "example_string".humanize #=> "Example string" ``` ####Titleize:#### Use the `titleize` and `titleize!` method to capitalize each word in a string. `Rails Safe` ```ruby "example string".titleize #=> "Example String" "example_string".titleize #=> "Example String" "ExampleString".titleize #=> "Example String" ``` ####Underscore:#### Use the `underscore` and `underscore!` method to transform a string to snakecase. `Rails Safe` ```ruby "ExampleString".underscore #=> "example_string" "exampleString".underscore #=> "example_string" ``` ####Domain:#### Use the `domain` method to extract the domain name from a URL. ```ruby "http://www.example.com/fake-page".domain #=> "www.example.com" ``` ####Downcase:#### Use the `downcase?` method to determine if all characters are lowercase. ```ruby "example".downcase? #=> true "Example".downcase? #=> false "EXAMPLE".downcase? #=> false ``` ####Upcase:#### Use the `upcase?` method to determine if all characters are uppercase. ```ruby "EXAMPLE".upcase? #=> true "example".upcase? #=> false "Example".upcase? #=> false ``` ####Mixcase:#### Use the `mixcase?` method to determine if characters are mixedcase. ```ruby "Example".mixedcase? #=> true "EXAMPLE".mixedcase? #=> false "example".mixedcase? #=> false ``` ####Ellipsize:#### Use the `ellipsize` method to truncate a string in the middle. **Options** * Length: default to 30 * Offset: default to 4 * Separator: default to "..." ```ruby "example string".ellipsize #=> "example string" "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ".ellipsize #=> "0123...WXYZ" "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ".ellipsize(offset: 2, separator: "+++") #=> "01+++YZ" ``` ####Nix:#### Use the `nix` and `nix!` method to remove the first instance of a string. ```ruby "this thing that thing".nix("thing") #=> "this that thing" ``` ####GNix:#### Use the `gnix` and `gnix!` method to remove the every instance of a string. ```ruby "this thing that thing".gnix("thing") #=> "this that " ``` ####Pollute:#### Use the `pollute` method to pollute the space between every letter in a string, so it will be exempt from any impending string searches. ```ruby "test".pollute #=> "t^--^--^e^--^--^s^--^--^t^--^--^" "test".pollute("-") #=> "t-e-s-t-" ``` ####Unpollute:#### Use the `unpollute` to remove the default or custom pollution character. Can also be used to remove an unwanted character. ```ruby "t^--^--^e^--^--^s^--^--^t^--^--^".unpollute #=> "test" "t-e-s-t-".unpollute #=> "test" ``` ####Slugify:#### Use the `slugify` and `slugify!` method to generate a permalink-style string, with odd characters removed. ```ruby "example".slugify #=> "example" "example string".slugify #=> "example-string" "Example string @@@ test!".slugify #=> "example-string-test" ``` ####Strip Tags:#### Use the `strip_tags` and `strip_tags!` method to remove HTML tags from a string. ```ruby "example".strip_tags #=> "example" "click".strip_tags #=> "click" "this is bold and emphatic".strip_tags #=> "this is bold and emphatic" ``` ####Strip Whitespace:#### Use the `strip_whitespace` and `strip_whitespace!` method removes tab characters and instances of more than one space. ```ruby "example string test".strip_whitespace #=> "example string test" " this \t is also a test ".strip_whitespace #=> "this is also a test" ``` ####Truncate Preserving Words:#### Use the `truncate_preserving_words` method to truncate a string while preserving words. **Options** * max_words: default to nil * max_characters: default to 30 * Separator: default to "..." ```ruby "example string".truncate_preserving_words #=> "example string" "example string test another1 another2 another3".truncate_preserving_words #=> "example string test another1 ..." "example string test another1 another2 another3".truncate_preserving_words(max_chars: 10, separator: "+++") #=> "example +++" ``` ### TimeExtensions ####Format:#### Use the `format` method on a Date or Time object to format it using a human readable string. **Rules** * Characters: a-z 0-9 _ * Characters can only be used to generate a format part ```ruby Time.now.format("year") #=> "2014" Time.now.format("month_name day, year hour:minute ampm") #=> "January 09, 2014 02:31 pm" ``` | Name | Key | Equivalent `strftime` | Result | | --- | --- | --- | --- | | Month - digits zero-padded | `m` or `month` or `month_zero` | %m | (01..12) | | Month - digits unpadded | `mm` or `Month` or `month_unpadded` | %-m | (1..12) | | Month - digits blank-padded | `mmm` or `MONTH` or `day_blank` | %_m | ( 1..12) | | Month - name | `mmmm` or `month_name` | %B | January | | Month - name abbreviated | `mmmmm` or `month_name_abbr` | %b | Jan | | Day - digits zero-padded | `d` or `day` or `day_zero` | %d | (01..31) | | Day - digits unpadded | `dd` or `Day` or `day_unpadded` | %-d | (1..31) | | Day - digits blank-padded | `ddd` or `DAY` or `day_blank` | %_d | ( 1..31) | | Day - digits of the year | `dddd` or `day_of_the_year` | %j | (001..366) | | Week - starting monday | `wwwww` or `week` | %M | (00..53) | | Week - starting sunday | `wwwwww` or `weekday_offset` | %M | (00..53) | | Weekday - starting monday | `w` or `weekday` | %M | (1..7) | | Weekday - starting sunday | `ww` or `weekday` | %M | (0..6) | | Weekday - name | `www` or `weekday_name` | %M | Sunday | | Weekday - name abbreviated | `wwww` or `weekday_name_abbr` | %M | Sun | | Year - digits two | `yy` or `yr` | %y | (00..99) | | Year - digits four | `yyyy` or `year` | %Y | 1999 | | Hour - digits zero-padded | `h` or `hour` or `hour_zero` | %H | (00..23) | | Hour - digits blank-padded | `hh` or `HOUR` or `hour_blank` | %k | ( 0..23) | | Hour - digits zero-padded | `hhh` or `hour_imperical` or `hour_imperical_zero` | %I | (01..12) | | Hour - digits blank-padded | `hhhh` or `HOUR_IMPERICAL` or `hour_imperical_blank` | %l | ( 1..12) | | Minute - minute | `n` or `minute` | %M | (00..59) | | Second - second | `s` or `second` | %S | (00..60) | | Meridian - lowercase | `ampm` or `meridian` | %p | am..pm | | Meridian - uppercase | `AMPM` or `MERIDIAN` | %P | AM..PM | | Time Zone - time zone | `z` or `time_zone` | %z | +0900 | | Time Zone - hour and minute offset | `zz` or `time_zone_offset` | %z | +09:00 | | Time Zone - hour, minute and second offset | `zzz` or `time_zone_offset_full` | %z | +09:00:00 | ####To Format:#### Use the `to_format` method on a Date or Time object to format it without having to use `strftime` method. ```ruby Time.now.to_format(:year) #=> "2014" Time.now.to_format(:datetime) #=> "January 09, 2014 02:31 pm" ``` | Name | Key | Equivalent `strftime` | Result | | --- | --- | --- | --- | | Month - digits zero-padded | `:month` or `:month_zero` | %A | (01..31) | | Month - digits unpadded | `:month_unpadded` | %a | (1..31) | | Month - digits blank-padded | `:month_blank` | %a | ( 1..31) | | Month - name | `:month_name` | %A | January | | Month - name abbreviated | `:month_name_abbr` | %a | Jan | | Weekday - digits zero-padded | `:Weekday_zero` | %A | (01..12) | | Weekday - digits unpadded | `:Weekday_unpadded` | %a | (1..12) | | Weekday - digits blank-padded | `:Weekday_blank` | %a | ( 1..12) | | Weekday - name | `:weekday_name` | %A | Sunday | | Weekday - name abbreviated | `:Weekday_name_abbr` | %a | Sun | | Year - digits two | `:yr` | %y | (00..99) | | Year - digits four | `:year` | %Y | 1999 | | Hour - digits zero-padded | `:hour` or `:hour_zero` | %H | (00..23) | | Hour - digits blank-padded | `:hour_blank` | %k | ( 0..23) | | Hour - digits zero-padded imperical | `:hour_imperical_zero` | %I | (01..12) | | Hour - digits blank-padded imperical | `:hour_imperical_blank` | %l | ( 1..12) | | Minute - minute | `:minute` | %M | (00..59) | | Second - second | `:second` | %S | (00..60) | | Time Zone - time zone | `:time_zone` | %z | +0900 | | Time Zone - hour and minute offset | `:time_zone_offset` | %z | +09:00 | | Time Zone - hour, minute and second offset | `:time_zone_offset_full` | %z | +09:00:00 | | Date - name | `:date` | %B %-d, %Y | January 9, 2014 | | Date - name abbreviated | `:date_abbr` | %b %-d, %Y | Jan 9, 2014 | | Date - iso | `:date_iso` | %Y-%m-%d | 2014-01-09 | | Datetime - name | `:datetime` | %B %-d, %Y %H:%M | January 9, 2014 00:31 | | Datetime - name abbreviated | `:datetime_abbr` | %b %-d, %Y %H:%M | Jan 9, 2014 00:31 | | Datetime - iso | `:datetime_iso` | %Y-%m-%d %H:%M | 2014-01-09 00:31 | | Datetime - name imperical | `:datetime_imperical` | %B %-d, %Y %H:%M | January 9, 2014 12:31 am | | Datetime - name abbreviated imperical | `:datetime_imperical_abbr` | %b %-d, %Y %H:%M | Jan 9, 2014 12:31 am | | Datetime - iso imperical | `:datetime_imperical_iso` | %Y-%m-%d %H:%M | 2014-01-09 12:31 am | | Datetime - name time zone | `:datetime_tzn` | %B %-d, %Y %H:%M %Z | January 9, 2014 00:31 UTC | | Datetime - name abbreviated time zone | `:datetime_abbr_tzn` | %b %-d, %Y %H:%M %Z | Jan 9, 2014 00:31 UTC | | Datetime - iso time zone | `:datetime_iso_tzn` | %Y-%m-%d %H:%M %z | 2014-01-09 00:31 +0000 | | Datetime - name imperical time zone | `:datetime_imperical_tzn` | %B %-d, %Y %H:%M %Z | January 9, 2014 12:31 am UTC | | Datetime - name abbreviated imperical time zone | `:datetime_imperical_abbr_tzn` | %b %-d, %Y %H:%M %Z | Jan 9, 2014 12:31 am UTC | | Datetime - iso imperical time zone | `:datetime_imperical_iso_tzn` | %Y-%m-%d %H:%M %z | 2014-01-09 12:31 am +0000 | | Day - name | `:day` | %B %-d | January 9 | | Day - name abbreviated | `:day_abbr` | %b %-d | Jan 9 | | Day - iso | `:day_iso` | %m-%d | 01-09 | | Daytime - name | `:daytime` | %B %-d %H:%M | January 9 00:31 | | Daytime - name abbreviated | `:daytime_abbr` | %b %-d %H:%M | Jan 9 00:31 | | Daytime - iso | `:daytime_iso` | %m-%d %H:%M | 01-09 00:31 | | Daytime - name imperical | `:daytime_imperical` | %B %-d %H:%M | January 9 12:31 am | | Daytime - name abbreviated imperical | `:daytime_imperical_abbr` | %b %-d %H:%M | Jan 9 12:31 am | | Daytime - iso imperical | `:daytime_imperical_iso` | %m-%d %H:%M | 01-09 12:31 am | | Time - zero-padded | `:time` or `:time_zero` | %H:%M | 00:31 | | Time - blank-padded | `:time_blank` | %k:%M %z | 0:31 | | Time - with time zone | `:time_tz` | %H:%M %z | 00:31 +0000 | | Time - with time zone name | `:time_tzn` | %H:%M %Z | 00:31 UTC | ## Contributing 1. Fork it ( http://github.com//flash_extensions/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 new Pull Request