Skip to content

Commit 87ca02e

Browse files
migrate to vite bundler and clear out unnecessary deps
1 parent c168b3a commit 87ca02e

File tree

8 files changed

+100
-188
lines changed

8 files changed

+100
-188
lines changed

bun.lockb

-23.3 KB
Binary file not shown.

package.json

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,41 @@
11
{
22
"name": "@curiousleaf/utils",
33
"description": "A lightweight set of utilities",
4-
"version": "1.0.41",
4+
"version": "1.1.0",
55
"license": "MIT",
66
"author": {
77
"name": "Piotr Kulpinski",
88
"email": "[email protected]",
99
"url": "https://kulpinski.pl"
1010
},
1111
"repository": "curious-leaf/utils",
12-
"main": "./dist/index.js",
13-
"types": "./dist/index.d.ts",
1412
"files": ["dist"],
13+
"main": "./dist/index.umd.cjs",
14+
"module": "./dist/index.js",
15+
"exports": {
16+
".": {
17+
"import": "./dist/index.js",
18+
"require": "./dist/index.umd.cjs"
19+
}
20+
},
1521
"scripts": {
16-
"build": "tsup",
22+
"clean": "rimraf ./dist",
23+
"build": "vite build && tsc --emitDeclarationOnly",
24+
"prebuild": "bun run clean",
1725
"lint": "bun biome lint --apply .",
1826
"format": "bun biome format --write ."
1927
},
2028
"dependencies": {
21-
"@sindresorhus/slugify": "^2.2.1",
22-
"@uiw/color-convert": "^2.3.4",
23-
"scule": "^1.3.0"
29+
"@sindresorhus/slugify": "^2.2.1"
2430
},
2531
"devDependencies": {
26-
"@babel/runtime": "^7.26.0",
2732
"@biomejs/biome": "^1.9.4",
28-
"@types/bun": "^1.1.14",
29-
"prettier": "^3.4.2",
30-
"tsup": "^8.3.5",
31-
"typescript": "^5.7.2"
33+
"@types/bun": "^1.1.17",
34+
"rimraf": "^6.0.1",
35+
"typescript": "^5.7.2",
36+
"vite": "^6.0.7"
3237
},
3338
"peerDependencies": {
34-
"@sindresorhus/slugify": "^2.2.1",
35-
"@uiw/color-convert": "^2.0.9",
36-
"scule": "^1.3.0"
39+
"@sindresorhus/slugify": "^2.2.1"
3740
}
3841
}

src/colors/colors.test.ts

Lines changed: 39 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,51 @@
11
import { describe, expect, it } from "bun:test"
22

3-
import { getColorObject, isLightColor } from "./colors"
3+
import { isLightColor } from "./colors"
44

