11'use strict' ;
22
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+
333// Assign metadata to array elements
434// data - array of objects
535// metadata - data describes PrototypeClass structure
636// Returns: built PrototypeClass
737//
838function assignMetadata ( data , metadata ) {
9- let proto = buildPrototype ( metadata ) ;
39+ const proto = buildPrototype ( metadata ) ;
1040 assignPrototype ( data , proto ) ;
1141 return proto ;
1242}
@@ -24,9 +54,9 @@ function assignPrototype(data, proto) {
2454// Build Prototype from Metadata
2555//
2656function buildPrototype ( metadata ) {
27- let protoClass = function ProtoClass ( ) { } ;
57+ const protoClass = function ProtoClass ( ) { } ;
2858 let index = 0 , fieldDef , buildGetter , fieldType ;
29- for ( let name in metadata ) {
59+ for ( const name in metadata ) {
3060 fieldDef = metadata [ name ] ;
3161 fieldType = typeof ( fieldDef ) ;
3262 if ( fieldType !== 'function' ) fieldType = fieldDef ;
@@ -36,40 +66,10 @@ function buildPrototype(metadata) {
3666 return protoClass ;
3767}
3868
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- } ;
6969
7070// Define Data Source
7171//
72- let data = [
72+ const data = [
7373 [ 'Marcus Aurelius' , 'Rome' , '212-04-26' ] ,
7474 [ 'Victor Glushkov' , 'Rostov on Don' , '1923-08-24' ] ,
7575 [ 'Ibn Arabi' , 'Murcia' , '1165-11-16' ] ,
@@ -79,11 +79,11 @@ let data = [
7979
8080// Define metadata to build prototype dynamically
8181//
82- let metadata = {
82+ const metadata = {
8383 name : 'string' ,
8484 city : 'string' ,
8585 born : 'Date' ,
86- age : function ( ) {
86+ age ( ) {
8787 return (
8888 new Date ( ) . getFullYear ( ) -
8989 new Date ( this . born + '' ) . getFullYear ( )
@@ -93,7 +93,7 @@ let metadata = {
9393
9494// Define query using regular JavaScript syntax
9595//
96- let query = person => (
96+ const query = person => (
9797 person . name !== '' &&
9898 person . age > 25 &&
9999 person . city === 'Rome'
@@ -103,5 +103,5 @@ let query = person => (
103103assignMetadata ( data , metadata ) ;
104104
105105// Apply query to dataset
106- let res = data . filter ( query ) ;
106+ const res = data . filter ( query ) ;
107107console . dir ( res ) ;
0 commit comments