-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(list replace): add
list.replace()
and list.replace-string()
.
- Loading branch information
1 parent
16bdb40
commit 66fdaef
Showing
3 changed files
with
51 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |