README.md in flash_extensions-0.0.1 vs README.md in flash_extensions-1.0.0

- old
+ new

@@ -2,14 +2,20 @@ [![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, hash, objects, strings, and time. +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' @@ -28,188 +34,303 @@ ####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"] +["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_element` method removes the first element from an array. Like Array.shift, but returns the array instead of removed the 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_element => ["2", "3"] +["1", "2", "3"].remove_first #=> ["2", "3"] ``` ####Remove Last Element:#### -Use the `remove_last_element` method removes the last element from an array. Like Array.pop, but returns the array instead of removed the 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_element => ["1", "2"] +["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` method to rename the keys of a hash. +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' } +{ 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' } +{ 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' } +{ 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' } +{ '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 +"".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 +"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 +"-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 +"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 +"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` method to transfrom a string to camelcase. `Rails Safe` +Use the `camelize` and `camelize!` method to transfrom a string to camelcase. `Rails Safe` ```ruby -"example_string".camelize => "ExampleString" -"example_string".camelize(:lower) => "exampleString" +"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 +"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 +"example string".starts_with?("e") #=> true +"example string".starts_with?("ex") #=> true +"example string".starts_with?("g") #=> false ``` ####Humanize:#### -Use the `humanize` method to transform a string to a human readable string. `Rails Safe` +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" +"ExampleString".humanize #=> "Example string" +"example_string".humanize #=> "Example string" ``` ####Titleize:#### -Use the `titleize` method to capitalize each word in a string. `Rails Safe` +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" +"example string".titleize #=> "Example String" +"example_string".titleize #=> "Example String" +"ExampleString".titleize #=> "Example String" ``` ####Underscore:#### -Use the `underscore` method to transform a string to snakecase. `Rails Safe` +Use the `underscore` and `underscore!` method to transform a string to snakecase. `Rails Safe` ```ruby -"ExampleString".underscore => "example_string" -"exampleString".underscore => "example_string" +"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" +"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 +"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 +"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 +"Example".mixedcase? #=> true +"EXAMPLE".mixedcase? #=> false +"example".mixedcase? #=> false ``` ####Ellipsize:#### Use the `ellipsize` method to truncate a string in the middle. @@ -217,69 +338,69 @@ * 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" +"example string".ellipsize #=> "example string" +"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ".ellipsize #=> "0123...WXYZ" +"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ".ellipsize(offset: 2, separator: "+++") #=> "01+++YZ" ``` ####Nix:#### -Use the `nix` method to remove the first instance of a string. +Use the `nix` and `nix!` method to remove the first instance of a string. ```ruby -"this thing that thing".nix("thing") => "this that thing" +"this thing that thing".nix("thing") #=> "this that thing" ``` ####GNix:#### -Use the `gnix` method to remove the every instance of a string. +Use the `gnix` and `gnix!` method to remove the every instance of a string. ```ruby -"this thing that thing".gnix("thing") => "this that " +"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-" +"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" +"t^--^--^e^--^--^s^--^--^t^--^--^".unpollute #=> "test" +"t-e-s-t-".unpollute #=> "test" ``` ####Slugify:#### -Use the `slugify` method to generate a permalink-style string, with odd characters removed. +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" +"example".slugify #=> "example" +"example string".slugify #=> "example-string" +"Example string @@@ test!".slugify #=> "example-string-test" ``` ####Strip Tags:#### -Use the `strip_tags` method to remove HTML tags from a string. +Use the `strip_tags` and `strip_tags!` method to remove HTML tags from a string. ```ruby -"example".strip_tags => "example" -"<a href='http://example.com'>click</a>".strip_tags => "click" -"this is <b>bold</b> and <em>emphatic</em>".strip_tags => "this is bold and emphatic" +"example".strip_tags #=> "example" +"<a href='http://example.com'>click</a>".strip_tags #=> "click" +"this is <b>bold</b> and <em>emphatic</em>".strip_tags #=> "this is bold and emphatic" ``` ####Strip Whitespace:#### -Use the `strip_whitespace` method removes tab characters and instances of more than one space. +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" +"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. @@ -287,13 +408,13 @@ * 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 +++" +"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:#### @@ -302,12 +423,12 @@ **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" +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) | @@ -341,11 +462,11 @@ ####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" +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) | \ No newline at end of file