dogu-utils 0.0.3 → 0.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,7 +1,10 @@
1
1
  {
2
2
  "name": "dogu-utils",
3
- "version": "0.0.3",
3
+ "version": "0.0.5",
4
4
  "main": "./dist/index.js",
5
+ "files": [
6
+ "dist"
7
+ ],
5
8
  "types": "./dist/index.d.ts",
6
9
  "type": "module",
7
10
  "devDependencies": {
package/rollup.config.js DELETED
@@ -1,23 +0,0 @@
1
- import babel from "@rollup/plugin-babel"
2
- import typescript from "@rollup/plugin-typescript"
3
-
4
- export default {
5
- input: "./src/index.ts",
6
- output: {
7
- file: "./dist/index.js",
8
- format: "es",
9
- sourcemap: true
10
- },
11
- plugins: [
12
- babel({
13
- babelHelpers: "bundled",
14
- presets: [
15
- "@babel/preset-env",
16
- "@babel/preset-react",
17
- "@babel/preset-typescript"
18
- ],
19
- extensions: [".js", ".jsx", ".ts", ".tsx"]
20
- }),
21
- typescript()
22
- ]
23
- }
package/src/index.ts DELETED
@@ -1 +0,0 @@
1
- export * from "./react"
@@ -1,10 +0,0 @@
1
- import { useCallback, useState } from "react"
2
-
3
- export const useOpenCloseState = (initialState = false) => {
4
- const [isOpen, setIsOpen] = useState(initialState)
5
-
6
- const open = useCallback(() => setIsOpen(true), [])
7
- const close = useCallback(() => setIsOpen(false), [])
8
-
9
- return [isOpen, open, close] as const
10
- }
@@ -1,2 +0,0 @@
1
- export * from "./hooks/useOpenCloseState"
2
- export * from "./utils/createSafeContext"
@@ -1,26 +0,0 @@
1
- import { ReactNode, createContext, useContext } from "react"
2
-
3
- type ProviderProps<Value> = {
4
- children: ReactNode
5
- value: Value
6
- }
7
-
8
- export const createSafeContext = <Value,>(defaultValue?: Value) => {
9
- const Context = createContext<Value | null>(defaultValue ?? null)
10
-
11
- const Provider = ({ children, value }: ProviderProps<Value>) => {
12
- return <Context.Provider value={value}>{children}</Context.Provider>
13
- }
14
-
15
- const useSafeContext = () => {
16
- const context = useContext(Context)
17
- if (context === null) {
18
- throw new Error(
19
- "useSafeContext must be used within a Provider, or give it a default value."
20
- )
21
- }
22
- return context
23
- }
24
-
25
- return [Provider, useSafeContext] as const
26
- }
@@ -1,36 +0,0 @@
1
- import { describe, it, expect } from "vitest"
2
- import { EarlyReturn } from "./EarlyReturn"
3
-
4
- describe("EarlyReturn", () => {
5
- it("should return value when condition is true", () => {
6
- const earlyReturn = new EarlyReturn<number>()
7
- const result = earlyReturn.case(true, () => 42).default(() => 0)
8
- expect(result).toBe(42)
9
- })
10
-
11
- it("should not override return value if already set", () => {
12
- const earlyReturn = new EarlyReturn<number>()
13
- const result = earlyReturn
14
- .case(true, () => 42)
15
- .case(true, () => 24)
16
- .default(() => 0)
17
- expect(result).toBe(42)
18
- })
19
-
20
- it("should return default value if no case matches", () => {
21
- const earlyReturn = new EarlyReturn<number>()
22
- const result = earlyReturn
23
- .case(false, () => 42)
24
- .case(false, () => 24)
25
- .default(() => 0)
26
- expect(result).toBe(0)
27
- })
28
-
29
- it("should return undefined if no case matches and no default is provided", () => {
30
- const earlyReturn = new EarlyReturn<number>()
31
- const result = earlyReturn
32
- .case(false, () => 42)
33
- .case(false, () => 24).returnValue
34
- expect(result).toBeUndefined()
35
- })
36
- })
@@ -1,29 +0,0 @@
1
- type Callback<T> = (...params: unknown[]) => T
2
-
3
- export class EarlyReturn<T> {
4
- returnValue: T | undefined
5
-
6
- case(condition: boolean, func: Callback<T>): EarlyReturn<T> {
7
- if (this.isAlreadyReturned(this.returnValue)) {
8
- return this
9
- }
10
-
11
- if (condition) {
12
- this.returnValue = func()
13
- return this
14
- }
15
-
16
- return this
17
- }
18
-
19
- default(func: Callback<T>): T {
20
- if (this.isAlreadyReturned(this.returnValue)) {
21
- return this.returnValue
22
- }
23
- return func()
24
- }
25
-
26
- isAlreadyReturned<T>(returnValue: T | undefined) {
27
- return returnValue !== undefined
28
- }
29
- }
@@ -1 +0,0 @@
1
- export * from "./EarlyReturn"
package/tsconfig.json DELETED
@@ -1,14 +0,0 @@
1
- {
2
- "compilerOptions": {
3
- "target": "es5",
4
- "lib": ["dom", "esnext"],
5
- "jsx": "react-jsx",
6
- "module": "es6",
7
- "moduleResolution": "node",
8
- "baseUrl": "./",
9
- "strict": true,
10
- "esModuleInterop": true,
11
- "declaration": true,
12
- "declarationDir": "./dist"
13
- }
14
- }
package/vitest.config.ts DELETED
@@ -1,7 +0,0 @@
1
- import { defineConfig } from "vitest/config"
2
-
3
- export default defineConfig({
4
- test: {
5
- include: ["**/*.spec.ts?(x)"]
6
- }
7
- })