All Projects → wookieb → Predicates

wookieb / Predicates

Licence: mit
Predicates for type checking, assertions, filtering etc

Programming Languages

typescript
32286 projects

Projects that are alternatives of or similar to Predicates

Tl
The compiler for Teal, a typed dialect of Lua
Stars: ✭ 716 (+704.49%)
Mutual labels:  type-checking
Keras Sincnet
Keras (tensorflow) implementation of SincNet (Mirco Ravanelli, Yoshua Bengio - https://github.com/mravanelli/SincNet)
Stars: ✭ 47 (-47.19%)
Mutual labels:  filtering
Queryql
Easily add filtering, sorting, and pagination to your Node.js REST API through your old friend: the query string!
Stars: ✭ 76 (-14.61%)
Mutual labels:  filtering
Quiver
Validation, searching and filtering made easy for swift.
Stars: ✭ 27 (-69.66%)
Mutual labels:  filtering
Bazel Mypy Integration
🐍🌿💚 Integrate MyPy type-checking into your Python Bazel builds
Stars: ✭ 40 (-55.06%)
Mutual labels:  type-checking
Rbs
Type Signature for Ruby
Stars: ✭ 1,067 (+1098.88%)
Mutual labels:  type-checking
Filterizr
✨ Filterizr is a JavaScript library that sorts, shuffles and filters responsive galleries using CSS3 transitions ✨
Stars: ✭ 546 (+513.48%)
Mutual labels:  filtering
Dilate
Nearly zero runtime object allocation powered by scalameta. Value class and Unboxed Tagged Type generation at compile-time.
Stars: ✭ 80 (-10.11%)
Mutual labels:  type-checking
Openimager
Image processing Toolkit in R
Stars: ✭ 45 (-49.44%)
Mutual labels:  filtering
Easygrid
EasyGrid - VanillaJS Responsive Grid
Stars: ✭ 77 (-13.48%)
Mutual labels:  filtering
Fastp
An ultra-fast all-in-one FASTQ preprocessor (QC/adapters/trimming/filtering/splitting/merging...)
Stars: ✭ 966 (+985.39%)
Mutual labels:  filtering
Flatlist React
A helpful utility component to handle lists in react like a champ
Stars: ✭ 34 (-61.8%)
Mutual labels:  filtering
Feedly Filtering And Sorting
Enhance the feedly website with advanced filtering and sorting capabilities
Stars: ✭ 73 (-17.98%)
Mutual labels:  filtering
Sincnet
SincNet is a neural architecture for efficiently processing raw audio samples.
Stars: ✭ 764 (+758.43%)
Mutual labels:  filtering
Elixir Type check
TypeCheck: Fast and flexible runtime type-checking for your Elixir projects.
Stars: ✭ 80 (-10.11%)
Mutual labels:  type-checking
Openmessaging Java
OpenMessaging Runtime Interface for Java
Stars: ✭ 685 (+669.66%)
Mutual labels:  filtering
Distil
💧 In memory dataset filtering, inspired by snikch/aggro
Stars: ✭ 49 (-44.94%)
Mutual labels:  filtering
Imscript
a collection of small and standalone utilities for image processing, written in C
Stars: ✭ 86 (-3.37%)
Mutual labels:  filtering
Drep
dynamic regular expression print
Stars: ✭ 80 (-10.11%)
Mutual labels:  filtering
Dms Filter Bundle
Provides a FilterService for Symfony to allow users to implement input filtering in entities using Annotations
Stars: ✭ 74 (-16.85%)
Mutual labels:  filtering

Predicates

Build Status NPM version Coverage Status

Set of various predicates for type checking, simple assertions, filtering etc.

Features

  • written in typescript (with type guards and function overloading)
  • well defined API design principles
  • predicates description every predicate contains a proper description for easier debugging and automatic assertion generation
  • supports simple type checks with unnecessary verbosity
  • every predicate supports currying if possible

Install

npm install predicates

Full API

Example

const is = require('predicates');

is.string(1); // false
is.string('test'); // true

is.undefinedOr(String, undefined); // true
is.undefinedOr(String, 'string'); // true
is.undefinedOr(is.string, undefined); // true
is.undefinedOr(is.string, 'timmy'); // true
is.undefinedOr(is.string)(undefined); // true
is.undefinedOr(is.string)('timmy'); // true

const isPerson = is.structure({
	name: is.string,
	surname: is.undefinedOr(is.string),
	age: is.number
});

isPerson({name: 'Tom', age: 10}); // true
isPerson({surname: 'Welling', age: 100}); // false, lack of name property
const assertName = is.assert(is.string);
const assertSurname = is.assert(is.notBlank);
const assertAge = is.assert(is.undefinedOr(is.positive));

const Person = function(name, surname, age) {
    assertName(name);
    assertSurname(surname);
    assertAge(age);
}

new Person('Tom', 'Welling', 33); // OK!
new Person('Tom', 'Welling'); // OK!
new Person('Tom', '', 33); // Error: Surname must be a string and cannot be emptye

API design

Generated predicates can be called with more than one argument

Most of type checking, utility libraries force you to use predicates with only one argument but predicates doesn't. Additionally, predicates preserves the context of function calls which allows you to create even more powerful logic.

const is = require('predicates');

const isOkToModifyTags = is.all(
    is.arrayOf(is.string), 
    function(tags, previousTags) {
        // no need to save them again if they are the same as previous ones
        return tags.join(',') !== previousTags.join(',');
    }
);

Module.prototype.modifyTags = function(entityId, tags) {
    var previousTags = getTags(entityId);
    if (isOkToModifyTags(tags, previousTags)) {
        this.saveTags(entityId, tags);
    } else {
        // no need to save them again if they are the same as previous ones
    }
}

Prevents stupid mistakes (fail fast)

Predicates checks whether generated a predicate is misconfigured and throws an error as fast as possible.

is.startsWith(''); // Error: Prefix cannot be empty
// since that would be true for all strings

is.in([]); // Error: Collection cannot be empty
// always false

That's why it's a good practice to create predicates at the beginning of a module definition to quickly catch any mistakes.

// some module
const is = require('predicates');
const isImage = is.in([]); // Error: Collection cannot be empty

// You don't need to run the whole application to get the error that your predicate is wrong
export class Module {
    
}

Defined and generated predicates will never throw any error

You don't need to check the arguments provided to predicates to make sure they won't cause an error - predicates does it for you.

const isDuck = is.hasProperty('quack');

isDuck(undefined); // false - no error
isDuck(1); // false - no error
isDuck('duck'); // false - no error

is.matches(/.*\.ts/)(100); // false - no error

NOTE! This rule applies only for predicates defined in the library. Any user-defined predicate MAY throw errors (however I don't advise to do so) and predicates will not catch them.

const assertName = is.all(is.string, function(value) {
    if (value === 'admin') {
        throw new Error('Admin is a reserved user name');
    }
});

assertName('admin'); // Error: Admin is a reserved user name

Type guards

Every predicate (if possible) is a type guard

if (is.string(value)) {
   // at this point typescript compiler knows that value is a string 
}

Core types are automatically translated to proper predicate

is.property('foo', is.string)({foo: 'bar'}); // true

// for less verbosity this is possible as well
is.property('foo', String)({foo: 'bar'}); // true
is.arrayOf(String)(['foo']); // true
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].