Skip to content

Commit bc1fc83

Browse files
committed
feat: add more options to imploder
1 parent 1014e7b commit bc1fc83

File tree

2 files changed

+206
-2
lines changed

2 files changed

+206
-2
lines changed

libs/dfts-helper/src/lib/helper/string/imploder/imploder.spec.ts

+195-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {s_imploder} from './imploder';
1+
import { ImploderBuilder, s_imploder } from "./imploder";
22

33
describe('imploder', () => {
44
it('imploder', () => {
@@ -12,3 +12,197 @@ describe('imploder', () => {
1212
expect(s_imploder(test).separator('; ').build()).toBe('Apple; Bannana; Rasberry; Pie; Ananas; Lannanas');
1313
});
1414
});
15+
16+
describe('ImploderBuilder', () => {
17+
it('should create an instance of ImploderBuilder', () => {
18+
const builder = new ImploderBuilder();
19+
expect(builder).toBeInstanceOf(ImploderBuilder);
20+
});
21+
22+
it('should create an instance of ImploderBuilder using static get method', () => {
23+
const builder = ImploderBuilder.get();
24+
expect(builder).toBeInstanceOf(ImploderBuilder);
25+
});
26+
27+
it('should set and retrieve the source correctly', () => {
28+
const source = ['one', 'two', 'three'];
29+
const builder = new ImploderBuilder().source(source);
30+
expect(builder.build()).toEqual(source.join(''));
31+
});
32+
33+
it('should map source correctly', () => {
34+
const source = [1, 2, 3];
35+
const builder = new ImploderBuilder().mappedSource(source, (it) => it.toString());
36+
expect(builder.build()).toEqual(source.join(''));
37+
});
38+
39+
it('should set and retrieve the maxLength correctly', () => {
40+
const maxLength = 5;
41+
const builder = new ImploderBuilder().maxLength(maxLength);
42+
expect(builder.build().length).toBeLessThanOrEqual(maxLength);
43+
});
44+
45+
it('should set and retrieve the offset correctly', () => {
46+
const offset = 2;
47+
const source = ['one', 'two', 'three'];
48+
const expected = source.slice(offset).join('');
49+
const builder = new ImploderBuilder().source(source).offset(offset);
50+
expect(builder.build()).toEqual(expected);
51+
});
52+
53+
it('should set and retrieve the separator correctly', () => {
54+
const separator = ', ';
55+
const source = ['one', 'two', 'three'];
56+
const expected = source.join(separator);
57+
const builder = new ImploderBuilder().source(source).separator(separator);
58+
expect(builder.build()).toEqual(expected);
59+
});
60+
61+
it('should set and retrieve the suffix correctly', () => {
62+
const suffix = '...';
63+
const source = ['one', 'two', 'three'];
64+
const builder = new ImploderBuilder().source(source).suffix(suffix);
65+
expect(builder.build()).toEqual(source.join(''));
66+
});
67+
68+
it('should set and retrieve the suffix and max length correctly', () => {
69+
const suffix = '...';
70+
const source = ['one', 'two', 'three'];
71+
const builder = new ImploderBuilder().source(source).suffix(suffix).maxLength(10);
72+
expect(builder.build()).toEqual('onetwothre...');
73+
});
74+
75+
it('should handle a null source gracefully', () => {
76+
const builder = new ImploderBuilder().source(null);
77+
expect(builder.build()).toEqual('');
78+
});
79+
80+
it('should handle undefined values gracefully', () => {
81+
const builder = new ImploderBuilder();
82+
expect(builder.build()).toEqual('');
83+
});
84+
});
85+
86+
describe('s_imploder', () => {
87+
it('should create an instance of ImploderBuilder using s_imploder', () => {
88+
const builder = s_imploder();
89+
expect(builder).toBeInstanceOf(ImploderBuilder);
90+
});
91+
92+
it('should create an instance of ImploderBuilder with the provided source using s_imploder', () => {
93+
const source = ['one', 'two', 'three'];
94+
const builder = s_imploder(source);
95+
expect(builder.build()).toEqual(source.join(''));
96+
});
97+
});
98+
99+
describe('Edge Cases', () => {
100+
it('should handle a null source gracefully when using s_imploder', () => {
101+
const builder = s_imploder(null);
102+
expect(builder.build()).toEqual('');
103+
});
104+
105+
it('should handle undefined values gracefully when using s_imploder', () => {
106+
const builder = s_imploder(undefined);
107+
expect(builder.build()).toEqual('');
108+
});
109+
110+
it('should handle mapping with a null source when using mappedSource', () => {
111+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
112+
// @ts-ignore
113+
const builder = new ImploderBuilder().mappedSource(null, (it) => it.toString());
114+
expect(builder.build()).toEqual('');
115+
});
116+
117+
it('should handle mapping without a map function when using mappedSource and numbers', () => {
118+
const source = [1, 2, 3];
119+
const builder = new ImploderBuilder().mappedSource(source);
120+
expect(builder.build()).toEqual(source.join(''));
121+
});
122+
123+
it('should handle mapping without a map function when using mappedSource and strings', () => {
124+
const source = ['1', '2', '3'];
125+
const builder = new ImploderBuilder().mappedSource(source);
126+
expect(builder.build()).toEqual(source.join(''));
127+
});
128+
129+
it('should handle maxLength exceeding the string length when using maxLength', () => {
130+
const maxLength = 100;
131+
const source = ['one', 'two', 'three'];
132+
const builder = new ImploderBuilder().source(source).maxLength(maxLength);
133+
expect(builder.build()).toEqual(source.join(''));
134+
});
135+
136+
it('should handle a negative offset when using offset', () => {
137+
const offset = -2;
138+
const source = ['one', 'two', 'three'];
139+
const builder = new ImploderBuilder().source(source).offset(offset);
140+
expect(builder.build()).toEqual('twothree');
141+
});
142+
143+
it('should handle a negative offset when using offset and max length', () => {
144+
const offset = -2;
145+
const source = ['one', 'two', 'three'];
146+
const builder = new ImploderBuilder().source(source).offset(offset);
147+
expect(builder.build()).toEqual('twothree');
148+
});
149+
150+
it('should handle null separator when using separator', () => {
151+
const source = ['one', 'two', 'three'];
152+
const builder = new ImploderBuilder().source(source).separator(null);
153+
expect(builder.build()).toEqual(source.join(''));
154+
});
155+
156+
it('should handle null suffix when using suffix', () => {
157+
const source = ['one', 'two', 'three'];
158+
const builder = new ImploderBuilder().source(source).suffix(null);
159+
expect(builder.build()).toEqual(source.join(''));
160+
});
161+
162+
it('should handle an empty source when using source', () => {
163+
const source: string[] = [];
164+
const builder = new ImploderBuilder().source(source);
165+
expect(builder.build()).toEqual('');
166+
});
167+
168+
it('should handle a source with empty strings when using source', () => {
169+
const source = ['', '', ''];
170+
const builder = new ImploderBuilder().source(source);
171+
expect(builder.build()).toEqual('');
172+
});
173+
174+
it('should build correctly with maxLength and suffix', () => {
175+
const source = ['one', 'two', 'three'];
176+
const maxLength = 5;
177+
const suffix = '...';
178+
const builder = new ImploderBuilder().source(source).maxLength(maxLength).suffix(suffix);
179+
const expected = source.join('').slice(0, maxLength) + suffix;
180+
expect(builder.build()).toEqual(expected);
181+
});
182+
183+
it('should build correctly with offset and separator', () => {
184+
const source = ['one', 'two', 'three', 'four'];
185+
const offset = 1;
186+
const separator = ', ';
187+
const builder = new ImploderBuilder().source(source).offset(offset).separator(separator);
188+
const expected = source.slice(offset).join(separator);
189+
expect(builder.build()).toEqual(expected);
190+
});
191+
192+
it('should handle mapping with a custom mapping function when using mappedSource', () => {
193+
const source = [1, 2, 3];
194+
const mappingFunction = (it: number) => `Item ${it}`;
195+
const builder = new ImploderBuilder().mappedSource(source, mappingFunction);
196+
const expected = source.map(mappingFunction).join('');
197+
expect(builder.build()).toEqual(expected);
198+
});
199+
200+
it('should handle mapping with a custom mapping function and maxLength when using mappedSource', () => {
201+
const source = [1, 2, 3];
202+
const mappingFunction = (it: number) => `Item ${it}`;
203+
const maxLength = 5;
204+
const builder = new ImploderBuilder().mappedSource(source, mappingFunction).maxLength(maxLength);
205+
const expected = source.map(mappingFunction).join('').slice(0, maxLength);
206+
expect(builder.build()).toEqual(expected);
207+
});
208+
});

libs/dfts-helper/src/lib/helper/string/imploder/imploder.ts

+11-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { s_from } from "../from/from";
2+
13
export const s_imploder = (source?: string[] | null): ImploderBuilder => {
24
return ImploderBuilder.get(source);
35
};
@@ -7,6 +9,7 @@ export class ImploderBuilder {
79
private _offset?: number | null;
810
private _separator?: string | null;
911
private _suffix?: string | null;
12+
private _alwaysShowSuffix?: boolean | null;
1013
private _source?: string[] | null;
1114

1215
static get(source?: string[] | null): ImploderBuilder {
@@ -21,6 +24,8 @@ export class ImploderBuilder {
2124
mappedSource<T>(source?: T[] | null, mapFn?: (it: T) => string): this {
2225
if (mapFn && source) {
2326
this._source = source.map(mapFn);
27+
} else if (source && source.every((it: T) => typeof it === 'string' || typeof it === 'boolean' || typeof it === 'number')) {
28+
this._source = source.map((it) => s_from(it as string | boolean | number));
2429
}
2530
return this;
2631
}
@@ -45,6 +50,11 @@ export class ImploderBuilder {
4550
return this;
4651
}
4752

53+
alwaysShowSuffix(alwaysShowSuffix?: boolean | null): this {
54+
this._alwaysShowSuffix = alwaysShowSuffix;
55+
return this;
56+
}
57+
4858
build(): string {
4959
if (!this._source) {
5060
return '';
@@ -60,6 +70,6 @@ export class ImploderBuilder {
6070
if (this._maxLength && toReturn.length > this._maxLength) {
6171
return `${toReturn.slice(0, this._maxLength)}${this._suffix ?? ''}`;
6272
}
63-
return toReturn;
73+
return `${toReturn}${this._alwaysShowSuffix ? this._suffix ?? '' : ''}`;
6474
}
6575
}

0 commit comments

Comments
 (0)