スネークケース ⇔ キャメルケースの変換
概要
スネークケースとキャメルケースを相互変換するためのマクロです。
スネークケースをキャメルケースに変換
#title="スネークケースをキャメルケースに変換"
/**
* スネークケースをキャメルケースに変換するためのマクロ
*/
// カーソル位置とウィンドウ位置を保存
var pointX = document.selection.GetActivePointX(mePosLogical);
var pointY = document.selection.GetActivePointY(mePosLogical);
var scrollX = window.ScrollX;
var scrollY = window.ScrollY;
// テキストを選択していない場合は全選択
if (document.selection.Text == "") {
document.selection.SelectAll();
}
// 選択中のテキストを取得
var selectionText = document.selection.Text;
// キャメルケースに置換
selectionText = selectionText.replace(/(_)([a-z])/g, function(s) {
return s.slice(1).toUpperCase();
});
// 置換後の文字列を設定
document.selection.Text = selectionText;
// カーソル位置とウィンドウ位置を復元
document.selection.SetActivePoint(mePosLogical, pointX, pointY, false);
window.ScrollX = scrollX;
window.ScrollY = scrollY;
キャメルケースをスネークケースに変換
#title="キャメルケースをスネークケースに変換"
/**
* キャメルケースをスネークケースに変換するためのマクロ
*/
// カーソル位置とウィンドウ位置を保存
var pointX = document.selection.GetActivePointX(mePosLogical);
var pointY = document.selection.GetActivePointY(mePosLogical);
var scrollX = window.ScrollX;
var scrollY = window.ScrollY;
// テキストを選択していない場合は全選択
if (document.selection.Text == "") {
document.selection.SelectAll();
}
// 選択中のテキストを取得
var selectionText = document.selection.Text;
// キャメルケースに置換
selectionText = selectionText.replace(/([A-Z])/g, function(s) {
return '_' + s.toLowerCase();
});
// 置換後の文字列を設定
document.selection.Text = selectionText;
// カーソル位置とウィンドウ位置を復元
document.selection.SetActivePoint(mePosLogical, pointX, pointY, false);
window.ScrollX = scrollX;
window.ScrollY = scrollY;
更新履歴
- 2019/03/03 初版
スポンサーリンク