Comparing version 1.0.0 to 2.0.0
{ | ||
"name": "macroable", | ||
"version": "1.0.0", | ||
"version": "2.0.0", | ||
"description": "A simple ES6 class that can be extended to provide macros and getters functionality", | ||
"main": "index.js", | ||
"main": "build/index.js", | ||
"files": [ | ||
"build/index.js", | ||
"build/index.d.ts" | ||
], | ||
"scripts": { | ||
"test": "node test.js" | ||
"mrm": "mrm --preset=@adonisjs/mrm-preset", | ||
"pretest": "npm run lint", | ||
"test": "nyc node japaFile.js", | ||
"prepublishOnly": "npm run build", | ||
"lint": "tslint --project tsconfig.json", | ||
"clean": "del build", | ||
"compile": "npm run lint && npm run clean && tsc", | ||
"build": "npm run compile", | ||
"coverage": "nyc report --reporter=text-lcov | coveralls", | ||
"commit": "git-cz" | ||
}, | ||
@@ -15,7 +28,45 @@ "keywords": [ | ||
"devDependencies": { | ||
"japa": "^1.0.1" | ||
"@adonisjs/mrm-preset": "^1.0.14", | ||
"@types/node": "^10.12.0", | ||
"commitizen": "^3.0.4", | ||
"coveralls": "^3.0.2", | ||
"cz-conventional-changelog": "^2.1.0", | ||
"del-cli": "^1.1.0", | ||
"japa": "^2.0.6", | ||
"mrm": "^1.2.1", | ||
"nyc": "^13.1.0", | ||
"pkg-ok": "^2.3.1", | ||
"ts-node": "^7.0.1", | ||
"tslint": "^5.11.0", | ||
"tslint-eslint-rules": "^5.4.0", | ||
"typescript": "^3.1.3", | ||
"yorkie": "^2.0.0" | ||
}, | ||
"dependencies": { | ||
"node-exceptions": "^2.0.1" | ||
} | ||
"node-exceptions": "^4.0.1" | ||
}, | ||
"nyc": { | ||
"exclude": [ | ||
"test.ts" | ||
], | ||
"extension": [ | ||
".ts" | ||
] | ||
}, | ||
"config": { | ||
"commitizen": { | ||
"path": "cz-conventional-changelog" | ||
} | ||
}, | ||
"gitHooks": { | ||
"commit-msg": "node ./node_modules/@adonisjs/mrm-preset/validateCommit/conventional/validate.js" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/poppinss/macroable.git" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/poppinss/macroable/issues" | ||
}, | ||
"homepage": "https://github.com/poppinss/macroable#readme" | ||
} |
171
README.md
# Macroable | ||
> Extend `class` prototype in style 😎 | ||
Macroable is a small ES6 class that can be extended to add functionality of macros and getters to your own class. | ||
[![travis-image]][travis-url] | ||
[![appveyor-image]][appveyor-url] | ||
[![coveralls-image]][coveralls-url] | ||
[![npm-image]][npm-url] | ||
![](https://img.shields.io/badge/Uses-Typescript-294E80.svg?style=flat-square&colorA=ddd) | ||
It is helpful if you want to expose an API to get your classes extended. | ||
Macroable is a simple class that your classes can extend in order to expose an API for extending the class. Let's see how a class can be extended without Macroable first. | ||
> This library is used by AdonisJs to offer extend interface for core class. | ||
## Traditional approach | ||
## Installation | ||
As always, pull it from `npm`. | ||
```bash | ||
npm i --save macroable | ||
```js | ||
class Foo {} | ||
module.exports = Foo | ||
``` | ||
## Usage | ||
Someone can extend it follows. | ||
```js | ||
const Macroable = require('macroable') | ||
class User extends Macroable { | ||
const Foo = require('./Foo') | ||
Foo.prototype.greet = function () { | ||
return 'Hello!' | ||
} | ||
// it's required to define empty objects for macros and getters. | ||
User._macros = {} | ||
User._getters = {} | ||
// or add getter as follow | ||
Object.defineProperty(Foo.prototype, 'username', { | ||
get: function () { | ||
return 'virk' | ||
} | ||
}) | ||
``` | ||
<br > | ||
## Using macroable it's simpler | ||
--- | ||
```js | ||
const { Macroable } from 'macroable' | ||
<br > | ||
class Foo extends Macroable { | ||
} | ||
Foo._macros = {} | ||
Foo._getters = {} | ||
Once your class extends `Macroable` class, it get's a bunch of static methods to define **macros** and **getters**. | ||
module.exports = Foo | ||
``` | ||
## Using Macros | ||
```js | ||
const Foo = require('./Foo') | ||
```js | ||
User.macro('getUsers', function () { | ||
// do some work | ||
Foo.macro('greet', function () { | ||
return 'Hello!' | ||
}) | ||
Foo.getter('username', function () { | ||
return 'virk' | ||
}) | ||
``` | ||
and now you can use the **method** from the class instance. | ||
You can see the API is simpler and less verbose. However, their are couple more benefits to using Macroable. | ||
```js | ||
const user = new User() | ||
user.getUsers() | ||
1. You can add singleton getters, which are evaluated only once and then cached value is returned. | ||
2. Cleanup all `macros` and `getters` added using Macroable. | ||
## Installation | ||
```bash | ||
npm i macroable | ||
``` | ||
<br > | ||
## Usage | ||
```js | ||
const { Macroable } from 'macroable' | ||
--- | ||
class Foo extends Macroable { | ||
} | ||
<br > | ||
Foo._macros = {} | ||
Foo._getters = {} | ||
## Using Getters | ||
Getters are values evaluated everytime someone access them. | ||
module.exports = Foo | ||
``` | ||
## API | ||
#### macro(name, callback) => void | ||
Add a function to the prototype | ||
```js | ||
User.getter('username', function () { | ||
// return username | ||
Foo.macro('greet', function (name) { | ||
return `Hello ${name}!` | ||
}) | ||
``` | ||
and now you can use the **property** from the class instance | ||
#### hasMacro(name) => boolean | ||
Find if macro exists. | ||
```js | ||
const user = new User() | ||
user.username | ||
Foo.hasMacro('greet') | ||
``` | ||
<br > | ||
#### getter(name, callback, isSingleton?) => void | ||
Add getter to the prototype and optionally make it singleton. | ||
--- | ||
```js | ||
Foo.getter('username', function () { | ||
return 'virk' | ||
}, true) | ||
``` | ||
<br > | ||
#### hasGetter(name) => boolean | ||
Find if getter exists. | ||
## Singleton Getters | ||
```js | ||
Foo.hasGetter('greet') | ||
``` | ||
Calling the getter callback everytime may be unrequired, since you do not want to re-compute the values. A **singleton** getter can also be defined. | ||
#### hydrate | ||
Remove all macros and getters added using `Macroable`. | ||
```js | ||
User.getter('username', function () { | ||
// I am only called once | ||
Foo.getter('username', function () { | ||
return 'virk' | ||
}, true) | ||
``` | ||
```js | ||
const user = new User() | ||
Foo.hydrate() | ||
user.username // invokes callback and caches value | ||
user.username // returns from cache | ||
Foo.hasGetter('username') // false | ||
``` | ||
<br > | ||
## Change log | ||
--- | ||
The change log can be found in the [CHANGELOG.md](CHANGELOG.md) file. | ||
<br > | ||
## Contributing | ||
## Hydrating Class | ||
If for some reason you want to remove all getters and macros, you can call the `hydrate` method. | ||
Everyone is welcome to contribute. Please go through the following guides, before getting started. | ||
```js | ||
User.macro('getUsers', callback) | ||
1. [Contributing](https://adonisjs.com/contributing) | ||
2. [Code of conduct](https://adonisjs.com/code-of-conduct) | ||
User.hasMacro('getUsers') // true | ||
User.hydrate() | ||
## Authors & License | ||
[thetutlage](https://github.com/thetutlage) and [contributors](https://github.com/poppinss/macroable/graphs/contributors). | ||
User.hasMacro('getUsers') // false | ||
``` | ||
MIT License, see the included [MIT](LICENSE.md) file. | ||
<br > | ||
[travis-image]: https://img.shields.io/travis/poppinss/macroable/master.svg?style=flat-square&logo=travis | ||
[travis-url]: https://travis-ci.org/poppinss/macroable "travis" | ||
--- | ||
[appveyor-image]: https://img.shields.io/appveyor/ci/thetutlage/macroable/master.svg?style=flat-square&logo=appveyor | ||
[appveyor-url]: https://ci.appveyor.com/project/thetutlage/macroable "appveyor" | ||
<br > | ||
[coveralls-image]: https://img.shields.io/coveralls/poppinss/macroable/master.svg?style=flat-square | ||
[coveralls-url]: https://coveralls.io/github/poppinss/macroable "coveralls" | ||
## Checking Existence | ||
You can also find whether a **getter** or **macro** already exists or not. | ||
```js | ||
User.hasMacro('getUsers') | ||
User.hasGetter('username') | ||
``` | ||
[npm-image]: https://img.shields.io/npm/v/macroable.svg?style=flat-square&logo=npm | ||
[npm-url]: https://npmjs.org/package/macroable "npm" |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No bug tracker
MaintenancePackage does not have a linked bug tracker in package.json.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
No website
QualityPackage does not have a website.
Found 1 instance in 1 package
0
0
159
0
8598
15
58
2
+ Addednode-exceptions@4.0.1(transitive)
- Removednode-exceptions@2.0.2(transitive)
Updatednode-exceptions@^4.0.1