@zelgadis87/utils-core 4.7.0 → 4.8.0

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/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "$schema": "https://json.schemastore.org/package",
3
3
  "name": "@zelgadis87/utils-core",
4
- "version": "4.7.0",
4
+ "version": "4.8.0",
5
5
  "author": "Zelgadis87",
6
6
  "license": "ISC",
7
7
  "private": false,
@@ -0,0 +1,33 @@
1
+ import { isFunction, type TProducer } from "./index.ts";
2
+ import { type TKeysOfType } from "./records.ts";
3
+
4
+ export type TCSSRuleDeclarationKey = TKeysOfType<CSSStyleDeclaration, string> & string;
5
+ export type TCSSVariableDeclarationKey = `--${string}`;
6
+ export type TCSSGenericDeclarationKey = TCSSRuleDeclarationKey | TCSSVariableDeclarationKey;
7
+ export type TCSSDeclarationRulesDictionary = Partial<Record<TCSSGenericDeclarationKey, string>>;
8
+ // export type TCSSDeclarationProduceableRulesDictionary = Partial<Record<TCSSGenericDeclarationKey, TProduceable<string>>>;
9
+ type TProduceable<T> = T | TProducer<T>;
10
+
11
+ const newLine = '\n', tabulation = '\t', colon = ':', semiColon = ';', space = ' ', openBracket = '{', closeBracket = '}';
12
+
13
+ export function cssDeclarationRulesDictionaryToCss( syleDeclarationRulesForSelectors: Record<string, TCSSDeclarationRulesDictionary> ) {
14
+ return Object.entries( syleDeclarationRulesForSelectors ).map( ( [ selector, styleDeclarationRules ] ) => {
15
+ const cssRules = cssSelectorDeclarationRulesDictionaryToCss( styleDeclarationRules );
16
+ if ( !cssRules.length ) return null;
17
+ return selector + space + openBracket + newLine + cssRules.map( rule => tabulation + rule + semiColon ).join( newLine ) + newLine + closeBracket;
18
+ } ).filter( Boolean ).join( newLine + newLine );
19
+ }
20
+
21
+ /* exported for test purposes only */
22
+ export function cssSelectorDeclarationRulesDictionaryToCss( styleDeclarationRulesProduceable: TCSSDeclarationRulesDictionary ): string[] {
23
+ const styleDeclarationRules = produceableToValue( styleDeclarationRulesProduceable );
24
+ return Object.entries( styleDeclarationRules ).map( ( [ key, value ] ) => pascalCaseToKebabCase(key) + colon + space + produceableToValue( value ) );
25
+ }
26
+
27
+ function pascalCaseToKebabCase( s: string ) {
28
+ return s.split( /([A-Z][a-z]*)/ ).filter( Boolean ).map( n => n.toLowerCase() ).join( '-' );
29
+ }
30
+
31
+ function produceableToValue<T>( t: TProduceable<T> ): T {
32
+ return isFunction( t ) ? t() : t;
33
+ }
@@ -1,6 +1,7 @@
1
1
 
2
2
  export * from './arrays.js';
3
3
  export * from './booleans.js';
4
+ export { TCSSDeclarationRulesDictionary, cssDeclarationRulesDictionaryToCss } from './css.js';
4
5
  export * from './empties.js';
5
6
  export * from './errors.js';
6
7
  export * from './functions.js';
@@ -111,4 +111,15 @@ export class StringParts {
111
111
  return new StringParts( ...parts );
112
112
  };
113
113
 
114
+ /**
115
+ * Tries to convert a word in PascalCase to an array of words. Will not work if there are spaces or special characters. Does not cope well on uppercase abbreviations (eg, "CSS").
116
+ * @param s a string in PascalCase.
117
+ * @returns a StringParts where each sub-word in PascalCase is now a part.
118
+ */
119
+ public static tryToParsePascalCaseWord( s: string ): StringParts {
120
+ return new StringParts( ...s.split( /([A-Z][a-z]*)/ ).filter( Boolean ).map( n => n.toLowerCase() ) );
121
+ }
122
+
114
123
  }
124
+
125
+