@yookue/ts-lang-utils
Version:
Common lang utilities for typescript
60 lines (58 loc) • 2.09 kB
JavaScript
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/util/ObjectUtils/getPropIgnoreCase.ts
var getPropIgnoreCase_exports = {};
__export(getPropIgnoreCase_exports, {
getPropIgnoreCase: () => getPropIgnoreCase
});
module.exports = __toCommonJS(getPropIgnoreCase_exports);
var findPropIgnoreCase = (object, propName) => {
if (typeof object !== "object" || !object) {
return void 0;
}
return Object.keys(object).find((key) => key.toLowerCase() === propName.toLowerCase());
};
function getPropIgnoreCase(object, prop, defaultValue) {
if (typeof object !== "object" || !prop) {
return defaultValue;
}
if (!prop.includes(".")) {
const actualProp = findPropIgnoreCase(object, prop);
return actualProp !== void 0 ? object[actualProp] : defaultValue;
}
const props = prop.replace(/\[/g, ".").replace(/]/g, "").split(".");
if (!props || !props.length) {
return defaultValue;
}
let result = object;
for (let i = 0; i < props.length; i++) {
if (typeof result !== "object" || !result) {
return defaultValue;
}
const actualProp = findPropIgnoreCase(result, props[i]);
if (actualProp === void 0) {
return defaultValue;
}
result = result[actualProp];
}
return result;
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
getPropIgnoreCase
});