UNPKG

@digitalwalletcorp/sql-builder

Version:
37 lines 1.28 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getProperty = getProperty; /** * entityで指定したオブジェクトからドットで連結されたプロパティキーに該当する値を取得する * * @param {Record<string, any>} entity * @param {string} property * @returns {any} */ function getProperty(entity, property) { // `?.` または `.` でパスを分割 const propertyPath = property.split(/(\?\.)|\./).filter(Boolean); // 再帰呼び出し用のヘルパー関数 const get = (obj, keys) => { if (keys.length === 0) { return obj; } // オプショナルチェイニングのチェック if (obj == null) { return undefined; } const currentKey = keys[0]; const remainingKeys = keys.slice(1); // `?.` のトークンはスキップ if (currentKey === '?.') { return get(obj, remainingKeys); } // プロパティが存在しない場合は undefined を返す if (!obj.hasOwnProperty(currentKey)) { return undefined; } return get(obj[currentKey], remainingKeys); }; return get(entity, propertyPath); } //# sourceMappingURL=common.js.map