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

@telemok/valda

v0.0.7

Published

Javascript ES6 library for fast short easy analyse, assert, check, parse, validate input fields and json responses.

Downloads

2

Readme

valda (VALiDAtor)

[valda] https://github.com/Telemok/valda is a JavaScript ES6 library for fast short easy analyse, assert, check, parse, validate input fields and json responses.

Features

  • Function arguments parsing, validation and assertion
  • Form fields parsing, validation and assertion
  • JSON REST parsing
  • Type checking and converting
  • Fast and easy to learn
  • No schemas
  • NodeJs and browser Javascript support

Instalation:

  1. Create your NodeJs, Browser or Webview app.
  2. import { valda } from "@telemok/valda.js"
  3. let checkedPercents = valda.integerMinMax.assert(uncheckedPercents, 0, 100);//throw if wrong

Functionality:


// valda.***.is return true if OK, false if not OK
valda.defined.is = (value);
valda.boolean.is = (value);
valda.number.is = (value);
valda.numberMinMax.is = (value, minimal = Number.NEGATIVE_INFINITY, maximal = Number.POSITIVE_INFINITY);
valda.numberMin.is = (value, minimal = Number.NEGATIVE_INFINITY);
valda.numberMax.is = (value, maximal = Number.POSITIVE_INFINITY);
valda.integerMinMax.is = (value, minimal = Number.MIN_SAFE_INTEGER, maximal = Number.MAX_SAFE_INTEGER);
valda.integer.is = (value);
valda.integerMin.is = (value, minimal = Number.MIN_SAFE_INTEGER);
valda.integerMax.is = (value, maximal = Number.MAX_SAFE_INTEGER);
valda.object.is = (value);
valda.array.is = (value);
valda.uint8Array.is = (value);
valda.string.is = (value);
valda.function.is = (value);
valda.extends.is = (value, ParentClass);
valda.instance.is = (value, Class);
valda.varName.is = (value);
valda.hex.is = (value);
valda.hexFixed.is = (value, sizeBytes);

// valda.***.whyNot return false if OK, description why wrong if not OK
// valda.object.whyNot({}) will return false
// valda.object.whyNot(5) will return `Type (number) must be "object"`
valda.defined.whyNot = (value);
valda.boolean.whyNot = (value);
valda.number.whyNot = (value);
valda.numberMinMax.whyNot = (value, minimal = Number.NEGATIVE_INFINITY, maximal = Number.POSITIVE_INFINITY);
valda.numberMin.whyNot = (value, minimal = Number.NEGATIVE_INFINITY);
valda.numberMax.whyNot = (value, maximal = Number.POSITIVE_INFINITY);
valda.integerMinMax.whyNot = (value, minimal = Number.MIN_SAFE_INTEGER, maximal = Number.MAX_SAFE_INTEGER);
valda.integer.whyNot = (value);
valda.integerMin.whyNot = (value, minimal = Number.MIN_SAFE_INTEGER);
valda.integerMax.whyNot = (value, maximal = Number.MAX_SAFE_INTEGER);
valda.object.whyNot = (value);
valda.array.whyNot = (value);
valda.uint8Array.whyNot = (value);
valda.string.whyNot = (value);
valda.function.whyNot = (value);
valda.extends.whyNot = (value, ParentClass);
valda.instance.whyNot = (value, Class);
valda.varName.whyNot = (value);
valda.hex.whyNot = (value);
valda.hexFixed.whyNot = (value, sizeBytes);

// valda.***.codeWhyNot return false if OK, codeWhyNot why wrong if not OK
// valda.object.codeWhyNot({}) will return false
// valda.object.codeWhyNot(5) will return `IS_NOT_OBJECT`
valda.defined.codeWhyNot = (value);
valda.boolean.codeWhyNot = (value);
valda.number.codeWhyNot = (value);
valda.numberMinMax.codeWhyNot = (value, minimal = Number.NEGATIVE_INFINITY, maximal = Number.POSITIVE_INFINITY);
valda.numberMin.codeWhyNot = (value, minimal = Number.NEGATIVE_INFINITY);
valda.numberMax.codeWhyNot = (value, maximal = Number.POSITIVE_INFINITY);
valda.integerMinMax.codeWhyNot = (value, minimal = Number.MIN_SAFE_INTEGER, maximal = Number.MAX_SAFE_INTEGER);
valda.integer.codeWhyNot = (value);
valda.integerMin.codeWhyNot = (value, minimal = Number.MIN_SAFE_INTEGER);
valda.integerMax.codeWhyNot = (value, maximal = Number.MAX_SAFE_INTEGER);
valda.object.codeWhyNot = (value);
valda.array.codeWhyNot = (value);
valda.uint8Array.codeWhyNot = (value);
valda.string.codeWhyNot = (value);
valda.function.codeWhyNot = (value);
valda.extends.codeWhyNot = (value, ParentClass);
valda.instance.codeWhyNot = (value, Class);
valda.varName.codeWhyNot = (value);
valda.hex.codeWhyNot = (value);
valda.hexFixed.codeWhyNot = (value, sizeBytes);

