SCALE — Build Lab
開発パターン · JAVASCRIPT PATTERN

ペースト一括追加(改行区切り)

CATEGORY開発パターン TYPEJavaScript Pattern EFFORT30〜90分 DIFFICULTY
PRIMARY CODE
js
function parsePastedLines(text) {
  return text.split(/\r?\n/)
    .map(s => s.trim())
    .filter(Boolean);
}

function bulkAdd(collection, pastedText, makeItem) {
  const lines = parsePastedLines(pastedText);
  const existing = PD(collection, []);
  const existingKeys = new Set(existing.map(x => x.key));
  const added = lines
    .map(line => makeItem(line))
    .filter(it => !existingKeys.has(it.key));
  setPD(collection, [...existing, ...added]);
  return added.length;  // 追加件数
}

// 使い方:
// const count = bulkAdd('targets', textarea.value,
//   line => ({ key: line, company: line, createdAt: Date.now() }));
// alert(count + '件追加しました');
前提条件
Tailwind CSS v4TypeScript 5
USE CASES
  • 任意のダッシュボードに組み込み

ペースト一括追加(改行区切り)

:LiTarget: 用途

テキストエリアに改行区切りで複数項目をペースト → 一気にレコード追加。テレアポリスト等で便利。

:LiSparkle: 特徴

  • 改行区切りパース
  • 空行スキップ
  • 重複チェック
  • 一括Insert

:LiCode: コード(コピペ用)

function parsePastedLines(text) {
  return text.split(/\r?\n/)
    .map(s => s.trim())
    .filter(Boolean);
}

function bulkAdd(collection, pastedText, makeItem) {
  const lines = parsePastedLines(pastedText);
  const existing = PD(collection, []);
  const existingKeys = new Set(existing.map(x => x.key));
  const added = lines
    .map(line => makeItem(line))
    .filter(it => !existingKeys.has(it.key));
  setPD(collection, [...existing, ...added]);
  return added.length;  // 追加件数
}

// 使い方:
// const count = bulkAdd('targets', textarea.value,
//   line => ({ key: line, company: line, createdAt: Date.now() }));
// alert(count + '件追加しました');

:LiHandPointer: 使い方

対象プロジェクトに該当ファイルをコピーして、props を流し込むだけ。

:LiAlertCircle: 注意事項

  • 依存パッケージを忘れず追加