55
describe("isLightColor", () => {
6-
it("should return true for a light color", () => {
7-
const hexa = "#FFFFFF"
8-
const result = isLightColor(hexa)
9-
expect(result).toBe(true)
6+
// Basic cases
7+
it("should identify white as light", () => {
8+
expect(isLightColor("#FFFFFF")).toBe(true)
109
})
1110

12-
it("should return false for a dark color", () => {
13-
const hexa = "#000000"
14-
const result = isLightColor(hexa)
15-
expect(result).toBe(false)
11+
it("should identify black as dark", () => {
12+
expect(isLightColor("#000000")).toBe(false)
13+
})
14+
15+
// Edge cases
16+
it("should handle hex without #", () => {
17+
expect(isLightColor("FFFFFF")).toBe(true)
18+
expect(isLightColor("000000")).toBe(false)
19+
})
20+
21+
it("should handle different hex case formats", () => {
22+
expect(isLightColor("#ffffff")).toBe(true)
23+
expect(isLightColor("#FFFFFF")).toBe(true)
24+
expect(isLightColor("#fFfFfF")).toBe(true)
25+
})
26+
27+
// Trimming cases
28+
it("should trim longer hex strings", () => {
29+
expect(isLightColor("#FFFFFF00")).toBe(true) // Should trim off the alpha
30+
expect(isLightColor("#000000FF")).toBe(false) // Should trim off the alpha
31+
expect(isLightColor("#FF0000AABBCC")).toBe(false) // Should only use first 6 chars
1632
})
17-
})
1833

19-
describe("getColorObject", () => {
20-
it("should return the correct color result for a valid hex string", () => {
21-
const hexa = "#FF0000"
22-
const result = getColorObject(hexa)
23-
24-
expect(result).toEqual({
25-
rgb: { r: 255, g: 0, b: 0 },
26-
hsl: { h: 0, s: 100, l: 50 },
27-
hsv: { h: 0, s: 100, v: 100 },
28-
rgba: { r: 255, g: 0, b: 0, a: 1 },
29-
hsla: { h: 0, s: 100, l: 50, a: 1 },
30-
hsva: { h: 0, s: 100, v: 100, a: 1 },
31-
hex: "#ff0000",
32-
hexa: "#ff0000ff",
33-
})
34+
// Borderline cases
35+
it("should handle colors near the brightness threshold", () => {
36+
expect(isLightColor("#BBBBBB")).toBe(true) // Just above threshold
37+
expect(isLightColor("#999999")).toBe(false) // Just below threshold
3438
})
3539

36-
it("should return the correct color result for a HsvaColor object", () => {
37-
const hsva = { h: 0, s: 100, v: 100, a: 1 }
38-
const result = getColorObject(hsva)
39-
40-
expect(result).toEqual({
41-
rgb: { r: 255, g: 0, b: 0 },
42-
hsl: { h: 0, s: 100, l: 50 },
43-
hsv: { h: 0, s: 100, v: 100 },
44-
rgba: { r: 255, g: 0, b: 0, a: 1 },
45-
hsla: { h: 0, s: 100, l: 50, a: 1 },
46-
hsva: { h: 0, s: 100, v: 100, a: 1 },
47-
hex: "#ff0000",
48-
hexa: "#ff0000ff",
49-
})
40+
// Complex colors
41+
it("should correctly evaluate complex colors", () => {
42+
expect(isLightColor("#FF0000")).toBe(false) // Pure red (dark)
43+
expect(isLightColor("#00FF00")).toBe(false) // Pure green (dark)
44+
expect(isLightColor("#0000FF")).toBe(false) // Pure blue (dark)
45+
expect(isLightColor("#FF69B4")).toBe(false) // Hot pink (dark)
46+
expect(isLightColor("#800080")).toBe(false) // Purple (dark)
47+
expect(isLightColor("#87CEEB")).toBe(true) // Sky blue (light)
48+
expect(isLightColor("#98FB98")).toBe(true) // Pale green (light)
49+
expect(isLightColor("#800000")).toBe(false) // Maroon (dark)
5050
})
5151
})

src/colors/colors.ts

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,21 @@
1-
import { type ColorResult, type HsvaColor, color, hexToRgba } from "@uiw/color-convert"
2-
31
/**
42
* Check if a given color in hexadecimal format is a light color.
3+
* Only supports 6-digit hex colors (RGB). If longer string is provided, it will be trimmed.
54
*
6-
* @param hexa - The hexadecimal color code to check.
5+
* @param hexa - The hexadecimal color code to check (e.g. "#FF0000").
76
* @returns A boolean indicating if the color is light.
87
*/
98
export const isLightColor = (hexa: string): boolean => {
10-
const { r, g, b, a } = hexToRgba(hexa)
11-
const brightness = r * 0.299 + g * 0.587 + b * 0.114 + (1 - a) * 255
9+
// Remove # if present and trim to 6 characters
10+
const hex = hexa.replace("#", "").substring(0, 6)
1211

13-
return brightness > 186
14-
}
12+
// Parse RGB values
13+
const r = Number.parseInt(hex.substring(0, 2), 16)
14+
const g = Number.parseInt(hex.substring(2, 4), 16)
15+
const b = Number.parseInt(hex.substring(4, 6), 16)
1516

16-
/**
17-
* Returns a ColorResult object based on the provided string or HsvaColor.
18-
* @param str - The string or HsvaColor to convert into a ColorResult object.
19-
* @returns The ColorResult object.
20-
*/
21-
export const getColorObject = (str: string | HsvaColor): ColorResult => {
22-
return color(str)
17+
// Calculate perceived brightness
18+
const brightness = r * 0.299 + g * 0.587 + b * 0.114
19+
20+
return brightness > 186
2321
}

src/index.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
1-
// External exports
2-
export * from "scule"
3-
export * from "@uiw/color-convert"
4-
5-
// Internal exports
61
export * from "./colors/colors"
72
export * from "./errors/errors"
83
export * from "./events/events"

tsconfig.json

Lines changed: 11 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -1,109 +1,15 @@
11
{
2+
"include": ["src"],
3+
"exclude": ["node_modules", "dist", "**/*.test.*", "**/*.spec.*"],
24
"compilerOptions": {
3-
/* Visit https://aka.ms/tsconfig to read more about this file */
4-
5-
/* Projects */
6-
// "incremental": true, /* Save .tsbuildinfo files to allow for incremental compilation of projects. */
7-
// "composite": true, /* Enable constraints that allow a TypeScript project to be used with project references. */
8-
// "tsBuildInfoFile": "./.tsbuildinfo", /* Specify the path to .tsbuildinfo incremental compilation file. */
9-
// "disableSourceOfProjectReferenceRedirect": true, /* Disable preferring source files instead of declaration files when referencing composite projects. */
10-
// "disableSolutionSearching": true, /* Opt a project out of multi-project reference checking when editing. */
11-
// "disableReferencedProjectLoad": true, /* Reduce the number of projects loaded automatically by TypeScript. */
12-
13-
/* Language and Environment */
14-
"target": "es2016" /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */,
15-
// "lib": [], /* Specify a set of bundled library declaration files that describe the target runtime environment. */
16-
// "jsx": "preserve", /* Specify what JSX code is generated. */
17-
// "experimentalDecorators": true, /* Enable experimental support for legacy experimental decorators. */
18-
// "emitDecoratorMetadata": true, /* Emit design-type metadata for decorated declarations in source files. */
19-
// "jsxFactory": "", /* Specify the JSX factory function used when targeting React JSX emit, e.g. 'React.createElement' or 'h'. */
20-
// "jsxFragmentFactory": "", /* Specify the JSX Fragment reference used for fragments when targeting React JSX emit e.g. 'React.Fragment' or 'Fragment'. */
21-
// "jsxImportSource": "", /* Specify module specifier used to import the JSX factory functions when using 'jsx: react-jsx*'. */
22-
// "reactNamespace": "", /* Specify the object invoked for 'createElement'. This only applies when targeting 'react' JSX emit. */
23-
// "noLib": true, /* Disable including any library files, including the default lib.d.ts. */
24-
// "useDefineForClassFields": true, /* Emit ECMAScript-standard-compliant class fields. */
25-
// "moduleDetection": "auto", /* Control what method is used to detect module-format JS files. */
26-
27-
/* Modules */
28-
"module": "commonjs" /* Specify what module code is generated. */,
29-
// "rootDir": "./", /* Specify the root folder within your source files. */
30-
// "moduleResolution": "node10", /* Specify how TypeScript looks up a file from a given module specifier. */
31-
// "baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */
32-
// "paths": {}, /* Specify a set of entries that re-map imports to additional lookup locations. */
33-
// "rootDirs": [], /* Allow multiple folders to be treated as one when resolving modules. */
34-
// "typeRoots": [], /* Specify multiple folders that act like './node_modules/@types'. */
35-
// "types": [], /* Specify type package names to be included without being referenced in a source file. */
36-
// "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */
37-
// "moduleSuffixes": [], /* List of file name suffixes to search when resolving a module. */
38-
// "allowImportingTsExtensions": true, /* Allow imports to include TypeScript file extensions. Requires '--moduleResolution bundler' and either '--noEmit' or '--emitDeclarationOnly' to be set. */
39-
// "resolvePackageJsonExports": true, /* Use the package.json 'exports' field when resolving package imports. */
40-
// "resolvePackageJsonImports": true, /* Use the package.json 'imports' field when resolving imports. */
41-
// "customConditions": [], /* Conditions to set in addition to the resolver-specific defaults when resolving imports. */
42-
// "resolveJsonModule": true, /* Enable importing .json files. */
43-
// "allowArbitraryExtensions": true, /* Enable importing files with any extension, provided a declaration file is present. */
44-
// "noResolve": true, /* Disallow 'import's, 'require's or '<reference>'s from expanding the number of files TypeScript should add to a project. */
45-
46-
/* JavaScript Support */
47-
// "allowJs": true, /* Allow JavaScript files to be a part of your program. Use the 'checkJS' option to get errors from these files. */
48-
// "checkJs": true, /* Enable error reporting in type-checked JavaScript files. */
49-
// "maxNodeModuleJsDepth": 1, /* Specify the maximum folder depth used for checking JavaScript files from 'node_modules'. Only applicable with 'allowJs'. */
50-
51-
/* Emit */
52-
"declaration": true /* Generate .d.ts files from TypeScript and JavaScript files in your project. */,
53-
// "declarationMap": true, /* Create sourcemaps for d.ts files. */
54-
// "emitDeclarationOnly": true, /* Only output d.ts files and not JavaScript files. */
55-
// "sourceMap": true, /* Create source map files for emitted JavaScript files. */
56-
// "inlineSourceMap": true, /* Include sourcemap files inside the emitted JavaScript. */
57-
// "outFile": "./", /* Specify a file that bundles all outputs into one JavaScript file. If 'declaration' is true, also designates a file that bundles all .d.ts output. */
58-
"outDir": "./dist" /* Specify an output folder for all emitted files. */,
59-
// "removeComments": true, /* Disable emitting comments. */
60-
// "noEmit": true, /* Disable emitting files from a compilation. */
61-
// "importHelpers": true, /* Allow importing helper functions from tslib once per project, instead of including them per-file. */
62-
// "importsNotUsedAsValues": "remove", /* Specify emit/checking behavior for imports that are only used for types. */
63-
// "downlevelIteration": true, /* Emit more compliant, but verbose and less performant JavaScript for iteration. */
64-
// "sourceRoot": "", /* Specify the root path for debuggers to find the reference source code. */
65-
// "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */
66-
// "inlineSources": true, /* Include source code in the sourcemaps inside the emitted JavaScript. */
67-
// "emitBOM": true, /* Emit a UTF-8 Byte Order Mark (BOM) in the beginning of output files. */
68-
// "newLine": "crlf", /* Set the newline character for emitting files. */
69-
// "stripInternal": true, /* Disable emitting declarations that have '@internal' in their JSDoc comments. */
70-
// "noEmitHelpers": true, /* Disable generating custom helper functions like '__extends' in compiled output. */
71-
// "noEmitOnError": true, /* Disable emitting files if any type checking errors are reported. */
72-
// "preserveConstEnums": true, /* Disable erasing 'const enum' declarations in generated code. */
73-
// "declarationDir": "./", /* Specify the output directory for generated declaration files. */
74-
// "preserveValueImports": true, /* Preserve unused imported values in the JavaScript output that would otherwise be removed. */
75-
76-
/* Interop Constraints */
77-
// "isolatedModules": true, /* Ensure that each file can be safely transpiled without relying on other imports. */
78-
// "verbatimModuleSyntax": true, /* Do not transform or elide any imports or exports not marked as type-only, ensuring they are written in the output file's format based on the 'module' setting. */
79-
// "allowSyntheticDefaultImports": true, /* Allow 'import x from y' when a module doesn't have a default export. */
80-
"esModuleInterop": true /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */,
81-
// "preserveSymlinks": true, /* Disable resolving symlinks to their realpath. This correlates to the same flag in node. */
82-
"forceConsistentCasingInFileNames": true /* Ensure that casing is correct in imports. */,
83-
84-
/* Type Checking */
85-
"strict": true /* Enable all strict type-checking options. */,
86-
// "noImplicitAny": true, /* Enable error reporting for expressions and declarations with an implied 'any' type. */
87-
// "strictNullChecks": true, /* When type checking, take into account 'null' and 'undefined'. */
88-
// "strictFunctionTypes": true, /* When assigning functions, check to ensure parameters and the return values are subtype-compatible. */
89-
// "strictBindCallApply": true, /* Check that the arguments for 'bind', 'call', and 'apply' methods match the original function. */
90-
// "strictPropertyInitialization": true, /* Check for class properties that are declared but not set in the constructor. */
91-
// "noImplicitThis": true, /* Enable error reporting when 'this' is given the type 'any'. */
92-
// "useUnknownInCatchVariables": true, /* Default catch clause variables as 'unknown' instead of 'any'. */
93-
// "alwaysStrict": true, /* Ensure 'use strict' is always emitted. */
94-
// "noUnusedLocals": true, /* Enable error reporting when local variables aren't read. */
95-
// "noUnusedParameters": true, /* Raise an error when a function parameter isn't read. */
96-
// "exactOptionalPropertyTypes": true, /* Interpret optional property types as written, rather than adding 'undefined'. */
97-
// "noImplicitReturns": true, /* Enable error reporting for codepaths that do not explicitly return in a function. */
98-
// "noFallthroughCasesInSwitch": true, /* Enable error reporting for fallthrough cases in switch statements. */
99-
// "noUncheckedIndexedAccess": true, /* Add 'undefined' to a type when accessed using an index. */
100-
// "noImplicitOverride": true, /* Ensure overriding members in derived classes are marked with an override modifier. */
101-
// "noPropertyAccessFromIndexSignature": true, /* Enforces using indexed accessors for keys declared using an indexed type. */
102-
// "allowUnusedLabels": true, /* Disable error reporting for unused labels. */
103-
// "allowUnreachableCode": true, /* Disable error reporting for unreachable code. */
104-
105-
/* Completeness */
106-
// "skipDefaultLibCheck": true, /* Skip type checking .d.ts files that are included with TypeScript. */
107-
"skipLibCheck": true /* Skip type checking all .d.ts files. */
5+
"target": "ESNext",
6+
"module": "commonjs",
7+
"declaration": true,
8+
"outDir": "./dist",
9+
"esModuleInterop": true,
10+
"forceConsistentCasingInFileNames": true,
11+
"strict": true,
12+
"skipLibCheck": true,
13+
"types": ["bun"]
10814
}
10915
}

tsup.config.ts

Lines changed: 0 additions & 10 deletions
This file was deleted.

vite.config.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { resolve } from "node:path"
2+
import { defineConfig } from "vite"
3+
4+
// https://vitejs.dev/config/
5+
export default defineConfig({
6+
build: {
7+
lib: {
8+
entry: resolve(__dirname, "./src/index.ts"),
9+
name: "@curiousleaf/utils",
10+
fileName: "index",
11+
},
12+
rollupOptions: {
13+
external: ["@sindresorhus/slugify"],
14+
},
15+
sourcemap: true,
16+
},
17+
optimizeDeps: {
18+
exclude: ["**/*.test.*", "**/*.spec.*"],
19+
},
20+
})

0 commit comments

Comments
 (0)