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

@tmurphree/validation-predicates

v2.0.4

Published

Functions that take some of the pain out of validating input

Downloads

4

Readme

validation-predicates

Why

Functions that take some of the pain out of validating input. Turns validation syntax into readable, easily-combinable functions. This makes you more likely to do it.

There are other great libraries out there that do similar things:

Please use them if you're looking for a different take on the same subject.

Change log

Link to changes

Installation

npm install --save @tmurphree/validation-predicates

Usage

const { 
  isNumberGreaterThan,
  isPopulatedArray,
  isString,  
} = require('@tmurphree/validation-predicates');

// the old way: not as readable
// const firstIsValid = function firstIsValid(x) {
//   return typeof x === 'number' &&
//     x > 5;
// };
//
// const secondIsValid = function secondIsValid(x) {
//   return Array.isArray(x) &&
//      x.length > 0 &&
//      x.every((el) => typeof el === 'string') &&
//      x.every((el) => el.length > 2);
// };

// the same tests using validation-predicates 
const firstIsValid = function firstIsValid(x) {
  return isNumberGreaterThan(x, 5);
};

const secondIsValid = function secondIsValid(x) {
  return isPopulatedArray(x) &&
    x.every(isString) &&
    // mix and match with predicates not in the package
    x.every((el) => el.length > 2);
};

const someFunction = function someFunction(first, second) {
  if (!(firstIsValid(first))) {
    throw new Error('Expected first to be a number > 5.');
  }

  if (!(secondIsValid(second))) {
    throw new Error('Expected second to be an array of strings longer than 2 characters.');
  }

  // amazing code goes here
};

someFunction(42, ['some','array']);

Strict mode

Some functions have options. Strict mode is a convenient way to set some of the options. You can always, in both modes, set the options manually.

Without strict mode:

const { isDate, isObjectLike } = require('@tmurphree/validation-predicates');

// isDate, which has no options, is not changed

// isObjectLike(a, b) is called with { checkType: false } by default
// but you can still call isObjectLike(a, b, { checkType: true })

With strict mode:

const { isDate, isObjectLike } = require('@tmurphree/validation-predicates').strict;

// isDate, which has no options, is not changed

// isObjectLike(a, b) is called with { checkType: true } by default
// but you can still call isObjectLike(a, b, { checkType: false })

Functions

  • All functions return boolean.
  • Parameters not in brackets are required.
  • Parameters in brackets are optional.
  • 'x' in the parameters list is the thing you want to test.

|Function|Summary|
|---|---|
|isArray(x)|x is an array|
|isBigInt(x)|x is a BigInt|
|isBoolean(x)|x is a boolean|
|isDate(x)|x is a Date|
|isDateAfter(x, someDate)|alias for isDateGreaterThan|
|isDateBefore(x, someDate)|alias for isDateLessThan|
|isDateGreaterThan(x, someDate)|x is a Date greater than (after) someDate|
|isDateLessThan(x, someDate)|x is a Date less than (before) someDate|
|isFloat(x)|x is a number (see isNumber) and has a nonzero decimal. e.g. 5.0 is NOT a float, but 5.01 is.|
|isFunction(x)|x is a function|
|isInteger(x)|x is a number (see isNumber) and has a zero-value decimal. e.g. 5 and 5.0 are both integers, but 5.01 is NOT.|
|isIsoDateTimeString(x)|x is a string and matches a subset of the ISO 8601 date time string format.Checks for these patterns (note that the plus can be plus or minus and that 'milliseconds' is 'one or more digits'):* YYYY-MM-DDThh:mm:ssZ* YYYY-MM-DDThh:mm:ss.millisecondsZ* YYYY-MM-DDThh:mm:ss+hh:mm* YYYY-MM-DDThh:mm:ss.milliseconds+hh:mm|
|isNull(x)|x is null|
|isNullOrUndefined(x)|x is null or undefined|
|isNumber(x)|x is a number and is not NaN|
|isNumberGreaterThan(x, anotherNumber)|x is a number and is greater than anotherNumber|
|isNumberLessThan(x, anotherNumber)|x is a number and is less than anotherNumber|
|isObject(x)|x is an object per the definition in 'typeof' EXCEPT that it returns false for null. Basically, this means it isn't another known type of primitive and is not null. See MDN.|
|isObjectLike(x, template, [options={ allowExtraProps: false, checkType: false, debug: false }])|x is an object and has exactly the same properties as template. x does not have properties not in template. Does not check symbols.Strict mode changes the default to { checkType: true }. Other defaults remain the same.Options: * allowExtraProps Return true if the object to test has properties not in template and other tests pass.* checkType If true, checks that the properties are the same type BUT this is rudimentary checking (with typeof). * debug If true, prints debug information to the console.|
|isObjectWithExpectedProps(x, arrayOfStrings)|x is an object and every string in arrayOfStrings is a property of x. x may have properties not in arrayOfStrings.|
|isPopulatedArray(x)|x is an array with at least one element|
|isPopulatedObject(x)|x is an object with at least one property or Symbol| |isPopulatedString(x)|x is a string with at least one character|
|isString(x)|x is a string|
|isSymbol(x)|x is a symbol|
|isUndefined(x)|x is undefined|
|isZeroLength(x)|x has a length property that === 0|