Skip to content

Commit

Permalink
feat(pojos): strict mode friendly
Browse files Browse the repository at this point in the history
  • Loading branch information
nartc committed Jan 31, 2021
1 parent 64abad4 commit deedc1b
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 33 deletions.
3 changes: 2 additions & 1 deletion packages/pojos/.eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"ignorePatterns": ["!**/*"],
"rules": {
"@typescript-eslint/no-non-null-assertion": 0,
"no-prototype-builtins": 0
"no-prototype-builtins": 0,
"@typescript-eslint/no-explicit-any": 0
}
}
10 changes: 5 additions & 5 deletions packages/pojos/src/lib/create-metadata-map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { pojosSymbolStorage } from './storages';
* })
*/

export function createMetadataMap<TModel extends Dictionary<TModel> = unknown>(
export function createMetadataMap<TModel extends Dictionary<TModel> = any>(
key: string,
metadata: {
[key in keyof TModel]?:
Expand All @@ -24,7 +24,7 @@ export function createMetadataMap<TModel extends Dictionary<TModel> = unknown>(
| string;
}
): void;
export function createMetadataMap<TModel extends Dictionary<TModel> = unknown>(
export function createMetadataMap<TModel extends Dictionary<TModel> = any>(
key: string,
existMetadataMap: string,
metadata?: {
Expand All @@ -37,7 +37,7 @@ export function createMetadataMap<TModel extends Dictionary<TModel> = unknown>(
| null;
}
): void;
export function createMetadataMap<TModel extends Dictionary<TModel> = unknown>(
export function createMetadataMap<TModel extends Dictionary<TModel> = any>(
key: string,
metadataOrMetadataMap:
| string
Expand Down Expand Up @@ -81,7 +81,7 @@ export function createMetadataMap<TModel extends Dictionary<TModel> = unknown>(
return;
}

const result = [];
const result: unknown[] = [];
for (const [existProp, existMeta] of toMergeMetadata) {
if (entries.some(([entryProp]) => existProp === entryProp)) {
continue;
Expand All @@ -93,6 +93,6 @@ export function createMetadataMap<TModel extends Dictionary<TModel> = unknown>(
result.push(...entries);
pojosSymbolStorage.set(
symbol,
result.filter(([, meta]) => meta !== null)
result.filter(([, meta]: any) => meta !== null) as []
);
}
4 changes: 2 additions & 2 deletions packages/pojos/src/lib/pojos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ export const pojos: MapPluginInitializer<string> = (errorHandler) => {
return createInitialMapping(
sourceObj,
destinationObj,
sourceNestedMetadataMap,
destinationNestedMetadataMap,
sourceNestedMetadataMap as unknown[],
destinationNestedMetadataMap as unknown[],
(mapping) => {
mappingStorage.set(source, destination, mapping);
},
Expand Down
2 changes: 1 addition & 1 deletion packages/pojos/src/lib/storages/pojos-mapping.storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export class PojosMappingStorage implements MappingStorage<string> {
return this.storage.get(source)?.get(destination);
}

has(source: string, destination: string): boolean {
has(source: string, destination: string): boolean | undefined {
return this.storage.get(source)?.has(destination);
}

Expand Down
2 changes: 1 addition & 1 deletion packages/pojos/src/lib/storages/pojos-symbol.storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class PojosSymbolStorage {
propertyKey: string,
metadata: ReturnType<Metadata<string>[MetadataClassId.metadataFn]>
][] {
return this.storage.get(key);
return this.storage.get(key) || [];
}

has(key: symbol): boolean {
Expand Down
22 changes: 13 additions & 9 deletions packages/pojos/src/lib/utils/instantiate.util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,23 @@ export function instantiate<TModel extends Dictionary<TModel>>(
return [obj];
}

const nestedMetadataMap = [];
const nestedMetadataMap: unknown[] = [];
let i = metadata.length;
while (i--) {
const [key, meta] = metadata[i];
const valueAtKey = obj[key];
const valueAtKey = (obj as Record<string, unknown>)[key];
const metaResult = meta();

if (isPrimitiveConstructor(metaResult)) {
obj[key] = isDefined(valueAtKey) ? valueAtKey : undefined;
(obj as Record<string, unknown>)[key] = isDefined(valueAtKey)
? valueAtKey
: undefined;
continue;
}
if (isDateConstructor(metaResult)) {
obj[key] = isDefined(valueAtKey) ? new Date(valueAtKey) : new Date();
(obj as Record<string, unknown>)[key] = isDefined(valueAtKey)
? new Date(valueAtKey as number)
: new Date();
continue;
}

Expand All @@ -38,7 +42,7 @@ export function instantiate<TModel extends Dictionary<TModel>>(

nestedMetadataMap.push([key, metaResult]);
if (Array.isArray(valueAtKey)) {
obj[key] = valueAtKey.map((val) => {
(obj as Record<string, unknown>)[key] = valueAtKey.map((val) => {
const [childObj] = instantiate(metadataStorage, metaResult, val);
return childObj;
});
Expand All @@ -49,19 +53,19 @@ export function instantiate<TModel extends Dictionary<TModel>>(
const [instantiateResult] = instantiate(
metadataStorage,
metaResult,
valueAtKey
valueAtKey as Dictionary<unknown>
);
obj[key] = instantiateResult;
(obj as Record<string, unknown>)[key] = instantiateResult;
continue;
}

if (isDefined(defaultValue)) {
obj[key] = valueAtKey;
(obj as Record<string, unknown>)[key] = valueAtKey;
continue;
}

const [result] = instantiate(metadataStorage, metaResult);
obj[key] = result;
(obj as Record<string, unknown>)[key] = result;
}

return [obj, nestedMetadataMap];
Expand Down
23 changes: 9 additions & 14 deletions packages/pojos/src/lib/utils/specs/instantiate.util.spec.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
// eslint-disable-next-line @nrwl/nx/enforce-module-boundaries
import {
createSpyObject,
resetAllWhenMocks,
when,
} from '@automapper/test-util';
import { createSpyObj } from 'jest-createspyobj';
import { resetAllWhenMocks, when } from 'jest-when';
import { PojosMetadataStorage } from '../../storages';
import { instantiate } from '../instantiate.util';

describe('instantiate', () => {
const mockedMetadataStorage = createSpyObject(PojosMetadataStorage, {
getMetadata: jest.fn(),
});
const mockedMetadataStorage = createSpyObj<PojosMetadataStorage>(
PojosMetadataStorage,
['getMetadata']
);

interface Bar {
bar: string;
Expand Down Expand Up @@ -98,17 +95,15 @@ describe('instantiate', () => {
when(mockedMetadataStorage.getMetadata)
.calledWith('Foo')
.mockReturnValueOnce([
// eslint-disable-next-line @typescript-eslint/no-explicit-any
['foo', () => String as any],
['foo', () => (String as unknown) as string],
['bar', () => 'Bar'],
]);

when(mockedMetadataStorage.getMetadata)
.calledWith('Bar')
.mockReturnValueOnce([
// eslint-disable-next-line @typescript-eslint/no-explicit-any
['bar', () => String as any],
['date', () => Date],
['bar', () => (String as unknown) as string],
['date', () => (Date as unknown) as Date],
]);
}
});

0 comments on commit deedc1b

Please sign in to comment.