@briklab/lib 1.0.1 → 1.0.9

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.
@@ -52,6 +52,7 @@
52
52
  *
53
53
  * @module cli-john
54
54
  */
55
+ import InlineStyle, { StyleSheet } from "../stylesheet/index.js";
55
56
  /**
56
57
  * # CLI
57
58
  * @classdesc The main class for **CLI**.
@@ -87,7 +88,7 @@ export declare class CLI {
87
88
  }
88
89
  export declare namespace CLI {
89
90
  const ValidEvents: readonly ["command"];
90
- type ValidEvent = typeof ValidEvents[number];
91
+ type ValidEvent = (typeof ValidEvents)[number];
91
92
  /**
92
93
  * ## CLI.Command
93
94
  * A command in a CLI Command
@@ -127,9 +128,24 @@ export declare namespace CLI.Command {
127
128
  get name(): string;
128
129
  }
129
130
  }
130
- /**
131
- * Will be implemented in v1.2.0
132
- * @experimental v1.2.0
133
- */
134
- export declare namespace Utilities {
131
+ declare class UtilitiesClass {
132
+ styleSheet: StyleSheet;
133
+ tags: Record<string, {
134
+ tag: string;
135
+ showErrorInTag: boolean;
136
+ paddingLeft: number;
137
+ paddingRight: number;
138
+ styleName: string;
139
+ }>;
140
+ constructor();
141
+ /** Add a new tag */
142
+ addTag(name: string, config?: Partial<(typeof this.tags)["error"]>): this;
143
+ /** Set style for a tag */
144
+ setTagStyle(tagName: string, style: InlineStyle): this;
145
+ log(tagName: string, message: string): void;
146
+ error(msg: string): this;
147
+ warning(msg: string): this;
148
+ info(msg: string): this;
135
149
  }
150
+ export declare const Utilities: UtilitiesClass;
151
+ export {};
@@ -64,7 +64,10 @@ var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (
64
64
  return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
65
65
  };
66
66
  var _CLI_instances, _CLI_commands, _CLI_onCmdFunctions, _CLI_figureOutCommand, _CLI_process, _CLI_ErrorClass, _CLI_createErr, _CLI_createWarn;
67
- import JSTC from "../jstc";
67
+ // -------------------------------------------------------------------------------------------------------
68
+ //#region Defination of custom JSTC handler
69
+ import JSTC from "../jstc/index.js";
70
+ import InlineStyle, { StyleSheet } from "../stylesheet/index.js";
68
71
  JSTC.addCustomHandler("NodeJS Process", (p) => {
69
72
  return (p &&
70
73
  typeof p === "object" &&
@@ -210,9 +213,7 @@ class CLIErrors extends Error {
210
213
  }
211
214
  (function (CLI) {
212
215
  var _Command_instances, _Command_name, _Command_createWarn, _Command_options;
213
- CLI.ValidEvents = [
214
- "command"
215
- ];
216
+ CLI.ValidEvents = ["command"];
216
217
  /**
217
218
  * ## CLI.Command
218
219
  * A command in a CLI Command
@@ -302,6 +303,146 @@ class CLIErrors extends Error {
302
303
  })(CLI || (CLI = {}));
303
304
  //#endregion
304
305
  // -------------------------------------------------------------------------------------------------------
306
+ //#region CLI Utilities
307
+ JSTC.addCustomHandler("Utilities Tag Config", (p) => {
308
+ return (p &&
309
+ typeof p === "object" &&
310
+ typeof p.tag === "string" &&
311
+ typeof p.showErrorInTag === "boolean" &&
312
+ typeof p.paddingLeft === "number" &&
313
+ typeof p.paddingRight === "number" &&
314
+ (typeof p.styleName === "string" || p.styleName === undefined));
315
+ });
316
+ class UtilitiesClass {
317
+ constructor() {
318
+ this.styleSheet = new StyleSheet();
319
+ this.tags = {};
320
+ this.addTag("error", {
321
+ tag: "ERROR",
322
+ showErrorInTag: false,
323
+ paddingLeft: 1,
324
+ paddingRight: 1,
325
+ });
326
+ this.addTag("warning", {
327
+ tag: "WARNING",
328
+ showErrorInTag: true,
329
+ paddingLeft: 1,
330
+ paddingRight: 1,
331
+ });
332
+ this.addTag("info", {
333
+ tag: "INFO",
334
+ showErrorInTag: true,
335
+ paddingLeft: 1,
336
+ paddingRight: 1,
337
+ });
338
+ this.setTagStyle("error", new InlineStyle({ color: "red", fontWeight: "bold" }));
339
+ this.setTagStyle("warning", new InlineStyle({ color: "orange", fontWeight: "bold" }));
340
+ this.setTagStyle("info", new InlineStyle({ color: "blue" }));
341
+ }
342
+ /** Add a new tag */
343
+ addTag(name, config = {}) {
344
+ if (!JSTC.for([name, config]).check(["string", "object"])) {
345
+ console.warn(`[UtilitiesClass.addTag] @briklab/lib/cli-john: Invalid Arguments!
346
+ Hint: The first argument must be a string, and the second argument must be a object.
347
+ Using String(argument1) and {} as fallback.`);
348
+ name = String(name);
349
+ config = {};
350
+ }
351
+ const fullConfig = {
352
+ tag: name.toUpperCase(),
353
+ showErrorInTag: false,
354
+ paddingLeft: 0,
355
+ paddingRight: 0,
356
+ styleName: "",
357
+ ...config,
358
+ };
359
+ if (!JSTC.for([fullConfig]).check(["Utilities Tag Config"])) {
360
+ console.warn(`[UtilitiesClass.addTag] @briklab/lib/cli-john: Invalid tag config passed for "${name}"
361
+ Hint: The config must be in format {tag?: string, showErrorInTag?:boolean, paddingLeft?:number, paddingRight?:number, styleName?:string}`);
362
+ console.warn(fullConfig);
363
+ return this;
364
+ }
365
+ this.tags[name] = fullConfig;
366
+ return this;
367
+ }
368
+ /** Set style for a tag */
369
+ setTagStyle(tagName, style) {
370
+ if (typeof tagName !== "string" || !(style instanceof InlineStyle)) {
371
+ console.warn(`[UtilitiesClass.setTagStyle] @briklab/lib/cli-john: Invalid arguments!
372
+ Hint: The first argument must be a string and the second argument must be a instance of InlineStyle
373
+ Using String(firstArgument) and new InlineStyle({}) as fallback`);
374
+ tagName = String(tagName);
375
+ style = new InlineStyle({});
376
+ }
377
+ if (!this.tags[tagName]) {
378
+ console.warn(`[UtilitiesClass.setTagStyle] @briklab/lib/cli-john: Tag "${tagName}" does not exist!
379
+ Hint: Use a valid tag that you have defined or use "error"|"warn"|"info"`);
380
+ return this;
381
+ }
382
+ const styleName = `${tagName} Tag Color`;
383
+ this.styleSheet.set(styleName, style);
384
+ this.tags[tagName].styleName = styleName;
385
+ return this;
386
+ }
387
+ log(tagName, message) {
388
+ if (!JSTC.for([tagName, message]).check(["string", "string"])) {
389
+ console.warn(`[UtilitiesClass.log] @briklab/lib/cli-john: Invalid Arguments!
390
+ Hint: The first argument must be a string and the second argument must be a string
391
+ Using String(argument1) and String(argument2) as fallback`);
392
+ tagName = String(tagName);
393
+ message = String(message);
394
+ }
395
+ const tag = this.tags[tagName];
396
+ if (!tag) {
397
+ console.warn(`[UtilitiesClass.log] @briklab/lib/cli-john: Tag "${tagName}" does not exist!
398
+ Hint: Use a valid tag that you have defined or use "error"|"warn"|"info"`);
399
+ console.log(message);
400
+ return;
401
+ }
402
+ const style = this.styleSheet.get(tag.styleName)?.text ?? "";
403
+ const leftPad = " ".repeat(tag.paddingLeft);
404
+ const rightPad = " ".repeat(tag.paddingRight);
405
+ if (tag.showErrorInTag) {
406
+ console.log(`[%c${leftPad}${tag.tag}${rightPad}%c]: ${message}`, style);
407
+ }
408
+ else {
409
+ console.log(`%c[${leftPad}${tag.tag}${rightPad}]%c: ${message}`, style);
410
+ }
411
+ }
412
+ error(msg) {
413
+ if (typeof msg !== "string") {
414
+ console.warn(`[UtilitiesClass.error] Invalid First Argument!
415
+ Hint: The first argument must be a string.
416
+ Using String(firstArgument) as fallback`);
417
+ msg = String(msg);
418
+ }
419
+ this.log("error", msg);
420
+ return this;
421
+ }
422
+ warning(msg) {
423
+ if (typeof msg !== "string") {
424
+ console.warn(`[UtilitiesClass.warning] Invalid First Argument!
425
+ Hint: The first argument must be a string.
426
+ Using String(firstArgument) as fallback`);
427
+ msg = String(msg);
428
+ }
429
+ this.log("warning", msg);
430
+ return this;
431
+ }
432
+ info(msg) {
433
+ if (typeof msg !== "string") {
434
+ console.warn(`[UtilitiesClass.info] Invalid First Argument!
435
+ Hint: The first argument must be a string.
436
+ Using String(firstArgument) as fallback`);
437
+ msg = String(msg);
438
+ }
439
+ this.log("info", msg);
440
+ return this;
441
+ }
442
+ }
443
+ export const Utilities = new UtilitiesClass();
444
+ //#endregion
445
+ // -------------------------------------------------------------------------------------------------------
305
446
  //#region TODO
306
447
  // TODO: Wire Options to Commands
307
448
  // TODO: Create metadata getter-s in both commands and options
@@ -0,0 +1,29 @@
1
+ /**
2
+ * Easy way to use colors
3
+ */
4
+ type ColorInput = string | {
5
+ r: number;
6
+ g: number;
7
+ b: number;
8
+ a?: number;
9
+ } | {
10
+ h: number;
11
+ s: number;
12
+ l: number;
13
+ a?: number;
14
+ };
15
+ export default class Color {
16
+ #private;
17
+ private r;
18
+ private g;
19
+ private b;
20
+ private a;
21
+ constructor(input: ColorInput);
22
+ hex(): string;
23
+ rgb(): string;
24
+ rgba(): string;
25
+ hsl(): string;
26
+ hsla(): string;
27
+ css(): string;
28
+ }
29
+ export {};
@@ -0,0 +1,153 @@
1
+ /**
2
+ * Easy way to use colors
3
+ */
4
+ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
5
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
6
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
7
+ return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
8
+ };
9
+ var _Color_instances, _Color_clamp, _Color_toHex, _Color_parseString, _Color_hslToRgb, _Color_rgbToHsl;
10
+ const NAMED_COLORS = {
11
+ red: "#ff0000",
12
+ blue: "#0000ff",
13
+ green: "#00ff00",
14
+ yellow: "#ffff00",
15
+ orange: "#ffa500",
16
+ black: "#000000",
17
+ white: "#ffffff",
18
+ gray: "#808080",
19
+ };
20
+ class Color {
21
+ constructor(input) {
22
+ _Color_instances.add(this);
23
+ this.r = 0;
24
+ this.g = 0;
25
+ this.b = 0;
26
+ this.a = 1;
27
+ if (typeof input === "string") {
28
+ __classPrivateFieldGet(this, _Color_instances, "m", _Color_parseString).call(this, input);
29
+ }
30
+ else if ("r" in input && "g" in input && "b" in input) {
31
+ this.r = __classPrivateFieldGet(this, _Color_instances, "m", _Color_clamp).call(this, input.r);
32
+ this.g = __classPrivateFieldGet(this, _Color_instances, "m", _Color_clamp).call(this, input.g);
33
+ this.b = __classPrivateFieldGet(this, _Color_instances, "m", _Color_clamp).call(this, input.b);
34
+ this.a = input.a ?? 1;
35
+ }
36
+ else if ("h" in input && "s" in input && "l" in input) {
37
+ const { r, g, b } = __classPrivateFieldGet(this, _Color_instances, "m", _Color_hslToRgb).call(this, input.h, input.s, input.l);
38
+ this.r = r;
39
+ this.g = g;
40
+ this.b = b;
41
+ this.a = input.a ?? 1;
42
+ }
43
+ else {
44
+ console.warn(`[Color.constructor] Invalid first argument!
45
+ Hint: The first argument must be a valid color array
46
+ Using black as fallback.`);
47
+ }
48
+ }
49
+ // -----------------------
50
+ // Public Methods
51
+ // -----------------------
52
+ hex() {
53
+ return `#${__classPrivateFieldGet(this, _Color_instances, "m", _Color_toHex).call(this, this.r)}${__classPrivateFieldGet(this, _Color_instances, "m", _Color_toHex).call(this, this.g)}${__classPrivateFieldGet(this, _Color_instances, "m", _Color_toHex).call(this, this.b)}`;
54
+ }
55
+ rgb() {
56
+ return `rgb(${this.r}, ${this.g}, ${this.b})`;
57
+ }
58
+ rgba() {
59
+ return `rgba(${this.r}, ${this.g}, ${this.b}, ${this.a})`;
60
+ }
61
+ hsl() {
62
+ const { h, s, l } = __classPrivateFieldGet(this, _Color_instances, "m", _Color_rgbToHsl).call(this, this.r, this.g, this.b);
63
+ return `hsl(${h}, ${s}%, ${l}%)`;
64
+ }
65
+ hsla() {
66
+ const { h, s, l } = __classPrivateFieldGet(this, _Color_instances, "m", _Color_rgbToHsl).call(this, this.r, this.g, this.b);
67
+ return `hsla(${h}, ${s}%, ${l}%, ${this.a})`;
68
+ }
69
+ css() {
70
+ return this.a === 1 ? this.hex() : this.rgba();
71
+ }
72
+ }
73
+ _Color_instances = new WeakSet(), _Color_clamp = function _Color_clamp(value) {
74
+ return Math.max(0, Math.min(255, value));
75
+ }, _Color_toHex = function _Color_toHex(value) {
76
+ return value.toString(16).padStart(2, "0");
77
+ }, _Color_parseString = function _Color_parseString(str) {
78
+ str = str.trim().toLowerCase();
79
+ if (NAMED_COLORS[str]) {
80
+ str = NAMED_COLORS[str];
81
+ }
82
+ if (str.startsWith("#")) {
83
+ const hex = str.slice(1);
84
+ if (hex.length === 3) {
85
+ this.r = parseInt(hex[0] + hex[0], 16);
86
+ this.g = parseInt(hex[1] + hex[1], 16);
87
+ this.b = parseInt(hex[2] + hex[2], 16);
88
+ }
89
+ else if (hex.length === 6) {
90
+ this.r = parseInt(hex.slice(0, 2), 16);
91
+ this.g = parseInt(hex.slice(2, 4), 16);
92
+ this.b = parseInt(hex.slice(4, 6), 16);
93
+ }
94
+ else {
95
+ console.warn(`[Color class] @briklab/lib/color: Invalid hex!
96
+ Hint: You must pass a valid hex color string!
97
+ Using black as fallback.`);
98
+ }
99
+ }
100
+ else if (str.startsWith("rgb")) {
101
+ const vals = str.match(/[\d.]+/g)?.map(Number);
102
+ if (vals && vals.length >= 3) {
103
+ [this.r, this.g, this.b] = vals;
104
+ this.a = vals[3] ?? 1;
105
+ }
106
+ }
107
+ else if (str.startsWith("hsl")) {
108
+ const vals = str.match(/[\d.]+/g)?.map(Number);
109
+ if (vals && vals.length >= 3) {
110
+ const { r, g, b } = __classPrivateFieldGet(this, _Color_instances, "m", _Color_hslToRgb).call(this, vals[0], vals[1], vals[2]);
111
+ this.r = r;
112
+ this.g = g;
113
+ this.b = b;
114
+ this.a = vals[3] ?? 1;
115
+ }
116
+ }
117
+ else {
118
+ console.warn(`[Color class] @briklab/lib/color: Unknown color string "${str}"!
119
+ Hint: The argument must be a valid color string!
120
+ Using black as fallback.`);
121
+ }
122
+ }, _Color_hslToRgb = function _Color_hslToRgb(h, s, l) {
123
+ s /= 100;
124
+ l /= 100;
125
+ const k = (n) => (n + h / 30) % 12;
126
+ const a = s * Math.min(l, 1 - l);
127
+ const f = (n) => l - a * Math.max(-1, Math.min(k(n) - 3, Math.min(9 - k(n), 1)));
128
+ return { r: Math.round(f(0) * 255), g: Math.round(f(8) * 255), b: Math.round(f(4) * 255) };
129
+ }, _Color_rgbToHsl = function _Color_rgbToHsl(r, g, b) {
130
+ r /= 255;
131
+ g /= 255;
132
+ b /= 255;
133
+ const max = Math.max(r, g, b), min = Math.min(r, g, b);
134
+ let h = 0, s = 0, l = (max + min) / 2;
135
+ if (max !== min) {
136
+ const d = max - min;
137
+ s = l > 0.5 ? d / (2 - max - min) : d / (max + min);
138
+ switch (max) {
139
+ case r:
140
+ h = (g - b) / d + (g < b ? 6 : 0);
141
+ break;
142
+ case g:
143
+ h = (b - r) / d + 2;
144
+ break;
145
+ case b:
146
+ h = (r - g) / d + 4;
147
+ break;
148
+ }
149
+ h *= 60;
150
+ }
151
+ return { h: Math.round(h), s: Math.round(s * 100), l: Math.round(l * 100) };
152
+ };
153
+ export default Color;
@@ -1,4 +1,5 @@
1
1
  /**
2
+ * # JSTC
2
3
  * @packageDocumentation
3
4
  * Runtime JS Type Checker
4
5
  * @module JSTC
@@ -1,4 +1,5 @@
1
1
  /**
2
+ * # JSTC
2
3
  * @packageDocumentation
3
4
  * Runtime JS Type Checker
4
5
  * @module JSTC
@@ -21,8 +22,20 @@ export class JSTypeChecker {
21
22
  * @param {unknown[]} args
22
23
  */
23
24
  for(args) {
25
+ if (!Array.isArray(args)) {
26
+ console.warn(`[JSTC.for] @briklab/lib/jstc: Invalid first argument!
27
+ Hint: The first argument must be a array.
28
+ Using [givenValue] as fallback`);
29
+ args = [args];
30
+ }
24
31
  return {
25
32
  check: (types) => {
33
+ if (!Array.isArray(types)) {
34
+ console.warn(`[JSTC.for().check] @briklab/lib/jstc: Invalid first argument!
35
+ Hint: The first argument must be a valid array!
36
+ Using [givenValue] as fallback`);
37
+ types = [types];
38
+ }
26
39
  if (args.length < types.length)
27
40
  return false;
28
41
  for (let i = 0; i < types.length; i++) {
@@ -61,6 +74,13 @@ export class JSTypeChecker {
61
74
  };
62
75
  }
63
76
  addCustomHandler(name, handler) {
77
+ if (!(typeof name === "string" && typeof handler === "function")) {
78
+ console.warn(`[JSTC.addCustomHandler] @briklab/lib/jstc: Invalid Arguments!
79
+ Hint: The first argument must be a string, and the second argument must be a function
80
+ Using String(argument1) and ()=>false as fallbacks`);
81
+ name = String(name);
82
+ handler = () => false;
83
+ }
64
84
  __classPrivateFieldGet(this, _JSTypeChecker___CustomHandler, "f")[name] = handler;
65
85
  }
66
86
  }
@@ -1,5 +1,51 @@
1
1
  /**
2
- * SOON
3
- * @experimental v1.1.0
2
+ * # @briklab/lib/stylesheet
3
+ * Create inline styles in JS/TS
4
4
  */
5
- export {};
5
+ /**
6
+ * # InlineStyle
7
+ * @classdesc Create a CSS Inline style.
8
+ * @class
9
+ */
10
+ export default class InlineStyle {
11
+ #private;
12
+ /**
13
+ * ## constructor
14
+ * construct a InlineStyle
15
+ */
16
+ constructor(styleObject: {
17
+ [key: string]: string;
18
+ });
19
+ generate(): string;
20
+ get text(): string;
21
+ addStyleWithObject(styleObject: object): this;
22
+ addStyleWithInlineCSS(inlineCSS: string): this;
23
+ removeStyle(styles: string[] | string): this | undefined;
24
+ applyTo(element: HTMLElement): this;
25
+ }
26
+ export declare class StyleSheet {
27
+ #private;
28
+ constructor();
29
+ /**
30
+ * Add or update a rule in the stylesheet.
31
+ * @param name The rule name or selector (string).
32
+ * @param style An InlineStyle instance.
33
+ */
34
+ set(name: string, style: InlineStyle): this;
35
+ /**
36
+ * Get a rule by name.
37
+ */
38
+ get(name: string): InlineStyle | undefined;
39
+ /**
40
+ * Remove a rule by name.
41
+ */
42
+ remove(name: string): this;
43
+ /**
44
+ * Generate CSS text for the whole stylesheet.
45
+ */
46
+ generate(): string;
47
+ /**
48
+ * Export as a string for inline style usage or injection.
49
+ */
50
+ toString(): string;
51
+ }
@@ -1 +1,186 @@
1
- export {};
1
+ /**
2
+ * # @briklab/lib/stylesheet
3
+ * Create inline styles in JS/TS
4
+ */
5
+ var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
6
+ if (kind === "m") throw new TypeError("Private method is not writable");
7
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
8
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
9
+ return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
10
+ };
11
+ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
12
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
13
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
14
+ return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
15
+ };
16
+ var _InlineStyle_instances, _InlineStyle_cssStyleDec, _InlineStyle_convertFieldToHyphenCase, _InlineStyle_convertKeysToValidCSS, _InlineStyle_styleObject, _StyleSheet_styles;
17
+ import JSTC from "../jstc/index";
18
+ import { CSSStyleDeclaration as UUIII } from "cssom";
19
+ /**
20
+ * # InlineStyle
21
+ * @classdesc Create a CSS Inline style.
22
+ * @class
23
+ */
24
+ class InlineStyle {
25
+ /**
26
+ * ## constructor
27
+ * construct a InlineStyle
28
+ */
29
+ constructor(styleObject) {
30
+ _InlineStyle_instances.add(this);
31
+ _InlineStyle_cssStyleDec.set(this, void 0);
32
+ _InlineStyle_styleObject.set(this, void 0);
33
+ if (!JSTC.for([styleObject]).check(["object|undefined"])) {
34
+ console.warn(`[InlineStyle class] @briklab/lib/stylesheet: Invalid first argument!
35
+ Hint: The first argument must be a valid style object or not be given!
36
+ Using {"imeMode":givenValue} as fallback`);
37
+ styleObject = { imeMode: `${styleObject}` };
38
+ }
39
+ __classPrivateFieldSet(this, _InlineStyle_styleObject, styleObject, "f");
40
+ __classPrivateFieldSet(this, _InlineStyle_cssStyleDec, new UUIII(), "f");
41
+ }
42
+ generate() {
43
+ let a = __classPrivateFieldGet(this, _InlineStyle_cssStyleDec, "f");
44
+ let b = __classPrivateFieldGet(this, _InlineStyle_styleObject, "f");
45
+ let c = Object.keys(b);
46
+ let d = Object.values(b);
47
+ for (let i = 0; i < c.length; i++) {
48
+ let e = c[i];
49
+ let f = d[i];
50
+ a.setProperty(e, f);
51
+ }
52
+ return a.cssText;
53
+ }
54
+ get text() {
55
+ return this.generate();
56
+ }
57
+ addStyleWithObject(styleObject) {
58
+ if (!JSTC.for([styleObject]).check(["object"])) {
59
+ console.warn(`[InlineStyle.addStyleWithObject] @briklab/lib/stylesheet: Invalid first argument!
60
+ Hint: The first argument must be a valid style object or not be given!
61
+ Using {"imeMode":givenValue} as fallback`);
62
+ styleObject = { imeMode: styleObject };
63
+ }
64
+ __classPrivateFieldSet(this, _InlineStyle_styleObject, { ...__classPrivateFieldGet(this, _InlineStyle_styleObject, "f"), ...styleObject }, "f");
65
+ this.generate();
66
+ return this;
67
+ }
68
+ addStyleWithInlineCSS(inlineCSS) {
69
+ if (!JSTC.for([inlineCSS]).check(["string"])) {
70
+ console.warn(`[InlineStyle.addStyleWithInlineCSS] @briklab/lib/stylesheet: Invalid first argument!
71
+ Hint: The first argument must be a valid inline css string!
72
+ Returned with no operations.`);
73
+ return this;
74
+ }
75
+ let s = new UUIII();
76
+ s.cssText = __classPrivateFieldGet(this, _InlineStyle_instances, "m", _InlineStyle_convertKeysToValidCSS).call(this, inlineCSS);
77
+ let o = {};
78
+ for (let i = 0; i < s.length; i++) {
79
+ const a = s[i];
80
+ const v = s.getPropertyValue(a);
81
+ o[a] = v;
82
+ }
83
+ this.addStyleWithObject(o);
84
+ return this;
85
+ }
86
+ removeStyle(styles) {
87
+ if (!JSTC.for([styles]).check(["string[]|string"])) {
88
+ console.warn(`[InlineStyle.removeStyle] @briklab/lib/stylesheet: Invalid first argument!
89
+ Hint: The first argument must be a array of valid css properties or a single valid css property [accepts any string though]
90
+ Returned with no operations.`);
91
+ return this;
92
+ }
93
+ if (typeof styles === "string") {
94
+ styles = [styles];
95
+ }
96
+ for (let i = 0; i < styles.length; i++) {
97
+ let a = styles[i];
98
+ delete __classPrivateFieldGet(this, _InlineStyle_styleObject, "f")[a];
99
+ }
100
+ }
101
+ applyTo(element) {
102
+ element.style.cssText = this.generate();
103
+ return this;
104
+ }
105
+ }
106
+ _InlineStyle_cssStyleDec = new WeakMap(), _InlineStyle_styleObject = new WeakMap(), _InlineStyle_instances = new WeakSet(), _InlineStyle_convertFieldToHyphenCase = function _InlineStyle_convertFieldToHyphenCase(string) {
107
+ return string.replace(/([A-Z])/g, (match) => `-${match.toLowerCase()}`);
108
+ }, _InlineStyle_convertKeysToValidCSS = function _InlineStyle_convertKeysToValidCSS(string) {
109
+ let a = string.split(";");
110
+ let c = "";
111
+ for (let i = 0; i < a.length; i++) {
112
+ let b = a[i];
113
+ let [k, v] = b.split(":");
114
+ k = k.trim();
115
+ v = v.trim();
116
+ c += `${__classPrivateFieldGet(this, _InlineStyle_instances, "m", _InlineStyle_convertFieldToHyphenCase).call(this, k)}:${v};`;
117
+ }
118
+ return c;
119
+ };
120
+ export default InlineStyle;
121
+ export class StyleSheet {
122
+ constructor() {
123
+ _StyleSheet_styles.set(this, void 0);
124
+ __classPrivateFieldSet(this, _StyleSheet_styles, {}, "f");
125
+ }
126
+ /**
127
+ * Add or update a rule in the stylesheet.
128
+ * @param name The rule name or selector (string).
129
+ * @param style An InlineStyle instance.
130
+ */
131
+ set(name, style) {
132
+ if (!JSTC.for([name, style]).check(["string", "object"])) {
133
+ console.warn(`[StyleSheet.set] @briklab/lib/stylesheet: Invalid arguments! ` +
134
+ `Name must be string, style must be InlineStyle instance. Received: name=${name}, style=${style}`);
135
+ return this;
136
+ }
137
+ if (!(style instanceof InlineStyle)) {
138
+ console.warn(`[StyleSheet.set] @briklab/lib/stylesheet: Provided style is not an InlineStyle instance! ` +
139
+ `Received: ${style}`);
140
+ return this;
141
+ }
142
+ __classPrivateFieldGet(this, _StyleSheet_styles, "f")[name] = style;
143
+ return this;
144
+ }
145
+ /**
146
+ * Get a rule by name.
147
+ */
148
+ get(name) {
149
+ if (!JSTC.for([name]).check(["string"])) {
150
+ console.warn(`[StyleSheet.get] @briklab/lib/stylesheet: Invalid argument! Name must be a string. Received: ${name}`);
151
+ return undefined;
152
+ }
153
+ return __classPrivateFieldGet(this, _StyleSheet_styles, "f")[name];
154
+ }
155
+ /**
156
+ * Remove a rule by name.
157
+ */
158
+ remove(name) {
159
+ if (!JSTC.for([name]).check(["string"])) {
160
+ console.warn(`[StyleSheet.remove] @briklab/lib/stylesheet: Invalid argument! Name must be a string. Received: ${name}`);
161
+ return this;
162
+ }
163
+ delete __classPrivateFieldGet(this, _StyleSheet_styles, "f")[name];
164
+ return this;
165
+ }
166
+ /**
167
+ * Generate CSS text for the whole stylesheet.
168
+ */
169
+ generate() {
170
+ let css = "";
171
+ for (const key in __classPrivateFieldGet(this, _StyleSheet_styles, "f")) {
172
+ const style = __classPrivateFieldGet(this, _StyleSheet_styles, "f")[key];
173
+ if (style) {
174
+ css += `${key} { ${style.text} }\n`;
175
+ }
176
+ }
177
+ return css.trim();
178
+ }
179
+ /**
180
+ * Export as a string for inline style usage or injection.
181
+ */
182
+ toString() {
183
+ return this.generate();
184
+ }
185
+ }
186
+ _StyleSheet_styles = new WeakMap();
package/package.json CHANGED
@@ -18,7 +18,13 @@
18
18
  "keywords": [
19
19
  "helpers",
20
20
  "typescript",
21
- "types"
21
+ "types",
22
+ "cli",
23
+ "stylesheet",
24
+ "javascript",
25
+ "js",
26
+ "command",
27
+ "style"
22
28
  ],
23
29
  "license": "Apache-2.0",
24
30
  "main": "dist/index.js",
@@ -28,8 +34,9 @@
28
34
  "repository": {
29
35
  "url": "https://github.com/EkaanshPC/briklab-stdlib"
30
36
  },
31
- "version": "1.0.1",
37
+ "version": "1.0.9",
32
38
  "devDependencies": {
39
+ "@types/cssom": "^0.4.3",
33
40
  "@types/node": "^25.1.0",
34
41
  "typescript": "^5.9.3"
35
42
  },
@@ -45,8 +52,19 @@
45
52
  "./jstc": {
46
53
  "import": "./dist/jstc/index.js",
47
54
  "types": "./dist/jstc/index.d.ts"
55
+ },
56
+ "./stylesheet": {
57
+ "import": "./dist/stylesheet/index.js",
58
+ "types": "./dist/stylesheet/index.d.ts"
59
+ },
60
+ "./color": {
61
+ "import": "./dist/color/index.js",
62
+ "types": "./dist/color/index.d.ts"
48
63
  }
49
64
  },
65
+ "dependencies": {
66
+ "cssom": "^0.5.0"
67
+ },
50
68
  "scripts": {
51
69
  "build": "tsc"
52
70
  }