Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rid of lodash #12

Merged
merged 8 commits into from
Feb 4, 2020
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions __tests__/services/fhir.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,8 @@ describe('Service `fhir`', () => {
extraField: true,
})
).toBeFalsy();

expect(isReference({} as any)).toBeTruthy();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be false

});

test('method `extractBundleResources`', () => {
Expand Down
4 changes: 4 additions & 0 deletions __tests__/services/instance.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,17 @@ describe('Service `instance`', () => {
describe('method `buildQueryParams`', () => {
[
[{ a: 1 }, 'a=1'],
[{ a: null }, 'a=null'],
[{ a: undefined }, ''],
[{ a: true }, 'a=true'],
[{ a: '42' }, 'a=42'],
[{ a: [1, 2, 3] }, 'a=1&a=2&a=3'],
[{ a: [1, 2, [3, 4, [5, 6]]] }, 'a=1&a=2&a=3%2C4%2C5%2C6'],
[{ a: 1, b: 2 }, 'a=1&b=2'],
[{ a: 1, b: undefined }, 'a=1'],
[{ a: 1, _has: 1 }, 'a=1&_has:1'],
[{ a: 1, _has: [1, 2, 3] }, 'a=1&_has:1&_has:2&_has:3'],
[{ a: { b: { c: 42 } } }, 'a=%5Bobject%20Object%5D'],
].forEach(([params, query]) => {
test(`use for ${JSON.stringify(params)} 'returns ${query}`, () => {
expect(buildQueryParams(<object>params)).toEqual(query);
Expand Down
4 changes: 3 additions & 1 deletion hooks/pager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ export function usePager<T extends AidboxResource>(
resources,
{
loadNext: () => setPageToLoad((currentPage) => currentPage + 1),
hasNext: isSuccess(resources) && !!_.find(resources.data.link, { relation: 'next' }),
hasNext:
isSuccess(resources) &&
Boolean(resources.data.link && resources.data.link.some((link) => link.relation == 'next')),
ufocoder marked this conversation as resolved.
Show resolved Hide resolved
reload: () => {
setPageToLoad(1);
setReloadsCount((c) => c + 1);
Expand Down
2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,11 @@
"@testing-library/react": "^9.4.0",
"@testing-library/react-hooks": "^3.2.1",
"@types/jest": "^24.0.25",
"@types/lodash": "^4.14.149",
"axios": "^0.19.1",
"coveralls": "^3.0.9",
"husky": "^4.2.0",
"jest": "^24.9.0",
"lint-staged": "^10.0.2",
"lodash": "^4.17.15",
"prettier": "^1.19.1",
"react": "^16.12.0",
"react-test-renderer": "^16.12.0",
Expand Down
38 changes: 21 additions & 17 deletions services/fhir.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ export async function saveFHIRResources<R extends AidboxResource>(
url: '/',
data: {
type: bundleType,
entry: _.map(resources, (resource) => {
entry: resources.map((resource) => {
const versionId = resource.meta && resource.meta.versionId;

return {
Expand Down Expand Up @@ -225,35 +225,39 @@ export function makeReference<T extends AidboxResource>(
export function isReference<T extends AidboxResource>(
resource: T | AidboxReference<T>
): resource is AidboxReference<T> {
return _.isEmpty(
_.difference(_.keys(resource), [
'id',
'resourceType',
'_id',
'resource',
'display',
'identifier',
'uri',
'localRef',
'extension',
])
);
return !Object.keys(resource).filter(
(resource) =>
ufocoder marked this conversation as resolved.
Show resolved Hide resolved
['id', 'resourceType', '_id', 'resource', 'display', 'identifier', 'uri', 'localRef', 'extension'].indexOf(
resource
) === -1
).length;
}

export type ResourcesMap<T extends AidboxResource> = { [x: string]: T[] | undefined };

export function extractBundleResources<T extends AidboxResource>(bundle: Bundle<T>): ResourcesMap<T> {
const entriesByResourceType = _.groupBy(bundle.entry, (entry) => entry.resource!.resourceType);
const entriesByResourceType = {};

return _.mapValues(entriesByResourceType, (entries) => _.map(entries, (entry) => entry.resource!));
bundle.entry?.forEach(function(entry) {
ufocoder marked this conversation as resolved.
Show resolved Hide resolved
const type = entry.resource!.resourceType;
if (!entriesByResourceType[type]) {
entriesByResourceType[type] = [];
}
entriesByResourceType[type].push(entry.resource);
});

return entriesByResourceType;
}

export function getIncludedResource<T extends AidboxResource>(
// TODO: improve type for includedResources: must contain T
resources: ResourcesMap<T | any>,
reference: AidboxReference<T>
) {
return _.find<T>(resources[reference.resourceType], (resource) => resource.id === reference.id);
const typeResources = resources[reference.resourceType];
const index = typeResources ? typeResources.findIndex((resource: T) => resource.id === reference.id) : -1;
ufocoder marked this conversation as resolved.
Show resolved Hide resolved

return typeResources && index !== -1 ? typeResources[index] : undefined;
}

export function getIncludedResources<T extends AidboxResource>(
Expand Down
36 changes: 25 additions & 11 deletions services/instance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,32 @@ import _ from 'lodash';

import { Token } from './token';

const flatten = (list: Array<any>): Array<any> =>
list.reduce((a: Array<any>, b: any) => a.concat(Array.isArray(b) ? flatten(b) : b), []);

const encodeEntry = (key: string, value: any) =>
encodeURIComponent(key) + (key === '_has' ? ':' : '=') + encodeURIComponent(value);
ufocoder marked this conversation as resolved.
Show resolved Hide resolved

const packEntry = (accumulator: Array<string>, [key, value]: [string, any]) => {
if (typeof value === 'undefined') {
return accumulator;
}

if (Array.isArray(value)) {
value.forEach((value) => {
accumulator.push(encodeEntry(key, Array.isArray(value) ? flatten(value) : value));
});
} else {
accumulator.push(encodeEntry(key, value));
}

return accumulator;
};

export function buildQueryParams(params: object) {
return _.chain(params)
.keys()
.flatMap((k) =>
_.map(
_.reject(_.concat([], params[k]), _.isUndefined),
// TODO: get rid of _has - wrong usage
(v) => encodeURIComponent(k) + (k === '_has' ? ':' : '=') + encodeURIComponent(v)
)
)
.join('&')
.value();
return Object.entries(params)
.reduce(packEntry, [] as Array<string>)
.join('&');
}

export const axiosInstance = axios.create({
Expand Down
13 changes: 7 additions & 6 deletions services/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,16 @@ function createKeysMapTransformer<K = any>(keys: Array<K>) {

export function sequenceArray<T, F>(remoteDataArray: Array<RemoteDataResult<T, F>>): RemoteDataResult<T[], F[]> {
if (isSuccessAll(remoteDataArray)) {
return success(_.map(remoteDataArray, (remoteDataResult) => remoteDataResult.data));
return success(remoteDataArray.map((remoteDataResult) => remoteDataResult.data));
}

return failure(
_.compact(
_.map(remoteDataArray, (remoteDataResult) =>
isFailure(remoteDataResult) ? remoteDataResult.error : undefined
)
)
remoteDataArray.reduce((accumulator, remoteDataResult: RemoteDataResult<T, F>) => {
if (isFailure(remoteDataResult)) {
accumulator.push(remoteDataResult.error);
}
return accumulator;
}, [] as Array<F>)
);
}

Expand Down