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

jsigs

v1.1.0

Published

A type evaluation library.

Downloads

85

Readme

JSIGS

Badges/Status

Travis

Build Status

npm Version

npm version

Coveralls?? we need to get istanbul running with testem.

npm big badge

NPM

Concept

Coming from a strongly typed language I wanted to learn and know the typing in javascript. So I wrote the code to interpret it and made it available. I also never liked that the typeof operator led to string compare.

Finally I liked how you could just write an object inline in javascript and I saw that the best way to convey the signature of the object.

History

This is my first released node module param-signatures.

I put together my first node module with tests and uploaded it but it did not draw any use.

When I investigated changing it into a js library for the web it looked like a complete rewrite so I started a new repository.

API

In the browser a global jsigs is loaded. In node you require('jsigs'); and then use it.

jsigs.CODES = {
  BOOLEAN: 0,
  NUMBER: 1,
  STRING: 2,
  FUNCTION: 3,
  OBJECT: 4,
  UNDEFINED: 5,
  NULL: 6,
  DATE: 7,
  ARRAY: 8  
},
jsigs.getTypeCode = function(value) {}

jsigs.isTypeCode = function(value, typeCode) {}

jsigs.typeCodeToString = function(typeCode) {}

jsigs.validateFunction = function(value, parameterCount) {}

jsigs.validateArray = function(array, typeCode) {}

jsigs.validate = function(object, signature) {}

jsigs.mergeAndReturn = function(object, defaults) {}

jsigs.validateListData = function(list, childSig) {}

CODES

An enum or lookup of all the int values that represent the basic types in Javascript. This is because an int compare is one of the fastest hardware operation.

jsigs.CODES = {
  BOOLEAN: 0,
  NUMBER: 1,
  STRING: 2,
  FUNCTION: 3,
  OBJECT: 4,
  UNDEFINED: 5,
  NULL: 6,
  DATE: 7,
  ARRAY: 8  
}

getTypeCode

Returns a typecode that will match one of the code in jsig.CODES.

// Usage
var options = {};
var typeCode = jsigs.getTypeCode(options);
if (typeCode === jsigs.CODES.OBJECT) {
  doWork(options);
}

isTypeCode

Returns a boolean if the typecode provided matches the value.

// Usage
var options = {};
if (jsigs.isTypeCode(options, jsigs.CODES.OBJECT)) {
  doWork(options);
}

typeCodeToString

Converts an integer typecode into a string.

// Usage
function exposedApiCall(options) {
  var typeCode = jsigs.getTypeCode(options);
  if (typeCode !== jsgigs.CODES.OBJECT) {
    throw new Error('I need an ' + jsigs.typeCodeToString(jsigs.CODES.OBJECT) + ' and you passed me a ' + jsigs.typeCodeToString(typeCode));
  }
}

validateFunction

Returns a boolean if the value passed is a function and if the parameter counts match.

// Usage
function specialFunction(parameters) {
  if (parameters.onComplete  && jsigs.validateFunction(parameters.onComplete, 2)) {
    var result = doWork();
    parameters.onComplete(0, result);
  }
}

validateArray

Returns a boolean (True if successful; False otherwise) if the value passsed in is a list and all its members are the typeCode parameter passed in.

// Usage
function doSomethingCrazy(parameters) {
  if (jsigs.validateArray(parameters,jsigs.CODES.STRING)) {
    processStrings(parameters);
  }
}

validate

Throws an exception if the object passed does not match the signature object passed.

// Usage
function doWorkWithComplexOptions(params, options) {
  // this throws if options does not have these 3 members with their types.
  jsigs.validate(options, {
    log: false,
    nesting: 10,
    context: 'A string'
  });

  if (options.log) {
    console.log('Options passed', options);
  }
}

mergeAndReturn

Returns the original object if the signature matches or merges in the defaults provided in the signature.


function doWork(parameters, options) {
  var defaults = {
    verbose: false,
    onComplete: function(e) {}
  };

  var finalOpts = jsgis.mergeAndReturn(options, defaults);
  // finalOpts will have the orignal value for verbose passed in
  // and the default empty callback function

  if (finalOpts.verbose) {
    console.log('options to use', finalOpts);
  }

  finalOpts.onComplete(10);
}

doWork({ one:1, two:2, three: 3}, {
  verbose: true,
});

validateListData

Returns a boolean indicating if all the data in the array matches the child signature passed.

// Usage

var array = [
  { important: true, value: 3.14, name: 'Little PI' },
  { important: true, value: 2.718, name: 'Euler\'s Number' },
  { important: true, value: 299792458, name: 'Speed of Light' },
  { important: false, value: 115, name: 'Days until my birthday' },
];

var childSig = {
  important: false, // boolean
  value: 10, // NUMBER
  name: 'A name' //STRING
}

if (jsigs.validateList(array, childSig)) {
  array.forEach(function((item) {
    console.log(item.name);
    console.log('Important? ' + item.important);
    console.log('Times 2', item.value * 2);
  });
}

Technology

I used the following packages...

  • jasmine for node testing.
  • Testem for browser testing...(I never got karma off the ground).
  • Handlebars for merging javascript source files.
  • Cmder for command line execution.
  • Atom for code editing.

I program on a Windows 10 box so if you find a bug specific to platform I will try to enlist you.

TODO

  • Minimize with version jsigs.1.0.0.min.js

  • Get testem runnign with istanbul linky issue linky

  • Try to get node coverage and combine them. linky