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

ti2c

v2.20240205.1

Published

a {t}yping, {i}mmuting, {i}nterning and {c}aching system for javascript

Downloads

7

Readme

ti2c

is a{T}yping, {I}mmuting, {I}nterning and {C}aching framework for Javascript ti2c is designed to facilitate robust coding and improve performance with caching avoiding deep copy or protective copying of data structures.

a minimal working example

Following code can be downloaded from https://gitlab.com/ti2c/ti2c/-/tree/examples.

src/Point.js

Defines point as a ti2c class having two attributes. Also a function to add to points is defined.

def.attributes =
{
    x: { type: 'number' },
    y: { type: 'number' },
};

def.proto.add =
    function( p )
{
    return Self.create( 'x', this.x + p.x, 'y', this.y + p.y );
};

Currently ti2c follows a one class per file rule.

src/Root.js

Not lets use the "Point class". Since this file does not define a class to b einstanced it is declared to be "abstract".

Also the import syntax ti2c uses to import another ti2c class can be seen using {} in the import string.

def.abstract = true;

import { Self as Point } from '{Point}';

def.static.run =
    function( )
{
    const p1 = Point.create( 'x', 1, 'y', 2 );
    const p2 = Point.create( 'x', 1, 'y', 2 );
    const p3 = Point.create( 'x', 2, 'y', 4 );
    console.log( 'equal?', p1 === p2 );
    console.log( 'equal?', p1 === p3 );
    console.log( 'equal?', p1.add( p2 ) === p3 );
};

src/Start.js

Finally a ti2c application needs a starting driver running in the default node context.

global.CHECK = true;
await import( 'ti2c' );
const pkg =
  await ti2c.register(
    'name', 'example',
    'meta', import.meta,
    'source', 'src/',
    'relPath', 'Start',
    'codegen', 'codegen/'
  );
const Root = await pkg.import( 'Root' );
Root.run( );

Now we can run the example. Note that currently --experimental-vm-modules is needed as node parameter, since ti2c uses the esm vm module.

$ node --experimental-vm-modules src/Start.js

The output is: equal? true equal? false equal? true

p1 added to p1 returns the object that is also p2.

lazy evaluation

ti2c supports lazy evaluation and caching of the results. Lets enhance previous Point function with two lazy evaluated attributes, first the length from Point 0/0 as "length" as well a distance to another point.

src/Point.js

def.attributes =
{
    x: { type: 'number' },
    y: { type: 'number' },
};

def.lazy.length =
  function( )
{
    console.log( 'calculating length...' );
    const x = this.x;
    const y = this.y;
    return Math.sqrt( x * x + y * y );
};

def.lazyFunc.distanceTo =
  function( p )
{
    console.log( 'calculating distance...' );
    const dx = this.x - p.x;
    const dy = this.y - p.y;
    return Math.sqrt( dx * dx + dy * dy );
};

and now again a test program:

src/Root.js

def.abstract = true;

import { Self as Point } from '{Point}';

def.static.run =
    function( )
{
    const p1 = Point.create( 'x', 1, 'y', 2 );
    const p2 = Point.create( 'x', 2, 'y', 4 );
    console.log( 'p1 length is', p1.length );
    console.log( 'p1 length is', p1.length );
    console.log( 'p1-p2 distance is', p1.distanceTo( p2 ) );
    console.log( 'p1-p2 distance is', p1.distanceTo( p2 ) );
};

the output is

calculating length...
p1 length is 2.23606797749979
p1 length is 2.23606797749979
calculating distance...
p1-p2 distance is 2.23606797749979
p1-p2 distance is 2.23606797749979