1.0.3 • Published 8 years ago

smart-picker v1.0.3

Weekly downloads
2
License
MIT
Repository
github
Last release
8 years ago

smart-picker

Neat way to get a copy of an object with only the specified keys.

build status npm version

Install

To install the stable version:

npm install --save smart-picker

This assumes you are using npm as your package manager.

The smart-picker source code is written in ES5 so it works in any modern browser. You don't need to use Babel or a module bundler to get started with smart-picker.

Usage

Simple pick
import { createPicker } from 'smart-picker';

const picker = createPicker(`{
  a,
  c: c_alias
}`);

const result = picker.pick({
  a: 'a',
  b: 'b',
  c: 'c'
});

expect(result).toEqual({
  a: 'a',
  c_alias: 'c'
});
Pick from array
import { createPicker } from 'smart-picker';

const picker = createPicker(`{
  1: b
}`);

const result = picker.pick([
  1,
  2,
  3
]);

expect(result).toEqual({
  b: 2
});
Deep pick
import { createPicker } from 'smart-picker';

const picker = createPicker(`{
  a: {
    b: b_alias: {
      c
    },
    f: {
      g: g_alias
    }
  }
}`);

const result = picker.pick({
  a: {
    b: {
      c: 'a'
    },
    d: 'd',
    e: 'e',
    f: {
      g: 'g'
    }
  }
});

expect(result).toEqual({
  a: {
    b_alias: {
      c: 'a'
    },
    f: {
      g_alias: 'g'
    }
  }
});
pickUp
import { createPicker } from 'smart-picker';

const picker = createPicker(`{
  a: {
    b: b_alias: {
      c
    },
    f: {
      g: g_alias
    }
  }
}`);

const result = picker.pickUp({
  a: {
    b: {
      c: 'a'
    },
    d: 'd',
    e: 'e',
    f: {
      g: 'g'
    }
  }
});

expect(result).toEqual({
  c: 'a',
  g_alias: 'g'
});

Contributing

Pull requests and stars are welcome. For bugs and feature requests, please create an issue.

Author

Stanislav Solodii

1.0.3

8 years ago

1.0.2

8 years ago

1.0.0

8 years ago