Skip to content

Commit e649fe1

Browse files
authored
chore: improve types of recursiveRemoveEmptyAndNull (#323)
1 parent e513b8f commit e649fe1

File tree

1 file changed

+15
-14
lines changed

1 file changed

+15
-14
lines changed

src/utils/recursiveRemoveEmptyAndNull.ts

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ interface RecursiveRemoveEmptyAndNullOptions {
4444
/**
4545
* Recursively removes empty values from an object. This will also remove empty object or empty array.
4646
* @param object - the object being cleaned
47-
* @param propertiesToRemove - A string or array of strings of key(s) for key-value pair(s) to be cleaned from `object`
4847
* @param options - Optional object with options for cleaning
4948
* @returns the cleaned object
5049
*/
@@ -116,11 +115,12 @@ function cleanCyclicObject(
116115
}
117116

118117
for (let i = object.length - 1; i >= 0; i--) {
118+
const arrayElement = object[i];
119119
if (
120-
isEmpty(object[i]) &&
120+
isEmpty(arrayElement) &&
121121
!(
122-
isArray(object[i]) &&
123-
object[i].length === 0 &&
122+
isArray(arrayElement) &&
123+
arrayElement.length === 0 &&
124124
options?.removeEmptyArrayAndObject === false
125125
)
126126
) {
@@ -131,13 +131,14 @@ function cleanCyclicObject(
131131
for (const key of Reflect.ownKeys(object)) {
132132
const isIndex = typeof key === 'string' && /^\d+$/.test(key);
133133
if (!isIndex) {
134+
const value = object[key as any];
134135
if (
135136
(removeProperty && key === removeProperty) ||
136-
(!removeProperty && isEmpty(object[key]))
137+
(!removeProperty && isEmpty(value))
137138
) {
138139
Reflect.deleteProperty(object, key);
139140
} else {
140-
recursiveClean(object[key], object, key);
141+
recursiveClean(value, object, key);
141142
}
142143
}
143144
}
@@ -170,7 +171,7 @@ function repr(arg: unknown): string {
170171
* @param {} arg - unknown function argument
171172
* @returns returns true if `arg` is an Array, false otherwise
172173
*/
173-
function isArray(arg: unknown): boolean {
174+
function isArray(arg: unknown): arg is unknown[] {
174175
return Array.isArray ? Array.isArray(arg) : repr(arg) === '[object Array]';
175176
}
176177

@@ -179,7 +180,7 @@ function isArray(arg: unknown): boolean {
179180
* @param {} arg - unknown function argument
180181
* @returns returns true if `arg` is an object.
181182
*/
182-
function isObject(arg: unknown): boolean {
183+
function isObject(arg: unknown): arg is Record<string | symbol, unknown> {
183184
return repr(arg) === '[object Object]';
184185
}
185186

@@ -188,7 +189,7 @@ function isObject(arg: unknown): boolean {
188189
* @param {} arg - unknown function argument
189190
* @returns returns true if `arg` is a String, false otherwise
190191
*/
191-
function isString(arg: unknown): boolean {
192+
function isString(arg: unknown): arg is string {
192193
return repr(arg) === '[object String]';
193194
}
194195

@@ -197,7 +198,7 @@ function isString(arg: unknown): boolean {
197198
* @param {} arg - unknown function argument
198199
* @returns returns true if `arg` is of type Null, false otherwise
199200
*/
200-
function isNull(arg: unknown): boolean {
201+
function isNull(arg: unknown): arg is null {
201202
return repr(arg) === '[object Null]';
202203
}
203204

@@ -206,7 +207,7 @@ function isNull(arg: unknown): boolean {
206207
* @param {} arg - unknown function argument
207208
* @returns Returns true if `arg` is of type Undefined, false otherwise
208209
*/
209-
function isUndefined(arg: unknown): boolean {
210+
function isUndefined(arg: unknown): arg is undefined {
210211
return arg === undefined;
211212
}
212213

@@ -221,8 +222,8 @@ function isEmpty(arg: unknown): boolean {
221222
return (
222223
isUndefined(arg) ||
223224
isNull(arg) ||
224-
(isString(arg) && (arg as string).length === 0) ||
225-
(isArray(arg) && (arg as unknown[]).length === 0) ||
226-
(isObject(arg) && Object.keys(arg as object).length === 0)
225+
(isString(arg) && arg.length === 0) ||
226+
(isArray(arg) && arg.length === 0) ||
227+
(isObject(arg) && Object.keys(arg).length === 0)
227228
);
228229
}

0 commit comments

Comments
 (0)