Skip to content

Commit 1b0634d

Browse files
committed
feat(add first pass of yaml support): yAML can now be imported and exported via the static ObjectMap
affects: @nnode/objnav BREAKING CHANGE: materialize no longer public
1 parent a96b739 commit 1b0634d

4 files changed

Lines changed: 92 additions & 31 deletions

File tree

‎packages/objnav/package-lock.json‎

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎packages/objnav/package.json‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
},
2424
"types": "./lib/index.d.ts",
2525
"dependencies": {
26-
"@nnode/common": "^0.5.7"
26+
"@nnode/common": "^0.5.7",
27+
"yaml": "^1.10.0"
2728
}
2829
}

‎packages/objnav/specs/ObjectMap.spec.ts‎

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,29 @@ describe('when using ObjectMap', () => {
124124

125125
it('should materialize object', () => {
126126
const mapper = new ObjectMap(TEST_OBJECT)
127-
const materialized = mapper.materialze()
128-
expect(materialized).to.deep.equal(TEST_OBJECT)
127+
expect(mapper.toObject()).to.deep.equal(TEST_OBJECT)
128+
})
129+
130+
it('should parse yaml', () => {
131+
const TEST_YAML = `
132+
logins:
133+
- login: mpham
134+
password: passwordsexlove
135+
- login: mike.pham
136+
password: passwordsexlove
137+
user:
138+
firstname: mike
139+
lastname: pham
140+
address:
141+
address1: 4617 Some Dream Boulevard
142+
address2: Suite 101
143+
city: Middleton
144+
state: Florida
145+
zip:
146+
postal: 34243
147+
`
148+
const mapper = ObjectMap.fromYaml(TEST_YAML)
149+
const source = new ObjectMap(TEST_OBJECT, { include: { dates: false } })
150+
expect(mapper.toObject()).to.deep.equal(source.toObject())
129151
})
130152
})

‎packages/objnav/src/ObjectMap.ts‎

Lines changed: 53 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import YAML from 'yaml'
2+
13
import { types } from 'util'
24
import { DeepPartial, Merge } from '@nnode/common'
35

@@ -35,8 +37,22 @@ export class ObjectMap {
3537
Object.keys(instance).forEach((name) => this.map(this.objmap, name))
3638
}
3739

40+
static fromJson<T extends any>(source: T): ObjectMap {
41+
return new ObjectMap(source, { include: { functions: false } })
42+
}
43+
44+
static fromJsonString<T extends any>(json: string): ObjectMap {
45+
const source: T = JSON.parse(json)
46+
return ObjectMap.fromJson(source)
47+
}
48+
49+
static fromYaml<T extends any>(yaml: string): ObjectMap {
50+
const source: T = YAML.parse(yaml)
51+
return new ObjectMap(source, { include: { dates: false } })
52+
}
53+
3854
get nodes(): ObjectMapValue[] {
39-
return this.select((objmap) => objmap.properties.length === 0)
55+
return this.select()
4056
}
4157

4258
get root(): ObjectMapValue {
@@ -55,33 +71,6 @@ export class ObjectMap {
5571
return this.get(path).value
5672
}
5773

58-
materialze(): any {
59-
const conjure = (objmap: ObjectMapValue, target: any): any => {
60-
if (this.include(objmap.type) === false) {
61-
return target
62-
}
63-
64-
return objmap.properties
65-
.filter((property) => this.include(property.type))
66-
.reduce<any>((result, property) => {
67-
if (objmap.type === 'array') {
68-
const value = result[property.name] || []
69-
result[property.name] = value.push(property.value)
70-
} else if (property.type === 'object') {
71-
result[property.name] = conjure(property, {})
72-
} else {
73-
result[property.name] = property.value
74-
}
75-
76-
return result
77-
}, target)
78-
}
79-
80-
const material = {}
81-
conjure(this.root, material)
82-
return material
83-
}
84-
8574
paths(): string[] {
8675
return this.select().map((objmap) => this.pathstr(objmap))
8776
}
@@ -108,6 +97,15 @@ export class ObjectMap {
10897
return objmap
10998
}
11099

100+
toObject(): any {
101+
return this.materialze()
102+
}
103+
104+
toYaml(): string {
105+
const obj = this.toObject()
106+
return YAML.stringify(obj)
107+
}
108+
111109
private include(type: string): boolean {
112110
switch (type) {
113111
case 'array':
@@ -159,6 +157,33 @@ export class ObjectMap {
159157
return objvalue
160158
}
161159

160+
private materialze(): any {
161+
const conjure = (objmap: ObjectMapValue, target: any): any => {
162+
if (this.include(objmap.type) === false) {
163+
return target
164+
}
165+
166+
return objmap.properties
167+
.filter((property) => this.include(property.type))
168+
.reduce<any>((result, property) => {
169+
if (objmap.type === 'array') {
170+
const value = result[property.name] || []
171+
result[property.name] = value.push(property.value)
172+
} else if (property.type === 'object') {
173+
result[property.name] = conjure(property, {})
174+
} else {
175+
result[property.name] = property.value
176+
}
177+
178+
return result
179+
}, target)
180+
}
181+
182+
const material = {}
183+
conjure(this.root, material)
184+
return material
185+
}
186+
162187
private node(objmap: ObjectMapValue, property: string): ObjectMapValue {
163188
return objmap.properties.reduce((result, current) => (current.name === property ? current : result), objmap)
164189
}

0 commit comments

Comments
 (0)