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

monocle-api-props

v0.1.0

Published

Monocle API's solution for verifying deeply nested properties are set.

Downloads

920

Readme

Monocle API Props

Uses the Monocle API "props" syntax to determine if an object or array contains the specified properties.

Basic Usage

// Require this library
var MonocleApiProps = require('monocle-api-props');

// Create an instance with an array or object we want to check
var props = new MonocleApiProps(something);

// Check if deeply nested properties exist using Monocle API's path syntax
props.has('someProp.someNestedProp'); // returns true or false if the path exists

Syntax

  • .: a nested property, e.g. a.b will be found within the object { a: { b: 1 } }
  • @: a property within each object in an array, e.g. a@b will be find `{ a: [ { b: 1 }, { b: 2 } ] }

Advanced Example

// Sample object
var users = {
    total: 3,
    filters: {
        minAge: 20,
        maxAge: 40
    },
    items: [
        {
            name: 'Alice',
            age: 27,
            primaryPhoto: {
                url: 'http://...'
            }
        },
        {
            name: 'Jane',
            age: 32,
            primaryPhoto: {
                url: 'http://...'
            }
        },
        {
            name: 'Ed',
            age: 29,
            primaryPhoto: {
                url: 'http://...'
            }
        }
    ]
};

// Create an instance with the users data
var props = new MonocleApiProps(users);

// Basic top-level property access
props.has('total');             // returns true because users has a "total" property
props.has('filters');           // returns true because users has a "filters" property
props.has('foo');               // returns false because users does not have a "foo" property

// Nested properties use a "." separator
props.has('filters.minAge');    // returns true because users.filters has a "minAge" property
props.has('filters.maxAge');    // returns true because users.filters has a "maxAge" property
props.has('filters.derp');      // returns false because users.filters does not have a "derp" property

// Reach into arrays using an "@" separator to ensure they all contain the specified property
props.has('items@name');        // returns true because each object in the items array has a "name" property
props.has('items@age');         // returns true because each object in the items array has an "age" property
props.has('items@flerp');       // returns false because at least one object in the items array did not have a "flerp" property

// Mix and match "@" and "." as necessary to drill deeper
props.has('[email protected]'); // returns true because each object in the "items" array has a "primaryPhoto" object with a "url" property.
props.has('[email protected]'); // returns true because each object in the "items" array has a "primaryPhoto" object with a "url" property.

Files and Directory Structure

The following describes the various files in this repo and the directory structure.

Note: Files and directories prefixed by * are auto-generated and excluded from the repository via .gitignore.

.
├── Gruntfile.js            # grunt task configuration
├── README.md               # this file
├── *docs                   # autogenerated documentation
│   └── *index.html         # each JS file in `./lib` has a corresponding HTML file for documentation
├── lib                     # all code for this library will be placed here
│   └── index.js            # main entry point for your npm package
├── *node_modules           # all dependencies will be installed here by npm
├── package.json            # description of this package for npm, including dependency lists
└── test                    # unit test configuration, reports, and specs
    ├── *coverage.html      # code coverage report
    ├── lib                 # specs go here, preferably with a 1:1 mapping to code in `./lib`
    │   └── index_test.js   # example spec for `./lib/index.js`
    ├── mocha.opts          # runtime options for mocha
    └── test_runner.js      # configures mocha environment (e.g. chai, sinon, etc.)

What's Included?

Grunt

Grunt is a JavaScript task runner to automate common actions. The Tagged NPM Package Seed supports the following built-in grunt tasks:

test

Runs all unit tests through mocha.

$ grunt test

coverage

Runs all unit tests and generates a code coverage report in ./test/coverage.html

$ grunt coverage

watch

Automatically runs mocha tests each time a file changes in ./lib or ./test.

$ grunt watch

docs

Generates documentation for all JS files within ./lib using docco. Documentation is written to ./docs.

$ grunt docs

clean

Deletes all auto-generated files, including ./docs and ./test/coverage.html

Mocha, Sinon, Chai, Blanket

The ultimate TDD environment for node. Place your specs in ./test/lib, and run grunt test.

See ./test/lib/index_test.js for examples.