Research
Security News
Malicious npm Packages Inject SSH Backdoors via Typosquatted Libraries
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Minimal (564 bytes) js extensions
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();
}
}))
;
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);
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){}
FAQs
minimal js extensions
The npm package minimal-js receives a total of 44 weekly downloads. As such, minimal-js popularity was classified as not popular.
We found that minimal-js demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
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.
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.