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

objectro

v1.1.0

Published

Transform and validate objects

Downloads

48

Readme

🔀☑️🎁 Objectro

🔀 Transform and ☑️ validate 🎁 objects.

Why?

  • Destructure/restructure objects based on a map
  • Format specific values (sanitise, normalise, etc.)
  • Validate an object based on rules

Read more in the docs.

How?

const exampleObject = {
  example1: "This is a simple example",
  example2: "Another simple example",
  example3: "Another simple example to show arrays",
  emaxpel4: "Pluck properties from the object and rename them",
  example5: "DO SOME EXTRA FORMATTING AND RETURN AN OBJECT TO MERGE",
  example6: "Ignore this property"
};

objectro.transform(
  exampleObject,
  // Pluck specific properties from an object to output
  "example1",
  ["example2", "example3"],
  // Give a map that transforms one property name to another.
  {
    emaxpel4: "example4",
    // You can also pass a format function to transform that value
    // further, such as sanitising, normalising or otherwise processing
    // the value
    // If the format function returns an object, then that object will
    // be merged with the output object
    example5: (value, propName) => ({
      [propName]: value.toLowerCase(),
      example6: "This is new!"
    })
  }
);

// Outputs:
// {
//   example1: "This is a simple example",
//   example2: "Another simple example",
//   example3: "Another simple example to show arrays",
//   example4: "Pluck properties from the object and rename them",
//   example5: "do some extra formatting and return an object to merge",
//   example6: "This is new!"
// }
objectro.validate(
  {
    name: "Objectro",
    version: "1.1.0",
    lastUpdated: "2022-01-09",
    keywords: ["transform", "validate", "object", "javascript"],
    randomNumber: 123,
    nestedObject: {
      example: true
    }
  },
  {
    // Will only return true if all nested rules match
    all: {
      // Will return true if at least one nested rule matches
      any: {
        // Will match positive if input object has either property
        has: ["name", "version", "nestedObject.example"]
      },
      // Check if property value has one or many possible values
      // Since this is within the `all` rule, all values must be
      // present within the property value.
      has: {
        keywords: ["transform", "validate", "javascript"]
      },
      // Allows you to use validation rules to validate individual
      // property values.
      // Since this is within the `all` rule, all values must be
      // present within the property value.
      match: {
        // Check if the property value type is a string
        version: {
          type: "string"
        },
        // Check if property value is greater than another value
        randomNumber: {
          gt: 100
        },
        // You can also use custom functions to validate properties
        lastUpdated: value => new Date(value).getUTCFullYear() === 2019
      },
      // Negate the result of the following rules
      not: {
        has: {
          // You can also reference nested object properties
          "nestedObject.example": false
        }
      }
    }
  }
);

// Outputs:
// true

Read more in the docs.

Installation

Browser

<script src="//unpkg.com/[email protected]/dist/objectro.umd.js"></script>

<script>
  // window.objectro should then be available
  console.log(objectro);
</script>

ES Module

  npm i objectro
  yarn add objectro

Then in your source code:

// ES5
const objectro = require("objectro").default;
const { transform, validate } = require("objectro");

// ES6
import objectro from "objectro";
import { transform, validate } from "objectro";

// TypeScript
import { TransformFn } from "objectro/lib/types"; // if you ever need access to specific objectro type definitions
import { transform, validate } from "objectro";

Development

To download external dependencies:

  npm i

To run tests (using Jest):

  npm test

Contribute

Got cool ideas? Have questions or feedback? Found a bug? Post an issue

Added a feature? Fixed a bug? Post a PR

License

Apache 2.0