jack-of-all-decorators
v1.1.0-beta.1
Published
Decorator library for TypeScript. Serialize/deserialzie classes to JSON. Decorate methods with lodash, etc. Dependency injection.
Downloads
2
Maintainers
Readme
Jack-of-All-Decorators (beta)
Is a TypeScript library that contains may useful class decorators to help remove some of the tiresome boiler-plate from TypeScript/JavaScript development.
- Jsonables adds JSON Serialization/Deserialization to classes.
- Decorators wraps class members with convenient transformers, like lodash functions and others.
- Dependency Injection handles instantiation/destruction of class dependencies at the global and/or module level.
Installation
Install via npm
:
npm install jack-of-all-decorators
Documentation Wiki
View the docs at the GitHub Wiki.
Brief Intro to the Library...
Jsonables - JSON Serialization/Deserialization of Classes.
Jsonable decorators make classes JSON serializable by adding the toJSON()
method.
Example Class with Basic JSON Serialization:
@Json.Serializable()
class MyClass {
constructor (
public foo: string = "",
private _bar: string = ""
) {}
public getBar () {
return this._bar;
}
public setBar (v: string) {
this._bar = v;
}
}
const myClass = new MyClass ("a", "b");
const json = myClass.toJSON();
console.log(JSON.stringify(myClass));
// Outputs: {"foo": "a", "bar": "b"}
Example JSON Deserialization:
const myNewClass = Json.Serializer.deserialize<MyClass>(MyClass, json);
assert.equals(myClass.foo, myNewClass.foo);
assert.equals(myClass.getBar(), myNewClass.getBar());
Note: Jsonables offers highly customizable JSON serialization options beyond the basic example above. Explore the docs to learn more.
Decorators - Wrap Class Members with Useful Transformers
The Decorators module provides many decorators to augment the input and output of class members. Most deocrators use the amazing lodash library. A few others provide features beyond what lodash offers.
Example Decorator Usage:
class MyClass {
private _createdOn = new Date();
// Returns a copy of the _createdOn Date object.
@defensiveCopy
public get createdOn () {
return this._createdOn;
}
// Returns the sum of the array: 15.
@sum
public getTotal () {
return [1, 2, 3, 4, 5];
}
// Returns: <strong>TypeScript & JavaScript</strong>
@escape
pubic getHTMLSafe () {
return "<strong>TypeScript & JavaScript</strong>";
}
// Returns: Saturday, June 9, 2007
@dateFormat("fullDate")
public getDate() {
return new Date();
}
}
Dependency Injection - Handle Class Dependencies
The Dependency Injection module provides an easy way to manage class dependencies. Dependencies are instantiated as Singletons that are sandboxed to the global or given module namespace.
Example Dependency Injection Usage:
@Injectable()
class Foo implements IInjectable {
public guid = Math.random().toString(36).substr(2);
public destruct(): void {
this.guid = null;
}
}
class MyClass implements IInjectable {
constructor (
@Inject(Foo) public foo: Foo
) {}
public destruct () {
this.foo = null;
}
}
const myClass = DI.Modularize<MyClass>(MyClass, "module.path");
console.log(myClass.foo);
DI.Destruct("module.path");
Special Thanks
I stand upon the shoulders of giants
lodash -- https://github.com/lodash/lodash/
The lodash library is used extensively in the Decorators module.
dateformat -- https://github.com/felixge/node-dateformat
The dateformat library is used by the dateFormat decorator.
License
(The MIT License)
Copyright (c) 2016 Patrick Martin
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.