当 API 返回的数据对象层级较深时,业务代码里常会出现连续取值:
Data["key1"]["key2"]["key3"]如果中间某一层不存在,或者值为 null / undefined,后续访问就可能报错。
property-link-check 用于按属性链逐级检查对象,并返回当前检查结果,帮助你在真正读取深层属性前先确认链路是否存在。
npm install property-link-checkimport { checkProperty } from "property-link-check";
const data = {
id: "00",
parent: {
father: {
name: "john",
},
},
};
const result = checkProperty(data, ["parent", "father", "name"]);
console.log(result);输出示例:
{
type: "string",
value: "john",
exist_index: 2
}checkProperty(obj: any, properties: string[]): PropertyCheckResult参数说明:
obj: 要检查的目标对象properties: 属性链数组,按照访问顺序传入,例如["parent", "father", "name"]
返回值 PropertyCheckResult:
{
type: string | null
value: any
exist_index: number
}字段说明:
type: 当前命中值的typeof结果value: 当前命中的值exist_index: 最后一个成功命中的属性下标;如果一个属性都没命中,则为-1
- 完整命中
checkProperty(data, ["parent", "father", "name"]);结果:
{
type: "string",
value: "john",
exist_index: 2
}- 中途属性不存在
checkProperty(data, ["parent", "brother", "name"]);结果:
{
type: "undefined",
value: undefined,
exist_index: 0
}说明:
parent存在,所以exist_index是0brother不存在,因此返回该位置的undefined
- 属性值存在,但值本身为
null
const data2 = { profile: null };
checkProperty(data2, ["profile"]);结果:
{
type: "object",
value: null,
exist_index: 0
}properties必须是数组,否则会抛出异常- 当前版本中,如果
obj本身是undefined、null、false、0等假值,函数会直接返回,不继续校验属性链 - 当前实现使用的是对象自身属性检查,不会沿原型链查找
项目已包含测试用例,可直接运行:
npm test测试覆盖了完整命中、链路中断、undefined / null / false / 0、非法参数等常见场景。
When an API returns a deeply nested object, application code often accesses properties like this:
Data["key1"]["key2"]["key3"]If any intermediate level is missing, or the value is null / undefined, the next access may throw an error.
property-link-check checks an object step by step using a property path and returns the current lookup result, so you can verify the path before reading deeply nested values.
npm install property-link-checkimport { checkProperty } from "property-link-check";
const data = {
id: "00",
parent: {
father: {
name: "john",
},
},
};
const result = checkProperty(data, ["parent", "father", "name"]);
console.log(result);Example output:
{
type: "string",
value: "john",
exist_index: 2
}checkProperty(obj: any, properties: string[]): PropertyCheckResultParameters:
obj: the target object to inspectproperties: the property path in access order, for example["parent", "father", "name"]
Return type PropertyCheckResult:
{
type: string | null
value: any
exist_index: number
}Field meanings:
type: thetypeofresult of the current matched valuevalue: the current matched valueexist_index: the index of the last successfully matched property;-1means no property was matched
- Full path exists
checkProperty(data, ["parent", "father", "name"]);Result:
{
type: "string",
value: "john",
exist_index: 2
}- Path breaks in the middle
checkProperty(data, ["parent", "brother", "name"]);Result:
{
type: "undefined",
value: undefined,
exist_index: 0
}Notes:
parentexists, soexist_indexis0brotherdoes not exist, so the function returnsundefinedat that point
- Property exists but the value is
null
const data2 = { profile: null };
checkProperty(data2, ["profile"]);Result:
{
type: "object",
value: null,
exist_index: 0
}propertiesmust be an array, otherwise the function throws an error- In the current version, if
objitself is a falsy value such asundefined,null,false, or0, the function returns immediately without checking the property path - The current implementation checks only own properties and does not walk the prototype chain
The repository already includes test cases. Run:
npm testThe tests cover successful lookups, broken paths, undefined / null / false / 0, invalid parameters, and other common cases.