Giter Club home page Giter Club logo

shape's Introduction

Shape

This library provides an inference of object shape given a JavaScript data object. It probably won't work well with circular references. But any object that comes from JSON will not have circular references.

It works well on very small or very large objects due to sampling (which can be turned off if requested).

You can try it out online in the DataStation app.

screenshot

Install

$ yarn add @multiprocess/shape

Examples

import { shape, toString } from '@multiprocess/shape';

const schema = shape({
  "results": [
    { "name": "Kevin", "score": "1" },
    { "name": "Moira", "score": 1 },
    { "name": "Alexei", "score": 2 },
    { "name": "Tia", "score": 2, "admin": true }
  ],
  "pageSize": 5,
  "total": 100,
  "nextPage": "https://myapi.com/p=2",
});
console.log(JSON.stringify(schema, null, 2), toString(schema));

Will print out:

{    
  "kind": "object",
  "children": {
    "results": {
      "kind": "array",
      "children": {
        "kind": "object",
        "children": {
          "name": {
            "kind": "scalar",
            "name": "string"
          },      
          "score": {
            "kind": "varied",
            "children": [
              {
                "kind": "scalar",
                "name": "string"
              },
              {
                "kind": "scalar",
                "name": "number"
              }
            ]
          },
          "admin": {
            "kind": "scalar",
            "name": "boolean"
          }
        }
      }
    },
    "pageSize": {
      "kind": "scalar",
      "name": "number"
    },
    "total": {
      "kind": "scalar",
      "name": "number"
    },
    "nextPage": {
      "kind": "scalar",
      "name": "string"
    },
  }
} Object with
  'results' of
    Array of
      Object with
        'name' of
          string,
        'score' of
          string or
          number,
        'admin' of
          boolean,
  'pageSize' of
    number,
  'total' of
    number,
  'nextPage' of
    string

Sampling

This library defaults to checking up to only 5000 elements at any level which allows the inferrence to scale to very large datasets. It will pick up to N elements randomly with the object to sample. To disable this, pass null, undefined, or false as the second argument to shape. For example shape(data, false).

However, turning off sampling will eventually wreck your stack as you pass bigger objects to shape.

Users

License

Apache-2.0, see ./LICENSE.md.

shape's People

Contributors

eatonphil avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.