mirror of
https://github.com/coder/coder.git
synced 2026-09-24 15:04:27 +08:00
refactor: Add 'src' folder in 'site' (#445)
This refactoring re-organizes the `site` folder to have a nested `src` folder. Originally, [we wanted to keep the directory structure shallow](https://github.com/coder/coder/pull/8#issuecomment-1009578910) - but there were two points that motivated this change to introduce the `src` level. 1. We have several non-`src` folders now (`e2e`, `static`, `html_templates`, `.storybook`) 2. Having a `src` folder makes it easier to run XState Typegen So given those two data points - I believe it makes sense to revisit that and introduce a `src` folder.
This commit is contained in:
@@ -0,0 +1,17 @@
|
||||
import { firstOrItem } from "./array"
|
||||
|
||||
describe("array", () => {
|
||||
describe("firstOrItem", () => {
|
||||
it("returns null if empty array", () => {
|
||||
expect(firstOrItem([], null)).toBeNull()
|
||||
})
|
||||
|
||||
it("returns first item if array with more one item", () => {
|
||||
expect(firstOrItem(["a", "b"], "c")).toEqual("a")
|
||||
})
|
||||
|
||||
it("returns item if single item", () => {
|
||||
expect(firstOrItem("c", "d")).toEqual("c")
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,17 @@
|
||||
/**
|
||||
* Helper function that, given an array or a single item:
|
||||
* - If an array with no elements, returns null
|
||||
* - If an array with 1 or more elements, returns the first element
|
||||
* - If a single item, returns that item
|
||||
*/
|
||||
export const firstOrItem = <T>(itemOrItems: undefined | T | T[], defaults: T): T => {
|
||||
if (Array.isArray(itemOrItems)) {
|
||||
return itemOrItems.length > 0 ? itemOrItems[0] : defaults
|
||||
}
|
||||
|
||||
if (typeof itemOrItems === "undefined") {
|
||||
return defaults
|
||||
}
|
||||
|
||||
return itemOrItems
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
export * from "./array"
|
||||
export * from "./swr"
|
||||
@@ -0,0 +1,17 @@
|
||||
/**
|
||||
* unsafeSWRArgument
|
||||
*
|
||||
* Helper function for working with SWR / useSWR in the TypeScript world.
|
||||
* TypeScript is helpful in enforcing type-safety, but SWR is designed to
|
||||
* with the expectation that, if the argument is not available, an exception
|
||||
* will be thrown.
|
||||
*
|
||||
* This just helps in abiding by those rules, explicitly, and lets us suppress
|
||||
* the lint warning in a single place.
|
||||
*/
|
||||
export const unsafeSWRArgument = <T>(arg: T | null | undefined): T => {
|
||||
if (typeof arg === "undefined" || arg === null) {
|
||||
throw "SWR: Expected exception because the argument is not available"
|
||||
}
|
||||
return arg
|
||||
}
|
||||
Reference in New Issue
Block a user