Skip to content

Instantly share code, notes, and snippets.

@tjbenton
Last active August 29, 2015 14:06
Show Gist options
  • Save tjbenton/e51ba66b077377f5d6ad to your computer and use it in GitHub Desktop.
Save tjbenton/e51ba66b077377f5d6ad to your computer and use it in GitHub Desktop.
Generated by SassMeister.com.
// ----
// Sass (v3.4.4)
// Compass (v1.0.1)
// ----
@function list-slice($list, $start, $end: length($list)){
$new-list: ();
@if $start <= length($list){
@for $i from $start through $end{
$new-list: append($new-list, nth($list, $i), list-separator($list));
}
}
@return $new-list;
}
// @author Tyler Benton
// @description
// This function will recursively replace text in a string.
// **Note:** You can replace the `$to-replace` text with the same character(s)
//
// @arg {string} $string
// @arg {list | string} $to-replace - what you want to replace
// @arg {string} $to-insert - what you want to insert
// @arg {boolean} $case-sensitive (false)
// @arg {number} $start (0) - The index you want to start at.
//
// @requires list-slice()
//
// @returns {string} - With replaced text
@function str-replace($string, $to-replace, $to-insert: "", $case-sensitive: false, $start: 0){
// set $to-replace to be a list if it isn't a list already and store it for later
$to-replace-list: if(type-of($to-replace) != list, ($to-replace), $to-replace);
// reset $to-replace to be the first item in the list
$to-replace: nth($to-replace-list, 1);
// error handling
@if $string == "" or type-of($string) != string{
@error "#{$string} is not a valid string";
}@else if $to-replace == "" or type-of($to-replace) != string{
@error "#{$to-replace} is not a valid string";
}
// @else if str-length($string) < $start{
// @error "$string is less than starting point";
// }
// text to parse
$to-parse: str-slice($string, if($start != 0, $start + 1, 0));
// find the next index
$index: str-index(
if($case-sensitive, $to-parse, to-lower-case($to-parse)),
if($case-sensitive, $to-replace, to-lower-case($to-replace))
);
@return if($index == null,
// if true remove the first item from the $to-replace-list and keep going if the list is greater than 1 else return the string
if(length($to-replace-list) <= 1,
$string,
str-replace($string, list-slice($to-replace-list, 2), $to-insert, $case-sensitive, 0)
),
str-replace(
// string *before* index + $to-insert + string *after* index
str-slice($string, 0, $index + $start - 1) + $to-insert + str-slice($string, $index + $start + str-length($to-replace)),
$to-replace-list,
$to-insert,
$case-sensitive,
$index + $start + str-length($to-insert)
)
);
}
/* ====== Testing ================================= */
$string: "NO TOUCHING! I'm going to buy you the single healthiest call girl this town has ever seen. I said no touching!";
.str-replace{
/* original */
content: $string;
/* Replace `e` with `E`*/
content: str-replace($string, "e", "E");
/* replace `y` with `#yyyyyyy#` */
content: str-replace($string, "y", "#yyyyyyy#");
/* $case-sensitive is false */
content: str-replace($string, "NO TOUCHING", "YES");
/* $case-sensitive is true */
content: str-replace($string, "NO TOUCHING" "no", "YES", true);
}
/* ====== Testing ================================= */
.str-replace {
/* original */
content: "NO TOUCHING! I'm going to buy you the single healthiest call girl this town has ever seen. I said no touching!";
/* Replace `e` with `E`*/
content: "NO TOUCHING! I'm going to buy you thE singlE hEalthiEst call girl this town has EvEr sEen. I said no touching!";
/* replace `y` with `#yyyyyyy#` */
content: "NO TOUCHING! I'm going to bu#yyyyyyy# #yyyyyyy#ou the single healthiest call girl this town has ever seen. I said no touching!";
/* $case-sensitive is false */
content: "YES! I'm going to buy you the single healthiest call girl this town has ever seen. I said YES!";
/* $case-sensitive is true */
content: "YES! I'm going to buy you the single healthiest call girl this town has ever seen. I said YES touching!";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment