From 0d2d660e7a54a688427761ea83bfb3694ae2466b Mon Sep 17 00:00:00 2001 From: Marcin Wisnicki Date: Sun, 5 Jan 2014 17:55:03 +0100 Subject: [PATCH] Allow standalone usage and fix tests. --- underscore.string/underscore.string-tests.ts | 3 +- underscore.string/underscore.string.d.ts | 88 ++++++++++++++------ 2 files changed, 64 insertions(+), 27 deletions(-) diff --git a/underscore.string/underscore.string-tests.ts b/underscore.string/underscore.string-tests.ts index 58ff509c0..651cf51de 100644 --- a/underscore.string/underscore.string-tests.ts +++ b/underscore.string/underscore.string-tests.ts @@ -1,7 +1,8 @@ /// /// -_.mixin(_['string'].exports()); +_.mixin(_.string.exports()); +interface UnderscoreStatic extends UnderscoreStringStaticExports { } _.numberFormat(1000, 2); _.numberFormat(123456789.123, 5, '.', ','); diff --git a/underscore.string/underscore.string.d.ts b/underscore.string/underscore.string.d.ts index bd5774277..d016dc908 100644 --- a/underscore.string/underscore.string.d.ts +++ b/underscore.string/underscore.string.d.ts @@ -4,6 +4,45 @@ // Definitions: https://github.com/borisyankov/DefinitelyTyped interface UnderscoreStatic { + str: UnderscoreStringStatic; + string: UnderscoreStringStatic; +} + +interface UnderscoreStringStatic extends UnderscoreStringStaticExports { + /** + * Tests if string contains a substring. + * ('foobar', 'ob') => true + * @param str + * @param needle + */ + include(str: string, needle: string): boolean; + + /** + * Tests if string contains a substring. + * ('foobar', 'ob') => true + * @param str + * @param needle + */ + contains(str: string, needle: string): boolean; + + /** + * Return reversed string. + * ('foobar') => 'raboof' + * @param str + */ + reverse(str: string): string; +} + +/** + * Functions exported for mixing with underscore object. + * + * Usage: + * _.mixin(_.string.exports()); + * interface UnderscoreStatic extends UnderscoreStringStaticExports { } + */ +interface UnderscoreStringStaticExports { + + exports(): UnderscoreStringStaticExports; /** * Determine if a string is 'blank.' @@ -98,22 +137,6 @@ interface UnderscoreStatic { */ insert(str: string, i: number, substr: string): string; - /** - * Tests if string contains a substring. - * ('foobar', 'ob') => true - * @param str - * @param needle - */ - include(str: string, needle: string): boolean; - - /** - * Tests if string contains a substring. - * ('foobar', 'ob') => true - * @param str - * @param needle - */ - contains(str: string, needle: string): boolean; - /** * Joins strings together with given separator. * (' ', 'foo', 'bar') => 'foo bar' @@ -129,13 +152,6 @@ interface UnderscoreStatic { */ lines(str: string): any[]; - /** - * Return reversed string. - * ('foobar') => 'raboof' - * @param str - */ - reverse(str: string): string; - /** * Checks if string starts with another string. * ('image.gif', 'image') => true @@ -279,7 +295,27 @@ interface UnderscoreStatic { * @param str * @param delimiter */ - words(str: string, delimiter?: string): any[]; + words(str: string): string[]; + + /** + * Split string by delimiter (String or RegExp). + * /\s+/ by default. + * (' I love you ') => ['I','love','you'] + * ('I_love_you', '_') => ['I','love','you'] + * @param str + * @param delimiter + */ + words(str: string, delimiter: string): string[]; + + /** + * Split string by delimiter (String or RegExp). + * /\s+/ by default. + * (' I love you ') => ['I','love','you'] + * ('I_love_you', '_') => ['I','love','you'] + * @param str + * @param delimiter + */ + words(str: string, delimiter: RegExp): string[]; /** * Pads a string with characters until the total string length is equal to the passed length parameter. @@ -295,7 +331,7 @@ interface UnderscoreStatic { * @param padStr * @param type */ - pad(str: string, length: number, padStr:string, type?: string): string; + pad(str: string, length: number, padStr?:string, type?: string): string; /** * Left-pad a string. @@ -527,4 +563,4 @@ interface UnderscoreStatic { } -// TODO interface Underscore +// TODO interface UnderscoreString extends Underscore