Skip to content

Instantly share code, notes, and snippets.

@niemyjski
Created March 10, 2023 12:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save niemyjski/775eb15ea5712fd470cb5aef0f8fb929 to your computer and use it in GitHub Desktop.
Save niemyjski/775eb15ea5712fd470cb5aef0f8fb929 to your computer and use it in GitHub Desktop.
Exceptionless Prune
/**
* 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