Skip to content

Commit

Permalink
fix: use string endsWith to filter by extension (#3097)
Browse files Browse the repository at this point in the history
  • Loading branch information
erezrokah committed Jan 16, 2020
1 parent 9210843 commit 6a13a85
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
18 changes: 17 additions & 1 deletion packages/netlify-cms-lib-util/src/__tests__/backendUtil.spec.js
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -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' }]);
});
});
5 changes: 3 additions & 2 deletions packages/netlify-cms-lib-util/src/backendUtil.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down

0 comments on commit 6a13a85

Please sign in to comment.