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

falitjs

v0.1.0-2

Published

Static-typing and optional parameters in native JavaScript.

Downloads

10

Readme

Falit.js

Downloads License MIT NPM Version

Static-typing and optional parameters in native JavaScript.

npm install falitjs
var falit = require('falitjs'),
    req = falit.required,
    opt = falit.optional,
    _ = require('underscore');

// configure falit settings
falit.settings({
    debug: true,
    throwErrors: false
})

// common usage pattern
var add = falit.binder(
        req.int, // a 
        req.int, // b
        function(a, b) {
        
            return a + b;
        });

add(5, 5)

// template usage pattern
var tmpl = falit.template(req.int, req.int, opt.func(console.log)),

    mul = tmpl.for(function(a, b, callback){ return callback(a * b)}),
    add = tmpl.for(function(a, b, callback){ return callback(a + b)});

var dbl = falit.binder(req.num, function(x){ return x * 2});

var add5 = _.partial(add, 5);

add(5, 5)
add(5, 5, add5)
mul(5, mul(5, 5, dbl))

Returns

10
10
15
250
[Finished in 0.0s]

todo

  • unit tests (nodejs)
  • remove underscore.js (underscorejs.org) dependency
  • multiple, valid, types: falit.binder(oneOf(req.hex, req.posNum), ...)
  • performance benchmarks
  • ensure nodejs + webkit compatibility
  • ensure crossbrowser compatibility

API

###require('falitjs')

.binder([required parameters,] [optional parameters,] function)

Available Types
  • any
  • args
  • array
  • bool
  • char
  • date
  • element
  • finite
  • float
  • func
  • hex
  • int
  • NaN
  • negNum
  • null
  • num
  • posNum
  • obj
  • regex
  • str
  • undefined

.optional.type[(defaultValue)]

.required.type[(validationFn)]

Used as place holder objects in .binder and .template

[opt|req].availableType will reserve the parameter slot for that given type.

falit.binder(required.int, required.obj, optional.func, ...)

Calling the placeholder will allow one of two things to happen:

  • optional.availableType(..value of type..) will initiate a default value when omitted.
  • required.availableType(req.func) will pass the supplied varable through a custom validation.

Example:

var add5 = falit.binder(opt.int(0), function(a){ return a + 5; });

>>> add5()
5

>>> add5(10)
15

Example:

var checkStr = function(s){ return s.length < 10; }
var shortString = falit.binder(req.str(checkStr), function(s){ console.log(s); })

>>> shortString('blake')
blake

>>> shortString('blake vandemerwe')
Failed validation,
  Supplied value did not pass
  Expected: [str], position: 0

null

Example:

var realExample = falit.binder(
        opt.int(0),
        opt.obj({debug: false}),
        req.func,
        function(delaySec, options, callback) {
            callback(null, [delaySec, options]);
        }
    )

>>> realExample(console.log)
null [ 0, { debug: false } ]

>>> realExample(10, console.log)
null [ 10, { debug: false } ]

>>> realExample({debug: true}, console.log)
null [ null, { debug: true } ]

The following fails, because the first argument doesn't match the first two optionals and then is validated against req.func.

>>> realExample(1.5, {debug: true}, console.log) 

Error: Invalid type,
  Expected: [func], position: 2
  Received: [any,finite,float,num,posNum], value: 1.5

Changing the realExample template to opt.any, opt.obj({debug: true}), req.func fails in the following example because the callback console.log was matched against opt.any and we're thrown an error that says we're missing a required argument.

>>> realExample(console.log)

Error: Missing required [func], in position: 1

           position: 1 --------v
>>> realExample(console.log, _____)

>>> realExample(console.log, console.log)
null [ [Function], { debug: true } ]

>>> realExample({}, {}, console.log)
null [ {}, {} ]

.settings(req.obj)

  • debug, default: false
    • Determines whether debug messages should be printed to console or not.
  • enabled, default: true
    • Enable type-checking; disabling is useful in production environment to reduce overhead.
  • throwErrors, default: true
    • throw new Error on violation, when set to false execution integrity is unknown.

.template([required parameters,] [optional parameters])

.template(..) works like .binder(..) except that the last parameter is not treated as the function being bound to. Instead, you can construct a pattern of required and optional parameters that can be used in multiple instances for similar functions.

Example:


var twoInts = falit.template(req.int, req.int);

var add = twoInts.for(function(a, b){ return a + b; }),
    mul = twoInts.for(function(a, b){ return a * b; });

>>> add(5,5)
10

>>> mul(5,5)
25

>>> add('a', 5)
Invalid type,
  Expected: [int], position: 0
  Received: [any,char,str], value: a

>>> add(1.5, 1.5)
Invalid type,
  Expected: [int], position: 0
  Received: [any,finite,float,num,posNum], value: 1.5
.for(req.func)

Applies a required function to a template.

.whatIs(req.any)

Returns the meta-data associated with a given parameter. Useful for checking what types a known value will pass validation on.

Example:

>>> falit.whatIs([1,2,3])
{ posTypes: [ 'any', 'array' ], 
  bitMask: 5, 
  value: [ 1, 2, 3 ] }

>>> falit.whatIs(-1.5)
{ posTypes: [ 'any', 'finite', 'negNum', 'num' ],
  bitMask: 41089,
  value: -1.5 }

>>> falit.whatIs(undefined)
{ posTypes: [ 'any', 'undefined' ],
  bitMask: 1048577,
  value: undefined }

>>> falit.whatIs(Infinity)
{ posTypes: [ 'any', 'num', 'posNum' ],
  bitMask: 98305,
  value: Infinity }

>>> falit.whatIs(/\S+/)
{ posTypes: [ 'any', 'obj', 'regex' ],
  bitMask: 393217,
  value: /\S+/ }

Contributors

Blake VandeMerwe