Add feed validation and display
This commit is contained in:
@@ -310,6 +310,61 @@ describe("Tools", () => {
|
||||
})
|
||||
})
|
||||
|
||||
describe("displayList", () => {
|
||||
it("should return an empty string when the list is empty", () => {
|
||||
const list = []
|
||||
const output = T.displayList(list)
|
||||
|
||||
expect(output).toEqual("")
|
||||
})
|
||||
|
||||
it("should return a single entry when list length is one", () => {
|
||||
const list = ["Apple"]
|
||||
const output = T.displayList(list)
|
||||
|
||||
expect(output).toEqual("Apple")
|
||||
})
|
||||
|
||||
it("should return a string of both items when the list length is two", () => {
|
||||
const list = ["Apple", "Banana"]
|
||||
const output = T.displayList(list)
|
||||
|
||||
expect(output).toEqual("Apple and Banana")
|
||||
})
|
||||
|
||||
it("should return a string of all items when the list length is three", () => {
|
||||
const list = ["Apple", "Banana", "Cherry"]
|
||||
const output = T.displayList(list)
|
||||
|
||||
expect(output).toEqual("Apple, Banana, and Cherry")
|
||||
})
|
||||
|
||||
it("should return a truncated string of all items when the list is long", () => {
|
||||
const list = [
|
||||
"Apple",
|
||||
"Banana",
|
||||
"Orange",
|
||||
"Pear",
|
||||
"Grapes",
|
||||
"Mango",
|
||||
"Pineapple",
|
||||
"Kiwi",
|
||||
"Strawberry",
|
||||
"Blueberry",
|
||||
]
|
||||
const output = T.displayList(list)
|
||||
|
||||
expect(output).toEqual("Apple, Banana, Orange, Pear, Grapes, Mango, and 4 others")
|
||||
})
|
||||
|
||||
it("should return a string of both items with list of two numbers", () => {
|
||||
const list = [7, 17]
|
||||
const output = T.displayList(list)
|
||||
|
||||
expect(output).toEqual("7 and 17")
|
||||
})
|
||||
})
|
||||
|
||||
describe("Collection Operations", () => {
|
||||
it("should handle group operations", () => {
|
||||
const items = [
|
||||
|
||||
@@ -10,7 +10,7 @@ export type Nil = null | undefined
|
||||
|
||||
export const isNil = <T>(x: T, ...args: unknown[]) => x === undefined || x === null
|
||||
|
||||
export const isNotNil = <T>(x: T, ...args: unknown[]) => x !== undefined || x !== null
|
||||
export const isNotNil = <T>(x: T, ...args: unknown[]) => x !== undefined && x !== null
|
||||
|
||||
export const assertNotNil = <T>(x: T, ...args: unknown[]) => x!
|
||||
|
||||
@@ -1358,6 +1358,19 @@ export const ellipsize = (s: string, l: number, suffix = "...") => {
|
||||
return s + suffix
|
||||
}
|
||||
|
||||
/** Displays a list of items with oxford commas and a chosen conjunction */
|
||||
export const displayList = <T>(xs: T[], conj = "and", n = 6) => {
|
||||
if (xs.length > n + 2) {
|
||||
return `${xs.slice(0, n).join(", ")}, ${conj} ${xs.length - n} others`
|
||||
}
|
||||
|
||||
if (xs.length < 3) {
|
||||
return xs.join(` ${conj} `)
|
||||
}
|
||||
|
||||
return `${xs.slice(0, -1).join(", ")}, ${conj} ${xs.slice(-1).join("")}`
|
||||
}
|
||||
|
||||
/** Generates a hash string from input string */
|
||||
export const hash = (s: string) =>
|
||||
Math.abs(s.split("").reduce((a, b) => ((a << 5) - a + b.charCodeAt(0)) | 0, 0)).toString()
|
||||
|
||||
Reference in New Issue
Block a user