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

obut

v0.2.4

Published

Simple object utilities

Downloads

11

Readme

getpro

A collection of object--and only objects--utilities.

Build Status

Installation

npm install --save obut

API

obut.coverage(object, [refFieldName])

Return a copy of the object with all duplicate object references removed ("coverage tree").

Duplicate objects are replaced with a reference construct similar to the JSON Referece Draft (https://tools.ietf.org/html/draft-pbryan-zyp-json-ref-03).

The reference field name can be changed using the refFieldName argument.

    const { coverage } = require('obut');

    const inner = {};
    const object = { a : inner, b: inner};

    // object now contains two references to the same inner object


    const actual = coverage({ a : inner, b: inner});
    console.log(actual);

    // one of the reference is replaced by a JSON path object.
    // > { a: {}, b: { '$ref': '#/a' } }

obut.filter(object, test)

New in 0.2.3

Return a new object containing only own properties of object having passed test.

    const { filter } = require('obut');

    let original = { a: 1, b: undefined, c: { d: undefined } };
    let actual = filter(original, (key, value) => typeof value !== 'undefined');
    console.log(actual);

    // > { a: 1, c: { d: undefined } }

obut.defined(object)

New in 0.2.3

Synonym for (object) => _filter(object, (key, value) => (typeof value !== 'undefined'))

obut.pick(object, desc)

Yet another pick function to extract a subset of object properties.

This particular version is considering only object own properties. It performs a deep walk through the description desc to extract the corresponding fields from object:

    const { pick } = require('obut');

    let original = { a: 1, b: { c: 3, d: 4 }, f: 6 };
    let actual = pick(original, { a: 10, b: { d: 40 }});
    console.log(actual);

    // > { a: 1, b: { d: 4 } }

In object a field set to undefined is handled the same way as a missing field, and the default value provided in desc is substituted.

    let original = { a: 1, b: { c: undefined, d: 4 }, f: 6 };
    let actual = pick(original, { a: 10, b: { c: 30, d: 40, e: 50 }});
    console.log(actual);

    // > { a: 1, b: { c: 30, d: 4, e: 50 } }

The pick function never produces an undefined value. So if a field of desc is undefined if will be present in the output only if it was defined in the original object:

    let original = { a: 1, b: undefined };
    let actual = pick(original, { a: undefined, b: undefined, f: undefined});
    console.log(actual);

    // > { a: 1 } }

null is considered both as a value and as an object. When used in desc, a null value is used to copy a field verbatim, regardless if its type. The only exception is for undefined values, since the output of the pick function never contains an undefined field:

    let original = { a: 1, b: { c: undefined, d: 4 }, f: 6 };
    let actual = pick(original, { a: 10, b: null, f: null, g: null });
    console.log(actual);

    // > { a: 1, b: { d: 4 }, f: 6, g: null }

In the example above:

  • the object b is copied, excluding the undefined value;
  • the value f is copied;
  • the missing g field is added with the null value.

In object a field set to null is set to null in the result too, regardless of the type of the corresponding field in desc.

    let original = { a: null, b: null, f: 6 };
    let actual = pick(original, { a: 10, b: { c: 30 }});
    console.log(actual);

    // > { a: null, b: null }

Except in the cases explained above, if a field is an object in desc but not in the original object, and error is thrown.

Working with arrays

obut.pick handles array mostly like objects with numerical indexes. So you can write something like that:

    obut.pick([1,2,3],[10,20,30,40])
    // > [ 1, 2, 3, 40 ]

You can also mix objects and arrays, obut.pick will be able to match fields. Useful if you want a sparse array containing a sub-set of the original array.

    obut.pick([1,2,3],{ 0:0, 5:50}); // BEWARE: in JS arrays are 0-based!
    // > [ 1, <4 empty items>, 50 ]

Node version

Require NodeJS >= v7.0 Tested with v7.0, v7.6 and v8.9

License

(The MIT License)

Copyright (c) 2018 Sylvain Leroux

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.