Skip to content

Commit f9be5b7

Browse files
authored
Merge pull request #34 from CaliStyle/upgrade-package
[ix] fix map!
2 parents 434c8d6 + f1f2902 commit f9be5b7

13 files changed

+108
-158
lines changed

ng-package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"$schema": "./node_modules/ng-packagr/ng-package.schema.json",
33
"name": "ng-engine",
4-
"version": "7.0.2",
4+
"version": "7.1.0",
55
"licensePath": "LICENSE",
66
"lib": {
77
"entryFile": "public_api.ts",

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ng-engine",
3-
"version": "7.0.2",
3+
"version": "7.1.0",
44
"license": "MIT",
55
"repository": "https://github.com/CaliStyle/NgEngine.git",
66
"keywords": [

src/app/ngEngine/ng-engine.config.ts

Lines changed: 42 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// import * as ES6Map from 'es6-map'
22
import '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'
55
import { 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-9a-z]+)$/)[1]
179115
const parent = key.replace(/\.[0-9a-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
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { NgEngineCore } from './ng-engine.core'
2+
3+
describe('NgEngineCore', () => {
4+
it('should exists', () => {
5+
expect(NgEngineCore).toBeTruthy()
6+
})
7+
})

src/app/ngEngine/ng-engine.core.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { merge, union, defaultsDeep, isArray, toArray, mergeWith } from 'lodash'
22
import { ConfigValueError } from './ng-engine.errors'
33

4-
export const Core = {
4+
export const NgEngineCore = {
55
// An Exception convenience
66
BreakException: {},
77

@@ -42,7 +42,7 @@ export const Core = {
4242
let stack = [[]]
4343

4444
try {
45-
return !!JSON.stringify(obj, Core.collector.bind(null, stack))
45+
return !!JSON.stringify(obj, NgEngineCore.collector.bind(null, stack))
4646
}
4747
catch (e) {
4848
if (e.message.indexOf('circular') !== -1) {
@@ -93,12 +93,12 @@ export const Core = {
9393
toReturn[`${k}.${i}`] = val
9494
})
9595
}
96-
else if (!Core.isNotCircular(v)) {
96+
else if (!NgEngineCore.isNotCircular(v)) {
9797
toReturn[k] = v
9898
}
9999
// If the value is a normal object, keep flattening
100100
else {
101-
const flatObject = Core.flattenTree(v)
101+
const flatObject = NgEngineCore.flattenTree(v)
102102
Object.keys(flatObject).forEach(flatKey => {
103103
toReturn[`${k}.${flatKey}`] = flatObject[flatKey]
104104
})
@@ -112,7 +112,7 @@ export const Core = {
112112
return toReturn
113113
}
114114
catch (err) {
115-
if (err !== Core.BreakException) {
115+
if (err !== NgEngineCore.BreakException) {
116116
throw new RangeError('Tree is circular and can not be resolved, check that there are no circular references in the config')
117117
}
118118
return toReturn
@@ -127,7 +127,7 @@ export const Core = {
127127
const envConfig = initialConfig.env && initialConfig.env[appEnv] || { }
128128

129129
const configTemplate = {
130-
resources: Core.initialResources(initialConfig),
130+
resources: NgEngineCore.initialResources(initialConfig),
131131
lockResources: false,
132132
main: {
133133
packs: [ ],

src/app/ngEngine/ng-engine.module.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Angular Core
1+
// Angular NgEngineCore
22
import {
33
NgModule
44
} from '@angular/core'

src/app/ngEngine/ng-engine.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Import Core
1+
// Import NgEngineCore
22
import { Inject, Injectable, InjectionToken } from '@angular/core'
33

44
// Config Class

src/app/packs/home/home.component.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,7 @@ <h2><a target="_blank" rel="noopener" href="https://github.com/angular/angular-c
3131
<h2><a target="_blank" rel="noopener" href="https://blog.angular.io/">Angular blog</a></h2>
3232
</li>
3333
</ul>
34+
35+
<pre>
36+
{{ all | json }}
37+
</pre>

src/app/packs/home/home.component.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import * as fromHome from './store/reducers'
1616
export class HomeComponent implements OnInit {
1717
public appTitle
1818
public homeState$: Observable<any>
19+
public all
1920

2021
constructor(
2122
private _ngEngine: NgEngineService,
@@ -26,6 +27,8 @@ export class HomeComponent implements OnInit {
2627
this._store.dispatch(new home.HelloWorldAction(title))
2728
this._store.dispatch(new home.fabrixAction(null))
2829
this.homeState$ = this._store.pipe(select(fromHome.getHomeState))
30+
31+
this.all = this._ngEngine.config.dehydrate()
2932
}
3033

3134
ngOnInit() {}

0 commit comments

Comments
 (0)