src/modules/reduction.js
function step(collection) {
  switch (true) {
    case isImplemented(collection, 'step'):
      return collection[p.step];

    case isString(collection):
      return (acc, input) => {
        const value = isKvFormObject(input) ? input.v : input;
        return acc + value;
      };

    case isArray(collection):
      return (acc, input) => {
        const value = isKvFormObject(input) ? { [input.k]: input.v } : input;
        acc.push(value);
        return acc;
      };

    case isObject(collection):
      return (acc, input) => {
        let value = input;

        if (isKvFormObject(input)) {
          // if the object is kv-form, change the object from { k: key, v: value } to { key: value }
          value = { [input.k]: input.v };
        } else if (!isObject(input)) {
          // if the input isn't an object at all, turn it into an object with a key based on what's already in the
          // accumulator
          let max = -1;
          for (const k1 in acc) {
            const knum = parseInt(k1);
            if (knum > max) {
              max = knum;
            }
          }
          value = { [max + 1]: input };
        }

        for (const k2 in value) {
          if (value.hasOwnProperty(k2)) {
            acc[k2] = value[k2];
          }
        }
        return acc;
      };

    case isFunction(collection):
      return (acc, input) => collection(acc, input);

    default:
      return null;
  }
}
function step(collection) {
  switch (true) {
    case isImplemented(collection, 'step'):
      return collection[p.step];

    case isString(collection):
      return (acc, input) => {
        const value = isKvFormObject(input) ? input.v : input;
        return acc + value;
      };

    case isArray(collection):
      return (acc, input) => {
        const value = isKvFormObject(input) ? { [input.k]: input.v } : input;
        acc.push(value);
        return acc;
      };

    case isObject(collection):
      return (acc, input) => {
        let value = input;

        if (isKvFormObject(input)) {
          // if the object is kv-form, change the object from { k: key, v: value } to { key: value }
          value = { [input.k]: input.v };
        } else if (!isObject(input)) {
          // if the input isn't an object at all, turn it into an object with a key based on what's already in the
          // accumulator
          let max = -1;
          for (const k1 in acc) {
            const knum = parseInt(k1);
            if (knum > max) {
              max = knum;
            }
          }
          value = { [max + 1]: input };
        }

        for (const k2 in value) {
          if (value.hasOwnProperty(k2)) {
            acc[k2] = value[k2];
          }
        }
        return acc;
      };

    case isFunction(collection):
      return (acc, input) => collection(acc, input);

    default:
      return null;
  }
}
function step ( collection ) { switch ( true ) { case isImplemented ( collection , 'step' ) : return collection [ p . step ] ; case isString ( collection ) : return ( acc , input ) => { const value = isKvFormObject ( input ) ? input . v : input ; return acc + value ; } ; case isArray ( collection ) : return ( acc , input ) => { const value = isKvFormObject ( input ) ? { [ input . k ] : input . v } : input ; acc . push ( value ) ; return acc ; } ; case isObject ( collection ) : return ( acc , input ) => { let value = input ; if ( isKvFormObject ( input ) ) { // if the object is kv-form, change the object from { k: key, v: value } to { key: value } value = { [ input . k ] : input . v } ; } else if ( ! isObject ( input ) ) { // if the input isn't an object at all, turn it into an object with a key based on what's already in the // accumulator let max = - 1 ; for ( const k1 in acc ) { const knum = parseInt ( k1 ) ; if ( knum > max ) { max = knum ; } } value = { [ max + 1 ] : input } ; } for ( const k2 in value ) { if ( value . hasOwnProperty ( k2 ) ) { acc [ k2 ] = value [ k2 ] ; } } return acc ; } ; case isFunction ( collection ) : return ( acc , input ) => collection ( acc , input ) ; default : return null ; } }
Returns a step function for a collection. This is a function that takes an accumulator and a value and returns the
result of reducing the value into the accumulator. If the collection doesn't support reduction, `null` is returned.
The returned function itself simply reduces the input into the target collection without modifying it.

In order to support the conversion of functions into reducers, function support is also provided.

@private

@param {*} collection A collection to create a step function for. This can be anything that supports the ES2015
iteration protocol, a plain object, a pre-ES2015 string or array, or a function.
@return {module:xduce~step} A reduction function for the provided collection that simply adds an element to the
target collection without modifying it. If the provided collection is not iterable, `null` is returned.
Returns a step function for a collection . This is a function that takes an accumulator and a value and returns the result of reducing the value into the accumulator . If the collection doesn t support reduction null is returned . The returned function itself simply reduces the input into the target collection without modifying it .