
Research
Malicious npm Packages Impersonate Flashbots SDKs, Targeting Ethereum Wallet Credentials
Four npm packages disguised as cryptographic tools steal developer credentials and send them to attacker-controlled Telegram infrastructure.
Define argument types for functions and automatically add methods to prototype.
Define argument types for functions and automatically add methods to prototype.
npm install autoclass
import AutoClass from 'autoclass';
const Rider = AutoClass(
'Rider',
String,
Number,
function (nickname, number) {
return {nickname, number};
}
);
const ShowInfo = AutoClass(
'ShowInfo',
Rider,
function ({nickname, number}) {
console.log(`${nickname}, #${number}`);
}
);
const dovizioso = Rider('Dovi', 4);
const iannone = Rider('The Maniac', 29);
dovizioso.ShowInfo(); // Logs: "Dovi, #4"
iannone.ShowInfo(); // Logs: "The Maniac, #29"
A string to use as a method name. If an empty string or not a string, no methods are added.
Zero or more constructors. func
arguments are validated using these corresponding types. Methods are added to prototypes of constructors. Methods are not added to array or variadic types.
Function to execute, when instance as function or instance's method is invoked. func
should have equal amount of named arguments as types are declared. func
gets called with formatted and validated (see type) argument values. Its this
value is set to object with argument names of func
as keys and argument instances as values.
const MyClass = AutoClass(
'MyClass',
String,
function (text) {
return text;
}
);
Three kinds of types can be declared - basic, array and variadic.
const MyClass = AutoClass(
'MyClass',
String, // Basic types. Validated using `instanceof` operator.
Object,
[Number], // Array types. Each element is validated like basic type.
[[String]], // Variadic type. Function may also have one variadic type. Validated like array type.
Boolean, // More types...
function (someText, obj, nums, texts, bool) {
console.log('MyClass');
}
);
MyClass('text', null, [1, 2, 3], 'a', 'b', 'c', true); // Logs: "MyClass"
If instanceof
test fails, instance is passed to constructor.
const Rider = AutoClass(
'Rider',
String,
Number,
function (nickname, number) {
console.log(typeof number); // Logs: "number"
return {nickname, number};
}
);
const dovizioso = Rider('Dovi', '04');
If class requires at least two types (variadic type is not considered), constructing from object literal is possible.
// `ShowInfo` requires `Rider`
ShowInfo({
nickname: 'Petrux',
number: 9
}); // Logs: "Petrux, #9"
Function arguments are values. Use this.argumentName
to access instance.
const MyClass = AutoClass('MyClass', Number, function (number) {return number;});
const TestType = AutoClass(
'TestType',
MyClass,
AutoClass,
function (a, type) {
console.log(a);
console.log(this.a instanceof type);
}
);
TestType(123, MyClass); // Logs: 123; true
Function and method calls return instance. Use .valueOf()
to access return value.
const MyClass = AutoClass(
'MyClass',
Object,
function (obj) {
return obj;
}
);
const ref = {};
console.log(MyClass(ref).valueOf() === ref); // Logs: true
Validate inputs, return value.
const BikeNumber = AutoClass(
'BikeNumber',
Number,
function (number) {
if (isNaN(number)) {
throw new Error('Bike number must be positive integer, got NaN.');
}
if (number < 1) {
throw new Error(`Bike number must be positive integer, got ${number}.`);
}
return Math.floor(number);
}
);
const Rider = AutoClass(
'Rider',
String,
BikeNumber,
function (nickname, bikeNumber) {
return {nickname, bikeNumber};
}
);
try {
Rider('Kallio', -36); // Throws: "Error: Bike number must be positive integer, got -36."
} catch (e) {
console.log(e);
}
try {
Rider('The Doctor', 'VR46'); // Throws: "Error: Bike number must be positive integer, got NaN."
} catch (e) {
console.log(e);
}
Functions that return nothing or undefined can be chained.
AutoClass(
'SetNumber',
Rider,
BikeNumber,
function (rider, bikeNumber) {
rider.bikeNumber = bikeNumber;
}
);
AutoClass(
'ShowInfo',
Rider,
function ({nickname, bikeNumber}) {
console.log(`${nickname}, #${bikeNumber}`);
}
);
Rider('Stoner', 27)
.ShowInfo() // Logs: "Stoner, #27"
.SetNumber(1)
.ShowInfo(); // Logs: "Stoner, #1"
Function may require same type multiple times. Primary method uses instance as first argument of its type. To use instance as another argument of its type, use instance.Method.argumentName(...other args)
.
AutoClass(
'Append',
String,
String,
function (to, text) {
return to + text;
}
);
// "Hello" as `to` argument
console.log('Hello'.Append(' world!').valueOf()); // Logs: "Hello world!"
// "Hello" as `text` argument
console.log('Hello'.Append.text(' world!').valueOf()); // Logs: " world!Hello"
ISC
FAQs
Define argument types for functions and automatically add methods to prototype.
We found that autoclass 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
Four npm packages disguised as cryptographic tools steal developer credentials and send them to attacker-controlled Telegram infrastructure.
Security News
Ruby maintainers from Bundler and rbenv teams are building rv to bring Python uv's speed and unified tooling approach to Ruby development.
Security News
Following last week’s supply chain attack, Nx published findings on the GitHub Actions exploit and moved npm publishing to Trusted Publishers.