Skip to content

Commit 8696313

Browse files
Truncate Method (#52)
* Added the tests. * Added limit method. * Improved the formatting. * Rename method and the length parameter. * Changed to trimRight * Updated the parameters. * Fixed the tests. * Added the characters package. * Implemented includeSubstitutionLength and used the characters package. * Updated the tests and added tests for some edge cases. * Added docstring. * Renamed the parameter name. * Added new line. * Simplified the calculation of final truncate length. * Used the characters.length * Fixed typo. * Fixed formatting * Fixed formatting.
1 parent e2a81c8 commit 8696313

File tree

3 files changed

+124
-0
lines changed

3 files changed

+124
-0
lines changed

lib/string_basics.dart

+38
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
// All rights reserved. Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

5+
import 'package:characters/characters.dart';
6+
57
import 'src/slice_indices.dart';
68

79
/// Utility extension methods for the native [String] class.
@@ -201,6 +203,42 @@ extension StringBasics on String {
201203
return stringBuffer.toString();
202204
}
203205

206+
/// Returns a truncated version of the string.
207+
///
208+
/// Example:
209+
/// ```dart
210+
/// final sentence = 'The quick brown fox jumps over the lazy dog';
211+
/// final truncated = sentence.truncate(20); // 'The quick brown fox...'
212+
/// ```
213+
///
214+
/// The [length] is the truncated length of the string.
215+
/// The [substitution] is the substituting string of the truncated characters.
216+
/// If not null or empty it will be appended at the end of the truncated string.
217+
/// The [trimTrailingWhitespace] is whether or not to trim the spaces of the truncated string
218+
/// before appending the ending string.
219+
/// The [includeSubstitutionInLength] is whether or not that the length of the substitution string will be included
220+
/// with the intended truncated length.
221+
String truncate(
222+
int length, {
223+
String substitution = '',
224+
bool trimTrailingWhitespace = true,
225+
bool includeSubstitutionInLength = false,
226+
}) {
227+
if (this.length <= length) {
228+
return this;
229+
}
230+
231+
// calculate the final truncate length where whether or not to include the length of substitution string
232+
final truncatedLength = includeSubstitutionInLength
233+
? (length - substitution.characters.length)
234+
: length;
235+
final truncated = this.characters.take(truncatedLength).toString();
236+
237+
// finally trim the trailing white space if needed
238+
return (trimTrailingWhitespace ? truncated.trimRight() : truncated) +
239+
substitution;
240+
}
241+
204242
/// Returns a string with the first character in upper case.
205243
///
206244
/// This method can capitalize first character

pubspec.yaml

+3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ repository: https://github.com/google/dart-basics
1010
environment:
1111
sdk: ">=2.17.0 <3.0.0"
1212

13+
dependencies:
14+
characters: ^1.2.1
15+
1316
dev_dependencies:
1417
pedantic: ^1.8.0
1518
test: ^1.16.0

test/string_basics_test.dart

+83
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,89 @@ void main() {
353353
});
354354
});
355355

356+
group('truncate', () {
357+
test(
358+
'returns a truncated string that has the length'
359+
'based on the limit provided', () {
360+
final sentence = 'The quick brown fox jumps over the lazy dog';
361+
expect(sentence.truncate(20), 'The quick brown fox');
362+
});
363+
364+
test(
365+
'returns the same string if the length of the string'
366+
'is less than provided limit', () {
367+
final sentence = 'The quick brown fox';
368+
expect(sentence.truncate(20), 'The quick brown fox');
369+
});
370+
371+
test(
372+
'returns a truncated string that has the length based on the length'
373+
'provided without trimming the spaces at the end', () {
374+
final sentence = 'The quick brown fox jumps over the lazy dog';
375+
expect(sentence.truncate(20, trimTrailingWhitespace: false),
376+
'The quick brown fox ');
377+
});
378+
379+
test(
380+
'returns a truncated string that has the length based on the length'
381+
'provided with a custom substitution string', () {
382+
final sentence = 'The quick brown fox jumps over the lazy dog';
383+
expect(sentence.truncate(20, substitution: ' (...)'),
384+
'The quick brown fox (...)');
385+
expect(
386+
sentence.truncate(20, substitution: '...'), 'The quick brown fox...');
387+
});
388+
389+
test(
390+
'returns a truncated string that has the length based on the length'
391+
'provided with a custom ending string but the substitution length will be included',
392+
() {
393+
final sentence = 'The quick brown fox jumps over the lazy dog';
394+
expect(
395+
sentence.truncate(
396+
12,
397+
substitution: '...',
398+
includeSubstitutionInLength: true,
399+
),
400+
'The quick...',
401+
);
402+
});
403+
404+
test(
405+
'returns a truncated string with emojis that has the length'
406+
'based on the length provided', () {
407+
final sentence = 'The quick brown 🦊🦊🦊 jumps over the lazy 🐶🐶🐶';
408+
409+
expect(
410+
sentence.truncate(42),
411+
'The quick brown 🦊🦊🦊 jumps over the lazy 🐶🐶',
412+
);
413+
414+
expect(
415+
sentence.truncate(42, substitution: '🐾🐾🐾'),
416+
'The quick brown 🦊🦊🦊 jumps over the lazy 🐶🐶🐾🐾🐾',
417+
);
418+
419+
expect(
420+
sentence.truncate(
421+
18,
422+
substitution: '...',
423+
includeSubstitutionInLength: true,
424+
),
425+
'The quick brown...',
426+
);
427+
428+
expect(
429+
sentence.truncate(
430+
18,
431+
substitution: '😀',
432+
includeSubstitutionInLength: true,
433+
),
434+
'The quick brown 🦊😀',
435+
);
436+
});
437+
});
438+
356439
group('capitalize', () {
357440
test('returns a new string with the first character in upper case', () {
358441
expect('foo'.capitalize(), 'Foo');

0 commit comments

Comments
 (0)