// valda.*** return fixed value if OK, throw description if not OK
// valda.boolean.assert("0") will return false
// valda.boolean.assert("1") will return true
// valda.boolean.assert("2") will throw new Error(`Value not boolean. Only allowed: false, true, 0, 1, "0", "1", "false", "true".`) with name "IS_NOT_BOOLEAN"
valda.defined.assert = (value, callback);
valda.boolean.assert = (value, callback);
valda.number.assert = (value, callback);
valda.numberMinMax.assert = (value, minimal = Number.NEGATIVE_INFINITY, maximal = Number.POSITIVE_INFINITY, callback);
valda.numberMin.assert = (value, minimal = Number.NEGATIVE_INFINITY, callback);
valda.numberMax.assert = (value, maximal = Number.POSITIVE_INFINITY, callback);
valda.integerMinMax.assert = (value, minimal = Number.MIN_SAFE_INTEGER, maximal = Number.MAX_SAFE_INTEGER, callback);
valda.integer.assert = (value, callback);
valda.integerMin.assert = (value, minimal = Number.MIN_SAFE_INTEGER, callback);
valda.integerMax.assert = (value, maximal = Number.MAX_SAFE_INTEGER, callback);
valda.object.assert = (value, callback);
valda.array.assert = (value, callback);
valda.uint8Array.assert = (value, callback);
valda.string.assert = (value, callback);
valda.function.assert = (value, callback);
valda.extends.assert = (value, ParentClass, callback);
valda.instance.assert = (value, Class, callback);
valda.varName.assert = (value, callback);
valda.hex.assert = (value, callback);
valda.hexFixed.assert = (value, sizeBytes, callback);

// valda.*** return fixed value if OK, throw description if not OK
// valda.boolean.parse({qwe: 1}, "qwe") will return true
// valda.boolean.parse({qwe: 1}, "asd") will throw new Error(`typeof is "undefined"`) with name: "IS_UNDEFINED"
valda.defined.parse = (parameters, key, callback);
valda.boolean.parse = (parameters, key, callback);
valda.number.parse = (parameters, key, callback);
valda.numberMinMax.parse = (parameters, key, minimal = Number.NEGATIVE_INFINITY, maximal = Number.POSITIVE_INFINITY, callback);
valda.numberMin.parse = (parameters, key, minimal = Number.NEGATIVE_INFINITY, callback);
valda.numberMax.parse = (parameters, key, maximal = Number.POSITIVE_INFINITY, callback);
valda.integerMinMax.parse = (parameters, key, minimal = Number.MIN_SAFE_INTEGER, maximal = Number.MAX_SAFE_INTEGER, callback);
valda.integer.parse = (parameters, key, callback);
valda.integerMin.parse = (parameters, key, minimal = Number.MIN_SAFE_INTEGER, callback);
valda.integerMax.parse = (parameters, key, maximal = Number.MAX_SAFE_INTEGER, callback);
valda.object.parse = (parameters, key, callback);
valda.array.parse = (parameters, key, callback);
valda.uint8Array.parse = (parameters, key, callback);
valda.string.parse = (parameters, key, callback);
valda.function.parse = (parameters, key, callback);
valda.extends.parse = (parameters, key, ParentClass, callback);
valda.instance.parse = (parameters, key, Class, callback);
valda.varName.parse = (parameters, key, callback);
valda.hex.parse = (parameters, key, callback);
valda.hexFixed.parse = (parameters, key, sizeBytes, callback);

Examples:

  1. Parsing students database
import { valda } from "@telemok/valda.js"

function parseStudents(json, count, sort = "AGE") {
	json = valda.string.assert(json); /* json must be string or throw */
	count = valda.integerMin.assert(count, 1); /* count must be 1 <= integer or throw */
	sort = valda.enum.assert(sort, ['AGE', 'MARKS', 'NAME']); /* sort must be 'AGE', 'MARKS', 'NAME' or throw */

	let obj = JSON.parse(json); /* json must be json or throw */
	let timestamp = valda.number.parse(obj, 'timestamp'); /* timestamp must be number or throw */
	let data = valda.array.parse(obj, 'data'); /* data must be array or throw */
	//for (let line of Object.values(data)) {
	for (let i = 0; i < count && i < data.length; i++)
	{
		let line = data[i];
		let age = valda.integerMinMax.parse(line, 'age', 16, 150); /* age must be  16 <= integer <= 150 or throw */
		let name = valda.string.parse(line, 'name'); /* name must be sting or throw */
		if (name.length > 100)
			throw new Error(`Wrong student name length (${name.length}) > 100`);
		let averageMark = valda.numberMinMax.parse(line, 'averageMark', 0, 10);
		let studentId = valda.integerMin.parse(line, 'studentId', 1);
		console.log(`Parsed student #${studentId} with name "${name}" age (${age}) and average mark (${averageMark})! Timestamp is: ${timestamp}.`)
	}
}

let exampleObject = {
	timestamp: 1600000000,
	data: [{
		age: 20,
		name: "John Smith",
		averageMark: 7.7,
		studentId: 0x6354,
	},{
		age: 19,
		name: "Bogdan Hmelnitskiy",
		averageMark: 8.7,
		studentId: 0x1596,
	},]
};

parseStudents(JSON.stringify(exampleObject), 2)