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

script-type-js

v1.0.5

Published

Dynamic typed js code

Downloads

3

Readme

Script Type JS

Dynamic JS typed code, using an inheritance prototype and replacing class properties and methods with a validation arguments function. Generates errors with wrong type information and crashes front-end UI

Installation

Install Script Type JS with npm

  npm i script-type-js

Activation

Configuration file

// script_type.config.js
import ScriptTypeJS from "script-type-js";

// mode = 'dev' activate type checking and the errorMode behaviour, other value deactivate
// errorMode = 'frontend' destroy DOM and show error messages on the console and the document.body, other value for only console display
ScriptTypeJS.mode = 'dev';
ScriptTypeJS.errorMode = 'frontend';

export default ScriptTypeJS.apply();

Main file of our project

Just import our configuration file :

import ScriptTypeJS from "./script_type.config";

Usage/Examples

Enums

import { Enum } from "script-type-js";

const Sex = new Enum({
    male: 1,
    female: 2
});

Personalized types

// Object Type
const TPerson = T({ firstname: String, lastname: String, age: Number });

// Multi native types
const TAvailableType = T({String, Number});

Class

import ScriptTypeJS, { Enum } from "script-type-js";

Sex = new Enum({
    male: 1,
    female: 2
});

 class Person {    
    set sex(value = Sex) { this._sex = value; }
    get sex() { return this._sex; }

    get fullname() { return this._fullname; }
    set fullname(value) { this._fullname = value; }

    constructor() {
        this._fullname = '';
        this.firstname = '';
        this.language = Language;
        this.lastname = T({ String, Number });
    }

    // Function setFullName accept multitype for argument lastname
    setFullName(firstname = String, lastname = { String, Number }) {
        this.firstname = firstname;
        this.lastname = lastname;
        this._fullname = firstname + ' ' + lastname;
    }

    setSexe(sex = Sex) {
        this.sex = sex;
    }

    chooseLanguage(language = Language) {
        this.language = language       
    }

    apply(p = Person) {
        this.fullname = p.fullname;
        this.firstname = p.firstname;
        this.lastname = p.lastname;
    }

    static create(fullname = String('John Doe')) {
        const p = new Person();
        p.fullname = fullname;
        const names = fullname.split(' ');
        p.firstname = names[0];
        p.lastname = names[1];
        return p;
    }
}

// Declare to Script Type
ScriptTypeJS.declare({ Sex, Person });

Class with interface in function argument

class SayHello {
    static staticProp = Number(10);
    static staticFunc() { console.log('Hello'); }

    classProp = String('Hello');

    // Method need a Speaker implementation object
    static run(speaker = Speaker, person = Person) {
        const now = new Date();

        speaker.say('Hello', person, new Date());
    }
}

ScriptTypeJS.declare({ SayHello });

Interface

Class with multi-interfaces implementation

  • Interface file Translator.js
import ScriptTypeJS, { Enum } from "script-type-js";

const Language = new Enum({
    'FR': 'French',
    'EU': 'English',
    'DE': 'Deutch',
});

class Translator {
    translate(text = String) { }

    setLanguage(language = Language) { }
}
  • Interface file Speaker.js
class Speaker {
    static helloWorld() { }

    say(text = String, to = Person, at = new Date()) { }
}
  • Class file EnglishSpeaker.js
// Class with interfaces implementation
class EnglishSpeaker {    
    say(text, to, at = Date()) {
        console.log(`On ${at.toLocaleString()}\nTo ${to.fullname}: ${text}`);
    }

    translate(text) {
        console.log(`Ok, i'm going to translate "${text}" in english`);
    }

    setLanguage(language) {
        console.log(`I translate ${language}`);
    }
}

// Declare in ScriptTypeJS
// Interfaces are automatically declared with their implementations and could be uses as a type
ScriptTypeJS.declare(EnglishSpeaker).implements(Speaker, Translator);

In Action

Interface contract checking

  • Good script
class EnglishSpeaker {    
    say(text, to, at = Date()) {
        console.log(`On ${at.toLocaleString()}\nTo ${to.fullname}: ${text}`);
    }

    translate(text) {
        console.log(`Ok, i'm going to translate "${text}" in english`);
    }

    setLanguage(language) {
        console.log(`I translate ${language}`);
    }
}

ScriptTypeJS.declare(EnglishSpeaker).implements(Speaker, Translator);
  • Bad change

    Missing method translate

class EnglishSpeaker {    
    say(text, to, at = Date()) {
        console.log(`On ${at.toLocaleString()}\nTo ${to.fullname}: ${text}`);
    }

    setLanguage(language) {
        console.log(`I translate ${language}`);
    }
}

ScriptTypeJS.declare(EnglishSpeaker).implements(Speaker, Translator);

Throw error:

Methods [translate] are not defined on class EnglishSpeaker (class EnglishSpeaker implement interface Translator)

Bad argument naming

class EnglishSpeaker {    
    say(text, person, at = Date()) { // bad naming of agrument #2
        console.log(`On ${at.toLocaleString()}\nTo ${to.fullname}: ${text}`);
    }

   translate(text) {
        console.log(`Ok, i'm going to translate "${text}" in english`);
    }
    setLanguage(language) {
        console.log(`I translate ${language}`);
    }
}

ScriptTypeJS.declare(EnglishSpeaker ).implements(Speaker, Translator);

Throw error:

Argument #name are wrong in function say (text,person,at = Date()) declaration: to expected for name of argument, person is given

Value assignment checking

  • Good script
import ScriptTypeJS, { Enum } from "script-type-js";

import Person, { Sex } from "./Person";
import { EnglishSpeaker } from "./Test";
import { SayHello } from "./SayHello";
import { Language } from "./Translator";

const englishSpeaker = new EnglishSpeaker();

const john = new Person();
john.setFullName('John', 'DOE'); 
john.setFullName('John', 1);      // Accepted with the multi type { String, Number }
john.sex = Sex.male;

englishSpeaker.say('Hello my friend', john);
  • Bad changes
john.sex = 3;

Throw error:

Method set: Argument value (#1) must be Enum { male: 1, female: 2 }, Number 3 is given

john.setFullName('John', true);

Throw error:

Method setFullName: Argument lastname (#2) must be Type { String: String, Number: Number }, Boolean true is given

englishSpeaker.say('Hello my friend', 'john');

Throw error:

Method run: Argument person (#2) must be a Person, String is given

BABEL

If you use babel you must exclude the transform-parameter plugin.

"presets": [
        [
            "@babel/preset-env",
            {                
                "exclude": ["transform-parameters"]
            }
        ],
    ]

Limitation

Instance type checking is automatically applied when a first method or setter/getter call is made. In the case of a direct assignment of a property without any method call, getter or setter beforehand, the check is not performed. To fix this, you can use the static Class.instanciate(this) method at the end of the constructor or the static factory method