Last active
June 29, 2021 02:36
-
-
Save luiznasciment0/c39d1197574fb8590031d92c5e3c12d5 to your computer and use it in GitHub Desktop.
omitKeys
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
const cloneObject = <TObject extends { [key: string]: unknown }>(object: TObject): TObject => | |
JSON.parse(JSON.stringify(object)) | |
/* | |
* Essa função serve para remover propriedades de um objeto sem a necessidade de fazer mutações | |
* Dessa forma, você não perde a referência do objeto original se precisar utilizá-lo posteriormente | |
* | |
* | |
@param | |
object - O objeto que você quer remover uma propriedade. | |
* | |
@param | |
keysToRemove - As chaves das propriedades que você quer remover do objeto. | |
* */ | |
export const deleteKeysFromObject = < | |
TObject extends { [key: string]: unknown }, | |
TKeys extends Array<keyof TObject> | |
>( | |
object: TObject, | |
keysToRemove: TKeys, | |
): Omit<TObject, TKeys[number]> => { | |
const newObject = cloneObject(object) | |
keysToRemove.forEach(item => { | |
delete newObject[item] | |
}) | |
return newObject | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment