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

adts

v0.1.8

Published

Abstract Data Types in TypeScript

Downloads

13

Readme

adts

Abstract Data Types in TypeScript.

Also an exercise in figuring out best practice when distributing a TypeScript project via npm.

Concerns so far:

  • Would it be good to have an .npmignore containing src/*, Makefile, etc.?
  • Everything is packaged up in index.js; would that still be a good idea if it was a big project?

Example import configuration

If you have a plain JavaScript Node.js project, imports work as usual.

Download the adts package from the npm registry:

npm install --save adts

Then require and use:

var adts = require('adts');

var primes = new adts.Set([2, 3, 5, 7, 11, 13, 17, 19, 23]);
var even = new adts.Set([2, 4, 6, 8, 10, 12, 14, 16]);
var even_primes = adts.Set.intersection([even, primes]);
console.log('primes that are even: %j', even_primes);
// prints 'primes that are even: ["2"]'

If you have a TypeScript Node.js project, using tsd and DefinitelyTyped for most of your dependencies, imports are straightforward, but not idiomatic:

Download the adts package from the npm registry.

npm install --save adts

Assuming tsd is using the defaults, the aggregate d.ts file will be in typings/tsd.d.ts. Open that file, and add the following line:

/// <reference path="../node_modules/adts/adts.d.ts"/>

(The tsd documentation states that tsd link will handle that, but tsd link isn't actually a tsd command.)

Now importing the aggregate file will import the adts type declarations along with it. Suppose you want to use the adts module in index.ts:

/// <reference path="typings/tsd.d.ts"/>
import adts = require('adts');

var primes = new adts.Set([2, 3, 5, 7, 11, 13, 17, 19, 23]);
var even = new adts.Set([2, 4, 6, 8, 10, 12, 14, 16]);
var even_primes = adts.Set.intersection([even, primes]);
console.log('primes that are even: %j', even_primes);

Now try to compile:

tsc -m commonjs index.ts

Which produces the error message:

index.ts(4,27): error TS2345: Argument of type 'number[]' is not assignable to
  parameter of type 'string[]'. Type 'number' is not assignable to type 'string'.
index.ts(5,25): error TS2345: Argument of type 'number[]' is not assignable to
  parameter of type 'string[]'. Type 'number' is not assignable to type 'string'.

It turns out adts.Set only accepts strings in its constructor, since that's how it stores elements internally. The plain JS version automatically coerced them to strings, which is fine, but something we need to care about if we're using TypeScript. Rewrite the sets like so:

var primes = new adts.Set(['2', '3', '5', '7', '11', '13', '17', '19', '23']);
var even = new adts.Set(['2', '4', '6', '8', '10', '12', '14', '16']);

Now recompile (tsc -m commonjs index.ts) and run:

node index.js
// prints 'primes that are even: ["2"]'

TODO

  • Container
  • Deque
  • Graph
  • List
  • Map
  • Map/Associative array/Dictionary
  • Multimap
  • Multiset/Bag (partially done)
  • Priority queue
  • Queue
  • Set (done)
  • Stack (done)
  • Tree

... more?

Build notes

TypeScript does not make inter-project modularity easy. Here are some features that I'd like tsc to support.

  1. Generate a .d.ts type declaration for use by 3rd-party modules.

As it stands, I'm not sure what the point of the declaration file is, since if you're generating a declaration file that only allows TypeScript-powered imports, you have to TypeScript-import the original modules as well, which tsc is then obliged to compile.

Fixing this can be pretty simple for a small project: sed 's/declare module adts/declare module "adts"/g'

  1. Merge module declarations in the generated .d.ts file.

TypeScript (as least v1.1) does not like having multiple external module declarations in one file, e.g., 'module "adts" { ... } module "adts" { } ...'. Otherwise, it's quite happy at merging interfaces and modules, but only reads one external module declaration when there are multiple. (Maybe this is fixed in v1.3?)

  1. Export a specific module in the generated .d.ts file, and expose that module at root level in the generated .js file(s).

In a typical Node.js module, often the only place the module name shows up is in the package.json and documentation. I like using namespaces, but TypeScript's compiler does not use them in a way that plays nice with Node.js's import idioms.

Implementation hacks

The Makefile demonstrates how I've hacked together support for these features, at least for this admittedly simplistic project.

  • All JavaScript code is exported in a single index.js file.
  • All type declarations are exported in adts.d.ts. This filename is determined by the Makefile, not the project structure or TypeScript namespaces.
  • I can add TypeScripts files to src/, and these will automatically get picked up by the make process. (But adding files to subdirectories would require further hacks.)

License

Copyright 2014-2015 Christopher Brown. MIT Licensed.