Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dev #241

Merged
merged 8 commits into from
Sep 19, 2024
Merged

Dev #241

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions .github/workflows/release-please.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,11 @@ jobs:
node-version: 20
registry-url: "https://registry.npmjs.org"
if: ${{ steps.release.outputs.release_created }}
- run: npm i
- run: npm i -g bun && bun install
if: ${{ steps.release.outputs.release_created }}
- run: |
npm run code:publish
bun test
bun build.ts
npm publish
env:
NODE_AUTH_TOKEN: ${{secrets.NPM_PUBLISH}}
Expand Down
21 changes: 8 additions & 13 deletions build.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
// @ts-nocheck

import { build } from "bun";
import { build, $ } from "bun";
import { readdirSync } from "node:fs";
import { log } from "node:console";

const entryPoints = readdirSync("./lib", "utf-8")
.filter((file) => file !== "index.ts" && file !== "types.d.ts")
.map((file) => `./lib/${file}`),
baseOptions = {
entrypoints: entryPoints,
const baseOptions = {
entrypoints: ["./lib/index.ts"],
outdir: "./build",
minify: {
whitespace: true,
Expand All @@ -20,24 +17,22 @@ const entryPoints = readdirSync("./lib", "utf-8")
},
logger = (env) => log(`${env} build complete🏗`);

// Module builds minified
await build({ ...baseOptions, naming: "browser/[dir]/[name].min.[ext]" }).then(
logger("Browser (minified) modules")
);
await $`rm -rf build && echo 'Cleaned build/ directory'`;

// library bundle minified
await build({
...baseOptions,
entrypoints: ["./lib/index.ts"],
naming: "browser/huetiful.min.js",
}).then(logger("Browser (minified) entire library"));
}).then(logger("Browser ESM (minified) entire library"));

// node bundle
await build({
...baseOptions,
entrypoints: ["./lib/index.ts"],

minify: false,
target: "node",
external: ["culori"],
naming: "node/huetiful.esm.js",
}).then(logger("Node"));
await $`bun tsup --format=esm ./lib/index.ts --dts-only --outDir=./build`;
await $`du -sh build/*`;
Binary file modified bun.lockb
Binary file not shown.
19 changes: 11 additions & 8 deletions lib/accessibility.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
} from "culori/fn";
import { eq, or } from "./internal.js";
import { wcagContrast } from "culori/fn";
import { ColorToken, DeficiencyOptions } from "./types.js";

