-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Simplify
createOmitPropertiesSerializer
- Loading branch information
Showing
4 changed files
with
39 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,19 @@ | ||
import { omitProperties } from './omitProperties'; | ||
import type { SerializerFn } from './types'; | ||
|
||
export interface OmitPropertiesSerializerOptions { | ||
export const createOmitPropertiesSerializer = ( | ||
/** | ||
* A list of properties that should not be logged. | ||
*/ | ||
omitPropertyNames: string[]; | ||
} | ||
|
||
type OmitHeaderNamesFn = (record: unknown) => unknown; | ||
type OmitHeaderNamesSerializer = Record<string, OmitHeaderNamesFn>; | ||
properties: string[], | ||
): SerializerFn => { | ||
const uniquePropertySet = new Set(properties); | ||
|
||
/** Creates a serializer that operates on the logged object's top-level property named `topLevelPropertyName` | ||
* and omits the properties listed in `options.omitPropertyNames`. | ||
* @param topLevelPropertyName - The name of the root property on the logged object that to omit properties from. | ||
* @param options - Options for the serializer. | ||
*/ | ||
export const createOmitPropertiesSerializer = ( | ||
topLevelPropertyName: string, | ||
options: OmitPropertiesSerializerOptions, | ||
): OmitHeaderNamesSerializer => { | ||
const propertyNames = [...new Set(options.omitPropertyNames ?? [])].filter( | ||
(propertyName) => typeof propertyName === 'string', | ||
); | ||
|
||
if ( | ||
!topLevelPropertyName || | ||
typeof topLevelPropertyName !== 'string' || | ||
topLevelPropertyName.length === 0 || | ||
propertyNames.length === 0 | ||
) { | ||
return {}; | ||
if (uniquePropertySet.size === 0) { | ||
return (input) => input; | ||
} | ||
|
||
return { | ||
[topLevelPropertyName]: (record) => omitProperties(record, propertyNames), | ||
}; | ||
const uniqueProperties = Array.from(uniquePropertySet); | ||
|
||
return (input) => omitProperties(input, uniqueProperties); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export type SerializerFn = (input: unknown) => unknown; |