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

Does the protocol support object keys at all? #54

Open
@customcommander

Description

@customcommander
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!)?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions