Skip to content

Commit

Permalink
feat(list replace): add list.replace() and list.replace-string().
Browse files Browse the repository at this point in the history
  • Loading branch information
sciborrudnicki committed Jul 18, 2023
1 parent 16bdb40 commit 66fdaef
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 0 deletions.
2 changes: 2 additions & 0 deletions list/_index.scss
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
@forward 'list.limit.function';
@forward 'list.nth.function';
@forward 'list.range.function';
@forward 'list.replace-string.function';
@forward 'list.replace.function';
@forward 'list.to.function';
@forward 'list.type.function';

Expand Down
26 changes: 26 additions & 0 deletions list/_list.replace-string.function.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Sass.
@use 'sass:list';

// Modules.
@use '../string';

// Status: DONE
// The `list.replace-string()` function returns `$list` with replaced all substrings `$replace` by `$replacement`.
// @param `$list` A list in which `$replace` substring in the elements of `string` type are replaced by `$replacement`.
// @param `$replace` A substring to replace with `$replacement` in the `$list` elements of the `string` type.
// @param `$replacement` The string to replace `$replace` in the `string` type elements of `$list`.
// @return The return value is `$list` where substring `$replace` of `string` type elements are replaced with `$replacement`.
@function replace-string($list, $replace, $replacement) {
@if type-of($list) == list and list.length($list) > 0 {
@for $i from 1 through list.length($list) {
@if type-of(list.nth($list, $i)) == string {
$list: list.set-nth($list, $i, string.replace(list.nth($list, $i), all, $replace, $replacement));
}
}
}
@return $list;
}

// Example usage
// @debug replace-string(('__PARENT__1123', 'aaaa__PARENT__1123', 'sdfsdfsd-dasdsad__PARENT__1123'), __PARENT__, '&'); // "&1123", "aaaa&1123", "sdfsdfsd-dasdsad&1123"
// @debug replace-string((test, test1, test2), test1, something); // test, something, test2
23 changes: 23 additions & 0 deletions list/_list.replace.function.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Sass.
@use 'sass:list';

// Status:
// The `list.replace()` function returns `$list` with elements of `$replace` value replaced by `$replacement`.
// @param `$list` A list in which elements are elements `$replace` are replaced by `$replacement`.
// @param `$replace` The element's value is replaced with `$replacement` in `$list`.
// @param `$replacement` The replacement for `$replace` elements in `$list`.
// @return The return value is `$list` where elements of `$replace` value are replaced with `$replacement`.
@function replace($list, $replace, $replacement) {
@if type-of($list) == list and list.length($list) > 0 {
@for $i from 1 through list.length($list) {
@if list.nth($list, $i) == $replace {
$list: list.set-nth($list, $i, $replacement);
}
}
}
@return $list;
}

// Example usage
// @debug replace(('__PARENT__1123', 'aaaa__PARENT__1123', 'sdfsdfsd-dasdsad__PARENT__1123'), __PARENT__, '&'); // "__PARENT__1123", "aaaa__PARENT__1123", "sdfsdfsd-dasdsad__PARENT__1123"
// @debug replace((test, test1, test2), test1, something aaa); // test, something aaa, test2

0 comments on commit 66fdaef

Please sign in to comment.