0

I have this GorillaScript code to flatten arrays:

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 is kinda dead. Could someone translate this into LiveScript for me please? I'm quite new to LiveScript.

Sod Almighty
  • 1,768
  • 1
  • 16
  • 29

1 Answers1

1

A literal translation would look something like this.

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)]

A more idiomatic example might be:

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]

Watch out, as this was done off the top of my head any my LiveScript's a bit rusty, but the general ideas here are ok.

Ven
  • 19,015
  • 2
  • 41
  • 61
Dan Prince
  • 29,491
  • 13
  • 89
  • 120