You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

寻求空值/Null/Undefined检查优化方案及图书信息代码升级建议

Hey there! Let's refine your book info logic to match your business requirements, plus we'll use cleaner, more efficient null/undefined checks along the way.

1. Core Business Logic Breakdown

First, let's lock in the rules we need to enforce clearly:

  • If the book title is missing (empty string, undefined, or null), mark the book as unavailable and use your notAvailableMSG
  • If the title exists but other metadata fields are missing, keep the book marked as available and fill missing fields with unknownMSG
2. Efficient Null/Undefined/Empty Checks

Instead of writing repetitive == '' || == undefined checks, here are cleaner, more concise alternatives:

  • Falsy Check (!): A quick way to catch all falsy values (empty string, undefined, null, 0, false, etc.) – perfect for the title check since we want to block any "missing" title scenario
  • Nullish Coalescing Operator (??): Returns the right-hand value only if the left is null or undefined (ignores empty strings, 0, etc.) – ideal for metadata fields where empty strings might be intentional, but missing data should default to unknownMSG
  • Optional Chaining (?.): Safely access nested properties without throwing errors if a parent value is undefined/null (useful if your book data comes from nested API responses)
3. Refactored Code Snippet

Here's how to apply these to your code, with clearer structure and logic:

const notAvailableMSG = process.env.notAvailableMSG;
const unknownMSG = process.env.unknownMSG;
let bookInfo = {}; // Switched to object (arrays are for lists, objects fit key-value book metadata better)

// Check if title is missing (covers empty string, undefined, null)
if (!bookInfo.title) {
  // Mark book as unavailable
  bookInfo.isAvailable = false;
  bookInfo.status = notAvailableMSG;
  bookInfo.isbn = isbn; // Assuming `isbn` is defined elsewhere in your code
} else {
  // Book is available – fill missing metadata with unknownMSG
  bookInfo.isAvailable = true;
  // Example metadata fields – adjust to match your actual data
  bookInfo.author = bookInfo.author ?? unknownMSG;
  bookInfo.publisher = bookInfo.publisher ?? unknownMSG;
  bookInfo.publishDate = bookInfo.publishDate ?? unknownMSG;
  bookInfo.isbn = isbn;
}

Quick Refactor Notes:

  • Changed bookInfo from an array to an object – this makes it easier to manage key-value metadata like title, author, etc.
  • Added an explicit isAvailable flag to make the book's status clear at a glance (no need to parse messages to determine availability)
  • If you want to treat empty strings as "missing" metadata too, replace ?? with || (e.g., bookInfo.author || unknownMSG) – just note that || will replace all falsy values, including 0 or false
4. Bonus: DRY Helper Function

If you have lots of metadata fields to check, a small helper function will keep your code clean and reusable:

function getMetadataValue(value) {
  return value ?? unknownMSG;
}

// Use it like this for each field:
bookInfo.author = getMetadataValue(bookInfo.author);
bookInfo.publisher = getMetadataValue(bookInfo.publisher);
bookInfo.publishDate = getMetadataValue(bookInfo.publishDate);

内容的提问来源于stack exchange,提问作者Elvira

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 04:18:52