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

@norjs/utils

v1.0.18

Published

Common utils for ES6 and Node.js

Downloads

53

Readme

@norjs/utils

Common almost-non-dependable(*) utils for ES6 and Node.js, with MIT license.

(*) Only runtime dependency is to Lodash.


Install stable from NPM

npm install @norjs/utils

Install latest from Github

npm install norjs/utils


TypeUtils

Runtime JSDoc style type asserting and testing.

const TypeUtils = require('@norjs/utils/Type');

TypeUtils.assert(value, type)

This function throws a TypeError if value is not of the type definition.


// Basic types
TypeUtils.assert(          "123", "string");
TypeUtils.assert(            123, "number");
TypeUtils.assert(           true, "boolean");
TypeUtils.assert(          false, "boolean");
TypeUtils.assert(       () => {}, "function");
TypeUtils.assert(      undefined, "undefined");
TypeUtils.assert(           null, "null");

// Objects
TypeUtils.assert(             {}, "{}");
TypeUtils.assert(             {}, "Object");
TypeUtils.assert(   {foo: "bar"}, "Object.<string,string>");
TypeUtils.assert(    {foo:"bar"}, "{foo:string}");

// Arrays
TypeUtils.assert(      [1, 2, 3], "Array");
TypeUtils.assert(["1", "2", "3"], "Array.<string>");
TypeUtils.assert(["1", "2", "3"], "string[]");

// Multiple types
TypeUtils.assert("false", "boolean|string");

// Intersections
TypeUtils.assert( {"foo": 1, "bar": 2}, "{bar:number} & {foo:number}");

TypeUtils.test(value, type)

This function returns false if value is not of the type definition.

if (TypeUtils.test("123", "string")) {
    // ...
}

TypeUtils.defineType(name, type)

You can define your own JSDoc typedef definitions like this:

/** 
 * @typedef {string} MyStringType
 */
TypeUtils.defineType("MyStringType", "string");
TypeUtils.assert("bar", "MyStringType");

/** 
 * @typedef {Object} MyFooType
 * @property {string} foo - The foo property
 */
TypeUtils.defineType("MyFooType", {"foo": "string"});
TypeUtils.assert({foo:"bar"}, "MyFooType");

...and your classes like:

class Foo { 
    // ...
}
TypeUtils.defineType("Foo", TypeUtils.classToTestType(Foo));

...and your interfaces like:

/**
 * @interface
 */
class FooInterface {
    // ...    
}
TypeUtils.defineType(
  "FooInterface", 
  TypeUtils.classToObjectPropertyTypes(FooInterface),
  {
    acceptUndefinedProperties: true // Accept test subject to have properties not defined in FooInterface
  }
);

TypeUtils.toString(value)

Returns a human readable string presentation of value.


TypeUtils.isPromise(value)

Returns true if value is a promise (eg. it is an object and has then method).


LogicUtils

const LogicUtils = require('@norjs/utils/Logic');

LogicUtils.tryCatch(f, handleError)

Will call function f inside a try-catch block.

If an exception is thrown, the handleError(error) function will be called with an error object.

The result from f() will be returned, except if an error occurs, then the result from handleError(error).

Note: Some JS environments (eg. some versions of Google v8) will turn off performance optimizations and fallback in to the slower interpreter mode when a try-catch block is detected for the whole execution block. However, this function may be slower than try-catch block on modern environments.