minimal-js

0.2.1 • Public • Published

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

data.init wont be called (inheritance).

var
  BaseModel = defClass('BaseModel', {}),
  User = defClass('User', new BaseModel({
    init: function(){
      assert.fail();
    }
  }))
;

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){}

Readme

Keywords

none

Package Sidebar

Install

npm i minimal-js

Weekly Downloads

56

Version

0.2.1

License

BSD

Last publish

Collaborators

  • cztomsik