> For the complete documentation index, see [llms.txt](https://yamoo9.gitbook.io/scss-mixins/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://yamoo9.gitbook.io/scss-mixins/utilities/string/str-extract-count-keyword.md).

# str-extract-count-keyword()

카운트, 키워드 추출 유틸리티 함수

## 용도 <a href="#use" id="use"></a>

키워드와 카운트 횟수를 포함하는 문자를 분석해 카운트, 키워드를 맵으로 반환 받아야 할 경우 사용합니다.

> 예시 : "header x3"   →   (count: 3, keyword: "header")

## 사용법 <a href="#usage" id="usage"></a>

`str-extract-count-keyword()` 함수에 반복할 숫자, 반복할 문자를 전달합니다.

> str-extract-count-keyword($tex&#x74;**:string**) → **map**

```javascript
@debug str-extract-count-keyword("main x3"); // (count: 3, keyword: 'main') 반환
```

| 매개변수(parameter) | 유형(type) | 필수(required) | 기본 값(default) |
| :-------------: | :------: | :----------: | :-----------: |
|      $text      |  string  |      ✔︎      |               |

## 로직 <a href="#logic" id="logic"></a>

`str-extract-count-keyword()` 유틸리티는 다음의 로직에 의해 작성되었습니다.&#x20;

```javascript
@function str-extract-count-keyword($text: null) {
  $index: str-index($text, 'x');

  @if $index {
    $keyword: str-replace(str-slice($text, 1, $index - 1), ' ');
    $count: str-slice($text, $index + 1);
    @return (count: str-to-num($count), keyword: $keyword);
  } @else {
    @return $text;
  }
}
```

1. 전달 받은 인자 값에서 `x` 값의 인덱스 추출
2. 인덱스 값이 존재할 경우, 키워드 및 카운트 값을 추출해 맵으로 반환
3. 인덱스 값이 존재하지 않을 경우, 전달 받은 텍스트 그대로 반환

## 참고 <a href="#reference" id="reference"></a>

유틸리티 함수 로직에 사용된 Sass 빌트인 모듈은 다음과 같습니다.

* [@function](https://sass-lang.com/documentation/at-rules/function)
* [@if / @else](https://sass-lang.com/documentation/at-rules/control/if)
* [str-index()](https://sass-lang.com/documentation/modules/string#index)
* [str-slice()](https://sass-lang.com/documentation/modules/string#slice)

유틸리티 함수 로직에 사용된 자체 제작 모듈은 다음과 같습니다.

* [str-replace()](/scss-mixins/utilities/string/str-replace.md)
* [str-to-num()](/scss-mixins/utilities/string/str-to-num.md)
