Skip to content

Commit

Permalink
chore: Clean up some internals (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
72636c authored Jul 14, 2020
1 parent 942867b commit 4844e9a
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 27 deletions.
2 changes: 1 addition & 1 deletion src/competencies/oNet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* A selection of abilities and skills from the O*NET 24.3 Database, sponsored
* by the U.S. Department of Labor, Employment and Training Administration.
*
* The database is licensed under a Creative Commons Attribution 4.0
* The database is provided under a Creative Commons Attribution 4.0
* International License.
*
* {@link https://www.onetcenter.org/license_db.html}
Expand Down
13 changes: 13 additions & 0 deletions src/education/institutions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,20 @@ export const all: EducationInstitution[] = [
].flat();

interface FilterProps {
/**
* Limit institutions to those that operate in at least one of the specified
* countries.
*
* Omit this filter to return institutions from all countries.
*/
country?: CountryCode | CountryCode[];

/**
* Limit institutions to those that offer an education programme that
* corresponds to at least one of the specified ISCED 2011 levels.
*
* Omit this filter to return institutions regardless of education level.
*/
level?: EducationLevel | EducationLevel[];
}

Expand Down
4 changes: 2 additions & 2 deletions src/education/institutions/row.ts
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,6 @@ export const universities: EducationInstitution[] = ([
},
] as const).map((props) =>
Object.assign(props, {
levels: [6, 7, 8] as const,
}),
levels: [6, 7, 8],
} as const),
);
44 changes: 26 additions & 18 deletions src/filter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,40 +5,48 @@ describe('createSubsetMatcher', () => {
const match = createSubsetMatcher();

it.each([
['no elements', []],
['undefined', [undefined]],
['null', [null]],
['empty string', ['']],
['non-empty string', ['abc123']],
])('allows array with %s', (_, value) => expect(match(value)).toBe(true));
['undefined', undefined],
['null', null],
['empty string', ''],
['non-empty string', 'abc123'],
['array with no elements', []],
['array with undefined', [undefined]],
['array with null', [null]],
['array with empty string', ['']],
['array with non-empty string', ['abc123']],
])('allows %s', (_, value) => expect(match(value)).toBe(true));
});

describe('given one value', () => {
const match = createSubsetMatcher(1);

it.each([
['matching element only', [1]],
['matching elements only', [1, 1]],
['mix of matching and non-matching elements', [2, 1, 3]],
])('allows array with %s', (_, value) => expect(match(value)).toBe(true));
['matching element', 1],
['array with matching element only', [1]],
['array with matching elements only', [1, 1]],
['array with matching and non-matching elements', [2, 1, 3]],
])('allows %s', (_, value) => expect(match(value)).toBe(true));

it.each([
['no elements', []],
['non-matching elements only', [3, 2]],
])('denies array with %s', (_, value) => expect(match(value)).toBe(false));
['non-matching element', 2],
['array with no elements', []],
['array with non-matching elements only', [3, 2]],
])('denies %s', (_, value) => expect(match(value)).toBe(false));
});

describe('given two values', () => {
const match = createSubsetMatcher(['a', 'b']);

it.each([
['first matching element only', ['a']],
['second matching element only', ['b']],
['both matching elements', ['b', 'a']],
['mix of matching and non-matching elements', ['c', 'a', 'd', 'b']],
])('allows array with %s', (_, value) => expect(match(value)).toBe(true));
['matching element', 'a'],
['array with first matching element only', ['a']],
['array with second matching element only', ['b']],
['array with both matching elements', ['b', 'a']],
['array with matching and non-matching elements', ['c', 'a', 'd', 'b']],
])('allows %s', (_, value) => expect(match(value)).toBe(true));

it.each([
['non-matching element', 'c'],
['no elements', []],
['non-matching elements only', ['d', 'c']],
])('denies array with %s', (_, value) => expect(match(value)).toBe(false));
Expand Down
12 changes: 8 additions & 4 deletions src/filter.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
const isArray = <T>(input: T | ReadonlyArray<T>): input is ReadonlyArray<T> =>
Array.isArray(input);

export const createSubsetMatcher = <T>(
value?: T | T[],
): ((inputs: ReadonlyArray<T>) => boolean) => {
value?: T | ReadonlyArray<T>,
): ((input: T | ReadonlyArray<T>) => boolean) => {
if (typeof value === 'undefined') {
return () => true;
}

const values = new Set(Array.isArray(value) ? value : [value]);
const values = new Set(isArray(value) ? value : [value]);

return (inputs) => inputs.some((input) => values.has(input));
return (input) =>
isArray(input) ? input.some((i) => values.has(i)) : values.has(input);
};
2 changes: 0 additions & 2 deletions src/readme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

import { fakeHr } from '.';

console.log('hello there');

console.log(fakeHr.competencies.all[0]);

console.log(fakeHr.education.institutions.all[0]);

0 comments on commit 4844e9a

Please sign in to comment.