Coffeescript - how do I check string equality when passing string through a splat? -
i'm having trouble checking whether 2 strings equal when 1 of them passed through splat argument. because coffeescript uses strict comparisons, , because makes copy of arguments when go through splat, can't strings compare without resorting backticks. there better way? here's minimal piece of code demonstrates problem:
check=(arg) -> if arg == 'foo' "'#{arg}'=='foo'" else "'#{arg}'!='foo'" emit=(args...) -> check(args) console.log(emit('foo')) console.log(check('foo')) the output follows:
> coffee mincase.coffee 'foo'!='foo' 'foo'=='foo' edit: mu short gave me key, revised working code looks (everything same except emit)
emit=(args...)-> check.apply(null,args)
when use splat, splat puts splatted arguments array. example:
f = (x...) -> console.log(x instanceof array) f(6) will give true in console. fine manual isn't fine in case, doesn't spell out, assumes understand how javascript's arguments object works , leaves out explicit splat puts arguments array part.
so end passing array check , array compared string using coffeescript's == (or javascript's ===) never true.
if want emit check first argument, need so:
emit = (args...) -> check(args[0])
Comments
Post a Comment