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

straits-babel

v2.0.0

Published

Babel straits syntax plugin (babel7)

Downloads

6

Readme

Straits Babel

A babel7 parser and plugin implementing the straits syntax.

Note: the babel6 version is available on the babel6 branch (straits-babel@^1.x.x).

Installation

npm install --save-dev straits-babel

Usage

Write the following to babel.config.js in your project folder (or see other babel's config options):

module.exports = function( api ) {
	api.cache.forever();
	return {
		"plugins": [
			{ parserOverride:require('straits-babel').parse },
			"straits-babel/plugin.js",
		]
	};
};

Then use babel, babel-node or equivalent to transpile the straits syntax into valid JavaScript.

Straits syntax

The straits syntax transpiles the use straits statement and .* expression. These ease the usage of traits, implemented as symbol properties (see the Iteration protocols for an example of symbol properties implemented as traits in the ECMAScript standard).

The straits syntax offers several advantages over its alternatives:

  • it makes the code easier to write, read and understand,
  • it makes sure that the traits you access are offered by exactly one trait set you're using,
  • it doesn't pollute nor conflict with the normal scope variables.

Examples

Let's see a minimal example:

use traits * from Symbol;
[].*iterator();
// is equivalent to...
[][Symbol.iterator]();

Let's look at a more complete one:

const {TraitSet} = require('straits').utils;
const traitSet1 = new TraitSet('duplicatedTrait', 'trait1');
const traitSet2 = new TraitSet('duplicatedTrait', 'trait2');

// an object we're going to assign traits to
const object = {};

// static error:
//   .* used outside a `use traits` scope.
//object.*trait1 = {};

use traits * from traitSet1;
use traits * from traitSet2;


// the following variables won't be used.
// they're here to show that variables won't interfere with traits
const duplicatedTrait = {}, trait1 = {}, missingTrait = {};

// the following line would throw an exception:
//   No trait set is providing symbol missingTrait.
//object.*missingTrait = {};

// the following line would throw an exception:
//   Symbol duplicatedTrait offered by multiple trait sets.
//object.*duplicatedTrait = {};

// the following lines are working as expected
object.*trait1 = `a trait`;
object.*trait2 = function() {
	console.log( `Greetings from ${this.*trait1}` );
}
object.*trait2(); // prints `Greetings from a trait`