-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from tyler-johnson/2.0
2.0
- Loading branch information
Showing
15 changed files
with
626 additions
and
325 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { Record } from "immutable"; | ||
import toPath from "lodash.topath"; | ||
import Schema from "./schema"; | ||
import Field from "./field"; | ||
|
||
const DEFAULTS = { | ||
root: null, | ||
schema: null | ||
}; | ||
|
||
export default class Blueprint extends Record(DEFAULTS) { | ||
static create(props = {}) { | ||
if (Blueprint.isBlueprint(props)) { | ||
return props; | ||
} | ||
|
||
props.root = Field.create(props.root); | ||
props.schema = Schema.create(props.schema); | ||
|
||
return new Blueprint(props).normalize(); | ||
} | ||
|
||
static isBlueprint(b) { | ||
return b instanceof Blueprint; | ||
} | ||
|
||
static kind() { | ||
return "blueprint"; | ||
} | ||
|
||
getField(key) { | ||
const path = toPath(key); | ||
let field = this.root; | ||
|
||
while (path.length && field) { | ||
field = field.getChildField(path.shift()); | ||
} | ||
|
||
return field; | ||
} | ||
|
||
transform(value) { | ||
return this.schema.transform(value, this.root); | ||
} | ||
|
||
normalize() { | ||
return this.merge({ | ||
root: this.schema.normalize(this.root) | ||
}); | ||
} | ||
|
||
join(...blueprints) { | ||
const fields = [this].concat(blueprints).map(b => b.root); | ||
const root = this.schema.join(...fields); | ||
return this.merge({ root }); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { Record, Map, List } from "immutable"; | ||
|
||
const DEFAULTS = { | ||
key: null, | ||
type: null, | ||
children: null, | ||
props: null | ||
}; | ||
|
||
export default class Field extends Record(DEFAULTS) { | ||
static create(field = {}) { | ||
if (Field.isField(field)) { | ||
return field; | ||
} | ||
|
||
if (typeof field !== "object" || field == null) { | ||
throw new Error("Expecting object for field."); | ||
} | ||
|
||
let { | ||
key=null, | ||
type=null, | ||
children, | ||
...props | ||
} = field; | ||
|
||
children = Field.createList(children); | ||
props = Map.isMap(props) ? props : Map(props); | ||
|
||
return new Field({ key, type, props, children }); | ||
} | ||
|
||
static createList(fields) { | ||
if (List.isList(fields)) return fields; | ||
|
||
if (Array.isArray(fields)) { | ||
fields = fields.map(Field.create); | ||
} else if (typeof fields === "object" && fields != null) { | ||
fields = Object.keys(fields).map(key => { | ||
return Field.create({ ...fields[key], key }); | ||
}); | ||
} | ||
|
||
return List(fields); | ||
} | ||
|
||
static isField(b) { | ||
return b instanceof Field; | ||
} | ||
|
||
static kind() { | ||
return "field"; | ||
} | ||
|
||
getChildField(key) { | ||
return this.children.find(o => o.key === key); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,94 +1,16 @@ | ||
import {assign,omit,has,clone,reduce,assignWith} from "lodash"; | ||
|
||
// throw an error if blueprint is not valid | ||
export function validate(bp) { | ||
if (typeof bp !== "object" || bp == null) { | ||
throw new Error("Expecting object for blueprint."); | ||
} | ||
|
||
for (let section_key in bp) { | ||
if (!has(bp, section_key)) continue; | ||
let section = bp[section_key]; | ||
if (!validSection(section)) { | ||
throw new Error(`Invalid blueprint section '${section_key}'.`); | ||
} | ||
|
||
let options = section.options; | ||
for (let option_key in options) { | ||
if (!has(options, option_key)) continue; | ||
if (!validOption(options[option_key])) { | ||
throw new Error(`Invalid blueprint option '${section_key}.${option_key}'.`); | ||
} | ||
} | ||
} | ||
} | ||
|
||
// returns boolean for if section object is valid | ||
export function validSection(section) { | ||
return (typeof section === "object" && section != null) && | ||
(typeof section.options === "object" && section.options != null); | ||
} | ||
|
||
// returns boolean for if section option object is valid | ||
export function validOption(option) { | ||
return (typeof option === "object" && option != null) && | ||
(typeof option.type === "string" && option.type); | ||
} | ||
|
||
// merge several blueprints together | ||
export function merge(...args) { | ||
return args.reduce((m, obj) => { | ||
if (!obj) return m; | ||
|
||
for (let section_key in obj) { | ||
if (!has(obj, section_key)) continue; | ||
let section = obj[section_key]; | ||
if (!validSection(section)) continue; | ||
|
||
if (!has(m, section_key)) m[section_key] = { options: {} }; | ||
assign(m[section_key], omit(section, "options")); | ||
let options = section.options; | ||
|
||
for (let option_key in options) { | ||
if (!has(options, option_key)) continue; | ||
let option = options[option_key]; | ||
if (!validOption(option)) continue; | ||
|
||
m[section_key].options[option_key] = clone(option); | ||
} | ||
} | ||
|
||
return m; | ||
},{}); | ||
} | ||
|
||
// extract default values from a blueprint | ||
export function defaults(bp={}) { | ||
return reduce(bp, (m, s, k) => { | ||
m[k] = reduce(s.options, (m, o, k) => { | ||
m[k] = o.default; | ||
return m; | ||
}, {}); | ||
return m; | ||
}, {}); | ||
} | ||
|
||
// apply blueprint defaults onto an object | ||
export function applyDefaults(bp, obj) { | ||
let defaultOpts = defaults(bp); | ||
|
||
for (let k in defaultOpts) { | ||
if (!has(defaultOpts, k) || (obj[k] && !has(obj, k))) continue; | ||
|
||
if (obj[k] == null) obj[k] = {}; | ||
if (typeof obj[k] !== "object") continue; | ||
|
||
assignWith(obj[k], defaultOpts[k], (cur, def) => { | ||
return (typeof def !== "undefined") && | ||
(typeof cur === "undefined" || cur === "") ? | ||
def : cur; | ||
}); | ||
} | ||
|
||
return obj; | ||
import Blueprint from "./blueprint"; | ||
import Schema, {defaultSchema} from "./schema"; | ||
import Field from "./field"; | ||
import * as rules from "./rules/index"; | ||
|
||
export { | ||
Blueprint, | ||
Schema, | ||
Field, | ||
defaultSchema, | ||
rules | ||
}; | ||
|
||
export default function(field, schema=defaultSchema) { | ||
return Blueprint.create({ root: field, schema }); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// rule for defaults | ||
|
||
function match(field) { | ||
return field.props.has("default"); | ||
} | ||
|
||
function transform(value, field) { | ||
const def = field.props.get("default"); | ||
|
||
return (typeof def !== "undefined") && | ||
(typeof value === "undefined" || value === "") ? | ||
def : value; | ||
} | ||
|
||
export default { | ||
match, | ||
transform | ||
}; |
Oops, something went wrong.