CSS 印刷スタイルシートは、日常の開発でよく直面する課題です。本記事では実際のプロジェクトに基づき、具体的な実装方法とこれまでの経験を共有します。
基本概念
まずは基本的な使い方を見てみましょう:
css
function deepClone(obj, map = new WeakMap()) {
if (obj === null || typeof obj !== 'object') return obj
if (map.has(obj)) return map.get(obj)
const clone = Array.isArray(obj) ? [] : {}
map.set(obj, clone)
for (const key of Object.keys(obj)) {
clone[key] = deepClone(obj[key], map)
}
return clone
}
この書き方は簡潔で分かりやすく、多くの場面に適しています。
深掘り
中核となるコードは以下のとおりです:
css
function pLimit(concurrency) {
const queue = []
let active = 0
const next = () => {
if (active >= concurrency || queue.length === 0) return
active++
const { fn, resolve, reject } = queue.shift()
fn().then(resolve, reject).finally(() => {
active--
next()
})
}
return (fn) => new Promise((resolve, reject) => {
queue.push({ fn, resolve, reject })
next()
})
}
実際のプロジェクトでは、境界条件や例外処理も考慮する必要があります。
プロジェクト応用
以下は実際の例です:
css
class EventEmitter {
constructor() {
this.events = new Map()
}
on(event, handler) {
if (!this.events.has(event)) {
this.events.set(event, [])
}
this.events.get(event).push(handler)
return () => this.off(event, handler)
}
off(event, handler) {
const handlers = this.events.get(event)
if (handlers) {
const idx = handlers.indexOf(handler)
if (idx > -1) handlers.splice(idx, 1)
}
}
emit(event, ...args) {
const handlers = this.events.get(event) || []
handlers.forEach(h => h(...args))
}
}
このパターンをチームに広めたところ効果は良く、保守コストが明確に下がりました。
よくある質問
以下の方法で実現できます:
css
interface User {
id: number
name: string
email: string
role: 'admin' | 'user' | 'guest'
}
function createUser(data: Partial<User>): User {
return {
id: Date.now(),
name: data.name || '',
email: data.email || '',
role: data.role || 'user'
}
}
type UserKeys = keyof User // 'id' | 'name' | 'email' | 'role'
上のコードにあるパフォーマンス上の細部に注意し、不要な計算を避けましょう。
まとめ
- CSS 印刷スタイルシートの鍵は、中核となる概念を理解することであり、表面的な使い方にとどまってはいけません
- 実際のプロジェクトでは、シチュエーションに応じて適切なアプローチを選択する
- チーム内で統一された規約を持つことは、完璧な実装を追求するよりも重要です
- 継続的に学びと振り返りを続け、技術への感度を保ちましょう
