Module: Sass::Script::Functions
- Defined in:
- lib/sassy-strings.rb
Overview
Sassy String Functions
Instance Method Summary collapse
-
#split_string(string, key) ⇒ Sass::Script::List
Split a string into a list using a given key.
-
#str_extract(string, start_at, end_at = nil) ⇒ Sass::Script::String
Extract a substring from ‘string` from `start` index to `end` index.
-
#str_index(string, substring) ⇒ Sass::Script::String
Starting at the left, finds the index of the first location where ‘substring` is found in `string`.
-
#str_insert(original, insert, index) ⇒ Sass::Script::String
inserts a string into another string.
-
#str_length(string) ⇒ Sass::Script::Number
Returns the number of characters in a string.
-
#str_pos(needle, haystack) ⇒ Sass::Script::Number
Find the position of a substring within a string.
- #str_replace(string, find, replace) ⇒ Object
- #str_split(string, key) ⇒ Object
-
#str_to_number(string) ⇒ Object
Converts a String to a Number.
-
#to_lower_case(string) ⇒ Sass::Script::String
Convert a string to lower case.
- #to_lowercase(string) ⇒ Object
-
#to_upper_case(string) ⇒ Sass::Script::String
Convert a string to upper case.
- #to_uppercase(string) ⇒ Object
Instance Method Details
#split_string(string, key) ⇒ Sass::Script::List
Split a string into a list using a given key
14 15 16 17 18 19 20 21 |
# File 'lib/sassy-strings.rb', line 14 def split_string(string, key) items = string.value.split(key.value) if items.count == 1 Sass::Script::Bool.new(false) else Sass::Script::List.new(items.map{|i| Sass::Script::String.new(i)}, :comma) end end |
#str_extract(string, start_at, end_at = nil) ⇒ Sass::Script::String
Extract a substring from ‘string` from `start` index to `end` index.
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/sassy-strings.rb', line 158 def str_extract(string, start_at, end_at = nil) assert_type string, :String assert_type start_at, :Number unless start_at.unitless? raise ArgumentError.new("#{start_at.inspect} is not a unitless number") end if end_at.nil? if start_at.value < 0 end_at = start_at start_at = Sass::Script::Number.new(1) else end_at = Sass::Script::Number.new(-1) end end assert_type end_at, :Number unless end_at.unitless? raise ArgumentError.new("#{end_at.inspect} is not a unitless number") end s = start_at.value > 0 ? start_at.value - 1 : start_at.value e = end_at.value > 0 ? end_at.value - 1 : end_at.value extracted = string.value.slice(s..e) Sass::Script::String.new(extracted || "", string.type) end |
#str_index(string, substring) ⇒ Sass::Script::String
Starting at the left, finds the index of the first location where ‘substring` is found in `string`.
130 131 132 133 134 135 |
# File 'lib/sassy-strings.rb', line 130 def str_index(string, substring) assert_type string, :String assert_type substring, :String index = string.value.index(substring.value) || -1 Sass::Script::Number.new(index + 1) end |
#str_insert(original, insert, index) ⇒ Sass::Script::String
inserts a string into another string
Inserts the ‘insert` string before the character at the given index. Negative indices count from the end of the string. The inserted string will starts at the given index.
93 94 95 96 97 98 99 100 101 102 |
# File 'lib/sassy-strings.rb', line 93 def str_insert(original, insert, index) assert_type original, :String assert_type insert, :String assert_type index, :Number unless index.unitless? raise ArgumentError.new("#{index.inspect} is not a unitless number") end insertion_point = index.value > 0 ? [index.value - 1, original.value.size].min : [index.value, -original.value.size - 1].max Sass::Script::String.new(original.value.dup.insert(insertion_point, insert.value), original.type) end |
#str_length(string) ⇒ Sass::Script::Number
Returns the number of characters in a string.
72 73 74 75 |
# File 'lib/sassy-strings.rb', line 72 def str_length(string) assert_type string, :String Sass::Script::Number.new(string.value.size) end |
#str_pos(needle, haystack) ⇒ Sass::Script::Number
Find the position of a substring within a string
43 44 45 46 47 48 49 |
# File 'lib/sassy-strings.rb', line 43 def str_pos(needle, haystack) if haystack.value.to_s.index(needle.value.to_s) Sass::Script::Number.new(haystack.value.to_s.index(needle.value.to_s)) else Sass::Script::Number.new(-1) end end |
#str_replace(string, find, replace) ⇒ Object
112 113 114 115 116 |
# File 'lib/sassy-strings.rb', line 112 def str_replace(string, find, replace) assert_type string, :String assert_type replace, :String Sass::Script::String.new(string.value.gsub(find.value,replace.value), string.type) end |
#str_split(string, key) ⇒ Object
23 24 25 26 27 28 29 30 |
# File 'lib/sassy-strings.rb', line 23 def str_split(string, key) items = string.value.split(key.value) if items.count == 1 Sass::Script::Bool.new(false) else Sass::Script::List.new(items.map{|i| Sass::Script::String.new(i)}, :comma) end end |
#str_to_number(string) ⇒ Object
Converts a String to a Number
54 55 56 57 58 59 60 61 62 |
# File 'lib/sassy-strings.rb', line 54 def str_to_number(string) result = Sass::Script::Parser.parse(string.value, string.line || 0, 0) case result when Sass::Script::Number result else raise Sass::SyntaxError, "#{string.to_sass} is not a number" end end |
#to_lower_case(string) ⇒ Sass::Script::String
Convert a string to lower case
208 209 210 211 |
# File 'lib/sassy-strings.rb', line 208 def to_lower_case(string) assert_type string, :String Sass::Script::String.new(string.value.downcase, string.type) end |
#to_lowercase(string) ⇒ Object
212 213 214 215 |
# File 'lib/sassy-strings.rb', line 212 def to_lowercase(string) assert_type string, :String Sass::Script::String.new(string.value.downcase, string.type) end |
#to_upper_case(string) ⇒ Sass::Script::String
Convert a string to upper case
189 190 191 192 |
# File 'lib/sassy-strings.rb', line 189 def to_upper_case(string) assert_type string, :String Sass::Script::String.new(string.value.upcase, string.type) end |
#to_uppercase(string) ⇒ Object
194 195 196 197 |
# File 'lib/sassy-strings.rb', line 194 def to_uppercase(string) assert_type string, :String Sass::Script::String.new(string.value.upcase, string.type) end |