From cd8b2e297d599c6732a41c1063daafc138a06313 Mon Sep 17 00:00:00 2001 From: rolux Date: Mon, 4 Jun 2012 11:22:40 +0200 Subject: [PATCH] remove 'Ox.starts' and 'Ox.ends' aliases, update documentation; fix Ox.pad with negative length --- source/Ox/js/String.js | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/source/Ox/js/String.js b/source/Ox/js/String.js index 9b2f5ef9..2650a768 100644 --- a/source/Ox/js/String.js +++ b/source/Ox/js/String.js @@ -36,14 +36,15 @@ Ox.clean = function(string) { }; /*@ -Ox.endsWith Checks if a string ends with a given substring - If the substring is a string literal (and not a variable), - `/sub$/.test(str)` or `!!/sub$/.exec(str)` is shorter than `Ox.ends(str, - sub)`. +Ox.endsWith Tests if a string ends with a given substring + Equivalent to `new RegExp(Ox.escapeRegExp(substring) + '$').test(string)`. + (string, substring) -> True if string ends with substring > Ox.endsWith('foobar', 'bar') true + > Ox.endsWith('foobar', 'foo') + false @*/ -Ox.ends = Ox.endsWith = function(string, substring) { +Ox.endsWith = function(string, substring) { string = string.toString(); substring = substring.toString(); return string.slice(string.length - substring.length) == substring; @@ -100,14 +101,15 @@ Ox.pad Pad a string to a given length 'foo' > Ox.pad('foobar', 'left', 3) 'bar' + > Ox.pad('foo', -1) + '' @*/ Ox.pad = function(string, position, length, padding) { - // fixme: weird for negative numbers var hasPosition = Ox.isString(arguments[1]), isNumber = Ox.isNumber(arguments[0]), last = Ox.last(arguments); position = hasPosition ? arguments[1] : isNumber ? 'left' : 'right'; - length = hasPosition ? arguments[2] : arguments[1]; + length = Math.max(hasPosition ? arguments[2] : arguments[1], 0); padding = Ox.isString(last) ? last : isNumber && position == 'left' ? '0' : ' '; string = string.toString(); @@ -410,15 +412,15 @@ Ox.splice = function(string, index, remove) { }; /*@ -Ox.startsWith Checks if a string starts with a given substring - If the substring is a string literal (and not a variable), - `/^sub/.test(str)` or `!!/^sub/.exec(str)` is shorter than `Ox.starts(str, - sub)`. +Ox.startsWith Tests if a string ends with a given substring + Equivalent to `new RegExp('^' + Ox.escapeRegExp(substring)).test(string)`. + (string, substring) -> True if string starts with substring > Ox.startsWith('foobar', 'foo') true + > Ox.startsWith('foobar', 'bar') + false @*/ -Ox.starts = Ox.startsWith = function(string, substring) { - // fixme: rename to starts +Ox.startsWith = function(string, substring) { string = string.toString(); substring = substring.toString(); return string.slice(0, substring.length) == substring;