Skip to content
This repository has been archived by the owner on Jun 3, 2023. It is now read-only.

Does the protocol support object keys at all? #54

Open
customcommander opened this issue Feb 22, 2021 · 0 comments
Open

Does the protocol support object keys at all? #54

customcommander opened this issue Feb 22, 2021 · 0 comments

Comments

@customcommander
Copy link

customcommander commented Feb 22, 2021

var t = require('transducers-js');
t.into({}, t.map(x => x + 1), {foo: 1});

Expected: {foo:2} — Actual: {f: 'o'}

The "issue" is here:

transducers.objectReduce = function(xf, init, obj) {
    var acc = init;
    for(var p in obj) {
        if(obj.hasOwnProperty(p)) {
            acc = xf["@@transducer/step"](acc, [p, obj[p]]);
//                                             ^^^^^^^^^^^
//                                             The mapping function `x => x + 1` is applied to `['foo', 1]`
//                                             which returns `'foo,11'`
            if(transducers.isReduced(acc)) {
                acc = transducers.deref(acc);
                break;
            }
        }
    }
    return xf["@@transducer/result"](acc);
};

Then:

transducers.addEntry = function(obj, entry) {
  // entry: 'foo,11'
  // entry[0] = 'f'
  // entry[1] = 'o'
  obj[entry[0]] = entry[1];
  return obj;
};

This could be fixed by having the @@transducer/step function take three arguments:

  1. An accumulation
  2. A value
  3. A key (optional)

However I can't seem to find a lot of support for keys in the implementation. Just wondering whether this is by design or simply an oversight? Or just me not understanding something (most likely!)?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant