Skip to content

Commit

Permalink
fix: Fix checkObjInclude not correct for array
Browse files Browse the repository at this point in the history
  • Loading branch information
littleGnAl committed Nov 30, 2023
1 parent ea5b057 commit bbd76ef
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ export * from './parsers/update_simple_type_parser';
export * from './parsers/pointer_marker_parser';
export * from './renderers/mustache_renderer';
export * from './renderers/iris_doc_renderer';
export * from './utils/obj_utils';
22 changes: 21 additions & 1 deletion src/utils/obj_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,16 @@
* @returns True if the property is equal to the value, false otherwise.
*/
export function checkPropertyEq(obj: any, key: string, value: any): boolean {
function _arraysAreIdentical(arr1: any, arr2: any) {
if (arr1.length !== arr2.length) return false;
for (var i = 0, len = arr1.length; i < len; i++) {
if (arr1[i] !== arr2[i]) {
return false;
}
}
return true;
}

if (!obj.hasOwnProperty(key)) {
return false;
}
Expand All @@ -17,6 +27,10 @@ export function checkPropertyEq(obj: any, key: string, value: any): boolean {
eq = new RegExp(value).test(obj[key]);
}

if (!eq && Array.isArray(value) && Array.isArray(obj[key])) {
eq = _arraysAreIdentical(value, obj[key]);
}

return eq;
}

Expand All @@ -38,7 +52,13 @@ export function checkObjInclude(obj1: any, obj2: any): boolean {
let v1 = obj1[k2];
let v2 = obj2[k2];
if (v1) {
if (typeof v1 === 'object' && typeof v2 === 'object') {
// Object but not array
if (
typeof v1 === 'object' &&
!Array.isArray(v1) &&
typeof v2 === 'object' &&
!Array.isArray(v2)
) {
isIncluded = isIncluded && checkObjInclude(v1, v2);
} else {
isIncluded = isIncluded && checkPropertyEq(obj1, k2, obj2[k2]);
Expand Down

0 comments on commit bbd76ef

Please sign in to comment.