@first-ch/tools-mcp 0.4.1 → 0.24.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +52 -4
- package/aspect-ratio.mjs +706 -0
- package/base64.mjs +356 -0
- package/color.mjs +629 -0
- package/cron.mjs +633 -0
- package/diff.mjs +322 -0
- package/hash.mjs +234 -0
- package/html-escape.mjs +308 -0
- package/json-yaml.mjs +1693 -0
- package/jwt.mjs +639 -0
- package/lib.mjs +103 -0
- package/markdown-table.mjs +641 -0
- package/marp-theme.css +51 -0
- package/marp.mjs +219 -0
- package/package.json +130 -3
- package/px-rem.mjs +488 -0
- package/qr.mjs +744 -0
- package/server.mjs +942 -2
- package/sql-format.mjs +694 -0
- package/testdata.mjs +608 -0
- package/unixtime.mjs +505 -0
- package/url.mjs +415 -0
- package/user-agent.mjs +633 -0
- package/uuid.mjs +238 -0
package/url.mjs
ADDED
|
@@ -0,0 +1,415 @@
|
|
|
1
|
+
/* URLパラメータの分解・編集・再構築(url_params)
|
|
2
|
+
* 解析コアは FirstCHTools の site/url/app.js と同一
|
|
3
|
+
* (2箇所ルール: site側が正本・片方を直したらもう片方も同じ内容で反映する)。
|
|
4
|
+
*
|
|
5
|
+
* URL() は通さず、先頭から `#` → `?` の順に切って分解する。相対パスでも
|
|
6
|
+
* 壊れたパーセントエンコードでも例外にせず読める範囲まで分解でき、
|
|
7
|
+
* 編集していない行は生の文字列のまま書き戻すので、何も指定せずに
|
|
8
|
+
* 再構築したURLは入力と1バイトも変わらない(署名付きURLを壊さない)。 */
|
|
9
|
+
|
|
10
|
+
export class UrlParamsError extends Error {}
|
|
11
|
+
|
|
12
|
+
// 手動タグ付けで使うUTMパラメータ(GA4が既定で解釈する6種)
|
|
13
|
+
export const UTM_KEYS = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_content', 'utm_term', 'utm_id'];
|
|
14
|
+
|
|
15
|
+
// 広告プラットフォームが自動で付けるクリックID。共有・計測には不要で、消しても遷移先は変わらない
|
|
16
|
+
export const CLICK_IDS = [
|
|
17
|
+
'gclid', 'dclid', 'gbraid', 'wbraid', 'gad_source', 'gclsrc',
|
|
18
|
+
'fbclid', 'msclkid', 'ttclid', 'twclid', 'yclid', 'li_fat_id',
|
|
19
|
+
'igshid', 'mc_cid', 'mc_eid', 'vero_id', '_hsenc', '_hsmi', 'oly_enc_id', 'oly_anon_id',
|
|
20
|
+
];
|
|
21
|
+
|
|
22
|
+
// URLに載っていると事故になりうるキー(共有前に外す前提のもの)
|
|
23
|
+
const SECRET_KEY = /^(access[_-]?token|id[_-]?token|refresh[_-]?token|token|session[_-]?id|session|sid|auth|authorization|api[_-]?key|apikey|client[_-]?secret|secret|password|passwd|pwd|signature|sig)$/i;
|
|
24
|
+
|
|
25
|
+
const BAD_PERCENT = /%(?![0-9a-fA-F]{2})/;
|
|
26
|
+
const HAS_NON_ASCII = /[^\x00-\x7f]/;
|
|
27
|
+
|
|
28
|
+
/* ---- 文字列のエンコード / デコード ---- */
|
|
29
|
+
|
|
30
|
+
// パーセントエンコードをバイト単位で解く(%XX が非UTF-8のバイトでも例外にしない)
|
|
31
|
+
export function lenientDecode(str) {
|
|
32
|
+
const src = new TextEncoder().encode(String(str));
|
|
33
|
+
const out = [];
|
|
34
|
+
for (let i = 0; i < src.length; i++) {
|
|
35
|
+
if (src[i] === 0x25 && i + 2 < src.length) {
|
|
36
|
+
const hex = String.fromCharCode(src[i + 1], src[i + 2]);
|
|
37
|
+
if (/^[0-9a-f]{2}$/i.test(hex)) {
|
|
38
|
+
out.push(parseInt(hex, 16));
|
|
39
|
+
i += 2;
|
|
40
|
+
continue;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
out.push(src[i]);
|
|
44
|
+
}
|
|
45
|
+
return new TextDecoder('utf-8').decode(new Uint8Array(out));
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// クエリの1トークンを読める文字列へ戻す。壊れた %XX が混じっていても投げない
|
|
49
|
+
// (壊れたURLの中身を見るのがこのツールの用途なので、例外で止めない)。
|
|
50
|
+
// クエリ文字列では + は歴史的にスペースを表すため、既定で空白へ戻す。
|
|
51
|
+
export function decodeComponent(s, plusAsSpace) {
|
|
52
|
+
let t = String(s == null ? '' : s);
|
|
53
|
+
if (plusAsSpace !== false) t = t.replace(/\+/g, ' ');
|
|
54
|
+
try {
|
|
55
|
+
return decodeURIComponent(t);
|
|
56
|
+
} catch (e) {
|
|
57
|
+
return lenientDecode(t);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// 値をクエリに書き戻せる形へ。spaceAsPlus のときだけ %20 を + に寄せる
|
|
62
|
+
export function encodeComponent(s, spaceAsPlus) {
|
|
63
|
+
const e = encodeURIComponent(String(s == null ? '' : s));
|
|
64
|
+
return spaceAsPlus ? e.replace(/%20/g, '+') : e;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/* ---- URLの分解 ---- */
|
|
68
|
+
|
|
69
|
+
// 先頭から順に # → ? で切る。URL() を通さないので、相対パスでも壊れたURLでも必ず分解でき、
|
|
70
|
+
// 触っていない部分は1バイトも変えずに再構築できる。
|
|
71
|
+
export function splitUrl(input) {
|
|
72
|
+
const s = String(input == null ? '' : input).trim();
|
|
73
|
+
const hi = s.indexOf('#');
|
|
74
|
+
const hasHash = hi >= 0;
|
|
75
|
+
const hash = hasHash ? s.slice(hi + 1) : '';
|
|
76
|
+
const noHash = hasHash ? s.slice(0, hi) : s;
|
|
77
|
+
const qi = noHash.indexOf('?');
|
|
78
|
+
const hasQuery = qi >= 0;
|
|
79
|
+
return {
|
|
80
|
+
base: hasQuery ? noHash.slice(0, qi) : noHash,
|
|
81
|
+
query: hasQuery ? noHash.slice(qi + 1) : '',
|
|
82
|
+
hash,
|
|
83
|
+
hasQuery,
|
|
84
|
+
hasHash,
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
// 表示用にスキーム・ホスト・パスへ割る。スキームが無いときだけ https を仮定する
|
|
89
|
+
// (`example.com/a?b=1` のような貼り付けを受けるため)。相対パスなら null。
|
|
90
|
+
export function urlParts(base) {
|
|
91
|
+
const t = String(base == null ? '' : base).trim();
|
|
92
|
+
if (!t) return null;
|
|
93
|
+
let u = null;
|
|
94
|
+
let assumed = false;
|
|
95
|
+
try {
|
|
96
|
+
u = new URL(t);
|
|
97
|
+
} catch (e) { /* 絶対URLではない */ }
|
|
98
|
+
// `/path` は相対パスなので補わない(`https:///path` は余分なスラッシュが詰められて
|
|
99
|
+
// ホスト名 path になってしまう)。ホストらしい先頭要素があるときだけ補う。
|
|
100
|
+
if (!u && !/^[a-zA-Z][a-zA-Z0-9+.\-]*:/.test(t) && !t.startsWith('/')) {
|
|
101
|
+
const authority = t.split(/[/?#]/)[0];
|
|
102
|
+
if (/\./.test(authority) || /^localhost(:\d+)?$/i.test(authority)) {
|
|
103
|
+
try {
|
|
104
|
+
u = new URL('https://' + t);
|
|
105
|
+
assumed = true;
|
|
106
|
+
} catch (e) { /* ホストとしても読めない */ }
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
if (!u) return null;
|
|
110
|
+
if (assumed && !u.hostname) return null;
|
|
111
|
+
return {
|
|
112
|
+
protocol: u.protocol.replace(/:$/, ''),
|
|
113
|
+
username: u.username,
|
|
114
|
+
hasPassword: !!u.password,
|
|
115
|
+
host: u.host,
|
|
116
|
+
hostname: u.hostname,
|
|
117
|
+
port: u.port,
|
|
118
|
+
pathname: u.pathname,
|
|
119
|
+
assumedProtocol: assumed,
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/* ---- クエリパラメータ ---- */
|
|
124
|
+
|
|
125
|
+
// `a=1&b&c=2` を行の配列へ。生の文字列(rawKey/rawValue)と、
|
|
126
|
+
// 解析時点でのデコード結果(origKey/origValue)を両方持たせる。
|
|
127
|
+
// 再構築のとき「呼び出し側が触っていない行は生のまま出す」判定に使う。
|
|
128
|
+
export function parseQuery(query) {
|
|
129
|
+
const rows = [];
|
|
130
|
+
const q = String(query == null ? '' : query);
|
|
131
|
+
if (!q) return rows;
|
|
132
|
+
for (const token of q.split('&')) {
|
|
133
|
+
if (token === '') continue; // `&&` や末尾の `&` は空トークンなので捨てる
|
|
134
|
+
const eq = token.indexOf('=');
|
|
135
|
+
const rawKey = eq >= 0 ? token.slice(0, eq) : token;
|
|
136
|
+
const rawValue = eq >= 0 ? token.slice(eq + 1) : '';
|
|
137
|
+
const key = decodeComponent(rawKey);
|
|
138
|
+
const value = decodeComponent(rawValue);
|
|
139
|
+
rows.push({ key, value, rawKey, rawValue, origKey: key, origValue: value, hasEq: eq >= 0, on: true });
|
|
140
|
+
}
|
|
141
|
+
return rows;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
// 行の配列 → クエリ文字列。既定では未編集の行を生のまま出すので、
|
|
145
|
+
// 何も触らずに再構築したURLは入力と1バイトも変わらない。
|
|
146
|
+
export function buildQuery(rows, opts) {
|
|
147
|
+
const { reencode = false, spaceAsPlus = false } = opts || {};
|
|
148
|
+
const parts = [];
|
|
149
|
+
for (const r of rows || []) {
|
|
150
|
+
if (r.on === false) continue;
|
|
151
|
+
if (r.key === '' && r.value === '') continue;
|
|
152
|
+
const keep = !reencode;
|
|
153
|
+
const k = keep && r.rawKey != null && r.key === r.origKey ? r.rawKey : encodeComponent(r.key, spaceAsPlus);
|
|
154
|
+
const v = keep && r.rawValue != null && r.value === r.origValue ? r.rawValue : encodeComponent(r.value, spaceAsPlus);
|
|
155
|
+
parts.push(r.hasEq === false && v === '' ? k : k + '=' + v);
|
|
156
|
+
}
|
|
157
|
+
return parts.join('&');
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
export function buildUrl(parts) {
|
|
161
|
+
const { base = '', query = '', hash = '', hasHash = false } = parts || {};
|
|
162
|
+
return base + (query ? '?' + query : '') + (hash || hasHash ? '#' + hash : '');
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/* ---- パラメータの操作 ---- */
|
|
166
|
+
|
|
167
|
+
const findRow = (rows, key) => rows.findIndex((r) => r.on !== false && r.key === key);
|
|
168
|
+
|
|
169
|
+
// 既にあるキーは最初の1件を上書きし、無ければ末尾へ足す。値が空文字なら削除。
|
|
170
|
+
export function setParam(rows, key, value) {
|
|
171
|
+
const i = findRow(rows, key);
|
|
172
|
+
if (value === '' || value == null) {
|
|
173
|
+
if (i >= 0) rows.splice(i, 1);
|
|
174
|
+
return rows;
|
|
175
|
+
}
|
|
176
|
+
if (i >= 0) {
|
|
177
|
+
rows[i].value = String(value);
|
|
178
|
+
rows[i].hasEq = true;
|
|
179
|
+
} else {
|
|
180
|
+
rows.push({ key: String(key), value: String(value), rawKey: null, rawValue: null, origKey: null, origValue: null, hasEq: true, on: true });
|
|
181
|
+
}
|
|
182
|
+
return rows;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
export function removeParams(rows, keys) {
|
|
186
|
+
const drop = new Set(keys.map((k) => String(k).toLowerCase()));
|
|
187
|
+
return rows.filter((r) => !drop.has(String(r.key).toLowerCase()));
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
export function getUtm(rows) {
|
|
191
|
+
const out = {};
|
|
192
|
+
for (const k of UTM_KEYS) {
|
|
193
|
+
const i = findRow(rows, k);
|
|
194
|
+
if (i >= 0) out[k] = rows[i].value;
|
|
195
|
+
}
|
|
196
|
+
return out;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
/* ---- 診断 ---- */
|
|
200
|
+
|
|
201
|
+
// 返すのはコード+対象キーだけにして、文言は呼び出し側(サイトは日英・MCPは日本語)で当てる
|
|
202
|
+
export function analyzeUrl(state) {
|
|
203
|
+
const { rows = [], parts = null, url = '', hasHash = false, hash = '' } = state || {};
|
|
204
|
+
const live = rows.filter((r) => r.on !== false && !(r.key === '' && r.value === ''));
|
|
205
|
+
const warns = [];
|
|
206
|
+
const add = (code, level, keys) => warns.push({ code, level, keys: keys || [] });
|
|
207
|
+
|
|
208
|
+
// 重複キー
|
|
209
|
+
const seen = new Map();
|
|
210
|
+
const dup = [];
|
|
211
|
+
for (const r of live) {
|
|
212
|
+
if (seen.has(r.key)) {
|
|
213
|
+
if (!dup.includes(r.key)) dup.push(r.key);
|
|
214
|
+
} else seen.set(r.key, true);
|
|
215
|
+
}
|
|
216
|
+
if (dup.length) add('dup', 'warn', dup);
|
|
217
|
+
|
|
218
|
+
// 生の文字列の作り
|
|
219
|
+
const rawSpace = [];
|
|
220
|
+
const plusSpace = [];
|
|
221
|
+
const nonAscii = [];
|
|
222
|
+
const badPct = [];
|
|
223
|
+
const emptyVal = [];
|
|
224
|
+
for (const r of live) {
|
|
225
|
+
const raw = (r.rawKey || '') + (r.rawValue || '');
|
|
226
|
+
if (r.rawValue != null || r.rawKey != null) {
|
|
227
|
+
if (/ /.test(raw)) rawSpace.push(r.key);
|
|
228
|
+
if (/\+/.test(raw)) plusSpace.push(r.key);
|
|
229
|
+
if (HAS_NON_ASCII.test(raw)) nonAscii.push(r.key);
|
|
230
|
+
if (BAD_PERCENT.test(raw)) badPct.push(r.key);
|
|
231
|
+
}
|
|
232
|
+
if (r.hasEq !== false && r.value === '') emptyVal.push(r.key);
|
|
233
|
+
}
|
|
234
|
+
if (badPct.length) add('bad_percent', 'warn', badPct);
|
|
235
|
+
if (rawSpace.length) add('raw_space', 'warn', rawSpace);
|
|
236
|
+
if (nonAscii.length) add('raw_non_ascii', 'warn', nonAscii);
|
|
237
|
+
if (plusSpace.length) add('plus_space', 'info', plusSpace);
|
|
238
|
+
if (emptyVal.length) add('empty_value', 'info', emptyVal);
|
|
239
|
+
|
|
240
|
+
// 秘匿・追跡系
|
|
241
|
+
const secrets = live.filter((r) => SECRET_KEY.test(r.key)).map((r) => r.key);
|
|
242
|
+
if (secrets.length) add('secret', 'warn', secrets);
|
|
243
|
+
const clicks = live.filter((r) => CLICK_IDS.includes(String(r.key).toLowerCase())).map((r) => r.key);
|
|
244
|
+
if (clicks.length) add('click_id', 'info', clicks);
|
|
245
|
+
|
|
246
|
+
// UTM
|
|
247
|
+
const utm = getUtm(live);
|
|
248
|
+
const utmSet = Object.keys(utm).filter((k) => utm[k] !== '');
|
|
249
|
+
if (utmSet.length) {
|
|
250
|
+
const upper = utmSet.filter((k) => /[A-Z]/.test(utm[k]));
|
|
251
|
+
if (upper.length) add('utm_case', 'warn', upper);
|
|
252
|
+
const spaced = utmSet.filter((k) => /\s/.test(utm[k]));
|
|
253
|
+
if (spaced.length) add('utm_space', 'warn', spaced);
|
|
254
|
+
if (!utm.utm_source && (utm.utm_medium || utm.utm_campaign)) add('utm_no_source', 'warn', []);
|
|
255
|
+
if (utm.utm_source && !utm.utm_medium) add('utm_no_medium', 'warn', []);
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
// 構造
|
|
259
|
+
if (parts && parts.assumedProtocol) add('no_protocol', 'info', []);
|
|
260
|
+
if (parts && parts.protocol === 'http') add('http', 'info', []);
|
|
261
|
+
if (parts && parts.hasPassword) add('userinfo', 'warn', []);
|
|
262
|
+
if (hasHash && /[?=]/.test(hash)) add('hash_query', 'info', []);
|
|
263
|
+
if (url.length > 2000) add('long_url', 'warn', []);
|
|
264
|
+
|
|
265
|
+
return warns;
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
/* ---- ここから下はMCP向けの薄い層(Web版のUIに相当する部分) ---- */
|
|
269
|
+
|
|
270
|
+
const WARN_JA = {
|
|
271
|
+
dup: (k) => `同じキーが複数あります: ${k}。多くのサーバーはどちらか一方だけを採用します(どちらが残るかは実装依存)。`,
|
|
272
|
+
bad_percent: (k) => `${k} のパーセントエンコードが壊れています(% の後ろが16進2桁になっていません)。値は読める範囲までデコードしています。`,
|
|
273
|
+
raw_space: (k) => `${k} にエンコードされていない半角スペースが残っています。チャットやメールに貼るとその位置でリンクが切れます。`,
|
|
274
|
+
raw_non_ascii: (k) => `${k} に非ASCII文字がそのまま入っています。ブラウザは補正しますが、APIクライアントやログ解析では壊れることがあります。`,
|
|
275
|
+
plus_space: (k) => `${k} に + が含まれます。クエリ文字列の + はスペースとして解釈されます(記号としての + は %2B)。`,
|
|
276
|
+
empty_value: (k) => `値が空のパラメータがあります: ${k}。空文字として扱う実装と、未指定として扱う実装があります。`,
|
|
277
|
+
secret: (k) => `${k} は認証情報らしいキーです。URLの値はブラウザ履歴・Referer・アクセスログに残ります。共有しないでください。`,
|
|
278
|
+
click_id: (k) => `広告のクリックIDが残っています: ${k}。広告クリック直後にだけ意味がある値なので、共有・再利用の前に削除してください。`,
|
|
279
|
+
utm_case: (k) => `${k} に大文字が含まれます。GA4はutmの値の大文字小文字を区別するため、Google と google が別の参照元になります。`,
|
|
280
|
+
utm_space: (k) => `${k} にスペースが含まれます。- か _ に置き換えてください。`,
|
|
281
|
+
utm_no_source: () => 'utm_source がありません。GA4は source と medium の組で判定するため、セッションが「(not set)」になります。',
|
|
282
|
+
utm_no_medium: () => 'utm_medium がありません。チャネルグループに割り当てられず「(not set)」として集計されます。',
|
|
283
|
+
no_protocol: () => 'スキームが無いため、分解の表示では https:// を補っています。再構築されるURLは入力したままの形です。',
|
|
284
|
+
http: () => 'http:// のURLです。クエリパラメータは暗号化されずに流れます。',
|
|
285
|
+
userinfo: () => 'URLにパスワードが埋め込まれています(user:pass@host)。ログに残るので外してください。',
|
|
286
|
+
hash_query: () => 'フラグメント(#以降)にもパラメータらしき文字列があります。フラグメントはサーバーへ送信されません。',
|
|
287
|
+
long_url: () => '2,000文字を超えています。プロキシ・メールクライアント・古いサーバーで切り詰められることがあります。',
|
|
288
|
+
};
|
|
289
|
+
|
|
290
|
+
function encodeWith(scheme, s) {
|
|
291
|
+
if (scheme === 'uri') return encodeURI(s);
|
|
292
|
+
if (scheme === 'form') return encodeURIComponent(s).replace(/%20/g, '+');
|
|
293
|
+
return encodeURIComponent(s);
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
function decodeWith(scheme, s) {
|
|
297
|
+
if (scheme === 'uri') {
|
|
298
|
+
try {
|
|
299
|
+
return decodeURI(s);
|
|
300
|
+
} catch (e) {
|
|
301
|
+
return lenientDecode(s);
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
return decodeComponent(s, scheme === 'form');
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
/**
|
|
308
|
+
* URLの分解・編集・再構築、または文字列単体のURLエンコード/デコード。
|
|
309
|
+
* mode="parse"(既定)は url が必須。mode="encode"/"decode" は text が必須。
|
|
310
|
+
*/
|
|
311
|
+
export function urlParams(opts = {}) {
|
|
312
|
+
const {
|
|
313
|
+
url,
|
|
314
|
+
mode = 'parse',
|
|
315
|
+
text,
|
|
316
|
+
scheme = 'component',
|
|
317
|
+
set = null,
|
|
318
|
+
remove = null,
|
|
319
|
+
utm = null,
|
|
320
|
+
sort = false,
|
|
321
|
+
removeTracking = false,
|
|
322
|
+
reencode = false,
|
|
323
|
+
spaceAsPlus = false,
|
|
324
|
+
} = opts;
|
|
325
|
+
|
|
326
|
+
if (!['parse', 'encode', 'decode'].includes(mode)) {
|
|
327
|
+
throw new UrlParamsError(`mode は parse / encode / decode のいずれか(受け取った値: ${mode})`);
|
|
328
|
+
}
|
|
329
|
+
if (!['component', 'uri', 'form'].includes(scheme)) {
|
|
330
|
+
throw new UrlParamsError(`scheme は component / uri / form のいずれか(受け取った値: ${scheme})`);
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
if (mode === 'encode' || mode === 'decode') {
|
|
334
|
+
if (typeof text !== 'string') throw new UrlParamsError('mode="' + mode + '" では text(文字列)が必要');
|
|
335
|
+
const out = mode === 'encode' ? encodeWith(scheme, text) : decodeWith(scheme, text);
|
|
336
|
+
return { mode, scheme, input: text, output: out, length: out.length };
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
if (typeof url !== 'string' || url.trim() === '') throw new UrlParamsError('url(分解するURL)が必要');
|
|
340
|
+
|
|
341
|
+
const s = splitUrl(url);
|
|
342
|
+
let rows = parseQuery(s.query);
|
|
343
|
+
const removed = [];
|
|
344
|
+
|
|
345
|
+
if (removeTracking) {
|
|
346
|
+
const before = rows.length;
|
|
347
|
+
const hit = rows.filter((r) => CLICK_IDS.includes(String(r.key).toLowerCase())).map((r) => r.key);
|
|
348
|
+
rows = removeParams(rows, CLICK_IDS);
|
|
349
|
+
if (rows.length !== before) removed.push(...hit);
|
|
350
|
+
}
|
|
351
|
+
if (Array.isArray(remove) && remove.length) {
|
|
352
|
+
const hit = rows.filter((r) => remove.some((k) => String(k).toLowerCase() === String(r.key).toLowerCase())).map((r) => r.key);
|
|
353
|
+
rows = removeParams(rows, remove);
|
|
354
|
+
removed.push(...hit);
|
|
355
|
+
}
|
|
356
|
+
if (set && typeof set === 'object') {
|
|
357
|
+
for (const [k, v] of Object.entries(set)) setParam(rows, k, v == null ? '' : String(v));
|
|
358
|
+
}
|
|
359
|
+
if (utm && typeof utm === 'object') {
|
|
360
|
+
for (const [k, v] of Object.entries(utm)) {
|
|
361
|
+
// source / utm_source のどちらの書き方も受ける
|
|
362
|
+
const key = UTM_KEYS.includes(k) ? k : 'utm_' + k;
|
|
363
|
+
if (!UTM_KEYS.includes(key)) throw new UrlParamsError(`utm のキーが不正: ${k}(使えるのは ${UTM_KEYS.join(' / ')})`);
|
|
364
|
+
setParam(rows, key, v == null ? '' : String(v));
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
if (sort) rows.sort((a, b) => (a.key < b.key ? -1 : a.key > b.key ? 1 : 0));
|
|
368
|
+
|
|
369
|
+
const query = buildQuery(rows, { reencode, spaceAsPlus });
|
|
370
|
+
const rebuilt = buildUrl({ base: s.base, query, hash: s.hash, hasHash: s.hasHash });
|
|
371
|
+
const parts = urlParts(s.base);
|
|
372
|
+
const warns = analyzeUrl({ rows, parts, url: rebuilt, hasHash: s.hasHash, hash: s.hash });
|
|
373
|
+
const live = rows.filter((r) => r.on !== false && !(r.key === '' && r.value === ''));
|
|
374
|
+
const seen = new Set();
|
|
375
|
+
let duplicates = 0;
|
|
376
|
+
for (const r of live) {
|
|
377
|
+
if (seen.has(r.key)) duplicates++;
|
|
378
|
+
else seen.add(r.key);
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
return {
|
|
382
|
+
mode: 'parse',
|
|
383
|
+
input: String(url).trim(),
|
|
384
|
+
url: rebuilt,
|
|
385
|
+
changed: rebuilt !== String(url).trim(),
|
|
386
|
+
protocol: parts ? parts.protocol : null,
|
|
387
|
+
host: parts ? parts.host : null,
|
|
388
|
+
hostname: parts ? parts.hostname : null,
|
|
389
|
+
port: parts && parts.port ? parts.port : null,
|
|
390
|
+
path: parts ? parts.pathname : null,
|
|
391
|
+
assumed_protocol: parts ? parts.assumedProtocol : false,
|
|
392
|
+
hash: s.hasHash ? s.hash : null,
|
|
393
|
+
params: live.map((r) => ({
|
|
394
|
+
key: r.key,
|
|
395
|
+
value: r.value,
|
|
396
|
+
raw_key: r.rawKey,
|
|
397
|
+
raw_value: r.rawValue,
|
|
398
|
+
value_only_key: r.hasEq === false,
|
|
399
|
+
})),
|
|
400
|
+
utm: getUtm(live),
|
|
401
|
+
removed,
|
|
402
|
+
warnings: warns.map((w) => ({
|
|
403
|
+
code: w.code,
|
|
404
|
+
level: w.level,
|
|
405
|
+
keys: w.keys,
|
|
406
|
+
message_ja: (WARN_JA[w.code] || (() => w.code))(w.keys.join(', ')),
|
|
407
|
+
})),
|
|
408
|
+
stats: {
|
|
409
|
+
params: live.length,
|
|
410
|
+
duplicates,
|
|
411
|
+
url_length: rebuilt.length,
|
|
412
|
+
query_length: query.length,
|
|
413
|
+
},
|
|
414
|
+
};
|
|
415
|
+
}
|