dogu-utils 0.0.1 → 0.0.3

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/README.md ADDED
@@ -0,0 +1,17 @@
1
+ # Dogu-Utils
2
+
3
+ Dogu-Utils is a utility library for TypeScript and JavaScript. The name "Dogu" (도구) is a Korean word that translates to "tool" in English. This library aims to be a useful tool for developers, providing a collection of utility features.
4
+
5
+ ## Features
6
+
7
+ - TypeScript support
8
+ - React hooks
9
+ - Utility functions
10
+
11
+ ## Installation
12
+
13
+ To install Dogu-Utils, run the following command:
14
+
15
+ ```sh
16
+ npm install dogu-utils
17
+ ```
@@ -0,0 +1 @@
1
+ export * from "./react";
package/dist/index.js ADDED
@@ -0,0 +1,41 @@
1
+ import { useState, useCallback, createContext, useContext } from 'react';
2
+ import { jsx } from 'react/jsx-runtime';
3
+
4
+ var useOpenCloseState = function useOpenCloseState(initialState) {
5
+ if (initialState === void 0) {
6
+ initialState = false;
7
+ }
8
+ var _a = useState(initialState),
9
+ isOpen = _a[0],
10
+ setIsOpen = _a[1];
11
+ var open = useCallback(function () {
12
+ return setIsOpen(true);
13
+ }, []);
14
+ var close = useCallback(function () {
15
+ return setIsOpen(false);
16
+ }, []);
17
+ return [isOpen, open, close];
18
+ };
19
+
20
+ var createSafeContext = function createSafeContext(defaultValue) {
21
+ var Context = /*#__PURE__*/createContext(defaultValue !== null && defaultValue !== void 0 ? defaultValue : null);
22
+ var Provider = function Provider(_a) {
23
+ var children = _a.children,
24
+ value = _a.value;
25
+ return jsx(Context.Provider, {
26
+ value: value,
27
+ children: children
28
+ });
29
+ };
30
+ var useSafeContext = function useSafeContext() {
31
+ var context = useContext(Context);
32
+ if (context === null) {
33
+ throw new Error("useSafeContext must be used within a Provider, or give it a default value.");
34
+ }
35
+ return context;
36
+ };
37
+ return [Provider, useSafeContext];
38
+ };
39
+
40
+ export { createSafeContext, useOpenCloseState };
41
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sources":["../src/react/hooks/useOpenCloseState.ts","../src/react/utils/createSafeContext.tsx"],"sourcesContent":["import { useCallback, useState } from \"react\"\n\nexport const useOpenCloseState = (initialState = false) => {\n const [isOpen, setIsOpen] = useState(initialState)\n\n const open = useCallback(() => setIsOpen(true), [])\n const close = useCallback(() => setIsOpen(false), [])\n\n return [isOpen, open, close] as const\n}\n","import { ReactNode, createContext, useContext } from \"react\"\n\ntype ProviderProps<Value> = {\n children: ReactNode\n value: Value\n}\n\nexport const createSafeContext = <Value,>(defaultValue?: Value) => {\n const Context = createContext<Value | null>(defaultValue ?? null)\n\n const Provider = ({ children, value }: ProviderProps<Value>) => {\n return <Context.Provider value={value}>{children}</Context.Provider>\n }\n\n const useSafeContext = () => {\n const context = useContext(Context)\n if (context === null) {\n throw new Error(\n \"useSafeContext must be used within a Provider, or give it a default value.\"\n )\n }\n return context\n }\n\n return [Provider, useSafeContext] as const\n}\n"],"names":["useOpenCloseState","initialState","_a","useState","isOpen","setIsOpen","open","useCallback","close","createSafeContext","defaultValue","Context","createContext","Provider","children","value","_jsx","useSafeContext","context","useContext","Error"],"mappings":";;;IAEaA,iBAAiB,GAAG,SAApBA,iBAAiBA,CAAIC,YAAoB,EAAA;AAApB,EAAA,IAAAA,YAAA,KAAA,KAAA,CAAA,EAAA;AAAAA,IAAAA,YAAoB,GAAA,KAAA;AAAA;AAC9C,EAAA,IAAAC,EAAA,GAAsBC,QAAQ,CAACF,YAAY,CAAC;AAA3CG,IAAAA,MAAM,GAAAF,EAAA,CAAA,CAAA,CAAA;AAAEG,IAAAA,SAAS,GAAAH,EAAA,CAAA,CAAA,CAA0B;AAElD,EAAA,IAAMI,IAAI,GAAGC,WAAW,CAAC;IAAM,OAAAF,SAAS,CAAC,IAAI,CAAC;GAAA,EAAE,EAAE,CAAC;AACnD,EAAA,IAAMG,KAAK,GAAGD,WAAW,CAAC;IAAM,OAAAF,SAAS,CAAC,KAAK,CAAC;GAAA,EAAE,EAAE,CAAC;AAErD,EAAA,OAAO,CAACD,MAAM,EAAEE,IAAI,EAAEE,KAAK,CAAU;AACvC;;ICFaC,iBAAiB,GAAG,SAApBA,iBAAiBA,CAAYC,YAAoB,EAAA;AAC5D,EAAA,IAAMC,OAAO,gBAAGC,aAAa,CAAeF,YAAY,KAAA,IAAA,IAAZA,YAAY,KAAA,KAAA,CAAA,GAAZA,YAAY,GAAI,IAAI,CAAC;AAEjE,EAAA,IAAMG,QAAQ,GAAG,SAAXA,QAAQA,CAAIX,EAAyC,EAAA;QAAvCY,QAAQ,GAAAZ,EAAA,CAAAY,QAAA;MAAEC,KAAK,GAAAb,EAAA,CAAAa,KAAA;AACjC,IAAA,OAAOC,GAAA,CAACL,OAAO,CAACE,QAAQ,EAAA;AAACE,MAAAA,KAAK,EAAEA,KAAK;AAAAD,MAAAA,QAAA,EAAGA;AAAQ,KAAA,CAAoB;GACrE;AAED,EAAA,IAAMG,cAAc,GAAG,SAAjBA,cAAcA,GAAG;AACrB,IAAA,IAAMC,OAAO,GAAGC,UAAU,CAACR,OAAO,CAAC;IACnC,IAAIO,OAAO,KAAK,IAAI,EAAE;AACpB,MAAA,MAAM,IAAIE,KAAK,CACb,4EAA4E,CAC7E;AACH;AACA,IAAA,OAAOF,OAAO;GACf;AAED,EAAA,OAAO,CAACL,QAAQ,EAAEI,cAAc,CAAU;AAC5C;;;;"}
@@ -0,0 +1 @@
1
+ export declare const useOpenCloseState: (initialState?: boolean) => readonly [boolean, () => void, () => void];
@@ -0,0 +1,2 @@
1
+ export * from "./hooks/useOpenCloseState";
2
+ export * from "./utils/createSafeContext";
@@ -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,8 @@
1
+ type Callback<T> = (...params: unknown[]) => T;
2
+ export declare class EarlyReturn<T> {
3
+ returnValue: T | undefined;
4
+ case(condition: boolean, func: Callback<T>): EarlyReturn<T>;
5
+ default(func: Callback<T>): T;
6
+ isAlreadyReturned<T>(returnValue: T | undefined): returnValue is T & ({} | null);
7
+ }
8
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export * from "./EarlyReturn";
package/package.json CHANGED
@@ -1,19 +1,9 @@
1
1
  {
2
2
  "name": "dogu-utils",
3
- "version": "0.0.1",
4
- "description": "A utility library for TypeScript, JavaScript",
5
- "type": "module",
3
+ "version": "0.0.3",
6
4
  "main": "./dist/index.js",
7
5
  "types": "./dist/index.d.ts",
8
- "scripts": {
9
- "test": "echo \"Error: no test specified\" && exit 1",
10
- "build": "rollup -c",
11
- "watch": "rollup -cw"
12
- },
13
- "keywords": [],
14
- "author": "Jeon Jaewon <wksk3249@gmail.com>",
15
- "repository": "https://github.com/JeonJaewon/dogu-utils.git",
16
- "license": "ISC",
6
+ "type": "module",
17
7
  "devDependencies": {
18
8
  "@babel/core": "^7.24.4",
19
9
  "@babel/preset-env": "^7.24.4",
@@ -23,12 +13,14 @@
23
13
  "@rollup/plugin-typescript": "^11.1.6",
24
14
  "@types/react": "^18.2.74",
25
15
  "@types/react-dom": "^18.2.24",
26
- "eslint": "^9.0.0",
27
- "prettier": "^3.2.5",
28
- "tslib": "^2.6.2",
29
- "typescript": "^5.4.4"
16
+ "vitest": "^2.1.5"
30
17
  },
31
18
  "peerDependencies": {
32
19
  "react": "^18.2.0"
20
+ },
21
+ "scripts": {
22
+ "build": "rollup -c",
23
+ "watch": "rollup -cw",
24
+ "test": "vitest"
33
25
  }
34
- }
26
+ }
package/rollup.config.js CHANGED
@@ -1,12 +1,12 @@
1
- import babel from "@rollup/plugin-babel";
2
- import typescript from "@rollup/plugin-typescript";
1
+ import babel from "@rollup/plugin-babel"
2
+ import typescript from "@rollup/plugin-typescript"
3
3
 
