fix: use string endsWith to filter by extension (#3097)

This commit is contained in:
Erez Rokah 2020-01-16 12:03:30 +02:00 committed by GitHub
parent 92108431f0
commit 6a13a85e26
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 3 deletions

View File

@ -1,4 +1,9 @@
import { parseLinkHeader, getAllResponses, getPathDepth } from '../backendUtil';
import {
parseLinkHeader,
getAllResponses,
getPathDepth,
filterByPropExtension,
} from '../backendUtil';
import { oneLine } from 'common-tags';
import nock from 'nock';
@ -79,3 +84,14 @@ describe('getPathDepth', () => {
expect(getPathDepth('{{year}}/{{slug}}')).toBe(2);
});
});
describe('filterByPropExtension', () => {
it('should return filtered array based on extension', () => {
expect(
filterByPropExtension('.html.md', 'path')([{ path: 'file.html.md' }, { path: 'file.json' }]),
).toEqual([{ path: 'file.html.md' }]);
expect(
filterByPropExtension('html.md', 'path')([{ path: 'file.html.md' }, { path: 'file.json' }]),
).toEqual([{ path: 'file.html.md' }]);
});
});

View File

@ -1,14 +1,15 @@
import { flow, fromPairs, get } from 'lodash';
import { map } from 'lodash/fp';
import { fromJS } from 'immutable';
import { fileExtension } from './path';
import unsentRequest from './unsentRequest';
import APIError from './APIError';
type Formatter = (res: Response) => Promise<string | Blob | unknown>;
export const filterByPropExtension = (extension: string, propName: string) => <T>(arr: T[]) =>
arr.filter(el => fileExtension(get(el, propName)) === extension);
arr.filter(el =>
get(el, propName, '').endsWith(extension.startsWith('.') ? extension : `.${extension}`),
);
const catchFormatErrors = (format: string, formatter: Formatter) => (res: Response) => {
try {