/**
* Gets the contrast between the passed in colors.
Expand All @@ -17,17 +18,16 @@ import { wcagContrast } from "culori/fn";
*
* :::
*
* @param {ColorToken} a First color to query.
* @param {ColorToken} b The color to compare against.
* @returns {number}
* @param a First color to query.
* @param b The color to compare against.
* @example
*
* import { contrast } from 'huetiful-js'
*
* console.log(contrast("black", "white"));
* // 21
*/
function contrast(a, b) {
function contrast<Color extends ColorToken>(a: Color, b: Color): number {
// @ts-ignore
return wcagContrast(token(a), token(b));
}
Expand Down Expand Up @@ -124,8 +124,8 @@ function adaptive(color, options = undefined) {}
* * 'protanopia' - An inability to distinguish the color 'red'. The `kind` is `'red'`.
* :::

* @param {ColorToken} color The color to return its simulated variant
* @param {DeficiencyOptions} options
* @param color The color to return its simulated variant
* @param options
* @example
*
* import { deficiency } from 'huetiful-js'
Expand All @@ -137,9 +137,12 @@ console.log(deficiency(['rgb', 230, 100, 50, 0.5],{ kind:'blue', severity:0.5 })
// '#dd663680'

*/
function deficiency(color, options) {
function deficiency<
Color extends ColorToken,
Options extends DeficiencyOptions
>(color: Color, options?: Options): ColorToken {
let { kind, severity } = options || {};
color = token(color);
color = token(color) as Color;
const func = (c, t = 1) =>
({
blue: filterDeficiencyTrit(t)(c),
Expand Down
78 changes: 22 additions & 56 deletions lib/collection.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// @ts-nocheck

import {
sortedColl,
mcchn,
Expand All @@ -11,8 +9,9 @@ import {
map,
eq,
filteredColl,
values,
} from "./internal.js";
import { family, luminance, mc, token } from "./utils.js";
import { achromatic, family, luminance, mc, token } from "./utils.js";
import { contrast } from "./accessibility.js";
import {
averageAngle,
Expand All @@ -29,6 +28,7 @@ import {
SortByOptions,
StatsOptions,
Factor,
Stats,
} from "./types.js";

/**
Expand Down Expand Up @@ -67,7 +67,7 @@ import {
function stats<Iterable extends Collection, Options extends StatsOptions>(
collection: Iterable,
options: Options
) {
): Stats {
let { factor, relative, colorspace, against } = options || {};

factor = or(factor, undefined);
Expand Down Expand Up @@ -142,11 +142,10 @@ function stats<Iterable extends Collection, Options extends StatsOptions>(
},
statsObject = factorIterator(factor, commonStats);
statsObject["achromatic"] =
// @ts-ignore
values(collection).filter(achromatic).length / len;
statsObject["colorspace"] = colorspace;

return statsObject;
return statsObject as Stats;
}

/**
Expand All @@ -167,10 +166,8 @@ function stats<Iterable extends Collection, Options extends StatsOptions>(
*
* :::
*
* @param {Collection} collection The `collection` of colors to sort.
* @param {SortByOptions} options
* @returns {Collection}

* @param collection The `collection` of colors to sort.
* @param options
* @example

import { sortBy } from 'huetiful-js'
Expand Down Expand Up @@ -233,10 +230,9 @@ function sortBy<Iterable extends Collection, Options extends SortByOptions>(

/**
* Distributes the specified `factor` of a color in the collection with the specified `extremum` (i.e the color with the smallest/largest `hue` angle or `chroma` value) to all color tokens in the collection.
*@param factor The property you want to distribute to the colors in the collection for example `hue | luminance`
* @param Optional overrides to change the default configursation
*@param collection The property you want to distribute to the colors in the collection for example `hue | luminance`
* @param options Optional overrides to change the default configursation

@returns {undefined}
*/
function distribute<
Iterable extends Collection,
Expand Down Expand Up @@ -293,6 +289,9 @@ function distribute<
* @param {any} collection The colors to manipulate.
* @returns {any} The collection with each color's `factor` adjusted.
*/

let a;
return a as Collection;
}

/**
Expand All @@ -313,7 +312,7 @@ function distribute<
* :::
* @see https://culorijs.org/color-spaces/ For the expected ranges per colorspace.
* Supports expression strings e.g `'>=0.5'`. The supported symbols are `== | === | != | !== | >= | <= | < | >`
* @param {Collection} collection The collection of colors to filter.
* @param collection The collection of colors to filter.
* @param options
* @example
*
Expand Down Expand Up @@ -345,7 +344,7 @@ function filterBy<Iterable extends Collection, Options extends FilterByOptions>(
colorspace = or(colorspace, "lch");
against = or(against, "cyan");

const filter = (cb, start, end) => {
const filter = (cb) => {
return filteredColl(factor, cb)(collection, start, end);
},
chromaChannel = mcchn("c", colorspace, false),
Expand All @@ -360,7 +359,7 @@ function filterBy<Iterable extends Collection, Options extends FilterByOptions>(
luminance: [0, 1],
},
ctrst = (a) => (b) => contrast(b, a),
dstnce = (a) => differenceHyab()(a, against);
dstnce = (a) => differenceHyab()(a, against as string);

if (isArray(factor) || undefined) {
start = or(ranges[fact][0], defaultRanges[fact][0]);
Expand All @@ -372,49 +371,16 @@ function filterBy<Iterable extends Collection, Options extends FilterByOptions>(
}

return {
chroma: filter(
mc(mcchn("c", colorspace, true)),

// @ts-ignore
start,
end
),
lightness: filter(
mc(mcchn("l", colorspace, true)),
// @ts-ignore
start,
end
),
hue: filter(
mc(`${colorspace}.h`),
// @ts-ignore
start,
end
),
distance: filter(
dstnce(token(against)),

// @ts-ignore
start,
end
),
contrast: filter(
ctrst(against),

start,
end
),
luminance: filter(
luminance,
// @ts-ignore
start,
end
),
chroma: filter(mc(mcchn("c", colorspace, true))),
lightness: filter(mc(mcchn("l", colorspace, true))),
hue: filter(mc(`${colorspace}.h`)),
distance: filter(dstnce(token(against))),
contrast: filter(ctrst(against)),
luminance: filter(luminance),
}[fact];
};

// @ts-ignore
return factorIterator(factor, callback);
return factorIterator(factor, callback) as Collection;
}

export { stats, sortBy, filterBy, distribute };
Loading