Skip to content

Commit 3cd48eb

Browse files
Merge branch 'simplify-types'
2 parents 4ad6b49 + d3d5c99 commit 3cd48eb

7 files changed

+38
-34
lines changed

.eslintrc.json

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"globals": { "BigInt": true, "console": true, "WebAssembly": true },
1717
"rules": {
1818
"@typescript-eslint/explicit-module-boundary-types": "off",
19+
"@typescript-eslint/no-explicit-any": "off",
1920
"eslint-comments/disable-enable-pair": [
2021
"error",
2122
{ "allowWholeFile": true }

src/lib/clean-object.ts

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import { CleanObjectConfig } from '../types/clean-object';
1+
import {CleanObjectConfig, NonNullableFields} from '../types/clean-object';
22

33
import { isObject } from './is-object';
44

55
const defaultConfig: CleanObjectConfig = {
66
removeEmptyValues: false,
77
};
88

9-
const isEmptyObject = (o: object) => Object.keys(o).length === 0;
9+
const isEmptyObject = (o: Record<string, any>) => Object.keys(o).length === 0;
1010

1111
const needSkipValue = (
1212
value: unknown,
@@ -29,10 +29,10 @@ const needSkipValue = (
2929
return false;
3030
};
3131

32-
export const cleanObject = <T extends object>(
32+
export const cleanObject = <T extends Record<string, any>>(
3333
obj: T,
3434
config?: Partial<CleanObjectConfig>
35-
): object => {
35+
): NonNullableFields<T> => {
3636
const cfg = Object.assign({ ...defaultConfig }, config || {});
3737

3838
if (!isObject(obj)) {
@@ -50,7 +50,7 @@ export const cleanObject = <T extends object>(
5050
return obj;
5151
}
5252

53-
return keys.reduce((acc: object, key) => {
53+
return keys.reduce((acc: Record<string, any>, key) => {
5454
const item = obj[key as keyof T];
5555
if (needSkipValue(item, cfg)) {
5656
return acc;
@@ -59,7 +59,7 @@ export const cleanObject = <T extends object>(
5959
if (!item.length && cfg.removeEmptyValues) {
6060
return acc;
6161
}
62-
const mapped = item.reduce((arr: object[], curr) => {
62+
const mapped = item.reduce((arr: Record<string, any>[], curr: any) => {
6363
if (isObject(curr)) {
6464
const cleared = cleanObject(curr, cfg);
6565
if (isEmptyObject(cleared) && cfg.removeEmptyValues) {
@@ -82,5 +82,5 @@ export const cleanObject = <T extends object>(
8282
}
8383

8484
return { ...acc, [`${key}`]: item };
85-
}, {});
85+
}, {}) as NonNullableFields<T>;
8686
};

src/lib/create-form-data.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { getKeyString } from './utils/get-key-string';
99
* @param config - configuration object
1010
* @param formData - form data instance
1111
*/
12-
const fillFormData = <T extends object>(
12+
const fillFormData = <T extends Record<string, any>>(
1313
value: T | T[] | PrimitiveTypes,
1414
config: CreateFormDataConfig,
1515
formData: FormData
@@ -51,7 +51,7 @@ const fillFormData = <T extends object>(
5151
Object.keys(value).forEach((key) => {
5252
const keyString = getKeyString(keyPrefix, key, index);
5353
fillFormData(
54-
value[key as keyof object],
54+
value[key as keyof Record<string, any>],
5555
{ ...config, keyPrefix: keyString },
5656
formData
5757
);
@@ -79,7 +79,7 @@ const defaultConfig = {
7979
* @param options - configuration object
8080
* @param existingFormData - optional existing form data instance
8181
*/
82-
export const createFormData = <T extends object>(
82+
export const createFormData = <T extends Record<string, any>>(
8383
value: T | FormData,
8484
options?: Partial<CreateFormDataConfig>,
8585
existingFormData?: FormData

src/lib/create-url-params.ts

+13-17
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,23 @@ import { getKeyString } from './utils/get-key-string';
44

55
const defaultConfig: CreateURLParamsConfig = {
66
keyPrefix: '',
7-
toString: false,
87
index: null,
98
booleanMapper: (val: boolean) => (val ? '1' : '0'),
109
};
1110

12-
export const createURLParams = <T extends object>(
11+
export const createURLParams = <T extends Record<string, any>>(
1312
value: T | T[] | null | undefined,
1413
config?: Partial<CreateURLParamsConfig>
15-
): object => {
16-
const { keyPrefix, toString, index, booleanMapper }: CreateURLParamsConfig =
14+
): Record<string, string> => {
15+
const { keyPrefix, index, booleanMapper }: CreateURLParamsConfig =
1716
Object.assign({ ...defaultConfig }, config || {});
1817

1918
if (value === undefined || value === null) {
2019
return {};
2120
}
2221

2322
if (Array.isArray(value)) {
24-
return value.reduce((acc: object, curr, i) => {
23+
return value.reduce((acc: Record<string, any>, curr, i) => {
2524
if (typeof curr === 'object' && curr) {
2625
return {
2726
...acc,
@@ -38,16 +37,14 @@ export const createURLParams = <T extends object>(
3837
const value =
3938
typeof curr === 'boolean'
4039
? booleanMapper(curr)
41-
: toString
42-
? String(curr)
43-
: curr;
40+
: String(curr);
4441
return { ...acc, [`${keyPrefix}[${i}]`]: value };
4542
}, {});
4643
}
4744

48-
return Object.keys(value).reduce((acc: object, key: string) => {
45+
return Object.keys(value).reduce((acc: Record<string, string>, key: string) => {
4946
const keyString = getKeyString(keyPrefix, key, index);
50-
const item = value[key as keyof object];
47+
const item = value[key as keyof Record<string, string>];
5148

5249
if (Array.isArray(item)) {
5350
return {
@@ -59,21 +56,20 @@ export const createURLParams = <T extends object>(
5956
};
6057
}
6158

59+
if(item === null || item === undefined){
60+
return acc;
61+
}
62+
6263
if (typeof item === 'boolean') {
6364
return { ...acc, [`${keyString}`]: booleanMapper(item) };
6465
}
6566

66-
if (item && typeof item === 'object') {
67+
if (typeof item === 'object') {
6768
return {
6869
...acc,
6970
...createURLParams(item, { ...config, keyPrefix: keyString }),
7071
};
7172
}
72-
73-
if (item !== null && item !== undefined) {
74-
const val = toString ? String(item) : item;
75-
return { ...acc, [`${keyString}`]: val };
76-
}
77-
return acc;
73+
return { ...acc, [`${keyString}`]: String(item) };
7874
}, {});
7975
};

src/types/clean-object.ts

+8
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
11
export interface CleanObjectConfig {
22
removeEmptyValues: boolean;
33
}
4+
5+
export type NonNullableFields<T extends Record<string, any>> = {
6+
[Key in keyof T]: T[Key] extends infer U
7+
? U extends null
8+
? never
9+
: U
10+
: never;
11+
};

src/types/create-url-params.ts

-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
export interface CreateURLParamsConfig {
22
keyPrefix: string;
3-
toString: boolean;
43
index: number | null;
54
booleanMapper: (value: boolean) => string;
65
}

test/create-url-params.spec.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ describe('createURLParams function', () => {
4545
hasSubscription: false,
4646
test: [true],
4747
};
48-
expect(createURLParams(obj, {toString: true})).to.deep.equal({
48+
expect(createURLParams(obj)).to.deep.equal({
4949
name: 'test',
5050
5151
'additionalEmails[0]': '[email protected]',
@@ -66,7 +66,7 @@ describe('createURLParams function', () => {
6666

6767
it('should be success with to string arg', () => {
6868
const obj = { name: 'test', email: '[email protected]', age: 1 };
69-
expect(createURLParams(obj, {toString: true})).to.deep.equal({
69+
expect(createURLParams(obj)).to.deep.equal({
7070
name: 'test',
7171
7272
age: '1'
@@ -75,7 +75,7 @@ describe('createURLParams function', () => {
7575

7676
it('should be success with default parent arg', () => {
7777
const obj = { name: 'test', email: '[email protected]', age: 1 };
78-
expect(createURLParams(obj, {toString: true})).to.deep.equal({
78+
expect(createURLParams(obj)).to.deep.equal({
7979
name: 'test',
8080
8181
age: '1'
@@ -84,15 +84,15 @@ describe('createURLParams function', () => {
8484

8585
it('should be success with default parent arg', () => {
8686
const obj = { name: 'test', email: '[email protected]' };
87-
expect(createURLParams(obj, {keyPrefix: 'parent', toString: true})).to.deep.equal({
87+
expect(createURLParams(obj, {keyPrefix: 'parent'})).to.deep.equal({
8888
'parent[name]': 'test',
8989
'parent[email]': '[email protected]'
9090
});
9191
});
9292

9393
it('should be success with default index arg', () => {
9494
const obj = { name: 'test', email: '[email protected]' };
95-
expect(createURLParams(obj, {keyPrefix: 'parent', toString: false, index:3})).to.deep.equal({
95+
expect(createURLParams(obj, {keyPrefix: 'parent', index:3})).to.deep.equal({
9696
'parent[3][name]': 'test',
9797
'parent[3][email]': '[email protected]'
9898
});
@@ -125,7 +125,7 @@ describe('createURLParams function', () => {
125125
expect(createURLParams(arr)).to.deep.equal({
126126
"code": "545",
127127
"phone": "55555",
128-
"isConnectable": 1,
128+
"isConnectable": "1",
129129
"socials[0]": "viber",
130130
"socials[1]": "telegram"
131131
});

0 commit comments

Comments
 (0)