string.replace-multiple()

The string.replace-multiple() function handles multiple string replaces, using string.replace() function.

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

// The `string.replace-multiple()` function.
@function replace-multiple($string, $replaces...) {
  @each $occurrence, $substring, $replacement in $replaces {
    $string: string.replace($string, $occurrence, $substring, $replacement);
  }
  @return $string;
}
https://github.com/angular-package/sass/blob/main/string/_string.replace-multiple.function.scss
// Modules.
@use 'string.replace.function';

// Status: DONE
// The `string.replace-multiple()` function handles multiple string replaces, using `string.replace()` function.
// @param `$string` The string to do multiple `$replaces`.
// @arbitrary `$replaces...` Arbitrary replaces (occurrence, substring, replacement) to do on `$string`.
// @return The return value is a `string` with done `$replaces`.
@function replace-multiple($string, $replaces...) {
  @each $occurrence, $substring, $replacement in $replaces {
    $string: string.replace($string, $occurrence, $substring, $replacement);
  }
  @return $string;
}

// Examples.
// single replacement first occurrence
// @debug replace-multiple('bold king is hairy', first 'bold' 'baloon'); // baloon king is hairy
// @debug replace-multiple('bold king is hairy', first 'bold' ''); //  king is  hairy
// @debug replace-multiple(':==', first ':' '');

// single replacement all occurrences
// @debug replace-multiple('bold king is bold hairy', all 'bold' ''); //  king is  hairy
// @debug replace-multiple('bold king is bold hairy', all 'bold' 'test'); // test king is test hairy

// multiple replacements
// @debug replace-multiple('bold king is hairy', first (bold is) ''); //  king  hairy
// @debug replace-multiple('bold king is hairy', first bold baloon, first king baloon); // baloon baloon is hairy
// @debug replace-multiple('bold king is hairy', first (bold king) baloon, first (is hairy) bold); // baloon baloon bold bold
// @debug replace-multiple('atmosphere bold bold bold king is hairy', first (king bold) baloon); // atmosphere baloon bold bold baloon is hairy
// @debug replace-multiple('atmosphere bold bold bold king is hairy', all (king bold) baloon); // atmosphere baloon baloon baloon baloon is hairy

// multiple replacements different occurrences
// @debug replace-multiple('atmosphere bold bold bold king is hairy', first bold clean, all (king bold) baloon); // atmosphere clean baloon baloon baloon is hairy
string.multiple-replaces()

Parameters

$string

The string to do multiple $replaces.

$replaces...

Arbitrary replaces (occurrence, substring, replacement) to do on $string.

Return

The return value is a string with done $replaces.

Examples

// Use.
@use '@angular-package/sass/string';

// Examples.
// single replacement first occurrence
@debug string.replace-multiple('bold king is hairy', first 'bold' 'baloon'); // baloon king is hairy
@debug string.replace-multiple('bold king is hairy', first 'bold' ''); //  king is  hairy
@debug string.replace-multiple(':==', first ':' '');

// single replacement all occurrences
@debug string.replace-multiple('bold king is bold hairy', all 'bold' ''); //  king is  hairy
@debug string.replace-multiple('bold king is bold hairy', all 'bold' 'test'); // test king is test hairy

// multiple replacements
@debug string.replace-multiple('bold king is hairy', first (bold is) ''); //  king  hairy
@debug string.replace-multiple('bold king is hairy', first bold baloon, first king baloon); // baloon baloon is hairy
@debug string.replace-multiple('bold king is hairy', first (bold king) baloon, first (is hairy) bold); // baloon baloon bold bold
@debug string.replace-multiple('atmosphere bold bold bold king is hairy', first (king bold) baloon); // atmosphere baloon bold bold baloon is hairy
@debug string.replace-multiple('atmosphere bold bold bold king is hairy', all (king bold) baloon); // atmosphere baloon baloon baloon baloon is hairy

// multiple replacements different occurrences
@debug string.replace-multiple('atmosphere bold bold bold king is hairy', first bold clean, all (king bold) baloon); // atmosphere clean baloon baloon baloon is hairy

Last updated

Was this helpful?