@lookwe/omit-undefined
Version:
A small utility package for TypeScript and JavaScript projects to create a new object with all `undefined` properties removed, with strong support for TypeScript's `exactOptionalPropertyTypes`.
19 lines (17 loc) • 550 B
JavaScript
/**
* Creates a new object containing only the properties of the input object that are not `undefined`.
*
* @param {T} obj - The object to process.
* @returns {OmitUndefined<T>} A new object with properties that were not `undefined` in the original object.
* @template T - An object type.
*/
function omitUndefined(obj) {
const cleanedObj = { ...obj };
for (const key in cleanedObj) {
if (cleanedObj[key] === undefined) {
delete cleanedObj[key];
}
}
return cleanedObj;
}
export { omitUndefined };