mstade / funkis

Functional JavaScript
MIT License
6 stars 0 forks source link

Change the return value of iterating over object properties with `seq` #6

Closed mstade closed 10 years ago

mstade commented 10 years ago

Currently, seq will return objects when iterating over enumerable properties, where the only property on the returned object is the value of that iteration. For example:

props = seq({ foo: 'wibble', bar: 'bob' })

console.log(props.first) // { foo: 'wibble' }
console.log(props.rest.first) // { bar: 'bob' }

(NB: order isn't guaranteed, but this seems to be the result in node anyway.)

Now, this is all well and good, but there's still no easy way of getting the one and only property out of these objects without knowing the names in advance. As a solution, I'm thinking of adding two non-enumerable properties to these objects: name and value (or just 0 and 1 to make it array-like?)

That way, you could use props.first.name or props.first.value to get the name/value respectively without having to know anything upfront:

props = seq({ foo: 'wibble', bar: 'bob' })

console.log(props.first.name, props.first.value) // foo wibble
console.log(props.rest.first.name, props.rest.first.value) // bar bob

Since these would be non-enumerable properties, the key/value pair objects returned by the seq should still be usable in object merging scenarios since those usually only work on own properties and not prototypes or non-enumerable properties.

mstade commented 10 years ago

Just change it so each key/value pair is a vector of length two, where the first item is the key and the second is the value. This is how Clojure does it:

(seq {:name "King Hank" :job "Ranger's goalie"})
; ([:name "King Hank"] [:job "Ranger's goalie"])