Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including field-filter with all npm packages installed. Try it out:

var fieldFilter = require("field-filter")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

field-filter v0.0.3

api query parameter result object filter

Field filter

npm install field-filter

Implements the partial response query filter method as seen here

Examples

var filter = require('field-filter');

var input = {
  a: 'a',
  b: 'b',
  c: 'c'
};

var filterString = 'a,b';

filter(filterString, input);
{
  a: 'a',
  b: 'b'
}

var input = {
  a: {
    b: {
      c: {
        d: {
          e: 'e',
          f: 'f'
        },
        g: 'g'
      },
      f: 'f'
    },
    g: {
      h: 'h'
    }
  }
};

// () and / to sub-select
var filterString = 'a( b/c( d/e ) , g)';

filter(filterString, input)
{
  a: {
    b: {
      c: {
        d: {
          e: 'e'
        }
      }
    },
    g: {
      h: 'h'
    }
  }
}

// wildcard *
var input = {
  a: {
    b: {
      c: 'c',
      d: 'd'
    },
    e: {
      c: 'c',
      f: 'f'
    }
  },
  g: {
    h: {
      i: 'i',
      j: 'j'
    },
    k: {
      i: 'i',
      j: 'j'
    }
  },
  m: 'm'
};

var filterString = 'a/*/c,g(*(i, j)),*';

filter(filterString, input)
{
  a: {
    b: {
      c: 'c'
    },
    e: {
      c: 'c'
    }
  },
  g: {
    h: {
      i: 'i',
      j: 'j'
    },
    k: {
      i: 'i',
      j: 'j'
    }
  },
  m: 'm'
});


// array index
var input = [
  {
    a: 'a',
    b: [
      'c',
      'd'
    ]
  },
  {
    e: 'e'
  }
];

var filterString = '0(a,b/1)';

filter(filterString, input)
[
  {
    a: 'a',
    b: [
      undefined,
      'd'
    ]
  }
]);
RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free