4
4
  export default {
5
5
  input: "./src/index.ts",
6
6
  output: {
7
7
  file: "./dist/index.js",
8
8
  format: "es",
9
- sourcemap: true,
9
+ sourcemap: true
10
10
  },
11
11
  plugins: [
12
12
  babel({
@@ -14,10 +14,10 @@ export default {
14
14
  presets: [
15
15
  "@babel/preset-env",
16
16
  "@babel/preset-react",
17
- "@babel/preset-typescript",
17
+ "@babel/preset-typescript"
18
18
  ],
19
- extensions: [".js", ".jsx", ".ts", ".tsx"],
19
+ extensions: [".js", ".jsx", ".ts", ".tsx"]
20
20
  }),
21
- typescript(),
22
- ],
23
- };
21
+ typescript()
22
+ ]
23
+ }
@@ -0,0 +1,36 @@
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
+ })
@@ -0,0 +1,29 @@
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
+ }
@@ -0,0 +1 @@
1
+ export * from "./EarlyReturn"
@@ -0,0 +1,7 @@
1
+ import { defineConfig } from "vitest/config"
2
+
3
+ export default defineConfig({
4
+ test: {
5
+ include: ["**/*.spec.ts?(x)"]
6
+ }
7
+ })
package/.eslintrc DELETED
@@ -1,13 +0,0 @@
1
- {
2
- "env": {
3
- "browser": true,
4
- "node": true
5
- },
6
- "extends": ["eslint:recommended"],
7
- "parserOptions": {
8
- "ecmaVersion": 2021
9
- },
10
- "rules": {
11
- // Add your custom rules here
12
- }
13
- }
package/.prettierrc DELETED
@@ -1,10 +0,0 @@
1
- {
2
- "printWidth": 80,
3
- "tabWidth": 2,
4
- "useTabs": false,
5
- "semi": false,
6
- "singleQuote": false,
7
- "trailingComma": "none",
8
- "bracketSpacing": true,
9
- "arrowParens": "avoid"
10
- }
package/src/Hello.tsx DELETED
@@ -1,6 +0,0 @@
1
- import React from "react"
2
- import ReactDOM from "react-dom"
3
-
4
- export const Hello = () => {
5
- return <div>Hello World!</div>
6
- }