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

keywordsjs

v1.2.7

Published

A library that offers custom keywords in JS without compilers and transpilers.

Downloads

13

Readme

Keywords.js 🚀

A lightweight, zero dependency package to make custom keywords! Yes you understood that right, custom keywords! Well not real keywords, but emulated ones. This packge does it by defining global getters and setters. The no-argument keywords look very natural. For example you can implement die or use the computed propery style

die; // process.exit(0) for Node.js or throw new Error("exited") for Browsers

myComputedProperty; // Calls your function and returns anything you want

The argument-taking keywords look more weird. because of they are setters you need to use the assignment (=) operator to invoke the keyword. It looks more keywordy when you split the keyword name and the = sign (e.g save= value instead of save = value). Even more, setters can't return a custom value, so you have to use the comma (,) operator and the __value global variable. For example let's imagine a save keyword which takes an argument.

save= myValue // Invokes your function and passes "myValue" in

save= myValue, __value // Does the same, but returns the value you returned from your function

Thats the theory, lets dive in to practice!

Installation 💿

npm i keywordsjs for NodeJS <script src="https://cdn.jsdelivr.net/npm/keywordsjs@latest/keywords-browser.min.js"></script> for browsers.

Documentation 🔍

Well, this package is pretty easy, so some commented examples are enought to understand how it works.

// Defining the "die" keyword

const Keywords = require("keywordsjs"); // For Node.js

Keywords.define(`die`, {
    call() {
        process.exit(0); // For node
        throw new Error("Exited") // For browsers
    }
})

someCode...
die; // This will work

Using the state API

Keywords.define(`increment`, {
    init() { // this is binded to the keyword object
        this.state.value = 0;
    },

    call() {
        return this.state.value++;
    }
})

for(let i = 0; i < 10; i++) {
    console.log(increment);
}
// 0, 1, 2, 3... 9

You can dynamically attach and detach keywords:

const justdoitKeyword = Keywords.define(`justdoit`, {/* Define some keyword */})

justdoit // works

// Removes the keyword
justdoitKeyword.detach(); // OR Keywords.detach(`justdoit`)

justdoit // Error: justdoit not defined

// Puts it back
justdoitKeyword.attach();

justdoit // works

Multiple defining and detaching is also supported

Keywords.defineMultiple([
    {name: "die", config: {/* keyword config */}},
    {name: "justdoit", config: {/* keyword config */}}
])

Keywords.detachMultiple(["die", "justdoit"]) // Multiple attaching not supported

Defining argument-taking keywords

Keywords.define("reverse", {
    call() {
        throw new SyntaxError("reverse requires an argument")
    },

    callWithArgument(array) {
        array.reverse();
    }
})

let array = [1, 2, 3];
reverse= array;
console.log(array); // [3, 2, 1]

Defining multiple-argument-taking keywords is also possible, using arrays

Keywords.define("reverse", {
    call() {
        throw new SyntaxError("reverse requires an argument")
    },

    callWithArgument(arrays) {
        for(let i in arrays) {
            arrays[i].reverse;
        }
    }
})

let arrays = [[1,2,3], [4,5,6], [7,8,9]];
reverse= arrays;
console.log(arrays); // [[3, 2, 1], [6, 5, 4], [9, 8, 7]]

Returning values from argument-taking-keywords

Keywords.define("tohex", {
    call() {
        throw new SyntaxError("tohex requires an argument")
    },

    callWithArgument(arrays) {
        // some code
        return value; // pushes to __value stack
    }
})

tohex= 365 // returns 365
tohex= 365, __value // returns your value, but it's not used
const hex = tohex= 365, __value // returns your value and it's used

That's it, Thanks!