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

al-compiler

v2.0.1

Published

Function factory and generator

Downloads

9

Readme

al-compiler

Could we add macro to javascript or something similar ? Could we generate function, or module at runtime and use them ?

"May it was not a good practice, but so funny" :)

So, use it only if you known what you do, and may you shouldn't use this in production...

so, let's go to the dark side of the force.

How to generate function/module

We can do it (yes !) by :

  • use a template system: Mustache
  • call this template with parameters in order to generate our business

The result will be our complete module definition, or a single function definition.

If we generate an object or a function, we can add an export directive like this one:

'use strict';
module.exports = YOUR_GENERATED_CODE

At this point, we have generate some code, how to dynamicaly load it in our application ?

Old way

  • save it to a temporary file (at build time, or at runtime)
  • make a 'require' on it

Cons:

  • file system based
  • no context isolation

Pro:

  • simple to use
  • module is used in classic way (single init, ...)

A better way

  • use 'vm' module from node js.
  • use context definition to exchange data and code between caller/target

Cons:

  • if singleton is needed, we have to implement it.

Example with a simple function

Goal: generate a filter function like this one bellow, with a dynamic list of properties to test.

With 'A' and 'B':

(e) => true && !!e['A'] && !!e['B'];

With 'A' and 'B' and 'C':

(e) => true && !!e['A'] && !!e['B'] && !!e['C'];

So, by using this template:

(e) => true{{#tags}}  && (!!e['{{.}}']){{/tags}};

and call template with a array of tags we're done.

Sample code:

const ALC = require('al-compiler');

const TEMPLATE= "(e) => true{{#tags}}  && (!!e['{{.}}']){{/tags}};";

// fn is a function which return true if e has 'a' and 'b' property.
const fn = ALC.fnFactory(TEMPLATE, {tags: ['a', 'b']})();

const a = {a:1, b:2};
const b = { c: 1};

console.log("a: " + fn(a)); // ==> true
console.log("b: " + fn(b)); // ==> false

Behind the scene this tool:

  • use mustache to create template function
  • call this template with view of {tags: ... }
  • load it in your current context

Install

npm install al-compiler

API

See documentation

This module exports :

  • pop: instanciate a module from source code
  • fnFactory: a function to generate a set of function based on same template/view
  • Mustache: mustache instance what else.

Change log

2.0.1

  • add test unit with require directive

2.0.0

  • use mustache has template engine (no dependencies, more functionality, ...)
  • use node vm module to instance code
  • new API

1.0.1

  • expose internal method: compile, cleanup and addModuleExportDirective
  • bug fix on generate: don't add export if exportCheck === false

1.0.0

  • initial API : instanciate, generate, instanciateFromCode