1
1
'use strict' ;
2
2
3
+ const accessors = {
4
+
5
+ string ( proto , name , index ) {
6
+ Object . defineProperty ( proto . prototype , name , {
7
+ get ( ) {
8
+ return this [ index ] ;
9
+ } ,
10
+ set ( value ) {
11
+ this [ index ] = value ;
12
+ }
13
+ } ) ;
14
+ } ,
15
+
16
+ Date ( proto , name , index ) {
17
+ Object . defineProperty ( proto . prototype , name , {
18
+ get ( ) {
19
+ return new Date ( this [ index ] ) ;
20
+ } ,
21
+ set ( value ) {
22
+ this [ index ] = value instanceof Date ? value . toISOString ( ) : value ;
23
+ }
24
+ } ) ;
25
+ } ,
26
+
27
+ function ( proto , name , index , fieldDef ) {
28
+ Object . defineProperty ( proto . prototype , name , { get : fieldDef } ) ;
29
+ }
30
+
31
+ } ;
32
+
3
33
// Assign metadata to array elements
4
34
// data - array of objects
5
35
// metadata - data describes PrototypeClass structure
6
36
// Returns: built PrototypeClass
7
37
//
8
38
function assignMetadata ( data , metadata ) {
9
- let proto = buildPrototype ( metadata ) ;
39
+ const proto = buildPrototype ( metadata ) ;
10
40
assignPrototype ( data , proto ) ;
11
41
return proto ;
12
42
}
@@ -24,9 +54,9 @@ function assignPrototype(data, proto) {
24
54
// Build Prototype from Metadata
25
55
//
26
56
function buildPrototype ( metadata ) {
27
- let protoClass = function ProtoClass ( ) { } ;
57
+ const protoClass = function ProtoClass ( ) { } ;
28
58
let index = 0 , fieldDef , buildGetter , fieldType ;
29
- for ( let name in metadata ) {
59
+ for ( const name in metadata ) {
30
60
fieldDef = metadata [ name ] ;
31
61
fieldType = typeof ( fieldDef ) ;
32
62
if ( fieldType !== 'function' ) fieldType = fieldDef ;
@@ -36,40 +66,10 @@ function buildPrototype(metadata) {
36
66
return protoClass ;
37
67
}
38
68
39
- let accessors = {
40
-
41
- string : function ( proto , name , index ) {
42
- Object . defineProperty ( proto . prototype , name , {
43
- get : function ( ) {
44
- return this [ index ] ;
45
- } ,
46
- set : function ( value ) {
47
- this [ index ] = value ;
48
- }
49
- } ) ;
50
- } ,
51
-
52
- Date : function ( proto , name , index ) {
53
- Object . defineProperty ( proto . prototype , name , {
54
- get : function ( ) {
55
- return new Date ( this [ index ] ) ;
56
- } ,
57
- set : function ( value ) {
58
- this [ index ] = value instanceof Date ? value . toISOString ( ) : value ;
59
- }
60
- } ) ;
61
- } ,
62
-
63
- function : function ( proto , name , index , fieldDef ) {
64
- //console.log({proto, name, index, fieldDef});
65
- Object . defineProperty ( proto . prototype , name , { get : fieldDef } ) ;
66
- }
67
-
68
- } ;
69
69
70
70
// Define Data Source
71
71
//
72
- let data = [
72
+ const data = [
73
73
[ 'Marcus Aurelius' , 'Rome' , '212-04-26' ] ,
74
74
[ 'Victor Glushkov' , 'Rostov on Don' , '1923-08-24' ] ,
75
75
[ 'Ibn Arabi' , 'Murcia' , '1165-11-16' ] ,
@@ -79,11 +79,11 @@ let data = [
79
79
80
80
// Define metadata to build prototype dynamically
81
81
//
82
- let metadata = {
82
+ const metadata = {
83
83
name : 'string' ,
84
84
city : 'string' ,
85
85
born : 'Date' ,
86
- age : function ( ) {
86
+ age ( ) {
87
87
return (
88
88
new Date ( ) . getFullYear ( ) -
89
89
new Date ( this . born + '' ) . getFullYear ( )
@@ -93,7 +93,7 @@ let metadata = {
93
93
94
94
// Define query using regular JavaScript syntax
95
95
//
96
- let query = person => (
96
+ const query = person => (
97
97
person . name !== '' &&
98
98
person . age > 25 &&
99
99
person . city === 'Rome'
@@ -103,5 +103,5 @@ let query = person => (
103
103
assignMetadata ( data , metadata ) ;
104
104
105
105
// Apply query to dataset
106
- let res = data . filter ( query ) ;
106
+ const res = data . filter ( query ) ;
107
107
console . dir ( res ) ;
0 commit comments