如何将这个 GorillaScript 翻译成 LiveScript?
How to translate this GorillaScript into LiveScript?
我有这个 GorillaScript 代码来展平数组:
Array::compact := #
for filter value in this
value and (typeof value.isempty != 'function' or not value.isempty()) and (typeof value != 'object' or Object.keys(value).length != 0)
GorillaScript 有点死了。有人可以帮我把它翻译成 LiveScript 吗?我对 LiveScript 很陌生。
直译应该是这样的。
Array.prototype.compact = ->
[v for v in @ when v and (typeof v.isempty isnt \function or not v.isempty!) and (typeof v isnt \object or Object.keys value .length > 0)]
一个更惯用的例子可能是:
is-empty = ->
| not it => false
| typeof it.isempty isnt \function or not it.isempty! => true
| typeof it isnt \object or not Object.keys it .length > 0 => true
| otherwise => it
Array.prototype.compact = -> [x for x in @ when not is-empty x]
当心,因为这是我突然想到的,我的 LiveScript 有点生疏,但这里的一般想法是好的。
我有这个 GorillaScript 代码来展平数组:
Array::compact := #
for filter value in this
value and (typeof value.isempty != 'function' or not value.isempty()) and (typeof value != 'object' or Object.keys(value).length != 0)
GorillaScript 有点死了。有人可以帮我把它翻译成 LiveScript 吗?我对 LiveScript 很陌生。
直译应该是这样的。
Array.prototype.compact = ->
[v for v in @ when v and (typeof v.isempty isnt \function or not v.isempty!) and (typeof v isnt \object or Object.keys value .length > 0)]
一个更惯用的例子可能是:
is-empty = ->
| not it => false
| typeof it.isempty isnt \function or not it.isempty! => true
| typeof it isnt \object or not Object.keys it .length > 0 => true
| otherwise => it
Array.prototype.compact = -> [x for x in @ when not is-empty x]
当心,因为这是我突然想到的,我的 LiveScript 有点生疏,但这里的一般想法是好的。