Created
March 10, 2023 12:28
-
-
Save niemyjski/775eb15ea5712fd470cb5aef0f8fb929 to your computer and use it in GitHub Desktop.
Exceptionless Prune
This file contains hidden or 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 function will prune an object to a certain depth and return a new object. | |
* The following rules will be applied: | |
* 1. If the value is null or undefined, it will be returned as is. | |
* 2. If the value is a function or unsupported type it will be return undefined. | |
* 3. If the value is an array, it will be pruned to the specified depth and truncated. | |
* 4. If the value is an object, it will be pruned to the specified depth and | |
* a. If the object is a Circular Reference it will return undefined. | |
* b. If the object is a Map, it will be converted to an object. | |
* c. If the object is a Set, it will be converted to an array. | |
* d. If the object contains prototype properties, they will be picked up. | |
* e. If the object is is uniterable and not clonable (e.g., WeakMap, WeakSet, etc.), it will return undefined. | |
* 5. If the value is an Error, we will treat it as an object. | |
* 6. If the value is a primitive, it will be returned as is unless it is a string could be truncated. | |
* 7. If the value is a Regexp, Symbol we will convert it to the string representation. | |
* 8. BigInt and other typed arrays will be converted to a string unless number type works. | |
* 9. All other values will be returned as undedfined (E.g., Buffer, DataView, Promises, Generators etc..) | |
*/ | |
export function prune(value: unknown, depth: number = 10): unknown { | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment