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

minimal-js

v0.2.1

Published

minimal js extensions

Downloads

202

Readme

minimal-js

Minimal (564 bytes) js extensions

defClass(name, proto)

shorthand for js classes, returns one-arg constructor, initializing new instance with provided options, new class is also defined globally on window.

var
  Person = defClass('Person', {
    hello: function(){
      return 'Hello, ' + this.name;
    }
  }),
  batman = new Person({name: 'Batman'})
;

assert(batman.constructor === Person);
assert(batman.name === 'Batman');
assert(batman.hello() === 'Hello, Batman');
assert(('' + Person) === 'Person');
assert(window.Person === Person);

custom initialization is supported with optional init() method.

var
  Person = defClass('Person', {
    init: function(){
      this.initials = this.name.match(/[A-Z]/g).join('');
    }
  }),
  batman = new Person({name: 'Bruce Wayne'})
;
assert(batman.initials === 'BW');

prototype inheritance is still supported.

var
  Person = defClass('Person', {
    hello: function(){
      return 'Hello, ' + this.name;
    }
  }),
  Hero = defClass('Hero', new Person()),
  BadGuy = defClass('BadGuy', new Hero({
    hello: function(){
      return 'MUHAHAHA! ' + this.constructor.prototype.constructor.prototype.hello.call(this);
    }
  })),
  
  batman = new Hero({name: 'Batman'}),
  joker = new BadGuy({name: 'Joker'})
;

assert(batman.hello() === 'Hello, Batman');
assert(joker.hello() === 'MUHAHAHA! Hello, Joker');

it is possible to define static methods with extend, .

var
  ActiveRecord = extend(defClass('ActiveRecord', {}), {
    all: function(){
      return ;//...
    }
  }),
  User = defClass('User', new ActiveRecord({
    //...
  }))
;

User.all();

returned constructor can be used as mapping function.

var
  Person = defClass('Person', {
    hello: function(){
      return 'Hello, ' + this.name;
    }
  }),
  jsonData = [{name: 'Batman'}, {name: 'Joker'}],
  heroes = jsonData.map(Person)
;
assert(heroes[0].name === 'Batman');
assert(heroes[0].hello() === 'Hello, Batman');

init() can override returned instance from new.

//TODO: example

dot(prop, args...)

returns callback (property getter) suitable for Array.map().

var
  heroes = [{name: 'Batman'}, {name: 'Joker'}],
  heroNames = heroes.map(dot('name'))
;

assert.deepEqual(heroNames, ['Batman', 'Joker']);

works well with Array.filter() too.

var
  users = [{name: 'Admin', isAdmin: true}, {name: 'Test'}],
  admins = users.filter(dot('isAdmin'))
;

assert.deepEqual(admins, [users[0]]);

for methods, result from their call will be returned.

var
  heroNames = ['Batman', 'Joker'],
  uppers = heroNames.map(dot('toUpperCase'))
;

assert.deepEqual(uppers, ['BATMAN', 'JOKER']);

any additional arguments are passed to method as expected.

var
  heroNames = ['Batman', 'Joker'],
  initials = heroNames.map(dot('slice', 0, 1))
;

assert.deepEqual(initials, ['B', 'J']);

can be also used as a general utility function.

dot('log', 'Hello world!')(console);

extend(a, b, ...)

extends object with properties from another.

var dest = {prop: 'test'};

extend(dest, {newProp: 'test'});
assert(dest.newProp === 'test');

overrides existing properties.

var dest = {prop: 'test'};

extend(dest, {prop: 'new'});
assert(dest.prop === 'new');

always returns first argument.

var a = {}, b = {};

assert(extend(a) === a);
assert(extend(a, b) === a);

can be used to create shallow copies.

var src = {prop: 'test'};

assert.deepEqual(extend({}, src), src);

varargs are translated to subsequent recursive calls, nesting from left.

var a = {a: 1}, b = {b: 2}, c = {c: 3};

assert.deepEqual(extend(a, b, c), extend(extend(a, b), c));

extend(a, {}) does nothing to a.

extend(Object.freeze({}), {});

extend() throws an error.

try{
  extend();
  assert.fail();
}
catch(e){}