Module: Sass::Script::Functions

Defined in:
lib/sassy-strings.rb

Overview

Sassy String Functions

Instance Method Summary collapse

Instance Method Details

#split_string(string, key) ⇒ Sass::Script::List

Split a string into a list using a given key

Examples:

split-string('Hello World', ' ') => ('Hello', 'World')
str-split('Hello World', ' ') => ('Hello', 'World')

Returns:

  • (Sass::Script::List)

Raises:

  • (ArgumentError)

    if `string` isn't a string

  • (ArgumentError)

    if `key` isn't a 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.

Examples:

str-extract(abcd,2,3)    => bc
str-extract(abcd,2)      => cd
str-extract(abcd,-2)     => abc
str-extract(abcd,2,-2)   => bc
str-extract(abcd,3,-3)   => unquote("")
str-extract("abcd",3,-3) => ""
str-extract(abcd,1,1)    => a
str-extract(abcd,1,2)    => ab
str-extract(abcd,1,4)    => abcd
str-extract(abcd,-100,4) => abcd
str-extract(abcd,1,100)  => abcd
str-extract(abcd,2,1)    => unquote("")
str-extract("abcd",2,3)  => "bc"

Returns:

  • (Sass::Script::String)

Raises:

  • (ArgumentError)

    if `string` isn't a string or `start` and `end` aren't unitless numbers


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`.

Examples:

str-index(abcd, a)  => 1
str-index(abcd, ab) => 1
str-index(abcd, X)  => 0
str-index(abcd, c)  => 3

Returns:

  • (Sass::Script::String)

Raises:

  • (ArgumentError)

    if `original` isn't a string, `insert` isn't a string, or `index` isn't a number.


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.

Examples:

str-insert("abcd", "X", 1) => "Xabcd"
str-insert("abcd", "X", 4) => "abcXd"
str-insert("abcd", "X", 100) => "abcdX"
str-insert("abcd", "X", -100) => "Xabcd"
str-insert("abcd", "X", -4) => "aXbcd"
str-insert("abcd", "X", -1) => "abcdX"

Returns:

  • (Sass::Script::String)

Raises:

  • (ArgumentError)

    if `original` isn't a string, `insert` isn't a string, or `index` isn't a number.


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.

Examples:

str-length("foo") => 3

Returns:

  • (Sass::Script::Number)

Raises:

  • (ArgumentError)

    if `string` isn't 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

Examples:

str-pos('ello', 'hello') => 1
str-pos('a', 'hello') => -1

Returns:

  • (Sass::Script::Number)

Raises:

  • (ArgumentError)

    if `needle` isn't a string

  • (ArgumentError)

    if `haystack` isn't a key


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

Examples:

str_replace(abcd, a, zzz)  => zzzbcd

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

Examples:

to-lower-case(ABCD) => abcd
to-lower-case("ABCD") => "abcd"

Returns:

  • (Sass::Script::String)

Raises:

  • (ArgumentError)

    if `string` isn't a string


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

Examples:

to-upper-case(abcd) => ABCD
to-upper-case("abcd") => "ABCD"

Returns:

  • (Sass::Script::String)

Raises:

  • (ArgumentError)

    if `string` isn't a string


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