ISSUE-020 - Plain, static SlotSelector component defined. Not too fancy looking.

To save space and reduce redundant data I created a utility function called findPrefixCount() and wrote a suite of tests for it.
This commit is contained in:
Kyle Geib committed 2022-01-14 17:16:27 -05:00
1 parent 6a8e5f28b2
commit 35ca424dd6
6 files changed
+163 -20

No files matched your search

+53
View File
@@ -0,0 +1,53 @@
import { findPrefixCount } from "./strings";
describe("strings", () => {
describe("findPrefixCount", () => {
describe("finds matches", () => {
it("gets correct count all the same", () => {
expect(findPrefixCount([
"hello",
"hello",
"hello",
"hello",
], true)).toBe(5);
});
it("gets correct count for hello prefix", () => {
expect(findPrefixCount([
"hello world",
"hello mother",
"hello father",
"hello!",
], true)).toBe(5);
});
it("gets zero when nothing matches", () => {
expect(findPrefixCount([
"cat",
"dog",
"rat",
], true)).toBe(0);
});
it("returns 0 when only given a single string", () => {
expect(findPrefixCount([
"by myself",
], true)).toBe(0);
});
it("returns 0 when given no strings", () => {
expect(findPrefixCount([], true)).toBe(0);
});
it("is case sensitive", () => {
expect(findPrefixCount([
"Hello",
"HELLO",
"Hello",
], true)).toBe(1);
});
it("is correctly case-insensitive", () => {
expect(findPrefixCount([
"Hello",
"HELLO",
"Hello",
], false)).toBe(5);
});
});
});
});
+25
View File
@@ -0,0 +1,25 @@
/**
* Gives you the number of characters that prefix all the given strings.
* I use this to remove redundant repeated characters shared between file names.
* Basically, this is a method to do the " you find in a list.
* @param strings
* @returns number of characters that all strings share from the start
*/
export function findPrefixCount(strings: Array<string>, caseSensitive = true): number {
if (strings.length <= 1) {
return 0; // Cannot find anything. So, most neutral return is 0.
}
let maskLength = strings[0].length;
for (let x = 1; x < strings.length; ++x) {
for (let c = 0; c < maskLength; c++) {
const left = caseSensitive ? strings[0][c] : strings[0][c].toLowerCase();
const right = caseSensitive ? strings[x][c] : strings[x][c].toLowerCase();
if (left !== right) {
maskLength = c;
break;
}
}
}
return maskLength;
}