-
Notifications
You must be signed in to change notification settings - Fork 136
Expand file tree
/
Copy pathword_wrap.js
More file actions
130 lines (124 loc) · 3.37 KB
/
word_wrap.js
File metadata and controls
130 lines (124 loc) · 3.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import v from '../voca';
describe('wordWrap', function() {
it('should wrap the string with default parameters', function() {
expect(v.wordWrap('')).toBe('');
expect(v.wordWrap('Yes. The fire rises. ')).toBe('Yes. The fire rises. ');
expect(
v.wordWrap(
"Theatricality and deception are powerful agents to the uninitiated... but we are initiated, aren't " +
'we Bruce? Members of the League of Shadows!'
)
).toBe(
'Theatricality and deception are powerful agents to the uninitiated... but' +
'\n' +
"we are initiated, aren't we Bruce? Members of the League of Shadows!"
);
expect(
v.wordWrap('Theatricality-and-deception-are-powerful-agents-to-the-uninitiated...-but-we-are-initiated')
).toBe('Theatricality-and-deception-are-powerful-agents-to-the-uninitiated...-but-we-are-initiated');
});
it('should wrap the string for a specific width', function() {
expect(
v.wordWrap('Hello', {
width: 4,
})
).toBe('Hello');
expect(
v.wordWrap(' Hello ', {
width: 4,
})
).toBe('Hello\n ');
expect(
v.wordWrap('Hello World', {
width: 4,
})
).toBe('Hello\nWorld');
expect(
v.wordWrap('Yes. The fire rises.', {
width: 4,
})
).toBe('Yes.\nThe\nfire\nrises.');
expect(
v.wordWrap('And I think to myself what a wonderful world.', {
width: 10,
})
).toBe('And I\nthink to\nmyself\nwhat a\nwonderful\nworld.');
expect(
v.wordWrap('Hello World', {
width: 0,
})
).toBe('');
expect(
v.wordWrap('Hello World', {
width: -5,
})
).toBe('');
});
it('should wrap the string with indent', function() {
expect(
v.wordWrap('Hello', {
width: 4,
indent: '***',
})
).toBe('***Hello');
expect(
v.wordWrap('Hello World', {
width: 4,
indent: ' ',
})
).toBe(' Hello\n World');
expect(
v.wordWrap('Yes. The fire rises.', {
width: 4,
indent: '**',
})
).toBe('**Yes.\n**The\n**fire\n**rises.');
expect(
v.wordWrap('', {
width: 5,
indent: '000',
})
).toBe('000');
expect(
v.wordWrap('Wonderful world', {
width: 0,
indent: '000',
})
).toBe('000');
});
it('should wrap the string with a custom newline character', function() {
expect(
v.wordWrap('What A Wonderful World', {
width: 10,
indent: ' ',
newLine: '+',
})
).toBe(' What A+ Wonderful+ World');
expect(
v.wordWrap('I hear babies crying, I watch them grow', {
width: 5,
indent: '-',
newLine: '<br/>',
})
).toBe('-I<br/>-hear<br/>-babies<br/>-crying,<br/>-I<br/>-watch<br/>-them<br/>-grow');
});
it('should wrap the string with breaking long words', function() {
expect(
v.wordWrap('Hello', {
width: 4,
cut: true,
})
).toBe('Hell\no');
expect(
v.wordWrap('I hear babies crying, I watch them grow', {
width: 5,
cut: true,
})
).toBe('I\nhear\nbabie\ns\ncryin\ng, I\nwatch\nthem\ngrow');
});
it('should return empty string for null or undefined', function() {
expect(v.wordWrap()).toBe('');
expect(v.wordWrap(undefined)).toBe('');
expect(v.wordWrap(null)).toBe('');
});
});