Product
Introducing License Enforcement in Socket
Ensure open-source compliance with Socket’s License Enforcement Beta. Set up your License Policy and secure your software!
macro-decorators
Advanced tools
Sometimes, writing code for JavaScript getters and setters can get repetitive. Macro decorators allow you to DRY up your code by creating a decorator that can duplicate a getter/setter's functionality in multiple places!
Checkout the docs to get started, or continue reading for more information.
Using NPM:
npm install macro-decorators
Using Yarn:
yarn add macro-decorators
You can import the macro
decorator from macro-decorators
and use it to
define macros:
import macro from 'macro-decorators';
function fullNameMacro() {
return macro({
get() {
return `${this.firstName} ${this.lastName}`;
},
set(obj, key, value) {
let [firstName, lastName] = value.split(' ');
this.firstName = firstName;
this.lastName = lastName;
},
});
}
class User {
firstName;
lastName;
@fullNameMacro fullName;
}
class Admin {
firstName;
lastName;
@fullNameMacro fullName;
}
let captainMarvel = new User();
captainMarvel.fullName = 'Carol Danvers';
console.log(captainMarvel.firstName); // 'Carol'
console.log(captainMarvel.lastName); // 'Danvers'
console.log(captainMarvel.fullName); // 'Carol Danvers'
You can also create dynamic macros which accept parameters, to make them more reusable and composable:
import macro, { filter, reads } from 'macro-decorators';
function percent(dividendName, divisorName) {
return macro(function() {
let divisor = this[divisorName];
if (!divisor) {
return null;
}
return (this[dividendName] / divisor) * 100;
});
}
function formattedPercent(percentPropertyName) {
return macro(function() {
let value = this[percentPropertyName];
if (!value) {
return '--';
}
value = value.toFixed(2);
return `${value}%`;
});
}
class TestResultComponent {
testResults = [];
@filter('testResults', result => !result.finished)
errorBuilds;
@filter('testResults', result => result.finished && !result.succeeded))
failedBuilds;
@filter('testResults', result => result.finished && result.succeeded))
passedBuilds;
@reads('testResults.length') numberOfBuilds;
@reads('errorBuilds.length') numberOfErrorBuilds;
@reads('failedBuilds.length') numberOfFailedBuilds;
@reads('passedBuilds.length') numberOfPassedBuilds;
@percent('numberOfErrorBuilds', 'numberOfBuilds') percentOfErrorBuilds;
@percent('numberOfFailedBuilds', 'numberOfBuilds') percentOfFailedBuilds;
@percent('numberOfPassedBuilds', 'numberOfBuilds') percentOfPassedBuilds;
@formattedPercent('percentOfErrorBuilds') formattedPercentOfErrorBuilds;
@formattedPercent('percentOfFailedBuilds') formattedPercentOfFailedBuilds;
@formattedPercent('percentOfPassedBuilds') formattedPercentOfPassedBuilds;
}
The macro-decorators
library also ships with a number of predefined macros,
including the @filter
and @reads
decorators from the last example. Check out
the API doc for more information
on these macros.
Built in macros that receive a key to a different property as an argument can also receive a path of keys separated by periods:
import { reads } from 'macro-decorators';
class Person {
friends = [];
@reads('friends.length') numFriends;
}
Paths can be any length, but can only consist of string based property keys separated by periods. They cannot be dynamic.
See the Contributing guide for details.
macro-decorators
is built using TypeScript, and is compatible with both the
TypeScript and Babel Legacy stage 1 decorators transforms. This is following the
recommendation
of the decorators proposal champions.
The decorators spec is still changing and not finalized at all, but this library
intends to provide an upgrade path for the @macro
decorator and all of the
macros defined in it. This plan will be finalized when the decorators proposal
is at least stage 3 and beginning to see wide adoption. Ideally, it will:
Whether or not these goals will be technically feasible will depend on the final spec and its implementation.
This project is licensed under the MIT License.
FAQs
Decorators for property macros
The npm package macro-decorators receives a total of 7,199 weekly downloads. As such, macro-decorators popularity was classified as popular.
We found that macro-decorators 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.
Product
Ensure open-source compliance with Socket’s License Enforcement Beta. Set up your License Policy and secure your software!
Product
We're launching a new set of license analysis and compliance features for analyzing, managing, and complying with licenses across a range of supported languages and ecosystems.
Product
We're excited to introduce Socket Optimize, a powerful CLI command to secure open source dependencies with tested, optimized package overrides.