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

minim-typed

v0.0.2

Published

Annotating Types for Minim

Downloads

6

Readme

Minim Typed

This library is for annotating functions using Refract. The idea is the provide types for both the inputs and outputs for a given function and check those types when the function is executed.

Install

This library is a plugin for Minim and requires Minim to be installed.

npm install minim-typed

Loading Plugin

This plugin is loaded like any Minim plugin would be loaded.

var minim = require('minim');
var minimTyped = require('minim-typed');

// Load the plugin
var namespace = minim.namespace().use(minimTyped);

Usage

All functions for typed Minim are added to the namespace instance.

Checking a Value

The default function is the identity function. This means if you want to check the type of a value, you need to annotate both the input and output of that value as being the same type. Once this is done, you can check the type of simple values.

var stringChecker = namespace.typed.build({
  // string -> string
  annotations: [
    annotate('string'),
    annotate('string'),
  ],
});

stringChecker('foobar'); // Returns 'foobar'
stringChecker(1000); // Throws TypeError

Annotating Functions

Typed Minim is only used for annotating functions, and though the identity function is default, you may provide any function you like.

var sum = namespace.typed.build({
  // array[number] -> number
  annotations: [
    annotate('array', [annotate('number')]),
    annotate('number'),
  ],

  fn: function(numbers) {
    return numbers.reduce(function(total, number) {
      return total + number;
    });
  },
});

sum([1, 2, 3]); // Returns 6
sum(['a', 'b', 'c']); // Throws TypeError

Advanced Example

This solves question one of Project Euler with typed functions.

Find the sum of all the multiples of 3 or 5 below 1000.

var _ = require('lodash');
var minim = require('minim');
var minimTyped = require('minim-typed');
var annotate = require('minim-typed').annotate;

var namespace = minim.namespace().use(minimTyped);

var divBy = namespace.typed.build({
  // number number -> boolean
  annotations: [
    annotate('string'),
    annotate('string'),
    annotate('boolean'),
  ],

  fn: function(x, y) { return (x % y) === 0; }
});

var divBy3or5 = namespace.typed.build({
  // number -> boolean
  annotations: [
    annotate('number'),
    annotate('boolean'),
  ],

  fn: function(x) {
    var result = divBy(x, 3) || divBy(x, 5);
    return result;
  }
});

var sum = namespace.typed.build({
  // array[number] -> number
  annotations: [
    annotate('array', [annotate('number')]),
    annotate('number'),
  ],

  fn: function(numbers) {
    return numbers.reduce(function(total, number) {
      return total + number;
    });
  }
});

var take = namespace.typed.build({
  // number -> array
  annotations: [
    annotate('number'),
    annotate('array'),
  ],

  // Just wrapping Lodash's range function
  fn: _.range
});

var answer = sum(take(1000).filter(function(number) {
  return divBy3or5(number)
}));

console.log(answer);

License

This code is licensed under the MIT license.