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

clazz-js

v0.5.2

Published

Portable JavaScript library for class-style OOP programming

Downloads

38

Readme

ClazzJS

ClazzJS is portable JavaScript library for class-style OOP programming. Its main goal is to provide expressive DSL to write your JavaScript programs in easy-to-understand, well-known, convenient and flexible class base manner. It's works well both on client and server sides.

Features include:

  • Single inheritance
  • Expressive, extensible DSL for declaring of your class
  • Methods generation
  • Events emitting
  • Object properties changes observing
  • Namespaces and more...

You'll find the example bellow to have a common idea what I'm talking about.

Documentation

This docs describe many features of the library, but not all. Use source to learn ClazzJS better. In every doc chapter there are links on respective source files.

  1. Installation
  2. Clazz declaration
  3. Properties
  4. Methods
  5. Constants
  6. Events
  7. Namespaces

Example

Main goal of this example is to give you a common idea about ClazzJS. It's not discover all features of the library. Online working version of this example is available on plunker: http://plnkr.co/edit/c5Xveb. Feel free to play around with it!

Person clazz declaration:

clazz("Person", {
    constants: {
        SEX: ['male', 'female']
    },
    properties: {
        name: {
            type: 'string',
            methods: ['get']
        },
        phone: {
            type: ['string', {
                pattern: /\d{1,2}-\d{3}-\d{5,7}/
            }]
        },
        birthday: {
            type: 'datetime',
            constratins: {
                inPast: function(birthday) {
                    return birthday.getTime() < Date.now();
                }
            }
        },
        sex: {
            type: 'string',
            methods: ['get', 'set', 'is'],
            converters: {
                toFull: function(sex) {
                    switch(sex.toLowerCase()) {
                        case 'm': sex = 'male'; break;
                        case 'f': sex = 'female'; break;
                    }
                    return sex;
                }
            },
            constraints: {
                existedSex: function(sex) {
                    return -1 !== this.const('SEX').indexOf(sex);
                }
            }
        }
    },
    methods: {
        getAge: function() {
            return (new Date()).getFullYear() - this.getBirthday().getFullYear();
        }
    }
});

Teacher clazz inherited from Person:

clazz('Teacher', 'Person', {
    constants: {
        SUBJECT: ['physics', 'literature', 'mathematics']
    },
    properties: {
        subject: {
            type: 'string',
            constraints: {
                existedSubject: function(subject) {
                    return -1 !== this.const('SUBJECT').indexOf(subject);
                }
            }
        }
    }
});

Creation and manipulation of instances:


// Create just common person - John (without 'new' operator)
var john = clazz('Person').create({
    name: 'John Stewart',
    sex: 'M',
    phone: '1-925-123567',
    birthday: "1989-12-13"
});

john instanceof clazz("Person"); // true 

john.getName();  // 'John Stewart'
john.getAge();   // 24
john.getSex();   // 'male'
john.getPhone(); // 1-925-123567

john.setPhone('7-925-1'); // Throw phone pattern fail error with message: 
                          // 'Value "7-925-1" does not match
                          // pattern "/\d{1,2}-\d{3}-\d{5,7}/"'

john.isSex("male");   // true
john.isSex("female"); // false

john.setSex('unsupportedSex'); // Throw existedSex constraint fail error with message:
                               // 'Constraint "existedSex" was failed!'

john.setSex('female'); // Successfully change sex of John

john.getSex();        // 'female'
john.isSex("male");   // false
john.isSex("female"); // true

john.getBirthday() instanceof Date; // true
john.getBirthday().getMonth();      // 12
john.getBirthday().getFullYear();   // 1989

// Create math teacher - Mr. George Smith. (with 'new' operator)
var mathTeacher = new clazz('Teacher')({
    name: 'George Smith',
    sex: 'male',
    birthday: '1973-12-34',
    subject: 'mathematics'
});

mathTeacher instanceof clazz('Person');    // true
mathTeacher instanceof clazz('Teacher'));  // true

mathTeacher.getName(); // John Smith

License

Copyright (c) 2013 Aleksey Podskrebyshev. Licensed under the MIT license.

githalytics.com alpha

Bitdeli Badge