@zelgadis87/utils-core 4.3.1 → 4.3.2

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.3.1",
4
+ "version": "4.3.2",
5
5
  "author": "Zelgadis87",
6
6
  "license": "ISC",
7
7
  "private": false,
@@ -87,3 +87,10 @@ export function sortedArray<T>( arr: TReadableArray<T>, sortFn?: TComparisonFunc
87
87
  export function includes<T>( arr: TReadableArray<T>, item: unknown, fromIndex?: number ) {
88
88
  return arr.includes( item as any, fromIndex );
89
89
  }
90
+
91
+ export function mapTruthys<T, R>( arr: Array<T>, mapper: ( value: T, index: number, array: T[] ) => R | undefined ): Array<R> {
92
+ return arr.map( mapper ).filter( value => value !== undefined ) as Array<R>;
93
+ }
94
+ export function flatMapTruthys<T, R>( arr: Array<T>, mapper: ( value: T, index: number, array: T[] ) => R | undefined ): Array<R> {
95
+ return arr.flatMap( mapper ).filter( value => value !== undefined ) as Array<R>;
96
+ }
@@ -1,5 +1,5 @@
1
1
  import { range, TReadableArray } from "./arrays.js";
2
- import { isNullOrUndefined, TMaybe } from "./nulls.js";
2
+ import { ensureDefined, isNullOrUndefined, TMaybe } from "./nulls.js";
3
3
 
4
4
  // Semantic-only
5
5
  export type THtmlString = string;
@@ -31,6 +31,54 @@ export function stringToNumber( s: string | null | undefined ): number | null {
31
31
  return Number( s );
32
32
  }
33
33
 
34
+ export function pad( str: string, n: number, char: string, where: 'left' | 'right' = 'left' ): string {
35
+ const length = ensureDefined( str ).length;
36
+ if ( length >= ensureDefined( n ) ) return str;
37
+
38
+ if ( ensureDefined( char ).length !== 1 )
39
+ throw new Error( 'Illegal pad character' );
40
+ const padding = repeat( char, n - length );
41
+ return ( where === 'left' ? padding : '' ) + str + ( where === 'right' ? padding : '' );
42
+ }
43
+
44
+ export function padLeft( str: string, n: number, char: string ): string {
45
+ return pad( str, n, char, 'left' );
46
+ }
47
+
48
+ export function padRight( str: string, n: number, char: string ): string {
49
+ return pad( str, n, char, 'right' );
50
+ }
51
+
52
+ export function ellipsis( str: string, maxLength: number, padChar: string, padWhere: 'left' | 'right' = 'left' ): string {
53
+ if ( maxLength < 4 )
54
+ throw new Error( 'Invalid argument maxLength' );
55
+ if ( str.length <= maxLength ) {
56
+ return pad( str, maxLength, padChar, padWhere );
57
+ } else {
58
+ return str.substring( 0, maxLength - 3 ) + '...';
59
+ }
60
+ }
61
+
62
+ export default function pluralize( n: number, singular: string, plural?: string ): string {
63
+
64
+ if ( !singular || !singular.length )
65
+ throw new Error();
66
+ if ( n === 1 )
67
+ return singular;
68
+
69
+ plural = plural ?? singular + 's';
70
+
71
+ const firstUppercase = singular.charAt( 0 ) === singular.charAt( 0 ).toUpperCase();
72
+ if ( firstUppercase ) {
73
+ const PLURAL = plural.toUpperCase();
74
+ const isAllUppercase = plural === PLURAL;
75
+ plural = isAllUppercase ? PLURAL : plural.charAt( 0 ).toUpperCase() + plural.slice( 1 ).toLowerCase();
76
+ }
77
+
78
+ return plural;
79
+
80
+ }
81
+
34
82
  export class StringParts {
35
83
 
36
84
  private readonly _parts: string[]
@@ -9,8 +9,6 @@ export { default as jsonCloneDeep } from './jsonCloneDeep';
9
9
  export * from './math.js';
10
10
  export { default as noop } from './noop';
11
11
  export { default as omit } from './omit';
12
- export { default as pad } from './pad';
13
- export { default as pluralize } from './pluralize';
14
12
  export * from './round.js';
15
13
  export { default as sortBy } from './sortBy';
16
14
  export { default as throttle } from './throttle';
package/src/utils/pad.ts DELETED
@@ -1,20 +0,0 @@
1
- import { ensureDefined } from "../types/nulls.js";
2
- import { repeat } from "../types/strings.js";
3
-
4
- export default function pad( str: string, n: number, char: string, where: 'left' | 'right' = 'left' ): string {
5
- const length = ensureDefined( str ).length;
6
- if ( length >= ensureDefined( n ) ) return str;
7
-
8
- if ( ensureDefined( char ).length !== 1 )
9
- throw new Error( 'Illegal pad character' );
10
- const padding = repeat( char, n - length );
11
- return ( where === 'left' ? padding : '' ) + str + ( where === 'right' ? padding : '' );
12
- }
13
-
14
- export function padLeft( str: string, n: number, char: string ): string {
15
- return pad( str, n, char, 'left' );
16
- }
17
-
18
- export function padRight( str: string, n: number, char: string ): string {
19
- return pad( str, n, char, 'right' );
20
- }
@@ -1,20 +0,0 @@
1
-
2
- export default function pluralize( n: number, singular: string, plural?: string ): string {
3
-
4
- if ( !singular || !singular.length )
5
- throw new Error();
6
- if ( n === 1 )
7
- return singular;
8
-
9
- plural = plural ?? singular + 's';
10
-
11
- const firstUppercase = singular.charAt( 0 ) === singular.charAt( 0 ).toUpperCase();
12
- if ( firstUppercase ) {
13
- const PLURAL = plural.toUpperCase();
14
- const isAllUppercase = plural === PLURAL;
15
- plural = isAllUppercase ? PLURAL : plural.charAt( 0 ).toUpperCase() + plural.slice( 1 ).toLowerCase();
16
- }
17
-
18
- return plural;
19
-
20
- }