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

surfing

v0.0.1

Published

Realy simple and customizable schemas without recursion.

Downloads

4

Readme

Surfing

Realy simple and customizable schemas without recursion.

Build Status

  • [x] Multivariant typing
  • [x] Super fast one while implementation
  • [x] Custom operators

Install

npm install --save surfing

Example

// Example of custom DBRef/simple number pointer to some document
var ref = {
  or: {
    object: {
      props: {
        id: {
          string: true
        },
        collection: {
          string: true
        }
      }
    },
    number: true
  }
};
var surfing = new Surfing({
  object: {
    props: {
      source: ref,
      target: ref
    }
  }
}, {
  source: { id: '507f1f77bcf86cd799439011', collection: 'users' },
  target: 12345
});
surfing.travers();
surfing.errors.length; // 0

Documentation

Surfing

var Surfing = require('surfing'); var surfing = new Surfing(schema?: Schema, data?: any, options?: Options);

Constructor for a one surfing instance.

travers

surfing.travers();

Run travers by the scheme and data.

errors

surfing.errors: [{ error?: any, schemaPath: [any], dataPath: [any], operatorsPath: [any] }]

Stores error if they throwed as a result of the traversing.

Finding

var Finding = require('surfing/finding'); var finding = new Surfing(schema?: Schema, data?: any, options?: Options);

Targeted traversing of the tree in search of appropriate path.

byOperators

finding.byOperators(...path: [any]) => finding

Find stack level by operators path. Set finding.fail = true if the search failed.

bySchema

finding.bySchema(...path: [any]) => finding

Find stack level by schema path. Set finding.fail = true if the search failed.

byData

finding.byData(...path: [any]) => finding

Find stack level by data path. Set finding.fail = true if the search failed.

Options

Object

Global settings for surfing instance.

dictionary

Object = Surfing.dictionary

In this option you can set a custom dictionary operators.

details

Boolean = true

Defines the detail of error when traversing.

  • true by default, all errors will be saved, all paths will be traversed
  • false, and operator will stop after the first error, or operator throws one's own error

execute

Boolean = true

It allows you to disable validation on traversing. By default is enabled.

beforeExecution

Function = function(surfing) {}

Handler before execute the logic of the stack level.

afterExecution

Function = function(surfing) {}

Handler after execute the logic of the stack level.

beforeOperation

Function = function(surfing) {}

Handler before call operator of the stack level.

afterOperation

Function = function(surfing) {}

Handler after call operator of the stack level.

Operators

and

Object syntax
{
  string: true,
  custom: function(surfing) {/* do something */}
}
Array syntax
[
  'string',
  function(surfing) {/* do something */}
]

or

Object syntax
{
  string: true,
  number: true
}
Array syntax
[
  'string',
  'number'
]

props

Allows accurately move stack to a specific key in the data

var data = {
  abc: 123
};
var schema = {
  object: {
    props: {
      abc: {
        number: {
          equal: 123
        }
      }
    }
  }
}

each

It allows you to apply a description to all content.

var data = {
  abc: 123,
  cde: 123
};
var schema = {
  object: {
    each: {
      number: {
        equal: 123
      }
    }
  }
}

Typing

All operators of typing take and operator as scheme.

{
  string: {
    min: 10,
    max: 30
  },
  number: {
    greater: 15
  }
}

undefined

boolean

null

nan

number

string

object

array

function

date

Comparison

Applicable for strings, numbers or arrays.

{
  or: {
    string: {
      equal: 'abc'
    },
    number: {
      greater: 3,
      less: 31
    },
    array: {
      min: 3,
      max: 30
    }
  }
}

equal

greater

less

min

max

Actions

set

It allows to set value under certain conditions.

var data = {
  abc: 123
};
var schema = {
  object: {
    props: {
      abc: {
        number: {
          set: 234
        }
      }
    }
  }
}
var surfing = new Surfing(schema, data);
surfing.travers();
surfing.data.abc; // 234

delete

It allows to delete value under certain conditions.

var data = {
  abc: 123
};
var schema = {
  object: {
    props: {
      abc: {
        number: {
          delete: true
        }
      }
    }
  }
}
var surfing = new Surfing(schema, data);
surfing.travers();
surfing.data; // {}

default

It sets the value if it did not exist before.

var data = {};
var schema = {
  object: {
    props: {
      abc: {
        default: 123,
        number: {
          max: 234
        }
      }
    }
  }
}
var surfing = new Surfing(schema, data);
surfing.travers();
surfing.data; // { abc: 123 }

custom

Performs a custom action.

Object syntax
{
  custom: function(surfing) {
    if (surfing.getData() == 123)
      surfing.throw('My custom error');
  }
}
Array syntax
[
  function(surfing) {
    if (surfing.getData() == 123)
      surfing.throw('My custom error');
  }
]

regex

Verify with regular expression.

{
  regex: /^\d+$/
}

Versions

0.0.1

  • Date operator

0.0.0

  • Search in scheme by path
  • Merge Validation into Surfing class
  • Documentation

0.0.0-alpha.6

  • Options for Surfing and Validating classes
  • Move dictionary and handler to options
  • Default dictionary
  • Option details?: boolean = true

0.0.0-alpha.5

  • Support for array and/or difinition

0.0.0-alpha.4

  • Comparison and regex operators

0.0.0-alpha.3

  • pathSchema and pathData added to stack
  • Operators: equal, set, delete, default
  • Fix many operator's bugs

0.0.0-alpha.2

  • Operators: null, nan, custom and each
  • Remove temp comments

0.0.0-alpha.1

  • Fix "or" bug
  • Operators tests
  • Performance tests

0.0.0-alpha.0

  • Initial commit
  • Simple surfing class
  • Validation surfing class