[英]If a string after the last slash of a URL exists, capture the value; if not, capture the value before the last slash (javascript)
[英]Better way to implement (in javascript) after,after_Last, before and before_Last string prototype methods
我剛剛實現了這些JavaScript原型方法(在coffeescript中),但是我感覺有更好的方法來實現它們
String::after = (value )-> if (@.indexOf( value) == -1 or @.indexOf( value) + value.size() > @.size()) then '' else @.substring(@.indexOf( value) + value.size())
String::after_Last = (value )-> if (@.lastIndexOf(value) == -1 or @.lastIndexOf(value) + value.size() > @.size()) then '' else @.substring(@.lastIndexOf(value) + value.size())
String::before = (value )-> @.substring(0,@.indexOf(value))
String::before_Last = (value )-> @.substring(0,@.lastIndexOf(value))
這是我目前對這些方法的測試:
it 'after',->
value.after.assert_Is_Function()
value.after('3' ).assert_Is('' )
value.after('2' ).assert_Is('3' )
value.after('123').assert_Is('' )
value.after('b' ).assert_Is('c123' )
value.after('bc1').assert_Is('23' )
value.after('xxx').assert_Is('' )
value.after(null ).assert_Is('' )
'abb'.after('b' ).assert_Is('b' )
'a.b'.after('.' ).assert_Is('b' )
'a.b.c'.after('.').assert_Is('b.c' )
it 'after_Last',->
value.after_Last.assert_Is_Function()
value.after_Last('3' ).assert_Is('' )
value.after_Last('2' ).assert_Is('3' )
value.after_Last('123').assert_Is('' )
value.after_Last('b' ).assert_Is('c123' )
value.after_Last('bc1').assert_Is('23' )
value.after_Last('xxx').assert_Is('' )
value.after_Last(null ).assert_Is('' )
'abb'.after_Last('b' ).assert_Is('' )
'a.b'.after_Last('.' ).assert_Is('b' )
'a.b.c'.after_Last('.').assert_Is('c' )
it 'before',->
value.before.assert_Is_Function()
value.before('3' ).assert_Is('abc12')
value.before('2' ).assert_Is('abc1' )
value.before('123').assert_Is('abc' )
value.before('b' ).assert_Is('a' )
value.before('bc1').assert_Is('a' )
value.before('xxx').assert_Is('' )
value.before(null ).assert_Is('' )
'abb'.before('b' ).assert_Is('a' )
'a.b'.before('.' ).assert_Is('a' )
'a.b.c'.before('.').assert_Is('a' )
it 'before_Last',->
value.before_Last.assert_Is_Function()
value.before_Last('3' ).assert_Is('abc12')
value.before_Last('2' ).assert_Is('abc1' )
value.before_Last('123').assert_Is('abc' )
value.before_Last('b' ).assert_Is('a' )
value.before_Last('bc1').assert_Is('a' )
value.before_Last('xxx').assert_Is('' )
value.before_Last(null ).assert_Is('' )
'abb'.before_Last('b' ).assert_Is('ab' )
'a.b'.before_Last('.' ).assert_Is('a' )
'a.b.c'.before_Last('.').assert_Is('a.b' )
我特別不喜歡after和after_Last方法中的if語句
我也想知道我是否缺少針對before和before_Last方法的幾個測試用例(因為它們是非常簡單的實現:))
這些方法是https://github.com/o2platform/fluentnode的一部分
更新1:這里使用的是Patrick JS的版本
String::after = (value )-> if ((index = @.indexOf( value)) == -1 ) then '' else @.substr(index + value.size())
String::after_Last = (value )-> if ((index = @.lastIndexOf(value)) == -1 ) then '' else @.substr(index + value.size())
我還針對''案例添加了這些額外的測試:
value.after_Last ('' ).assert_Is('' )
value.after('' ).assert_Is('abc123')
value.before('' ).assert_Is('' )
value.before_Last('' ).assert_Is('abc123')
您總是需要某種區分大小寫的方法,因為indexOf
的-1
返回值相當不可用。 但是,通過一些魔術,您可以繞開if語句:
String::after = (value) -> @slice ~( ~@indexOf(value) || ~@length) + value.length
String::afterLast = (value) -> @slice ~(~@lastIndexOf(value) || ~@length) + value.length
String::before = (value) -> @slice 0, ~( ~@indexOf(value) || ~0)
String::beforeLast = (value) -> @slice 0, ~(~@lastIndexOf(value) || ~0)
在這里,它要短一些,無法擺脫if
但是第二個條件無論如何都不會發生(我假設您信任節點的indexOf
和lastIndexOf
),因此您可以排除它,還引入了一個變量。
String::after = (value )-> if ((indx = @indexOf( value)) == -1 ) then '' else @substr(indx + value.size())
String::after_Last = (value )-> if ((indx = @lastIndexOf(value)) == -1 ) then '' else @substr(indx + value.size())
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.