npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

pick-recursively

v0.2.0

Published

Pick keys from an object recursively

Readme

pick-resursively

English

pick-resursively는 중첩된 Object에서도 원하는 속성을 골라올 수 있는 라이브러리입니다. ES6 문법을 사용했습니다.

사용 예

아래 예제는 test.js에서 확인하실 수 있습니다.

모듈을 임포트해옵니다.

const pick = require('pick-recursively');

Case 1. 기본

const target1 = {
  author: 'Leeingnyo',
  uri: 'https://github.com/Leeingnyo/pick-recursively',
  country: 'Korea'
};
const query1 = {
  author: true, // author 속성을 가져옵니다
  uri: 0, // property 를 가져오려면 true를 써주셔야 합니다
  nothing: true // 이 속성은 target1 에 없으므로 무시됩니다
};
pick(target1, query1);

이것의 결과는 다음과 같이 나타날 것입니다.

{
  author: 'Leeingnyo'
}

Case 2. 중첩된 속성 가져오기

const target2 = {
  foo: {
    a: 'a',
    b: 'b'
  },
  bar: {
    c: 'c',
    d: 'd',
  },
  baz: 'baz'
};
const query2 = {
  foo: true, // foo 오브젝트를 전부 가져옵니다
  bar: {
    c: true // bar 오브젝트의 c 속성을 가져옵니다
  }
};
pick(target2, query2);

이것의 결과는 다음과 같이 나타날 것입니다.

{
  foo: {
    a: 'a',
    b: 'b'
  },
  bar: { c: 'c' }
}

Case 3. 배열 안의 속성 가져오기

const target3 = {
  articles: [
    {
      title: 'title 1',
      content: 'content 1',
      date: new Date()
    },
    {
      title: 'title 2',
      content: 'content 2',
      date: new Date()
    },
    {
      title: 'title 3',
      content: 'content 3',
      date: new Date()
    }
  ],
  count: 3
};
const query3 = {
  articles: { // 타겟에서의 타입이 Array면, 반드시 하나의 속성만 가져야 합니다.
    article: { // 속성은 아무 이름이나 쓰셔도 됩니다
        // 그래도 의미있는 이름을 쓰시는 것이 좋겠죠? (foreach 에 쓰시는 변수 이름처럼)
      title: true,
    } // 속성을 여러 개 쓰면 Object.key() 의 첫번째 원소가 들어가게 됩니다
  },
  count: true
};
pick(target3, query3);

이것의 결과는 다음과 같이 나타날 것입니다.

{
  articles: [
    { title: 'title 1', },
    { title: 'title 2', },
    { title: 'title 3', }
  ],
  count: 3
}

Case 4. 잘못된 대상과 질의

target의 타입은 오브젝트 일 것이라 기대되지만, 아닌 경우도 있을 것입니다. 이런 경우 그냥 그대로 내보냅니다.

const query4 = {
  foo: true
};
pick(undefined, query4);
pick(null, query4);
pick(NaN, query4);
pick(true, query4);
pick(0, query4);
pick(1, query4);
pick('', query4);
pick('asdf', query4);

이것의 결과는 다음과 같이 나타날 것입니다.

undefined
null
NaN
true
0
1

asdf

Case 5. 문자열 쿼리

query를 object 만으로는 너무 장황하여 string과 array를 이용하여 정의했습니다.

/*
target1 = {
  author: 'Leeingnyo',
  uri: 'https://github.com/Leeingnyo/pick-recursively',
  country: 'Korea'
};
*/
pick(target1, 'author'); // property author 를 가져옵니다
/*
{ author: 'Leeingnyo' }
*/
pick(target1, ['author', 'uri', 'nothing']); // 배열안의 property 들을 가져옵니다. nothing 은 무시됩니다
/*
{
  author: 'Leeingnyo',
  uri: 'https://github.com/Leeingnyo/pick-recursively'
}
*/

중첩된 상황에서도 사용가능합니다.

