11// import * as ES6Map from 'es6-map'
22import 'core-js/es6/map'
3- import { IllegalAccessError , ConfigValueError } from './ng-engine.errors'
4- import { Core } from './ng-engine.core'
3+ import { IllegalAccessError } from './ng-engine.errors'
4+ import { NgEngineCore } from './ng-engine.core'
55import { merge , isArray , defaults , union } from 'lodash'
66
77// declare var ES6Map: ES6MapConstructor
@@ -43,102 +43,13 @@ const ConfigurationProxyHandler: ProxyHandler<NgEngineConfig> = {
4343/**
4444 * Extend map class for getter/setter tuple config
4545 */
46- export class NgEngineConfig extends Map < any , any > {
46+ export class NgEngineConfig {
4747 public immutable : boolean
4848 public env : { }
49-
50- // static create (array?: any[]): NgEngineConfig {
51- // const inst = new Map(array)
52- // inst['__proto__'] = NgEngineConfig.prototype
53- // return inst as NgEngineConfig
54- // }
55- //
56- // /**
57- // * Flattens configuration tree
58- // * Recursive
59- // */
60- // static flattenTree (tree = { }) {
61- // const toReturn: { [key: string]: any } = {}
62- // // Try to flatten and fail if unable to resolve circular object
63- // try {
64- // Object.entries(tree).forEach(([k, v]) => {
65- // // if (typeof v === 'object' && v !== null) {
66- // if (
67- // v !== null
68- // && v instanceof Object
69- // && typeof v !== 'function'
70- // ) {
71- // // If value is an array, flatten by index and don't try to flatten further
72- // // Configs with Array will throw a warning in a later version
73- // if (Array.isArray(v)) {
74- // v.forEach((val, i) => {
75- // toReturn[`${k}.${i}`] = val
76- // })
77- // }
78- // else if (!Core.isNotCircular(v)) {
79- // toReturn[k] = v
80- // }
81- // // If the value is a normal object, keep flattening
82- // else {
83- // const flatObject = NgEngineConfig.flattenTree(v)
84- // Object.keys(flatObject).forEach(flatKey => {
85- // toReturn[`${k}.${flatKey}`] = flatObject[flatKey]
86- // })
87- // }
88- // }
89- // // Other wise, the value is a function, string, or number etc and should stop flattening
90- // toReturn[k] = v
91- // })
92- //
93- // // Return the consturcted return object
94- // return toReturn
95- // }
96- // catch (err) {
97- // if (err !== Core.BreakException) {
98- // throw new RangeError('Tree is circular and can not be resolved, check that there are no circular references in the config')
99- // }
100- // return toReturn
101- // }
102- // }
103- //
104- // /**
105- // * Defines the initial api resources
106- // */
107- // static initialResources (tree, resources = []) {
108- // if (tree.hasOwnProperty('main') && tree.main.hasOwnProperty('resources')) {
109- // // Configs with Array will throw a warning in v2.0 and an error in v3.0
110- // if (!isArray(tree.main['resources'])) {
111- // throw new ConfigValueError('if set, main.resources must be an array')
112- // }
113- // return tree.main['resources']
114- // }
115- // else {
116- // return resources
117- // }
118- // }
119- //
120- // /**
121- // * Copy and merge the provided configuration into a new object, decorated with
122- // * necessary default and environment-specific values.
123- // */
124- // static buildConfig (initialConfig: {env?: {[key: string]: any}} = { }, appEnv?: string) {
125- // const envConfig = initialConfig.env && initialConfig.env[appEnv] || { }
126- //
127- // const configTemplate = {
128- // resources: NgEngineConfig.initialResources(initialConfig),
129- // lockResources: false,
130- // main: {
131- // packs: [ ],
132- // paths: {
133- // root: ''
134- // },
135- // freezeConfig: true,
136- // createPaths: true
137- // }
138- // }
139- //
140- // return merge(configTemplate, initialConfig, envConfig, { env: appEnv })
141- // }
49+ public map : Map < any , any >
50+ // public get: any
51+ // public entries: any
52+ // public has: any
14253
14354 constructor (
14455 configTree : { [ key : string ] : any } = { } ,
@@ -149,11 +60,12 @@ export class NgEngineConfig extends Map<any, any> {
14960 ) {
15061 // Constants for configuration
15162 // const config = NgEngineConfig.buildConfig(configTree, processEnv['APP_ENV'] || 'development')
152- const config = Core . buildConfig ( configTree , processEnv [ 'APP_ENV' ] || 'development' )
63+ const config = NgEngineCore . buildConfig ( configTree , processEnv [ 'APP_ENV' ] || 'development' )
15364 // const configEntries = Object.entries(NgEngineConfig.flattenTree(config))
154- const configEntries = Object . entries ( Core . flattenTree ( config ) )
65+ const configEntries = Object . entries ( NgEngineCore . flattenTree ( config ) )
15566 // Add to the map constructor
156- super ( configEntries )
67+ // super(configEntries)
68+ this . map = new Map ( configEntries )
15769
15870 // Initial values
15971 this . immutable = false
@@ -164,12 +76,36 @@ export class NgEngineConfig extends Map<any, any> {
16476 this . set = this . set . bind ( this )
16577 this . entries = this . entries . bind ( this )
16678 this . has = this . has . bind ( this )
79+ this . keys = this . keys . bind ( this )
80+ this . values = this . values . bind ( this )
81+ this . dehydrate = this . dehydrate . bind ( this )
16782
16883 // return this
16984 // Return Proxy
17085 return new Proxy ( this , ConfigurationProxyHandler )
17186 }
17287
88+ public get ( key ) {
89+ return this . map . get ( key )
90+ }
91+ public entries ( ) {
92+ return this . map . entries ( )
93+ }
94+ public has ( key ) {
95+ return this . map . has ( key )
96+ }
97+ public values ( ) {
98+ return this . map . values ( )
99+ }
100+ public keys ( ) {
101+ return this . map . keys ( )
102+ }
103+ public dehydrate ( ) {
104+ const obj = { }
105+ this . map . forEach ( ( v , k ) => { obj [ k ] = v } )
106+ return obj
107+ }
108+
173109 /**
174110 * Recursively sets the tree values on the config map
175111 */
@@ -178,14 +114,14 @@ export class NgEngineConfig extends Map<any, any> {
178114 const decedent = ( key ) . match ( / \. ( [ 0 - 9 a - z ] + ) $ / ) [ 1 ]
179115 const parent = key . replace ( / \. [ 0 - 9 a - z ] + $ / , '' )
180116 const proto = Array . isArray ( value ) ? [ ] : { }
181- const newParentValue = Core . defaultsDeep ( { [ decedent ] : value } , this . get ( parent ) || proto )
182- super . set ( key , value )
117+ const newParentValue = NgEngineCore . defaultsDeep ( { [ decedent ] : value } , this . get ( parent ) || proto )
118+ this . map . set ( key , value )
183119 // Recursively reverse flatten the set back up the tree
184120 return this . _reverseFlattenSet ( parent , newParentValue )
185121 }
186122 else {
187123 // This is as high as it goes
188- return super . set ( key , value )
124+ return this . map . set ( key , value )
189125 }
190126 }
191127 /**
@@ -199,10 +135,10 @@ export class NgEngineConfig extends Map<any, any> {
199135 && ! Array . isArray ( value )
200136 ) {
201137 // Flatten the new value
202- const configEntries = Object . entries ( Core . flattenTree ( { [ key ] : value } ) )
138+ const configEntries = Object . entries ( NgEngineCore . flattenTree ( { [ key ] : value } ) )
203139 // Set the flat values
204140 configEntries . forEach ( ( [ _key , _value ] ) => {
205- return super . set ( _key , _value )
141+ return this . map . set ( _key , _value )
206142 } )
207143 }
208144 // Reverse flatten up the tree
@@ -223,7 +159,7 @@ export class NgEngineConfig extends Map<any, any> {
223159 * Merge tree into this configuration if allowed. Return overwritten keys
224160 */
225161 merge ( configTree : { [ key : string ] : any } , configAction = 'hold' ) : { hasKey : boolean , key : any } [ ] {
226- const configEntries = Object . entries ( Core . flattenTree ( configTree ) )
162+ const configEntries = Object . entries ( NgEngineCore . flattenTree ( configTree ) )
227163 return configEntries . map ( ( [ key , value ] ) => {
228164 const hasKey = this . has ( key )
229165 // If the key has never been set, it is added to the config
@@ -252,7 +188,7 @@ export class NgEngineConfig extends Map<any, any> {
252188 // Do Nothing
253189 }
254190 else {
255- this . set ( key , Core . defaultsDeep ( this . get ( key ) , value ) )
191+ this . set ( key , NgEngineCore . defaultsDeep ( this . get ( key ) , value ) )
256192 }
257193 }
258194 // If configAction is replaceable, and the key already exists, it's ignored completely
0 commit comments