Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

minimal-js

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

minimal-js

minimal js extensions

  • 0.2.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

minimal-js

Minimal (564 bytes) js extensions

  • defClass(name, proto)
  • dot(prop, args...)
  • extend(a, b, ...)

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

Keywords

FAQs

Package last updated on 29 Sep 2013

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc