1.2.0 • Published 4 years ago

project-object v1.2.0

Weekly downloads
1
License
MIT
Repository
github
Last release
4 years ago

project-object

A simple, light-weight MongoDB $project-like function allowing you to filter your objects properties with ease.

Installation

yarn add project-object

Basic usage

const project = require('project-object')

const thread = {
  title: 'Some title',
  ip: '0.0.0.0',
  messages: [{
    content: 'foo',
    ip: '0.0.0.0'
  }, {
    content: 'bar',
    ip: '0.0.0.0'
  }]
}
project(thread, {
  title: 1,
  messages: {
    content: 1
  }
})
/*
=>
{
  title: 'Some title',
  messages: [{
    content: 'foo'
  }, {
    content: 'bar'
  }]
}
*/

Examples

With an array instead of an object

const users = [{
  username: 'foo',
  password: 'some hash'
}, {
  username: 'bar',
  password: 'some other hash'
}]

project(users, { username: 1 })
/*
=>
[{
  username: 'foo'
}, {
  username: 'bar'
}]
*/

Using multiple specs

const userProjectionSpec = {
  username: 1
}

function projectUser (userObj, overrideProjectionSpec = {}) {
  return project(userObj, userProjectionSpec, overrideProjectionSpec)
}

const user = {
  username: 'foo',
  email: 'foo@bar.fr',
  password: 'some hash'
}

projectUser(user)
/*
=>
{
  username: 'foo',
  email: 'foo@bar.fr'
}
*/

projectUser(user, { email: 0, password: 1 })
/*
=>
{
  username: 'foo',
  password: 'some hash'
}
*/
1.2.0

4 years ago

1.1.1

4 years ago

1.1.0

4 years ago

1.0.0

5 years ago