Fix parseAPI not working with non-"namespaces" pathnames. (#450)

* fix parseApi not accepting versions that don't start with 'v'
* add unit tests

Signed-off-by: Sebastian Malton <smalton@mirantis.com>
Co-authored-by: Sebastian Malton <smalton@mirantis.com>
This commit is contained in:
Sebastian MaltonandSebastian Malton authored and GitHub committed 2020-06-16 10:51:39 -04:00
1 parent 0661954a27
commit bce7aec87d
10 files changed
+182 -37

No files matched your search

@@ -0,0 +1,31 @@
import { split } from "../arrays";
describe("split array on element tests", () => {
test("empty array", () => {
expect(split([], 10)).toStrictEqual([[], [], false]);
});
test("one element, not in array", () => {
expect(split([1], 10)).toStrictEqual([[1], [], false]);
});
test("ten elements, not in array", () => {
expect(split([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], 10)).toStrictEqual([[0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [], false]);
});
test("one elements, in array", () => {
expect(split([1], 1)).toStrictEqual([[], [], true]);
});
test("ten elements, in front array", () => {
expect(split([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], 0)).toStrictEqual([[], [1, 2, 3, 4, 5, 6, 7, 8, 9], true]);
});
test("ten elements, in middle array", () => {
expect(split([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], 4)).toStrictEqual([[0, 1, 2, 3], [5, 6, 7, 8, 9], true]);
});
test("ten elements, in end array", () => {
expect(split([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], 9)).toStrictEqual([[0, 1, 2, 3, 4, 5, 6, 7, 8], [], true]);
});
});
+19
View File
@@ -0,0 +1,19 @@
/**
* This function splits an array into two sub arrays on the first instance of
* element (from the left). If the array does not contain the element. The
* return value is defined to be `[array, [], false]`. If the element is in
* the array then the return value is `[left, right, true]` where `left` is
* the elements of `array` from `[0, index)` and `right` is `(index, length)`
* @param array the full array to split into two sub-arrays
* @param element the element in the middle of the array
* @returns the left and right sub-arrays which when conjoined with `element`
* is the same as `array`, and `true`
*/
export function split<T>(array: Array<T>, element: T): [Array<T>, Array<T>, boolean] {
const index = array.indexOf(element);
if (index < 0) {
return [array, [], false];
}
return [array.slice(0, index), array.slice(index+1, array.length), true]
}