@travetto/schema
Advanced tools
Comparing version 0.0.12 to 0.0.13
@@ -26,3 +26,3 @@ { | ||
}, | ||
"version": "0.0.12" | ||
"version": "0.0.13" | ||
} |
@@ -6,2 +6,49 @@ import { SchemaRegistry } from '../service'; | ||
static expandPaths(obj: { [key: string]: any }) { | ||
const out: { [key: string]: any } = {}; | ||
for (const k of Object.keys(obj)) { | ||
const val = obj[k]; | ||
const parts = k.split('.'); | ||
const last = parts.pop()!; | ||
let sub = out; | ||
while (parts.length > 0) { | ||
const part = parts.shift()!; | ||
const arr = part.indexOf('[') > 0; | ||
const name = part.split(/[^A-Za-z_0-9]/)[0]; | ||
const idx = arr ? part.split(/[\[\]]/)[1] : ''; | ||
const key = arr ? (/^\d+$/.test(idx) ? parseInt(idx, 10) : (idx.trim() || undefined)) : undefined; | ||
if (!(name in sub)) { | ||
sub[name] = typeof key === 'number' ? [] : {}; | ||
} | ||
sub = sub[name]; | ||
if (idx && key !== undefined) { | ||
if (sub[key] === undefined) { | ||
sub[key] = {}; | ||
} | ||
sub = sub[key]; | ||
} | ||
} | ||
if (last.indexOf('[') < 0) { | ||
sub[last] = val; | ||
} else { | ||
const arr = last.indexOf('[') > 0; | ||
const name = last.split(/[^A-Za-z_0-9]/)[0]; | ||
const idx = arr ? last.split(/[\[\]]/)[1] : ''; | ||
let key = arr ? (/^\d+$/.test(idx) ? parseInt(idx, 10) : (idx.trim() || undefined)) : undefined; | ||
if (sub[name] === undefined) { | ||
sub[name] = (typeof key === 'string') ? {} : []; | ||
sub = sub[name]; | ||
if (key === undefined) { | ||
key = sub.length; | ||
} | ||
sub[key!] = val; | ||
} | ||
} | ||
} | ||
return out; | ||
} | ||
static coerceType<T>(type: Class<T>, val: any): T { | ||
@@ -8,0 +55,0 @@ if (val.constructor !== type) { |
@@ -131,2 +131,8 @@ import { Field, Url, SchemaBound, View, Required, Alias, BindUtil, Schema, SchemaRegistry } from '../src'; | ||
} | ||
@Test('Should handle aliases') | ||
validateExpand() { | ||
assert(BindUtil.expandPaths({ 'a.b.c[]': 20 }) === { a: { b: { c: [20] } } }); | ||
assert(BindUtil.expandPaths({ 'a.d[0].c': 20 }) === { a: { d: [{ c: 20 }] } }); | ||
} | ||
} |
33132
930