diff --git a/modules/angular2/src/core/facade/lang.dart b/modules/angular2/src/core/facade/lang.dart index 140a0fd12f..fa7d3096ac 100644 --- a/modules/angular2/src/core/facade/lang.dart +++ b/modules/angular2/src/core/facade/lang.dart @@ -94,6 +94,14 @@ class StringWrapper { return s.startsWith(start); } + static String slice(String s, [int start = 0, int end]) { + //in JS if start > end an empty string is returned + if(end != null && start > end) { + return ""; + } + return s.substring(_startOffset(s, start), _endOffset(s, end)); + } + static String substring(String s, int start, [int end]) { return s.substring(start, end); } @@ -107,6 +115,21 @@ class StringWrapper { } static int compare(String a, String b) => a.compareTo(b); + + // JS slice function can take start < 0 which indicates a position relative to + // the end of the string + static int _startOffset(String s, int start) { + int len = s.length; + return start = start < 0 ? math.max(len + start, 0) : math.min(start, len); + } + + // JS slice function can take end < 0 which indicates a position relative to + // the end of the string + static int _endOffset(String s, int end) { + int len = s.length; + if (end == null) return len; + return end < 0 ? math.max(len + end, 0) : math.min(end, len); + } } class StringJoiner { diff --git a/modules/angular2/src/core/facade/lang.ts b/modules/angular2/src/core/facade/lang.ts index e38fd57158..dc84b854df 100644 --- a/modules/angular2/src/core/facade/lang.ts +++ b/modules/angular2/src/core/facade/lang.ts @@ -151,6 +151,10 @@ export class StringWrapper { return s.replace(from, replace); } + static slice(s: string, from: number = 0, to: number = null): string { + return s.slice(from, to === null ? undefined : to); + } + static toUpperCase(s: string): string { return s.toUpperCase(); } static toLowerCase(s: string): string { return s.toLowerCase(); } diff --git a/modules/angular2/test/core/facade/lang_spec.ts b/modules/angular2/test/core/facade/lang_spec.ts index d8e4931854..6799ccd6a9 100644 --- a/modules/angular2/test/core/facade/lang_spec.ts +++ b/modules/angular2/test/core/facade/lang_spec.ts @@ -42,7 +42,7 @@ export function main() { }); describe('String', () => { - var upper, lower; + var upper, lower, s; beforeEach(() => { upper = 'SOMETHING'; @@ -60,5 +60,25 @@ export function main() { expect(str).toEqual(lower); }); + + describe('slice', () => { + beforeEach(() => { s = "abcdefghij"; }); + + it('should return the whole string if neither start nor end are specified', + () => { expect(StringWrapper.slice(s)).toEqual("abcdefghij"); }); + + it('should return up to the end if end is not specified', + () => { expect(StringWrapper.slice(s, 1)).toEqual("bcdefghij"); }); + + it('should support negative start', + () => { expect(StringWrapper.slice(s, -1)).toEqual("j"); }); + + it('should support negative end', + () => { expect(StringWrapper.slice(s, -3, -1)).toEqual("hi"); }); + + it('should return empty string if start is greater than end', + () => { expect(StringWrapper.slice(s, 4, 2)).toEqual(""); }); + }); + }); }