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 all 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
6 changes: 4 additions & 2 deletions __tests__/services/instance.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,15 @@ 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
5 changes: 3 additions & 2 deletions hooks/pager.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import _ from 'lodash';
import { useState } from 'react';

import { AidboxResource, Bundle } from 'src/contrib/aidbox';
Expand Down Expand Up @@ -35,7 +34,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')),
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
45 changes: 25 additions & 20 deletions services/fhir.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import _ from 'lodash';

import { AidboxReference, AidboxResource, Bundle, ValueSet } from 'src/contrib/aidbox';

import { failure, RemoteDataResult } from '../libs/remoteData';
Expand Down Expand Up @@ -145,7 +143,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 +223,42 @@ 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(
(attribute) =>
['id', 'resourceType', '_id', 'resource', 'display', 'identifier', 'uri', 'localRef', 'extension'].indexOf(
attribute
) === -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);

return _.mapValues(entriesByResourceType, (entries) => _.map(entries, (entry) => entry.resource!));
const entriesByResourceType = {};
if (!bundle.entry) {
return entriesByResourceType;
}
bundle.entry.forEach(function(entry) {
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];
if (!typeResources) {
return undefined;
}
const index = typeResources.findIndex((resource: T) => resource.id === reference.id);
return typeResources[index];
}

export function getIncludedResources<T extends AidboxResource>(
Expand Down
37 changes: 24 additions & 13 deletions services/instance.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,31 @@
import axios from 'axios';
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) + '=' + encodeURIComponent(value);

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
14 changes: 7 additions & 7 deletions services/service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { AxiosRequestConfig } from 'axios';
import _ from 'lodash';
import { failure, isFailure, isSuccess, isSuccessAll, RemoteDataResult, success } from '../libs/remoteData';
import { axiosInstance } from './instance';

Expand Down Expand Up @@ -64,15 +63,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