@equinor/echo-base
Advanced tools
Comparing version 0.6.8 to 0.6.9
@@ -56,4 +56,9 @@ import { BaseErrorArgs } from '../types/error'; | ||
* @param objectWithProperties an object containing properties | ||
* @param args.ignoreEquals ignore/omit any property that equals this (case insensitive) | ||
* @param args.ignoreIncludes ignore/omit any property that includes this (case insensitive) | ||
* @returns a record containing all properties of the object | ||
*/ | ||
export declare function getAllProperties(objectWithProperties: Record<string, unknown> | Error | undefined): Record<string, unknown>; | ||
export declare function getAllProperties(objectWithProperties: Record<string, unknown> | object | undefined, args?: { | ||
ignoreEquals?: string[]; | ||
ignoreIncludes?: string[]; | ||
}): Record<string, unknown>; |
@@ -90,5 +90,4 @@ import { __extends } from "tslib"; | ||
var properties = getAllProperties(object); | ||
var value = properties[propertyName]; | ||
if (value) { | ||
return value; | ||
if (properties.hasOwnProperty(propertyName)) { | ||
return properties[propertyName]; | ||
} | ||
@@ -114,5 +113,7 @@ var maybeFound = undefined; | ||
* @param objectWithProperties an object containing properties | ||
* @param args.ignoreEquals ignore/omit any property that equals this (case insensitive) | ||
* @param args.ignoreIncludes ignore/omit any property that includes this (case insensitive) | ||
* @returns a record containing all properties of the object | ||
*/ | ||
export function getAllProperties(objectWithProperties) { | ||
export function getAllProperties(objectWithProperties, args) { | ||
if (!objectWithProperties) { | ||
@@ -133,7 +134,7 @@ return {}; | ||
var valueType = typeof value; | ||
if (valueType === 'function' || name === 'hasBeenLogged') { | ||
if (valueType === 'function' || isPropertyIgnored(name, args)) { | ||
//ignore | ||
} | ||
else if (value instanceof Error) { | ||
rec[name] = getAllProperties(value); | ||
else if (typeof value === 'object') { | ||
rec[name] = getAllProperties(value, args); | ||
} | ||
@@ -146,2 +147,14 @@ else { | ||
} | ||
function isPropertyIgnored(name, args) { | ||
if (!args) { | ||
return false; | ||
} | ||
name = name.toLocaleLowerCase(); | ||
if (args.ignoreIncludes && args.ignoreIncludes.some(function (item) { return name.includes(item.toLocaleLowerCase()); })) { | ||
return true; | ||
} | ||
if (args.ignoreEquals && args.ignoreEquals.some(function (item) { return name === item.toLocaleLowerCase(); })) { | ||
return true; | ||
} | ||
} | ||
//# sourceMappingURL=BaseError.js.map |
@@ -56,4 +56,9 @@ import { BaseErrorArgs } from '../types/error'; | ||
* @param objectWithProperties an object containing properties | ||
* @param args.ignoreEquals ignore/omit any property that equals this (case insensitive) | ||
* @param args.ignoreIncludes ignore/omit any property that includes this (case insensitive) | ||
* @returns a record containing all properties of the object | ||
*/ | ||
export declare function getAllProperties(objectWithProperties: Record<string, unknown> | Error | undefined): Record<string, unknown>; | ||
export declare function getAllProperties(objectWithProperties: Record<string, unknown> | object | undefined, args?: { | ||
ignoreEquals?: string[]; | ||
ignoreIncludes?: string[]; | ||
}): Record<string, unknown>; |
@@ -93,5 +93,4 @@ "use strict"; | ||
var properties = getAllProperties(object); | ||
var value = properties[propertyName]; | ||
if (value) { | ||
return value; | ||
if (properties.hasOwnProperty(propertyName)) { | ||
return properties[propertyName]; | ||
} | ||
@@ -118,5 +117,7 @@ var maybeFound = undefined; | ||
* @param objectWithProperties an object containing properties | ||
* @param args.ignoreEquals ignore/omit any property that equals this (case insensitive) | ||
* @param args.ignoreIncludes ignore/omit any property that includes this (case insensitive) | ||
* @returns a record containing all properties of the object | ||
*/ | ||
function getAllProperties(objectWithProperties) { | ||
function getAllProperties(objectWithProperties, args) { | ||
if (!objectWithProperties) { | ||
@@ -137,7 +138,7 @@ return {}; | ||
var valueType = typeof value; | ||
if (valueType === 'function' || name === 'hasBeenLogged') { | ||
if (valueType === 'function' || isPropertyIgnored(name, args)) { | ||
//ignore | ||
} | ||
else if (value instanceof Error) { | ||
rec[name] = getAllProperties(value); | ||
else if (typeof value === 'object') { | ||
rec[name] = getAllProperties(value, args); | ||
} | ||
@@ -151,2 +152,14 @@ else { | ||
exports.getAllProperties = getAllProperties; | ||
function isPropertyIgnored(name, args) { | ||
if (!args) { | ||
return false; | ||
} | ||
name = name.toLocaleLowerCase(); | ||
if (args.ignoreIncludes && args.ignoreIncludes.some(function (item) { return name.includes(item.toLocaleLowerCase()); })) { | ||
return true; | ||
} | ||
if (args.ignoreEquals && args.ignoreEquals.some(function (item) { return name === item.toLocaleLowerCase(); })) { | ||
return true; | ||
} | ||
} | ||
//# sourceMappingURL=BaseError.js.map |
{ | ||
"name": "@equinor/echo-base", | ||
"version": "0.6.8", | ||
"version": "0.6.9", | ||
"module": "esm/index.js", | ||
@@ -34,5 +34,5 @@ "main": "lib/index.js", | ||
"devDependencies": { | ||
"@types/node": "^17.0.21", | ||
"typedoc": "^0.22.13", | ||
"typescript": "^4.6.2" | ||
"@types/node": "^17.0.42", | ||
"typedoc": "^0.22.17", | ||
"typescript": "^4.7.3" | ||
}, | ||
@@ -45,4 +45,4 @@ "files": [ | ||
"dependencies": { | ||
"tslib": "^2.3.1" | ||
"tslib": "^2.4.0" | ||
} | ||
} | ||
} |
@@ -32,2 +32,3 @@ import { BaseError, getAllProperties } from '../../errors'; | ||
const properties = { | ||
hasBeenLogged: false, | ||
httpStatusCode, | ||
@@ -71,2 +72,3 @@ url, | ||
message, | ||
hasBeenLogged: false, | ||
httpStatusCode, | ||
@@ -73,0 +75,0 @@ url, |
@@ -73,2 +73,22 @@ import { BaseError, findPropertyByName, getAllProperties } from './BaseError'; | ||
describe('getAllProperties.ignore', () => { | ||
it('ignoreEquals should ignore all properties specified, including nested object', () => { | ||
const input = { abc: 'a', bcd: 1, cde: { nestedProp: 'prop1', abc: '123' } }; | ||
const actualError = getAllProperties(input, { ignoreEquals: ['abc', 'bcd'] }); | ||
expect(actualError).toStrictEqual({ cde: { nestedProp: 'prop1' } }); | ||
}); | ||
it('ignoreIncludes should ignore all properties specified, including nested object', () => { | ||
const input = { abc: 'a', bcd: 1, ddd: { nestedProp: 'prop1', bc: '123' } }; | ||
const actualError = getAllProperties(input, { ignoreIncludes: ['a', 'c'] }); | ||
expect(actualError).toStrictEqual({ ddd: { nestedProp: 'prop1' } }); | ||
}); | ||
it('ignore should handle both equals and includes at the same time, and it should be case insensitive', () => { | ||
const input = { stack: 'a', bcd: 1, ddd: { innerStack: 'prop1', Y: '1', N: '0' } }; | ||
const actualError = getAllProperties(input, { ignoreIncludes: ['stack'], ignoreEquals: ['y'] }); | ||
expect(actualError).toStrictEqual({ bcd: 1, ddd: { N: '0' } }); | ||
}); | ||
}); | ||
describe('findPropertyByName', () => { | ||
@@ -83,2 +103,19 @@ const message = 'This is a custom error message for testing'; | ||
it('should return found boolean with value false and true', () => { | ||
const error = new BaseError({ name: 'BaseError', message }); | ||
expect(findPropertyByName(error, 'hasBeenLogged')).toBe(false); | ||
error.hasBeenLogged = true; | ||
expect(findPropertyByName(error, 'hasBeenLogged')).toBe(true); | ||
}); | ||
it('should return found string which has empty value', () => { | ||
const error = new BaseError({ | ||
name: 'BaseError', | ||
message, | ||
innerError: { stringProperty: '', nestedObject: { innerStringProperty: '' } } | ||
}); | ||
expect(findPropertyByName(error, 'stringProperty')).toBe(''); | ||
expect(findPropertyByName(error, 'innerStringProperty')).toBe(''); | ||
}); | ||
it('should find first message on baseError instead of message on innerError', () => { | ||
@@ -85,0 +122,0 @@ const error = new BaseError({ |
@@ -101,5 +101,4 @@ import { BaseErrorArgs } from '../types/error'; | ||
const properties = getAllProperties(object); | ||
const value = properties[propertyName]; | ||
if (value) { | ||
return value; | ||
if (properties.hasOwnProperty(propertyName)) { | ||
return properties[propertyName]; | ||
} | ||
@@ -128,6 +127,9 @@ | ||
* @param objectWithProperties an object containing properties | ||
* @param args.ignoreEquals ignore/omit any property that equals this (case insensitive) | ||
* @param args.ignoreIncludes ignore/omit any property that includes this (case insensitive) | ||
* @returns a record containing all properties of the object | ||
*/ | ||
export function getAllProperties( | ||
objectWithProperties: Record<string, unknown> | Error | undefined | ||
objectWithProperties: Record<string, unknown> | object | undefined, | ||
args?: { ignoreEquals?: string[]; ignoreIncludes?: string[] } | ||
): Record<string, unknown> { | ||
@@ -152,6 +154,6 @@ if (!objectWithProperties) { | ||
const valueType = typeof value; | ||
if (valueType === 'function' || name === 'hasBeenLogged') { | ||
if (valueType === 'function' || isPropertyIgnored(name, args)) { | ||
//ignore | ||
} else if (value instanceof Error) { | ||
rec[name] = getAllProperties(value); | ||
} else if (typeof value === 'object') { | ||
rec[name] = getAllProperties(value, args); | ||
} else { | ||
@@ -163,1 +165,17 @@ rec[name] = value; | ||
} | ||
function isPropertyIgnored(name: string, args?: { ignoreEquals?: string[]; ignoreIncludes?: string[] }) { | ||
if (!args) { | ||
return false; | ||
} | ||
name = name.toLocaleLowerCase(); | ||
if (args.ignoreIncludes && args.ignoreIncludes.some((item) => name.includes(item.toLocaleLowerCase()))) { | ||
return true; | ||
} | ||
if (args.ignoreEquals && args.ignoreEquals.some((item) => name === item.toLocaleLowerCase())) { | ||
return true; | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
295043
261
5859
Updatedtslib@^2.4.0