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 🙏

© 2024 – Pkg Stats / Ryan Hefner

shake-tree

v1.1.0

Published

Shake the tastiest fruits from your deep object tree

Downloads

6

Readme

shake-tree

Shake the tastiest fruits from your deep object tree

Why?

This module is used by the Electron team to help internationalize our documentation. We parse all of our API docs into a structured format that looks a bit like this:

{
  name: 'BrowserWindow',
  description: 'Create and control browser windows.',
  process: {
    main: true,
    renderer: false
  },
  type: 'Class',
  instanceName: 'win',
  slug: 'browser-window',
  websiteUrl: 'http://electron.atom.io/docs/api/browser-window',
  repoUrl: 'https://github.com/electron/electron/blob/v1.4.0/docs/api/browser-window.md',
  staticMethods: ['...'],
  instanceMethods: ['...'],
  instanceProperties: ['...'],
  instanceEvents: ['...']
}

Some parts of this structured data (like method names and arguments) needs to stay in English, whereas other parts of it (like descriptions) need to extracted for translation into other languages.

That's where shake-tree comes in. It gives us a way to extract just the content we need from this deep object.

Installation

With npm:

npm install shake-tree --save

With Yarn:

yarn add shake-tree

Usage

const shakeTree = require('shake-tree')

// Given some arbitrarily-shaped object tree:
const input = {
  a: 'just a string',
  b: {
    description: 'this is b description'
  },
  c: {
    title: 'this is c title',
    description: 'this is c description',
    descriptionExtended: 'this is the extended description'
  }
}

// You can pull out just the values you want by key:
const output = shakeTree(input, 'description')

console.log(output)
// { b: { description: 'this is b description' },
//   c: { description: 'this is c description' } }

// You can also specify multiple keys to match:
shakeTree(input, ['description', 'title'])

Flattening

In some cases you might want the result as flat key-value object with period-delimited strings as keys. In this case, set the flat option to true:

This is useful if you want to preserve array indexes in the shaken tree.

const input = {
  a: [
    {description: 'first a'},
    {description: 'second a'}
  ]
}

const output = shakeTree(input, 'description', {flat: true})

console.log(output)
// {
//   'a.0.description': 'first a',
//   'a.1.description': 'second a'
// }

API

This module exports a single function.

`shakeTree(tree, targetKeys[, options])

  • tree - Object (required)
  • targetKeys - String or Array of Strings (required)
  • options - Object (optional)
    • flat - Boolean (defaults to false)

Tests

npm install
npm test

Dependencies

  • flat: Take a nested Javascript object and flatten it, or unflatten an object with delimited keys
  • lodash.set: The lodash method _.set exported as a module.

Dev Dependencies

  • chai: BDD/TDD assertion library for node.js and the browser. Test framework agnostic.
  • electron-api-docs: Electron's API documentation in a structured JSON format
  • mocha: simple, flexible, fun test framework
  • standard: JavaScript Standard Style
  • standard-markdown: Test your Markdown files for Standard JavaScript Style™

License

MIT