文字列から前後の空白、URL エンコード、引用符を取り除きます。
| 引数 |
説明 |
型 |
string |
整えたあとの文字列 |
string |
| パラメーター |
説明 |
型 |
既定値 |
str |
整える文字列 |
string |
必須 |
options |
設定のオプション |
ClearStrOption |
{} |
| パラメーター |
説明 |
型 |
既定値 |
urlencoded |
URL デコードを行うかどうか |
boolean |
true |
import { clearStr } from 'ranuts';
const str = ' "hello world" ';
const cleaned = clearStr(str);
console.log(cleaned); // 'hello world'
import { clearStr } from 'ranuts';
const encoded = ' "hello%20world" ';
const cleaned = clearStr(encoded);
console.log(cleaned); // 'hello world'(自動でデコードされます)
import { clearStr } from 'ranuts';
const str = ' "hello%20world" ';
const cleaned = clearStr(str, { urlencoded: false });
console.log(cleaned); // 'hello%20world'(デコードされません)
import { clearStr } from 'ranuts';
const str1 = "'test'";
const str2 = '"test"';
console.log(clearStr(str1)); // 'test'
console.log(clearStr(str2)); // 'test'
- 取り除くもの:前後の空白、シングルクォート、ダブルクォートを取り除きます。
- URL デコード:既定では URL デコードを行い、
urlencoded: false で止められます。
- 使いどころ:ユーザーの入力や、URL のパラメータから取り出した値を整えるのによく使われます。