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

@thing-description-playground/defaults

v1.4.0

Published

Adds/removes defaults for Thing Descriptions in the Web of Things - Thing Description Playground.

Downloads

78

Readme

@thing-description-playground/DEFAULTS

The package adds/removes defaults according to the Thing Description (TD) specification for every property with a default that is not filled with a value. Currently using this version of the TD specification. It is part of the Thingweb-Playground, you can find more information about the Thingweb-Playground here.

License

Licensed under the MIT license, see License.

Usage

You can use this package to integrate TD default value adding/removing in your application.

  • Install this package via NPM (npm install @thing-description-playground/defaults) (or clone repo and install the package with npm install)

  • Node.js or Browser import:

    • Node.js: Require the package and use the functions
    const { addDefaults, removeDefaults } = require("@thing-description-playground/defaults")
    • Browser: Import the tdDefaults object as a global by adding a script tag to your html (and optionally declare the variables without namespace).
    <script src="./node_modules/@thing-description-playground/add_defaults/dist/web-bundle.min.js"></script>
    const addDefaults = tdDefaults.addDefaults
    const removeDefaults = tdDefaults.removeDefaults
  • Now you can call the add/remove defaults function to extend/reduce a TD object.

    addDefaults(td)
    removeDefaults(td)

    You can find usage examples in the tests folder, or the web and cli packages.

Documentation

  • How it works:
    The algorithm shipped with package basically tries to check all possible place (in terms of object hierachy e.g. td.properties.temperature) type combinations (lines 1 - 3). A type refers to one objects default values (e.g. form -> op: application/json). All existing combinations in the given TD, are then passed to a handle function (lines 4, 5). Since there can be nested places of type DataSchema (e.g. if one DataSchema includes another by using oneOf), these need to be handled recursively (lines 6 - 10).
    The handle function (lines 13 - 20) checks the place & type combinations in the given TD, depending on whether it should add or remove default values:

    • add: If the default value(s) corresponding to the place type combination is/are not defined in the current TD, if this is true it adds the default value(s) (lines 14 - 16).
    • remove: If the place type combination value(s) equal the default value(s), if this is true it removes the value(s) (lines 17 - 19).
  • pseudocode:

    #1  | checkAllPossibleDefaultValuePlaces(td) {
    #2  |   for (every type of defaultTypes)
    #3  |     for (every place of possiblePlaces[type])
    #4  |       if (td[place] !== undefined)
    #5  |         handle(td[place], type)
    #6  |       if (type == "DataSchema")
    #7  |         currentPlace = td[place]
    #8  |         while (hasRecursiveElements(currentPlace))
    #9  |           handle(currentPlace, "DataSchema")
    #10 |           currentPlace = currentPlace[recursion]
    #11 | }
    #12 |
    #13 | handle(object, type) {
    #14 |   if (mode == "add")
    #15 |     if (object[type] == undefined)
    #16 |       object[type] = defaultTable[type]
    #17 |   if (mode == "remove")
    #18 |     if (object[type] == defaultTable[type])
    #19 |       delete object[type]
    #20 | }