Coder Social home page Coder Social logo

node-only's Introduction

only

Return whitelisted properties of an object.

Installation

$ npm install only

API

An array or space-delimited string may be given:

var obj = {
  name: 'tobi',
  last: 'holowaychuk',
  email: '[email protected]',
  _id: '12345'
};

var user = only(obj, 'name last email');

You can also use array form:

var user = only(obj, ['name', 'last', 'email']);

yields:

{
  name: 'tobi',
  last: 'holowaychuk',
  email: '[email protected]'
}

License

(The MIT License)

Copyright (c) 2012 TJ Holowaychuk <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

node-only's People

Contributors

brizer avatar timoxley avatar tj avatar yurysolovyov avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

node-only's Issues

Support for alias

I think alias is necessary. for example : var user = only(obj, ['name as myname', 'last as about', 'email']);

Add the array way to the docs

Right now this is also possible from the source:

var obj = {
  name: 'tobi',
  last: 'holowaychuk',
  email: '[email protected]',
  _id: '12345'
};

var user = only(obj, ['name', 'last']);

However it's not documented. I had to check the source to see if it was document, which fairly it was quite small and easy to read. But I still think it'd be nice if it was documented

Support for nested objects.

It would be nice if there was support for nested objects.

var obj = {
    name: 'tobi',
    last: 'holowaychuk',
    email: '[email protected]',
    _id: '12345',
    info: {
        logged_at: "2016-08-05T14:48:52Z",
        updated_at: "2016-08-05T14:48:52Z"
    }
};

New npm version

Can you please add a new npm version with last commit:

34db5e6

We have an automatically license checker that is failing without this information.

Thank you very much.

Rewrite in TypeScript?

I had a need for TypeScript definitions for this package, but it did not seem they existed within the DefinitelyTyped repository (https://github.com/DefinitelyTyped/DefinitelyTyped). I quickly tossed a TypeScript rewrite together, in case there was interest at https://github.com/kubejm/node-only-props

I can make any additional requested changes and submit a PR if there is desire. However, it does seem this project might not receive much attention, so I could also look to publish this under a different package name if there is no desire to continue to maintain this project.

Happy to do whatever makes the most sense.

Only for map

This is a very nice library. There is a pattern that I think would be awesome if it was supported. For this data definition, common in many situations:

const data = [{ a: 'b', c: 'd' }, { a: 'e', c: 'f' }];

I really like doing this:

const newData = data.map(only('a'));

I have a small library that I'm using, but was wondering if:

  • This is possible currently natively with only.
  • If there was a way to add it otherwise without complicating only.

Otherwise I'll just publish it as a tiny lib, just wondering if it's possible currently.

New maintainer?

Hey @tj!

I'd love to get this package updated on npm so it has SPDX license metadata. Would you be willing to add me or @tunnckoCore as a maintainer?

Cheers and hope all the Apex stuff is going great!
-Blaine

why not use modern syntax?

you can

module.exports = function(obj = {}, keys){
  if(!Object.keys(obj).length) return obj;

  if ('string' == typeof keys) keys = keys.split(/ +/);
  return keys.reduce(function(ret, key){
    if (!obj[key]) return ret;
    ret[key] = obj[key];
    return ret;
  }, {});
};

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.