/*
const target3 = {
  articles: [
    {
      title: 'title 1',
      content: 'content 1',
      date: new Date()
    },
    {
      title: 'title 2',
      content: 'content 2',
      date: new Date()
    },
    {
      title: 'title 3',
      content: 'content 3',
      date: new Date()
    }
  ],
  count: 3
};
*/
pick(target3, {
  articles: {
    article: ['title', 'content']
  },
  count: true // count 경우는 true 로 처리해야합니다 (string query 사용 불가)
});
/*
{
  articles: [
    { title: 'title 1',
      content: 'content 1', },
    { title: 'title 2',
      content: 'content 2', },
    { title: 'title 3',
      content: 'content 3', }
  ],
  count: 3
}
*/

Case 7. 함수 쿼리

함수도 쿼리의 value 로 사용하실 수 있습니다. 이 함수는 하나의 인수를 가져야 하고 그 인수는 쿼리에 해당하는 target의 value입니다. 함수가 false를 리턴하면 그 property 는 가져오지 않습니다.

/* target1
{
  author: 'Leeingnyo',
  uri: 'https://github.com/Leeingnyo/pick-recursively',
  country: 'Korea'
};
*/
pick(target1, {
  author: true,
  uri: uri => uri.indexOf('https') === 0 // 프로토콜이 https 면 가져옴
});

// result
{
  author: 'Leeingnyo',
  uri: 'https://github.com/Leeingnyo/pick-recursively' // 가져왔습니다
}

더 복잡한 형태의 일도 가능합니다.

pick({
  foo: undefined,
  bar: {
    baz: 1
  }
}, {
  foo: true,
  bar: item => {
    return item.baz > 1; // bar object의 baz가 1보다 크면 bar 오브젝트를 가져옵니다.
  }
});

// result
{
  foo: undefined // false 이기 때문에 안 가져오죠
}

좀 더 유용한 경우를 보여드리겠습니다. 리턴값으로 올바른 query가 오면 함수의 인자로 들어오는 value에 대해서 리턴된 query를 수행합니다.

pick({
  articles: [
    {
      isSecret: false,
      title: 'title 1',
      content: 'content 1'
    },
    {
      isSecret: true,
      title: 'title 2',
      content: 'content 2'
    },
    {
      isSecret: false,
      title: 'title 3',
      content: 'content 3'
    }
  ]
}, {
  articles: {
    article: article => {
      if (article.isSecret) return 'isSecret'; // article 에 대해서 다른 쿼리를 수행하게 합니다
      return ['isSecret', 'title']; // 문자열 배열 쿼리도 가능합니다
    }
  }
});

// result
{
  articles: [
    {
      isSecret: false,
      title: 'title 1'
    }, // content 는 빠짐
    {
      isSecret: true
    }, // isSecret 만 남음
    {
      isSecret: false,
      title: 'title 3'
    }
  ]
}

target 이 array인 경우에는 filter 역할을 할 수도 있습니다. 평가된 값이 false일 경우, 해당 element는 array에서 제외됩니다.

/* target3
{
  articles: [
    {
      title: 'title 1',
      content: 'content 1',
      date: new Date()
    },
    {
      title: 'title 2',
      content: 'content 2',
      date: new Date()
    },
    {
      title: 'title 3',
      content: 'content 3',
      date: new Date()
    }
  ],
  count: 3
};
*/
pick(target3, {
  articles: {
    article: article => {
      if (article.content.indexOf('2') < 0) {
        return ['title', 'content']; // 조건을 만족시키는 article엔 다른 쿼리를
      }
      return false; // 내용에 2가 포함되어있으면 배열에서 제외시킵니다
    }
  }
}

// result
{
  articles: [
    {
      title: 'title 1',
      content: 'content 1',
    },
    // content 2 를 내용으로 가진 object 는 빠졌습니다
    {
      title: 'title 3',
      content: 'content 3',
    }
  ]
}

Notice

function query 를 사용하셔도 target은 보존됩니다.

License

MIT License