SassyStrings 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.md ADDED
@@ -0,0 +1,3 @@
1
+ # Changelog
2
+
3
+ * `1.0.0`: initial commit
data/README.md ADDED
@@ -0,0 +1,45 @@
1
+ # SassyStrings
2
+
3
+ Here is a [Compass Extension](http://compass-style.org/) providing you all functions you need to manipulate your [Sass](http://sass-lang.com/) strings.
4
+
5
+ ## What's in there?
6
+
7
+ * `char-at($string, $index)`: returns the character from `$string` at index `$index`
8
+ * `levenshtein($a, $b)`: returns the Levenshtein distance betwee `$a` and `$b`
9
+ * `str-count($string, $needle)`: counts number of occurrences of `$needle` in `$string`
10
+ * `str-ends-width($string, $needle)`: returns wether `$string` ends with `$needle`
11
+ * `str-explode($string, $separator)`: explodes `$string` on `$separator` occurrences
12
+ * `str-implode($list)`: implodes `$list` into a string
13
+ * `str-last-index($string, $needle)`: returns last index of `$needle` in `$string`
14
+ * `str-lcfirst($string)`: turns first letter of `$string` into lower case
15
+ * `str-pad($string, $length, $pad: " ", $direction: left)`: pads `$string` with `$pad` to match `$length` starting from `$direction`
16
+ * `str-printf($string, $elements...)`: replaces occurrences of `%s` in `$string` by elements from `$elements`
17
+ * `str-repeat($string, $times)`: repeats `$string` `$times` times
18
+ * `str-replace($string, $old, $new: "", $case-sensitive: true)`: replaces `$old` by `$new` in `$string` respecting `$case-sensitive`
19
+ * `str-reverse($string)`: reverses string
20
+ * `str-rot($string, $rot: 13)`: rotates letters in `$string` of `$rot` position in alphabet
21
+ * `str-shuffle($string)`: shuffles letters in string
22
+ * `str-split($string)`: splits `$string` into a list of characters
23
+ * `str-starts-width($string, $needle)`: returns wether `$string` starts with `$needle`
24
+ * `str-trim($string)`: removes white spaces before and after `$string`
25
+ * `str-ucfirst($string)`: turns first letter of `$string` into upper case
26
+ * `str-word-count($string)`: counts number of words in `$string`
27
+ * `stringify($literal)`: casts to stringify
28
+
29
+ As well as default Sass core functions:
30
+
31
+ * `str-index`
32
+ * `str-slice`
33
+ * `str-length`
34
+ * `str-insert`
35
+ * `to-lower-case`
36
+ * `to-upper-case`
37
+
38
+ If you feel like an explorer, you can have a look at the code [here](https://github.com/HugoGiraudel/SassyStrings/tree/master/stylesheets).
39
+
40
+ ## Requirements
41
+
42
+ * Sass ~> 3.3.0
43
+ * Compass ~> 1.0
44
+
45
+ Some functions depend on other functions. If you include functions individually, make sure to check for these dependencies in their respective docs.
@@ -0,0 +1,14 @@
1
+ require 'compass'
2
+
3
+ extension_path = File.expand_path(File.join(File.dirname(__FILE__), ".."))
4
+ Compass::Frameworks.register("SassyStrings", :path => "#{File.dirname(__FILE__)}/..")
5
+
6
+ module SassyStrings
7
+ VERSION = "0.0.1"
8
+ DATE = "2014-02-16"
9
+ end
10
+
11
+ # Sassy String Functions
12
+ module Sass::Script::Functions
13
+
14
+ end
@@ -0,0 +1,23 @@
1
+ // -------------------------------------------------------------------------------
2
+ // SassyStrings - A couple of advanced Sass string functions
3
+ // -------------------------------------------------------------------------------
4
+ // Repository: https://github.com/HugoGiraudel/SassyStrings/
5
+ // -------------------------------------------------------------------------------
6
+
7
+ @import "functions/char-at";
8
+ @import "functions/levenshtein";
9
+ @import "functions/str-count";
10
+ @import "functions/str-ends-with";
11
+ @import "functions/str-explode";
12
+ @import "functions/str-implode";
13
+ @import "functions/str-lcfirst";
14
+ @import "functions/str-pad";
15
+ @import "functions/str-repeat";
16
+ @import "functions/str-replace";
17
+ @import "functions/str-rot";
18
+ @import "functions/str-shuffle";
19
+ @import "functions/str-starts-with";
20
+ @import "functions/str-trim";
21
+ @import "functions/str-ucfirst";
22
+ @import "functions/str-word-count";
23
+ @import "functions/stringify";
@@ -0,0 +1,25 @@
1
+ // Return character from $string at $index
2
+ // ----------------------------------------------------------------------------------------------------
3
+ // @param [string] $string: string
4
+ // @param [string] $index: index to inspect
5
+ // ----------------------------------------------------------------------------------------------------
6
+ // @return [string]
7
+
8
+ @function char-at($string, $index) {
9
+ @if type-of($string) != "string" {
10
+ @warn "`char-at` function expecting a string for $string; #{type-of($string)} given.";
11
+ @return false;
12
+ }
13
+
14
+ @if type-of($index) != "number" {
15
+ @warn "`char-at` function expecting a number for $index; #{type-of($index)} given.";
16
+ @return false;
17
+ }
18
+
19
+ @if $index < 1 or $index > str-length($string) {
20
+ @warn "Out of bounds $index for `char-at`.";
21
+ @return false;
22
+ }
23
+
24
+ @return str-slice($string, $index, $index);
25
+ }
@@ -0,0 +1,99 @@
1
+ // Calculating Levenshtein distance between two strings
2
+ // ----------------------------------------------------------------------------------------------------
3
+ // @param $a: first string
4
+ // @param $b: second string
5
+ // ----------------------------------------------------------------------------------------------------
6
+ // @return [number]
7
+
8
+ @function levenshtein($a, $b) {
9
+ @if type-of($a) != "string" {
10
+ @warn "`str-count` function expecting a string for $a; #{type-of($a)} given.";
11
+ @return false;
12
+ }
13
+
14
+ @if type-of($b) != "string" {
15
+ @warn "`str-count` function expecting a string for $b; #{type-of($b)} given.";
16
+ @return false;
17
+ }
18
+
19
+ $a: to-lower-case($a);
20
+ $b: to-lower-case($b);
21
+
22
+ $n: str-length($a);
23
+ $m: str-length($b);
24
+
25
+ $matrix: _matrix($n + 1, $m + 1);
26
+ $cost: _matrix($n, $m);
27
+
28
+ @if $a == $b { @return 0; }
29
+ @if $n == 0 { @return $m; }
30
+ @if $m == 0 { @return $n; }
31
+
32
+ @for $i from 0 through $n {
33
+ @for $j from 0 through $m {
34
+ $v: if($i == 0, $j, if($j == 0, $i, 0));
35
+ @if $v != 0 {
36
+ $matrix: _set-matrix($matrix, $i + 1, $j + 1, $v);
37
+ }
38
+ @if $i != 0 and $j != 0 {
39
+ $v: if(str-slice($a, $i, $i) == str-slice($b, $j, $j), 0, 1);
40
+ @if $v != 0 {
41
+ $cost: _set-matrix($cost, $i, $j, $v);
42
+ }
43
+ }
44
+ }
45
+ }
46
+
47
+ @for $i from 2 through length($matrix) {
48
+ @for $j from 2 through length(nth($matrix, $i)) {
49
+ $matrix: _set-matrix($matrix, $i, $j, min(_e($matrix, $i - 1, $j) + 1, _e($matrix, $i, $j - 1) + 1, _e($matrix, $i - 1, $j - 1) + _e($cost, $i - 1, $j - 1)));
50
+ }
51
+ }
52
+
53
+ @return _e($matrix, length($matrix), length(nth($matrix, 1)));
54
+ }
55
+
56
+
57
+ // Helper to target an element in a matrix
58
+ // ----------------------------------------------------------------------------------------------------
59
+ // @param $m: matrix
60
+ // @param $x: x coord
61
+ // @param $y: y coord
62
+ // ----------------------------------------------------------------------------------------------------
63
+ // @return [literal]
64
+
65
+ @function _e($m, $x, $y) {
66
+ @return nth(nth($m, $x), $y);
67
+ }
68
+
69
+ // Helper instanciation a matrix of $x by $y
70
+ // ----------------------------------------------------------------------------------------------------
71
+ // @param $x: number of cols
72
+ // @param $y: number of lines
73
+ // ----------------------------------------------------------------------------------------------------
74
+ // @return [list]
75
+
76
+ @function _matrix($x, $y) {
77
+ $matrix: ();
78
+ @for $i from 1 through $x {
79
+ $tmp: ();
80
+ @for $y from 1 through $y {
81
+ $tmp: append($tmp, 0)
82
+ }
83
+ $matrix: append($matrix, $tmp);
84
+ }
85
+ @return $matrix;
86
+ }
87
+
88
+ // Helper assigning $value at $matrix[$x, $y]
89
+ // ----------------------------------------------------------------------------------------------------
90
+ // @param $matrix: matrix to update
91
+ // @param $x: x coord
92
+ // @param $y: y coord
93
+ // @param $value: value to assign at $matrix[$x, $y]
94
+ // ----------------------------------------------------------------------------------------------------
95
+ // @return [list]
96
+
97
+ @function _set-matrix($matrix, $x, $y, $value) {
98
+ @return set-nth($matrix, $x, set-nth(nth($matrix, $x), $y, $value));
99
+ }
@@ -0,0 +1,31 @@
1
+ // Count the number of occurrences of $needle in $string
2
+ // ----------------------------------------------------------------------------------------------------
3
+ // @param [string] $string: string
4
+ // @param [string] $needle: substring to count in $string
5
+ // ----------------------------------------------------------------------------------------------------
6
+ // @return [number] | false
7
+
8
+ @function str-count($string, $needle) {
9
+ @if type-of($string) != "string" {
10
+ @warn "`str-count` function expecting a string for $string; #{type-of($string)} given.";
11
+ @return false;
12
+ }
13
+
14
+ @if type-of($needle) != "string" {
15
+ @warn "`str-count` function expecting a string for $needle; #{type-of($needle)} given.";
16
+ @return false;
17
+ }
18
+
19
+ $index: str-index($string, $needle);
20
+ $result: if($index, 1, 0);
21
+
22
+ @if $index {
23
+ @for $i from $index + str-length($needle) through str-length($string) {
24
+ @if str-slice($string, $i, $i + str-length($needle) - 1) == $needle {
25
+ $result: $result + 1;
26
+ }
27
+ }
28
+ }
29
+
30
+ @return $result;
31
+ }
@@ -0,0 +1,20 @@
1
+ // Check whether $string ends with $needle
2
+ // ----------------------------------------------------------------------------------------------------
3
+ // @param [string] $string: string
4
+ // @param [string] $needle: substring to check
5
+ // ----------------------------------------------------------------------------------------------------
6
+ // @return [bool]
7
+
8
+ @function str-ends-with($string, $needle) {
9
+ @if type-of($string) != "string" {
10
+ @warn "`str-starts-with` function expecting a string for $string; #{type-of($string)} given.";
11
+ @return false;
12
+ }
13
+
14
+ @if type-of($needle) != "string" {
15
+ @warn "`str-starts-with` function expecting a string for $needle; #{type-of($needle)} given.";
16
+ @return false;
17
+ }
18
+
19
+ @return str-slice($string, -1 * str-length($needle)) == $needle;
20
+ }
@@ -0,0 +1,45 @@
1
+ // Split $string into several parts using $delimiter
2
+ // ----------------------------------------------------------------------------------------------------
3
+ // @param [string] $string: string
4
+ // @param [string] $delimiter: string to use as a delimiter to split $string
5
+ // ----------------------------------------------------------------------------------------------------
6
+ // @return [list] | false
7
+
8
+ @function str-explode($string, $delimiter: '') {
9
+ @if type-of($string) != "string" {
10
+ @warn "`explode` function expecting a string; #{type-of($string)} given.";
11
+ @return false;
12
+ }
13
+
14
+ @if type-of($delimiter) != "string" {
15
+ @warn "`explode` function expecting a string; #{type-of($delimiter)} given.";
16
+ @return false;
17
+ }
18
+
19
+ $result: ();
20
+ $length: str-length($string);
21
+
22
+ @if str-length($delimiter) == 0 {
23
+ @for $i from 1 through $length {
24
+ $result: append($result, str-slice($string, $i, $i));
25
+ }
26
+ @return $result;
27
+ }
28
+
29
+ $running: true;
30
+ $remaining: $string;
31
+
32
+ @while $running {
33
+ $index: str-index($remaining, $delimiter);
34
+ @if $index {
35
+ $slice: str-slice($remaining, 1, $index - 1);
36
+ $result: append($result, $slice);
37
+ $remaining: str-slice($remaining, $index + str-length($delimiter));
38
+ }
39
+ @else {
40
+ $running: false;
41
+ }
42
+ }
43
+
44
+ @return append($result, $remaining);
45
+ }
@@ -0,0 +1,15 @@
1
+ // Implode $list into a string
2
+ // ----------------------------------------------------------------------------------------------------
3
+ // @param [list] $list: list to convert to string
4
+ // ----------------------------------------------------------------------------------------------------
5
+ // @return [string]
6
+
7
+ @function str-implode($list) {
8
+ $result: "";
9
+
10
+ @each $item in $list {
11
+ $result: $result + if(length($item) > 1, str-implode($item), $item);
12
+ }
13
+
14
+ @return $result;
15
+ }
@@ -0,0 +1,30 @@
1
+ // Return last index of $needle in $string
2
+ // ----------------------------------------------------------------------------------------------------
3
+ // @param [string] $string: string
4
+ // ----------------------------------------------------------------------------------------------------
5
+ // @return [number] | null | false
6
+
7
+ @function str-last-index($string, $needle) {
8
+ @if type-of($string) != "string" {
9
+ @warn "`str-last-index` function expecting a string for $string; #{type-of($string)} given.";
10
+ @return false;
11
+ }
12
+
13
+ @if type-of($needle) != "string" {
14
+ @warn "`str-last-index` function expecting a string for $needle; #{type-of($needle)} given.";
15
+ @return false;
16
+ }
17
+
18
+ $index: str-index($string, $needle);
19
+ $result: $index;
20
+
21
+ @if $index {
22
+ @for $i from $index + str-length($needle) through str-length($string) {
23
+ @if str-slice($string, $i, $i + str-length($needle) - 1) == $needle {
24
+ $result: $i;
25
+ }
26
+ }
27
+ }
28
+
29
+ @return $result;
30
+ }
@@ -0,0 +1,14 @@
1
+ // Lower case first character of $string
2
+ // ----------------------------------------------------------------------------------------------------
3
+ // @param [string] $string: string
4
+ // ----------------------------------------------------------------------------------------------------
5
+ // @return [string]
6
+
7
+ @function str-lcfirst($string) {
8
+ @if type-of($string) != "string" {
9
+ @warn "`str-lcfirst` function expecting a string for $string; #{type-of($string)} given.";
10
+ @return false;
11
+ }
12
+
13
+ @return to-lower-case(str-slice($string, 1, 1)) + str-slice($string, 2);
14
+ }
@@ -0,0 +1,45 @@
1
+ // Pad $string from $direction with $pad to reach $length characters
2
+ // ----------------------------------------------------------------------------------------------------
3
+ // @param [string] $string: string
4
+ // @param [number] $length: number of characters to go for in returned string
5
+ // @param [string] $pad: string to use to pad $string
6
+ // @param [string] $direction: direction left or right for padding
7
+ // ----------------------------------------------------------------------------------------------------
8
+ // @return [string] | false
9
+
10
+ @function str-pad($string, $length, $pad: " ", $direction: left) {
11
+ @if type-of($string) != "string" {
12
+ @warn "`str-pad` function expecting a string for $string; #{type-of($string)} given.";
13
+ @return false;
14
+ }
15
+
16
+ @if type-of($pad) != "string" {
17
+ @warn "`str-pad` function expecting a string for $pad; #{type-of($pad)} given.";
18
+ @return false;
19
+ }
20
+
21
+ @if type-of($length) != "number" {
22
+ @warn "`str-pad` function expecting a number for $length; #{type-of($length)} given.";
23
+ @return false;
24
+ }
25
+
26
+ $direction: if(index(left right, $direction), $direction, left);
27
+ $new-string: $string;
28
+ $index: 1;
29
+
30
+ @if $length > str-length($string) {
31
+ @while str-length($new-string) < $length {
32
+ $remains: $length - str-length($new-string);
33
+ $pad: if($remains < str-length($pad), str-slice($pad, 1, $remains), $pad);
34
+ @if $direction == left {
35
+ $new-string: str-insert($new-string, $pad, $index);
36
+ $index: $index + str-length($pad);
37
+ }
38
+ @else {
39
+ $new-string: $new-string + $pad;
40
+ }
41
+ }
42
+ }
43
+
44
+ @return $new-string;
45
+ }
@@ -0,0 +1,28 @@
1
+ // Replace occurrences of %s in $string by $elements
2
+ // ----------------------------------------------------------------------------------------------------
3
+ // @param [string] $string: string
4
+ // @param [arglist] $elements: strings to use for replacements in %s
5
+ // ----------------------------------------------------------------------------------------------------
6
+ // @return [string] | false
7
+
8
+ @function str-printf($string, $elements...) {
9
+ @if type-of($string) != "string" {
10
+ @warn "`str-printf` function expecting a string for $string; #{type-of($string)} given.";
11
+ @return false;
12
+ }
13
+
14
+ $breaker: '%s';
15
+ $result: $string;
16
+
17
+ @for $i from 1 through length($elements) {
18
+ $index: str-index($result, $breaker);
19
+ @if $index {
20
+ $result: str-slice($result, 1, $index - 1) + stringify(nth($elements, $i)) + str-slice($result, $index + str-length($breaker));
21
+ }
22
+ @else {
23
+ @return $result;
24
+ }
25
+ }
26
+
27
+ @return $result;
28
+ }
@@ -0,0 +1,26 @@
1
+ // Repeat $string $times times
2
+ // ----------------------------------------------------------------------------------------------------
3
+ // @param [string] $string: string
4
+ // @param [number] $times: number of times to repeat $string
5
+ // ----------------------------------------------------------------------------------------------------
6
+ // @return [string] | false
7
+
8
+ @function str-repeat($string, $times) {
9
+ @if type-of($string) != "string" {
10
+ @warn "`str-repeat` function expecting a string for $string; #{type-of($string)} given.";
11
+ @return false;
12
+ }
13
+
14
+ @if type-of($times) != "number" {
15
+ @warn "`str-repeat` function expecting a number for $times; #{type-of($times)} given.";
16
+ @return false;
17
+ }
18
+
19
+ $result: "";
20
+
21
+ @for $i from 1 through $times {
22
+ $result: $result + $string;
23
+ }
24
+
25
+ @return $result;
26
+ }
@@ -0,0 +1,47 @@
1
+ // Replace $old occurrences by $new in $string respecting $case-sensitive
2
+ // ----------------------------------------------------------------------------------------------------
3
+ // @param [string] $string: string
4
+ // @param [string] $old: old substring to replace by $new
5
+ // @param [string] $new: new substring to replace $old
6
+ // @param [bool] $case-sensitive: case-sensitivity
7
+ // ----------------------------------------------------------------------------------------------------
8
+ // @return [string] | false
9
+
10
+ @function str-replace($string, $old, $new: '', $case-sensitive: true) {
11
+ @if type-of($string) != "string" {
12
+ @warn "`str-replace` function expecting a string for $string; #{type-of($string)} given.";
13
+ @return false;
14
+ }
15
+
16
+ @if type-of($old) != "string" {
17
+ @warn "`str-replace` function expecting a string for $old; #{type-of($old)} given.";
18
+ @return false;
19
+ }
20
+
21
+ @if type-of($new) != "string" {
22
+ @warn "`str-replace` function expecting a string for $new; #{type-of($new)} given.";
23
+ @return false;
24
+ }
25
+
26
+ @if str-index($new, $old) {
27
+ @warn "The string to be replaced is contained in the new string. Infinite recursion avoided.";
28
+ @return $string;
29
+ }
30
+
31
+ $index: if(not $case-sensitive, str-index(to-lower-case($string), to-lower-case($old)), str-index($string, $old));
32
+
33
+ @if $index and $new != $old {
34
+ $result: if($index != 1, quote(str-slice($string, 1, $index - 1)), '');
35
+
36
+ @for $i from $index through str-length($string) {
37
+ @if $i < $index or $i >= $index + str-length($old) {
38
+ $result: $result + str-slice($string, $i, $i);
39
+ }
40
+ }
41
+
42
+ @return quote(str-replace(str-insert($result, $new, $index), $old, $new, $case-sensitive));
43
+ }
44
+
45
+ @return $string;
46
+ }
47
+
@@ -0,0 +1,20 @@
1
+ // Reverse $string
2
+ // ----------------------------------------------------------------------------------------------------
3
+ // @param [string] $string: string
4
+ // ----------------------------------------------------------------------------------------------------
5
+ // @return [string] | false
6
+
7
+ @function str-reverse($string) {
8
+ @if type-of($string) != "string" {
9
+ @warn "`str-reverse` function expecting a string for $string; #{type-of($string)} given.";
10
+ @return false;
11
+ }
12
+
13
+ $result: "";
14
+
15
+ @for $i from str-length($string) * -1 through -1 {
16
+ $result: $result + str-slice($string, abs($i), abs($i));
17
+ }
18
+
19
+ @return $result;
20
+ }
@@ -0,0 +1,43 @@
1
+ // Rotate all characters from the alphabet in $string by $rot positions
2
+ // ----------------------------------------------------------------------------------------------------
3
+ // @param [string] $string: string
4
+ // @param [number] $rot: number of positions to switch in alphabet
5
+ // ----------------------------------------------------------------------------------------------------
6
+ // @return [string] | false
7
+
8
+ @function str-rot($string, $rot: 13) {
9
+ @if type-of($string) != "string" {
10
+ @warn "`str-rot` function expecting a string for $string; #{type-of($string)} given.";
11
+ @return false;
12
+ }
13
+
14
+ @if type-of($rot) != "number" {
15
+ @warn "`str-rot` function expecting a number for $rot; #{type-of($rot)} given.";
16
+ @return false;
17
+ }
18
+
19
+ $alphabet: a b c d e f g h i j k l m n o p q r s t u v w x y z;
20
+ $result: "";
21
+ $rot: if(type-of($rot) == "number", floor($rot), 13);
22
+
23
+ @for $i from 1 through str-length($string) {
24
+ $char: str-slice($string, $i, $i);
25
+ $index: index($alphabet, to-lower-case($char));
26
+ $is-caps: $index and (index($alphabet, to-lower-case($char)) != index($alphabet, $char));
27
+ $new-char: if($index,
28
+ if($index + $rot > length($alphabet),
29
+ nth($alphabet, $index + $rot - length($alphabet)),
30
+ nth($alphabet, $index + $rot)
31
+ ),
32
+ $char
33
+ );
34
+
35
+ @if $is-caps {
36
+ $new-char: to-upper-case($new-char);
37
+ }
38
+
39
+ $result: $result + $new-char;
40
+ }
41
+
42
+ @return $result;
43
+ }
@@ -0,0 +1,26 @@
1
+ // Shuffle characters from $string
2
+ // ----------------------------------------------------------------------------------------------------
3
+ // @param [string] $string: string
4
+ // ----------------------------------------------------------------------------------------------------
5
+ // @return [string]
6
+
7
+ @function str-shuffle($string) {
8
+ @return str-implode(_shuffle(str-explode($string)));
9
+ }
10
+
11
+ // Shuffle a list
12
+ // ----------------------------------------------------------------------------------------------------
13
+ // @param [list] $list: shuffle a list
14
+ // ----------------------------------------------------------------------------------------------------
15
+ // @return [list]
16
+
17
+ @function _shuffle($list) {
18
+ @for $i from -1 * length($list) through -1 {
19
+ $i: abs($i);
20
+ $j: random(length($list) - 1) + 1;
21
+ $tmp: nth($list, $i);
22
+ $list: set-nth($list, $i, nth($list, $j));
23
+ $list: set-nth($list, $j, $tmp);
24
+ }
25
+ @return $list;
26
+ }
@@ -0,0 +1,20 @@
1
+ // Check whether $string stars with $needle
2
+ // ----------------------------------------------------------------------------------------------------
3
+ // @param [string] $string: string
4
+ // @param [string] $needle: substring to check
5
+ // ----------------------------------------------------------------------------------------------------
6
+ // @return [bool] | false
7
+
8
+ @function str-starts-with($string, $needle) {
9
+ @if type-of($string) != "string" {
10
+ @warn "`str-starts-with` function expecting a string for $string; #{type-of($string)} given.";
11
+ @return false;
12
+ }
13
+
14
+ @if type-of($needle) != "string" {
15
+ @warn "`str-starts-with` function expecting a string for $needle; #{type-of($needle)} given.";
16
+ @return false;
17
+ }
18
+
19
+ @return str-slice($string, 1, str-length($needle)) == $needle;
20
+ }
@@ -0,0 +1,30 @@
1
+ // Remove all trailing and leading whitespaces from $string
2
+ // ----------------------------------------------------------------------------------------------------
3
+ // @param [string] $string: string
4
+ // ----------------------------------------------------------------------------------------------------
5
+ // @return [string] | false
6
+
7
+ @function str-trim($string) {
8
+ @if type-of($string) != "string" {
9
+ @warn "`str-trim` function expecting a string for $string; #{type-of($string)} given.";
10
+ @return false;
11
+ }
12
+
13
+ $start: 1;
14
+ $end: str-length($string);
15
+
16
+ @for $i from 1 through str-length($string) {
17
+ $first: str-slice($string, $i, $i);
18
+ $last: str-slice($string, -$i, -$i);
19
+
20
+ @if $first == " " and $i + 1 == $start + 1 {
21
+ $start: $i + 1;
22
+ }
23
+
24
+ @if $last == " " and str-length($string) - $i == $end - 1 {
25
+ $end: str-length($string) - $i;
26
+ }
27
+ }
28
+
29
+ @return str-slice($string, $start, $end);
30
+ }
@@ -0,0 +1,14 @@
1
+ // Capitalize first letter from $strign
2
+ // ----------------------------------------------------------------------------------------------------
3
+ // @param [string] $string: string
4
+ // ----------------------------------------------------------------------------------------------------
5
+ // @return [string] | false
6
+
7
+ @function str-ucfirst($string) {
8
+ @if type-of($string) != "string" {
9
+ @warn "`str-ucfirst` function expecting a string for $string; #{type-of($string)} given.";
10
+ @return false;
11
+ }
12
+
13
+ @return to-upper-case(str-slice($string, 1, 1)) + str-slice($string, 2);
14
+ }
@@ -0,0 +1,26 @@
1
+ // Count number of words in $string
2
+ // ----------------------------------------------------------------------------------------------------
3
+ // @param [string] $string: string
4
+ // ----------------------------------------------------------------------------------------------------
5
+ // @return [string] | false
6
+
7
+ @function str-word-count($string) {
8
+ @if type-of($string) != "string" {
9
+ @warn "`str-word-count` function expecting a string for $string; #{type-of($string)} given.";
10
+ @return false;
11
+ }
12
+
13
+ $string: str-trim($string);
14
+ $words: ();
15
+ $i: str-length($string);
16
+
17
+ @while $i > 0 {
18
+ @if str-slice($string, $i, $i) == " " {
19
+ $words: append($words, str-slice($string, $i + 1));
20
+ $string: str-slice($string, 1, $i - 1);
21
+ }
22
+ $i: $i - 1;
23
+ }
24
+
25
+ @return length(append($words, $string));
26
+ }
@@ -0,0 +1,21 @@
1
+ // Cast anything to string
2
+ // ----------------------------------------------------------------------------------------------------
3
+ // @param [literal] $literal: literal to cast to string
4
+ // ----------------------------------------------------------------------------------------------------
5
+ // @return [string]
6
+
7
+ @function stringify($literal) {
8
+ $result: "";
9
+
10
+ @if length($literal) == 1 {
11
+ $result: $literal + unquote("");
12
+ }
13
+
14
+ @else {
15
+ @each $item in $literal {
16
+ $result: $result + stringify($item);
17
+ }
18
+ }
19
+
20
+ @return quote($result);
21
+ }
metadata ADDED
@@ -0,0 +1,101 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: SassyStrings
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Hugo Giraudel
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2014-02-16 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: sass
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '3.3'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '3.3'
30
+ - !ruby/object:Gem::Dependency
31
+ name: compass
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: 1.0.0
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: 1.0.0
46
+ description: A collection of Sass functions to manipulate strings.
47
+ email:
48
+ - hugo.giraudel@gmail.com
49
+ executables: []
50
+ extensions: []
51
+ extra_rdoc_files: []
52
+ files:
53
+ - README.md
54
+ - CHANGELOG.md
55
+ - lib/SassyStrings.rb
56
+ - stylesheets/functions/_char-at.scss
57
+ - stylesheets/functions/_levenshtein.scss
58
+ - stylesheets/functions/_str-count.scss
59
+ - stylesheets/functions/_str-ends-with.scss
60
+ - stylesheets/functions/_str-explode.scss
61
+ - stylesheets/functions/_str-implode.scss
62
+ - stylesheets/functions/_str-last-index.scss
63
+ - stylesheets/functions/_str-lcfirst.scss
64
+ - stylesheets/functions/_str-pad.scss
65
+ - stylesheets/functions/_str-printf.scss
66
+ - stylesheets/functions/_str-repeat.scss
67
+ - stylesheets/functions/_str-replace.scss
68
+ - stylesheets/functions/_str-reverse.scss
69
+ - stylesheets/functions/_str-rot.scss
70
+ - stylesheets/functions/_str-shuffle.scss
71
+ - stylesheets/functions/_str-starts-with.scss
72
+ - stylesheets/functions/_str-trim.scss
73
+ - stylesheets/functions/_str-ucfirst.scss
74
+ - stylesheets/functions/_str-word-count.scss
75
+ - stylesheets/functions/_stringify.scss
76
+ - stylesheets/_SassyStrings.scss
77
+ homepage: https://github.com/HugoGiraudel/SassyStrings
78
+ licenses: []
79
+ post_install_message:
80
+ rdoc_options: []
81
+ require_paths:
82
+ - lib
83
+ required_ruby_version: !ruby/object:Gem::Requirement
84
+ none: false
85
+ requirements:
86
+ - - ! '>='
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ required_rubygems_version: !ruby/object:Gem::Requirement
90
+ none: false
91
+ requirements:
92
+ - - ! '>='
93
+ - !ruby/object:Gem::Version
94
+ version: '1.2'
95
+ requirements: []
96
+ rubyforge_project: SassyStrings
97
+ rubygems_version: 1.8.24
98
+ signing_key:
99
+ specification_version: 3
100
+ summary: Advanced String handling for Sass
101
+ test_files: []