@zelgadis87/utils-core 4.6.2 → 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/dist/Optional.d.ts +24 -16
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/dist/utils/css.d.ts +7 -0
- package/dist/utils/index.d.ts +1 -0
- package/dist/utils/strings/StringParts.d.ts +6 -0
- package/esbuild/index.cjs +52 -21
- package/esbuild/index.cjs.map +3 -3
- package/esbuild/index.mjs +51 -21
- package/esbuild/index.mjs.map +3 -3
- package/package.json +1 -1
- package/src/Optional.ts +39 -36
- package/src/utils/css.ts +33 -0
- package/src/utils/index.ts +1 -0
- package/src/utils/strings/StringParts.ts +11 -0
package/package.json
CHANGED
package/src/Optional.ts
CHANGED
|
@@ -12,17 +12,21 @@ export class Optional<T> implements TOptional<T> {
|
|
|
12
12
|
this._present = defined;
|
|
13
13
|
}
|
|
14
14
|
|
|
15
|
-
public
|
|
15
|
+
public getRawValue(): T | undefined {
|
|
16
16
|
return this._value;
|
|
17
17
|
}
|
|
18
18
|
|
|
19
|
-
public get(): T
|
|
19
|
+
public get(): T {
|
|
20
|
+
return this.getOrElseThrow( () => new ErrorGetEmptyOptional() );
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
public getOrElseThrow( errorProducer: TProducer<Error> ): T {
|
|
20
24
|
if ( this.isEmpty() )
|
|
21
|
-
throw
|
|
22
|
-
return this.
|
|
25
|
+
throw errorProducer();
|
|
26
|
+
return this._value!;
|
|
23
27
|
}
|
|
24
28
|
|
|
25
|
-
public set(t: T) {
|
|
29
|
+
public set( t: T ) {
|
|
26
30
|
if ( isNullOrUndefined( t ) )
|
|
27
31
|
throw new ErrorSetEmptyOptional();
|
|
28
32
|
this._value = t;
|
|
@@ -47,13 +51,13 @@ export class Optional<T> implements TOptional<T> {
|
|
|
47
51
|
}
|
|
48
52
|
public ifPresent( callback: TConsumer<T> ) {
|
|
49
53
|
if ( this.isPresent() )
|
|
50
|
-
callback( this.
|
|
54
|
+
callback( this.get() );
|
|
51
55
|
}
|
|
52
56
|
public apply( callbackIfPresent: TConsumer<T>, callbackIfEmpty: TVoidFunction ) {
|
|
53
57
|
if ( this.isEmpty() ) {
|
|
54
58
|
callbackIfEmpty();
|
|
55
59
|
} else {
|
|
56
|
-
callbackIfPresent( this.
|
|
60
|
+
callbackIfPresent( this.get() );
|
|
57
61
|
}
|
|
58
62
|
}
|
|
59
63
|
|
|
@@ -69,9 +73,9 @@ export class Optional<T> implements TOptional<T> {
|
|
|
69
73
|
}
|
|
70
74
|
return this as TPresentOptional<T>;
|
|
71
75
|
}
|
|
72
|
-
public orElseThrow(
|
|
76
|
+
public orElseThrow( errorProducer: TProducer<Error> ) {
|
|
73
77
|
if ( this.isEmpty() )
|
|
74
|
-
throw
|
|
78
|
+
throw errorProducer();
|
|
75
79
|
return this as TPresentOptional<T>;
|
|
76
80
|
}
|
|
77
81
|
|
|
@@ -79,23 +83,15 @@ export class Optional<T> implements TOptional<T> {
|
|
|
79
83
|
return this.flatMapTo( t => Optional.ofNullable( mapper( t ) ) );
|
|
80
84
|
}
|
|
81
85
|
public flatMapTo<R>( mapper: TFunction<T, TOptional<R>> ) {
|
|
82
|
-
|
|
83
|
-
const newValue = mapper( this.rawValue );
|
|
84
|
-
return newValue;
|
|
85
|
-
}
|
|
86
|
-
return this as TEmptyOptional<T>;
|
|
86
|
+
return this.isPresent() ? mapper( this.get() ) : Optional.empty<R>();
|
|
87
87
|
}
|
|
88
88
|
|
|
89
89
|
public filter( predicate: TPredicate<T> ): this | TEmptyOptional<T> {
|
|
90
|
-
if ( this.
|
|
91
|
-
if ( predicate( this.rawValue ) ) {
|
|
92
|
-
return this;
|
|
93
|
-
} else {
|
|
94
|
-
return Optional.empty();
|
|
95
|
-
}
|
|
96
|
-
} else {
|
|
90
|
+
if ( this.isEmpty() )
|
|
97
91
|
return this;
|
|
98
|
-
|
|
92
|
+
if ( predicate( this.get() ) )
|
|
93
|
+
return this;
|
|
94
|
+
return Optional.empty();
|
|
99
95
|
}
|
|
100
96
|
|
|
101
97
|
public static empty<T>(): TEmptyOptional<T> {
|
|
@@ -114,19 +110,26 @@ export class Optional<T> implements TOptional<T> {
|
|
|
114
110
|
export default Optional;
|
|
115
111
|
|
|
116
112
|
export type TOptional<T> = {
|
|
117
|
-
readonly rawValue: T | undefined;
|
|
118
113
|
|
|
119
114
|
/**
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
get(): T
|
|
115
|
+
* @returns the currently stored value, if any; throws {@link ErrorGetEmptyOptional} otherwise;
|
|
116
|
+
*/
|
|
117
|
+
get(): T;
|
|
118
|
+
/**
|
|
119
|
+
* @returns the currently stored value, if any; returns undefined otherwise;
|
|
120
|
+
*/
|
|
121
|
+
getRawValue(): T | undefined;
|
|
122
|
+
/**
|
|
123
|
+
* @returns the currently stored value, if any; throws the error generated by errorProducer otherwise;
|
|
124
|
+
*/
|
|
125
|
+
getOrElseThrow: ( errorProducer: TProducer<Error> ) => T;
|
|
123
126
|
/**
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
set(t: T): void;
|
|
127
|
+
* Places a new value inside this optional. Throws {@link ErrorSetEmptyOptional} if t is null or undefined.
|
|
128
|
+
*/
|
|
129
|
+
set( t: T ): void;
|
|
127
130
|
/**
|
|
128
|
-
|
|
129
|
-
|
|
131
|
+
* Clears the current value from this optional, if any.
|
|
132
|
+
*/
|
|
130
133
|
clear(): void;
|
|
131
134
|
isEmpty(): boolean;
|
|
132
135
|
isPresent(): boolean;
|
|
@@ -135,9 +138,9 @@ export type TOptional<T> = {
|
|
|
135
138
|
ifPresent: ( callback: TConsumer<T> ) => void;
|
|
136
139
|
apply( callbackIfPresent: TConsumer<T>, callbackIfEmpty: TVoidFunction ): void;
|
|
137
140
|
|
|
138
|
-
orElse: (
|
|
141
|
+
orElse: ( newValue: T | null | undefined ) => TOptional<T>;
|
|
139
142
|
orElseGet: ( produceValue: TProducer<T | null | undefined> ) => TOptional<T>;
|
|
140
|
-
orElseThrow: (
|
|
143
|
+
orElseThrow: ( errorProducer: TProducer<Error> ) => TPresentOptional<T>;
|
|
141
144
|
|
|
142
145
|
mapTo<R>( mapper: TFunction<T, R> ): TOptional<R>;
|
|
143
146
|
flatMapTo<R>( mapper: TFunction<T, TOptional<R>> ): TOptional<R>;
|
|
@@ -145,19 +148,19 @@ export type TOptional<T> = {
|
|
|
145
148
|
filter( predicate: TPredicate<T> ): TOptional<T>;
|
|
146
149
|
}
|
|
147
150
|
export type TEmptyOptional<T> = TOptional<T> & {
|
|
148
|
-
|
|
151
|
+
get(): never;
|
|
149
152
|
isEmpty(): true;
|
|
150
153
|
isPresent(): false;
|
|
151
154
|
}
|
|
152
155
|
export type TPresentOptional<T> = TOptional<T> & {
|
|
153
|
-
|
|
156
|
+
get(): T;
|
|
154
157
|
isEmpty(): false;
|
|
155
158
|
isPresent(): true;
|
|
156
159
|
}
|
|
157
160
|
|
|
158
161
|
export class ErrorGetEmptyOptional extends Error {
|
|
159
162
|
constructor() {
|
|
160
|
-
super( "Cannot retrieve a value from an
|
|
163
|
+
super( "Cannot retrieve a value from an empty Optional." );
|
|
161
164
|
}
|
|
162
165
|
}
|
|
163
166
|
export class ErrorSetEmptyOptional extends Error {
|
package/src/utils/css.ts
ADDED
|
@@ -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
|
+
}
|
package/src/utils/index.ts
CHANGED
|
@@ -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
|
+
|