I am looking to develop a simple JavaScript/TypeScript library focused on color conversion. Some of the functions and types I aim to export include:
export type HEX = string;
export type RGB = { r: number; g: number; b: number };
export type RGBA = { r: number; g: number; b: number; a: number };
export type COLOR = HEX | RGB | RGBA;
// other types ...
export const hex2rgb = (hex: HEX): RGB => {
const m = hex.match(/\w\w/g);
if (m === null) throw new Error("");
const [r, g, b] = m.map(x => parseInt(x, 16));
return { r: r, g: g, b: b };
}
export const hex2rgba = (hex: HEX, alpha = 1): RGBA => {
const [r, g, b] = hex.match(/\w\w/g).map(x => parseInt(x, 16));
return { r: r, g: g, b: b, a: alpha };
};
// other functions...
I am planning to create a GitHub/Npm library for users to easily utilize these methods. However, I am unsure about how to proceed with structuring the project, folders, files, etc. This is my first time creating a library and any guidance would be appreciated. Despite not requiring React, I am currently using a Codesandbox in React for development.
The proposed structure of my code could be as follows:
project
|_ src
|_ index.ts contains all functions
|_ types.ts includes types
|_ test includes test code
What steps should I take next? Do I need to create a package.js
? Can I integrate libraries like Lodash
?