j-d-carmichael / object-reduce-by-map

Reduce an input object to only the key in the map and ensure the value of the keys match the map given.
MIT License
0 stars 1 forks source link

Suggestion: gracefully handle undefined #4

Closed p-mcgowan closed 3 years ago

p-mcgowan commented 4 years ago

suppose, hypothetically, a swagger code generator library turns:

paths:
  '/hello-wrld':
    get:
      tags:
        - nope
      summary: nope
      operationId: nope
      description: nope
      responses:
        '200':
          description: success
        '404':
          description: Not found

into:

const objectReduceByMap = require('object-reduce-by-map');
const { mockItGenerator } = require('generate-it-mockers');

const defaultUndefinedResponse = mockItGenerator({}); // {}
const transformSchema = { pathGet: {} };

// cool
const res = objectReduceByMap(defaultUndefinedResponse, transformSchema.pathGet);

const actualReturnValue = undefined;
// explosion
const res = objectReduceByMap(actualReturnValue, transformSchema.pathGet);

console.log(res);

if the input to the map is undefined, then mapping it to an object should just return undefined

currently, reducer.js#L152 tries to json parse, resulting in an error