use a reader of annotations for write you models with ES6 new syntax , easy usage.
installation
warning use in production is not recommanded while version beta.
usage
index.js
const ClassAnnotations = require('class-annotations')( __dirname ) ;
const annotations = new ClassAnnotations( './src/Foo.js' ) ;
console.log( annotations ) ;
./src/Foo.js
class Foo {
bar() {
}
} ;
static directory
class-annotations return a function the arguments 1 should be static directory, the root directory for your next read file.s.
const createClassAnnotations = require('class-annotations') ;
const staticDirectory = __dirname ;
const ClassAnnotations = createClassAnnotations(
staticDirectory
) ;
The static directory should be a absolute path.
auto persist the data type
class-annotations auto convert the data type of your annotations value,
but you can remote real value as: valueBrut
.
Foo.js
class Foo {
bar() {
}
} ;
ClassAnnotations {
items: ['Foo'],
countClass: 1,
Foo: {
classname: 'Foo',
data: {
Controller: {
valueBrut: '"Foo"', value: 'Foo'
}
}
} ,
methods: {
Foo: [
{
classname: 'Foo',
method: 'bar',
data: {
shouldBeArray: {
valueBrut: '["hello", "wold", 42]',
value: ["hello", "wold", 42],
}
shouldBeNumber: {
valueBrut: '42.24',
value: '42.24'
}
shouldBeString: {
valueBrut: '"hello world"',
value: "hello world"
}
ShouldBeParseError: {
valueBrut: "hello world !",
value: "Parse Error to line: 7 after opened annotations of class: Foo, error: hello wo..."
}
}
}
]
}
}
support-value-multilines
class-annotations can read an annotations extends multiline with this format syntax:
Foo.js
class Foo {
bar() {
}
} ;
read recursive folders
class-annotations can read a structure folders a filters files not: .js
Admitting this structure:
/project-name
- index.js
/ src
- Foo.js
- Bar.js
- README.md
/ stuff
- Foo.js
- Bar.js
from: index.js
const createClassAnnotations = require('class-annotations') ;
const ClassAnnotations = createClassAnnotations( __dirname ) ;
const annotations = new ClassAnnotations('./src/') ;
annotations:
ClassAnnotations {
src: ReadDirecory {
items: ['Foo.js','Bar.js','README.md','stuff'] ,
'Foo.js': ClassAnnotations {
} ,
'Bar.js': ClassAnnotations {
} ,
stuff: ReadDirectory {
items: ['Foo.js','Bar.js'] ,
'Foo.js': ClassAnnotations {
} ,
'Bar.js': ClassAnnotations {
} ,
}
}
}
read methods annotations
class-annotations read annotations inside body class and associate
annotations to a specific method class.
But you should follow rules associating annotations to a method, for exploit this feature, this rules enable you of write a simply comment in the body class and not considerate as annotation.
A annotations associate to a method should be not contains empty line
before header method.
class Foo {
bar() {
}
} ;
inside of exemple above the commentary:
is considerate as associate to index
method.
but below bar
method do not contains annotation
class Foo {
bar() {
}
} ;
filter methods annotations
after have read a file or structure folders
you can have needs use filter for annotation from method by name.
Stuff.js
class Foo {
index() {
}
} ;
class Bar {
index() {
}
}
annotations output:
ClassAnnotations {
countClass: 2,
methods: {
Foo: [
{
classname: "Foo",
method: "index",
data: {
a: {
valueBrut: '1',
value: 1
} ,
b: {
valueBrut: '2',
value: 2
} ,
c: {
valueBrut: '3',
value: 3
} ,
d: {
valueBrut: '4',
value: 4
}
}
} ,
getWidth: [Function]
] ,
Bar: [
{
classname: "Bar",
method: 'index',
data: {
a: {
valueBrut: '4',
value: 4
} ,
b: {
valueBrut: '3',
value: 3
} ,
c: {
valueBrut: '2',
value: 2
} ,
d: {
valueBrut: '1',
value: 1
}
}
},
getWidth: [Function]
]
}
}
the function getWidth
can filter annotation array and contains currently
an type because should be getWith
as ( matcher ) => object[]
matcher
arg1 should be an string or a instanceof RegExp
.
If you have detect an bug or anormal behavior with ClassAnnotaions
please remote a issues on github