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

shadowing

v0.3.1

Published

Object analyse tool

Downloads

8

Readme

shadowing

Light-weighted object/array analyse tool.

total downloads of shadowing shadowing's License latest version of shadowing coverage status of github.com/YounGoat/nodejs.shadowing dependencies of github.com/YounGoat/nodejs.shadowing devDependencies of github.com/YounGoat/nodejs.shadowing build status of github.com/YounGoat/nodejs.shadowing star github.com/YounGoat/nodejs.shadowing

Description

shadowing offers an easy way to judge whether the object / array / string / number is what you want.

Shadowing Logo

Table of Contents

Get Started

var shadowing = require('shadowing');

shadowing(
    /* origin */ { name: 'YounGoat', gender: 'male' },
    /* shadow */ { gender: 'male' }
);
// RETURN true

shadowing(
    /* origin */ { linkman: { name: 'YounGoat', gender: 'male' } },
    /* shadow */ { linkman: { gender: 'male' } }
);
// RETURN true

shadowing(
    /* origin */ { name: 'YounGoat', gender: 'male' },
    /* shadow */ { gender: shadowing.or('male', 'female') }
);
// RETURN true

shadowing(
    /* data */ 99,
    /* definition */ shadowing.numberRange('>90 <=120')
);
// RETURN true

APIs

const shadowing = require('shadowing');
const NumberRange = require('shadowing/NumberRange');
  • boolean shadowing( origin, shadow )
  • boolean shadowing( origin, shadow, string mode )
  • class shadowing.Shadow( Function judge )
  • symbol shadowing.EXIST
  • shadowing.Shadow shadowing.or( Shadow shadow1, Shadow shadow2 [, ...] )
  • shadowing.Shadow shadowing.and( Shadow shadow1, Shadow shadow2 [, ...] )
  • shadowing.Shadow shadowing.has( string propertyName [, ...] )
  • shadowing.Shadow shadowing.hasnot( string propertyName [, ...] )
  • shadowing.Shadow shadowing.numberRange( string numberRangeCode )

Move Forward

Deep Comparation Between Objects

Suppose that we want an object which:

  • has property named "linkman",
    and property "linkman" is an object with property "gender",
    which valued "male";

  • has property named "city",
    and property "city" is an object with property "name",
    which valued "Shanghai".

Before, we will code like:

if (foo
    && foo.linkman
    && foo.linkman.gender === 'male'
    && foo.city
    && foo.city.name === 'Shanghai') {
        // ...
    }

Or, JSON Schema may be a more formal choice:

// To run this snippet, do "npm install ajv" firstly.

var foo = {
    linkman: {
        name: 'YounGoat',
        gender: 'male'
    },
    city: {
        name: 'Shanghai'
    }
};

var schema = {
    type: 'object',
    properties: {
        linkman: {
            type: 'object',
            properties: {
                gender: {
                    type: 'string',
                    enum: ['male']
                }
            }
        },
        city: {
            type: 'object',
            properties: {
                name: {
                    type: 'string',
                    enum: ['Shanghai']
                }
            }
        }
    }
};

var Ajv = require('ajv');
if ((new Ajv).validate(schema, foo)) {
    // ...
}

Now, it is easier and more natural to achieve the target by shadowing:

var foo = {
    linkman: {
        name: 'YounGoat',
        gender: 'male'
    },
    city: {
        name: 'Shanghai'
    }
};

var shadow = {
    linkman: { gender: 'male' },
    city: { name: 'Shanghai' }
};

var shadowing = require('shadowing');
if (shadowing(foo, shadow)) {
    // ...
}

Shadow of Array

The module can also be used to calculate the shadow of an array. E.g.

shadowing( [ 1, 2, 3 ], [ 2 ] );
// RETURN true

shadowing( [ 1, 2, 3 ], [ 4 ] );
// RETURN false

shadowing( [ 1, 2, 3 ], 2 );
// RETURN false

According to shadowing, as you can see, the shadow of an array origin MUST be an array too. And, if items of origin array are scalar values, each items of the shadow array SHOULD be found in the origin array. However, while the origin array has vectorial items, the items of the shadow array NEED NOT strictly equal to any item of the origin. Instead, they MAY be shadow of at least one item of the origin. E.g.

var origin = [ { gender: 'male', age: 22 }, { gender: 'female', age: 20 } ];

shadowing( origin, [ { gender: 'male' }] );
// RETURN true

More Than Strictly Equal

Sometime, we wanna make little changes. The module offers following ways for you to create shadows a little more adaptable,

var shadow = {
    // The origin SHOULD have property "linkman".
    linkman: shadowing.EXIST
};

var shadow = {
    // The origin SHOULD have property "linkman",
    // and sub-property "gender" valued string "male" or "female".
    linkman: {
        gender: shadowing.or('male', 'female')
    }
};

/**
 * The origin SHOULD have both property "linkman" and "city", equals to
 * { linkman: shadowing.EXIST, city: shadowing.EXIST }
 */
var shadow = shadowing.and(
    { linkman: shadowing.EXIST },
    { city: shadowing.EXIST }
);

If there is some numbers, it is usual to know whether it belongs to a number set instead of whether it equals to some exact value. NumberRange is designed for such purpose. E.g.

var shadow = {
    year: shadowing.numberRange('1980 1987 >2017')
};

Shadowing Mode

Beyond the default mode named "strict", there are other two mode available: "normal" and "loose".

  • strict Mode
    This is the default mode. Each scalar value in origin SHOULD find its strict equal or an instance of shadowing.Shadow covering itself in shadow.

  • normal Mode
    This mode is close to strict mode but equal is accepted as replacement of strict equal. E.g.

    // Falsy values will shadow each other in normal mode.
    // So do truthy values.
    
    shadowing(0, false);
    // RETURN false
    
    shadowing(0, false, 'normal');
    // RETURN true
  • loose Mode
    Just as the name implies, this mode is looser than normal mode in:

    1. A string may be regarded as shadow of a number if the former string can be successfully construc a NumberRange which covers the latter number.

Examples

We offer some examples to explain how shadowing works. There will be an array of test cases in each example file. A test unit is also an array with four items:

[
    origin,  /* The origin object/array/others */
    shadow,  /* The shadow to be tested */
    ifValid, /* If the shadow is valid shadow to the origin according to shadowing */
    description
]