Giter Site home page Giter Site logo

shapeful's Introduction

shapeful

A shameful attempt at checking the shape of an object. I'm sure it exists, but my google-fu failed me.

Basic type assertions

    shapeful(true ,'boolean') === true;
    shapeful(0,'number') === true;
    shapeful('a','string') === true;
    shapeful(null, 'object') === true;
    shapeful({},'object') === true;
    shapeful(function(){},'function') === true;
    shapeful(undefined, 'undefined') === true;
    shapeful([], 'array') === true;

Assertions on object shape

  var obj = {oh: 'bar', my: 0, word: false};
  var assertions = {oh: 'string', my: 'number', word: 'boolean'};
  shapeful(obj, assertions) === true;

Optional attributes can be described

  var obj = {foo: 'bar', my: 0};
  var obj2 = {foo: 'bar', my: 0, word: false};
  var obj3 = {foo: 'bar', my: 0, word: 0};
  var assertions = {foo: 'string', my: 'number', word: {__optional: 'boolean'}};
  shapeful(obj, assertions) === true;
  shapeful(obj2, assertions) === true;
  shapeful(obj3, assertions) === false;

And some nested objects

  var obj = {attr: true, obj: {foo: 'bar', my: 0, word: false}};
  var assertions = {
    attr: 'boolean',
    obj: {foo: 'string', my: 'number', word: 'boolean'}
  };
  shapeful(obj, assertions);

Type type of array members can also be tested

  var obj = {list: ['a', 'b', 'c']};
  var obj2 = {list: [{attr: 1}, {attr: 2}, {attr: 3}]}
  var obj3 = {list: ['a', 'b', false]};
  var assertion = {list: {__array: 'string'}};
  var assertion2 = {list: {__array: {attr: 'number'}}};
  shapeful(obj, assertion) === true;
  shapeful(obj2, assertion2 === true;
  shapeful(obj3, assertion) === false;

Union of assertions

Returns true if any of the assertions in a list match the shape.

  var obj = {obj: {}, list: []};
  var asserts1 = {obj: ['object', 'array'], list: ['object', 'array'] };
  var asserts2 = [
    {obj: 'array', list: 'object'},
    {obj: 'object', list: 'object'},
    {obj: 'array',list:'array'},
    {obj: 'object',list: 'array'}
  ];
  shapeful(obj, asserts1) === true;
  shapeful(obj, asserts2) === true;

shapeful's People

Contributors

briely avatar

Watchers

 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.