dogu-utils 0.0.4 → 0.0.6
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/dist/index.js +1 -2
- package/dist/src/index.d.ts +2 -0
- package/dist/src/react/hooks/useOpenCloseState.d.ts +1 -0
- package/dist/src/react/index.d.ts +2 -0
- package/dist/src/react/utils/createSafeContext.d.ts +7 -0
- package/dist/src/types/EmptyObject.d.ts +1 -0
- package/dist/src/types/index.d.ts +1 -0
- package/dist/src/utils/EarlyReturn.d.ts +8 -0
- package/dist/src/utils/EarlyReturn.spec.d.ts +1 -0
- package/dist/src/utils/index.d.ts +1 -0
- package/dist/vitest.config.d.ts +2 -0
- package/package.json +11 -7
- package/rollup.config.js +0 -23
- package/src/index.ts +0 -2
- package/src/react/hooks/useOpenCloseState.ts +0 -10
- package/src/react/index.ts +0 -2
- package/src/react/utils/createSafeContext.tsx +0 -26
- package/src/types/EmptyObject.ts +0 -1
- package/src/types/index.ts +0 -1
- package/src/utils/EarlyReturn.spec.ts +0 -36
- package/src/utils/EarlyReturn.ts +0 -29
- package/src/utils/index.ts +0 -1
- package/tsconfig.json +0 -14
- package/vitest.config.ts +0 -7
package/dist/index.js
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
import { useState, useCallback,
|
1
|
+
import { useState, useCallback, useContext, createContext } from 'react';
|
2
2
|
import { jsx } from 'react/jsx-runtime';
|
3
3
|
|
4
4
|
var useOpenCloseState = function useOpenCloseState(initialState) {
|
@@ -38,4 +38,3 @@ var createSafeContext = function createSafeContext(defaultValue) {
|
|
38
38
|
};
|
39
39
|
|
40
40
|
export { createSafeContext, useOpenCloseState };
|
41
|
-
//# sourceMappingURL=index.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
export declare const useOpenCloseState: (initialState?: boolean) => readonly [boolean, () => void, () => void];
|
@@ -0,0 +1,7 @@
|
|
1
|
+
import { ReactNode } from "react";
|
2
|
+
type ProviderProps<Value> = {
|
3
|
+
children: ReactNode;
|
4
|
+
value: Value;
|
5
|
+
};
|
6
|
+
export declare const createSafeContext: <Value>(defaultValue?: Value) => readonly [({ children, value }: ProviderProps<Value>) => import("react/jsx-runtime").JSX.Element, () => Value & ({} | undefined)];
|
7
|
+
export {};
|
@@ -0,0 +1 @@
|
|
1
|
+
export type EmptyObject = Record<PropertyKey, never>;
|
@@ -0,0 +1 @@
|
|
1
|
+
export * from "./EmptyObject";
|
@@ -0,0 +1 @@
|
|
1
|
+
export {};
|
@@ -0,0 +1 @@
|
|
1
|
+
export * from "./EarlyReturn";
|
package/package.json
CHANGED
@@ -1,9 +1,18 @@
|
|
1
1
|
{
|
2
2
|
"name": "dogu-utils",
|
3
|
-
"version": "0.0.
|
3
|
+
"version": "0.0.6",
|
4
4
|
"main": "./dist/index.js",
|
5
|
+
"files": [
|
6
|
+
"dist"
|
7
|
+
],
|
5
8
|
"types": "./dist/index.d.ts",
|
6
9
|
"type": "module",
|
10
|
+
"scripts": {
|
11
|
+
"publish": "pnpm run build && npm publish --access=public",
|
12
|
+
"build": "rollup -c",
|
13
|
+
"watch": "rollup -cw",
|
14
|
+
"test": "vitest"
|
15
|
+
},
|
7
16
|
"devDependencies": {
|
8
17
|
"@babel/core": "^7.24.4",
|
9
18
|
"@babel/preset-env": "^7.24.4",
|
@@ -17,10 +26,5 @@
|
|
17
26
|
},
|
18
27
|
"peerDependencies": {
|
19
28
|
"react": "^18.2.0"
|
20
|
-
},
|
21
|
-
"scripts": {
|
22
|
-
"build": "rollup -c",
|
23
|
-
"watch": "rollup -cw",
|
24
|
-
"test": "vitest"
|
25
29
|
}
|
26
|
-
}
|
30
|
+
}
|
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,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
|
-
}
|
package/src/react/index.ts
DELETED
@@ -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
|
-
}
|
package/src/types/EmptyObject.ts
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
export type EmptyObject = Record<PropertyKey, never>
|
package/src/types/index.ts
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
export * from "./EmptyObject"
|
@@ -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
|
-
})
|
package/src/utils/EarlyReturn.ts
DELETED
@@ -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
|
-
private isAlreadyReturned<T>(returnValue: T | undefined) {
|
27
|
-
return returnValue !== undefined
|
28
|
-
}
|
29
|
-
}
|
package/src/utils/index.ts
DELETED
@@ -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
|
-
}
|