跳到正文
DEV Community· Tihmlo·· 1 小時前AI 評分29

如何用 JavaScript 表格記錄 USB-C 充電器各連接埠配置

Model each USB-C charger port configuration in a JavaScript table

AI 導讀

文章示範用 JavaScript 表格逐項記錄 USB-C 充電器在不同連接埠組合下的功率分配,避免把一種配置的數值套用到另一種配置。例子中的計算結果為 65 W、45 W 和未知;「符合需求」只代表已記錄的功率上限足夠,不代表裝置實際上會協商到該供電設定。

正文

Tihmlo

AI-generated worked example, checked by executing the JavaScript. Charger allocations below are fictional.

A charger can advertise 100 W in total while allocating 65 W to a laptop port in one configuration and 45 W in another. A hardware comparison should keep the allocation for each documented port combination. A laptop that needs 60 W under sustained use can pass the first calculation and fail the second even though the charger and cable stay the same.

Keep one row per documented port state

For this example, assume the same 20 V profile is documented for both known states, the cable is rated at 5 A, and the laptop accepts up to 65 W. The third combination has no published allocation. Leave that entry unknown. Do not borrow a wattage from a different port combination.

const cableWatts = 20 * 5;
const deviceWatts = 65;
const neededWatts = 60;
const states = [
  { ports: 'Laptop only', portWatts: 65 },
  { ports: 'Laptop plus phone', portWatts: 45 },
  { ports: 'Laptop plus two accessories', portWatts: null },
];

const results = states.map(({ ports, portWatts }) => {
  if (portWatts === null) return { ports, ceiling: null, result: 'unknown' };
  const ceiling = Math.min(portWatts, cableWatts, deviceWatts);
  const result = ceiling >= neededWatts ? 'within budget' : 'below target';
  return { ports, ceiling, result };
});

console.table(results);

The output is 65 W, 45 W, and unknown. “Within budget” means the stated limits leave enough arithmetic headroom. It says nothing about whether the devices will negotiate that profile in practice. Check that the voltage and the specific port combination are documented before comparing the rows. If the voltage changes with the port state, use the documented voltage for each state as well.

Once you have a documented allocation, TIHMLO’s USB-C power-budget worksheet checks that single charger, cable, and device path. Keep the table of simultaneous ports in your comparison so a reader can reproduce your input.

來源:DEV Community · dev.to