Skip to content

Commit 0808eea

Browse files
itslennyvsavkin
authored andcommitted
feat(StringWrapper): add support for JS slice method to string
1 parent bced3aa commit 0808eea

File tree

3 files changed

+48
-1
lines changed

3 files changed

+48
-1
lines changed

modules/angular2/src/core/facade/lang.dart

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,14 @@ class StringWrapper {
9494
return s.startsWith(start);
9595
}
9696

97+
static String slice(String s, [int start = 0, int end]) {
98+
//in JS if start > end an empty string is returned
99+
if(end != null && start > end) {
100+
return "";
101+
}
102+
return s.substring(_startOffset(s, start), _endOffset(s, end));
103+
}
104+
97105
static String substring(String s, int start, [int end]) {
98106
return s.substring(start, end);
99107
}
@@ -107,6 +115,21 @@ class StringWrapper {
107115
}
108116

109117
static int compare(String a, String b) => a.compareTo(b);
118+
119+
// JS slice function can take start < 0 which indicates a position relative to
120+
// the end of the string
121+
static int _startOffset(String s, int start) {
122+
int len = s.length;
123+
return start = start < 0 ? math.max(len + start, 0) : math.min(start, len);
124+
}
125+
126+
// JS slice function can take end < 0 which indicates a position relative to
127+
// the end of the string
128+
static int _endOffset(String s, int end) {
129+
int len = s.length;
130+
if (end == null) return len;
131+
return end < 0 ? math.max(len + end, 0) : math.min(end, len);
132+
}
110133
}
111134

112135
class StringJoiner {

modules/angular2/src/core/facade/lang.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,10 @@ export class StringWrapper {
151151
return s.replace(from, replace);
152152
}
153153

154+
static slice<T>(s: string, from: number = 0, to: number = null): string {
155+
return s.slice(from, to === null ? undefined : to);
156+
}
157+
154158
static toUpperCase(s: string): string { return s.toUpperCase(); }
155159

156160
static toLowerCase(s: string): string { return s.toLowerCase(); }

modules/angular2/test/core/facade/lang_spec.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ export function main() {
4242
});
4343

4444
describe('String', () => {
45-
var upper, lower;
45+
var upper, lower, s;
4646

4747
beforeEach(() => {
4848
upper = 'SOMETHING';
@@ -60,5 +60,25 @@ export function main() {
6060

6161
expect(str).toEqual(lower);
6262
});
63+
64+
describe('slice', () => {
65+
beforeEach(() => { s = "abcdefghij"; });
66+
67+
it('should return the whole string if neither start nor end are specified',
68+
() => { expect(StringWrapper.slice(s)).toEqual("abcdefghij"); });
69+
70+
it('should return up to the end if end is not specified',
71+
() => { expect(StringWrapper.slice(s, 1)).toEqual("bcdefghij"); });
72+
73+
it('should support negative start',
74+
() => { expect(StringWrapper.slice(s, -1)).toEqual("j"); });
75+
76+
it('should support negative end',
77+
() => { expect(StringWrapper.slice(s, -3, -1)).toEqual("hi"); });
78+
79+
it('should return empty string if start is greater than end',
80+
() => { expect(StringWrapper.slice(s, 4, 2)).toEqual(""); });
81+
});
82+
6383
});
6484
}

0 commit comments

Comments
 (0)