Browse Source

Better naming of library functions

Richard Knight 6 years ago
parent
commit
d76415a195
1 changed files with 17 additions and 15 deletions
  1. 17 15
      src/app/dynaform/libs/index.ts

+ 17 - 15
src/app/dynaform/libs/index.ts

@@ -7,7 +7,10 @@ import * as fmdModels from './../models';
  *
  * Exports
  * -------
- * buildFormGroup(metadata)  - builds FormGroups from metdata, recursively if necessary
+ * autoMeta(model) - generate basic metadata from a raw or mapped model, recursively if necessary
+ * combineExtraMeta(metadata, extraMeta) - combine extra metadata into metatdata, lazyly and recursively
+ * buildModedMeta(metadata) - use field metadta models to fill out metadata
+ * buildFormGroup(metadata) - builds FormGroups from modelled metdata, recursively if necessary
  *
  * Variable names
  * --------------
@@ -28,8 +31,11 @@ const addProp = (obj, key, val) => { obj[key] = val; return obj; };
 // Helper function to distinguish group from field
 const isGroup = metaFoG => metaFoG.meta;
 
+// ---------------------------------------------------------------------------------------------------------------------
 // Add Missing Names
 // Helper function to add any missing 'name' properties to Fields and Groups using property's key, recursively
+// MAYBE NOT NEEDED?
+
 const addNameIfMissing = (metaFoG, key) => metaFoG.name ? metaFoG : addProp(metaFoG, 'name', key);
 const addNameToSelfAndChildren = ( [key, metaFoG] ) => {
 	metaFoG = addNameIfMissing(metaFoG, key);
@@ -49,28 +55,24 @@ const isScalar = val => typeof val === 'boolean' || typeof val === 'number' || t
 
 const keyValPairToMeta = (val, key) => ({ name: key, [isScalar(val) ? 'value' : 'meta']: val });
 const keyValPairToMetaRecursive = ( [key, val] ) => {
-	const innerVal = isScalar(val) ? val : rawModelAutoMeta(val);
+	const innerVal = isScalar(val) ? val : autoMeta(val);
 	const metaVal = keyValPairToMeta(innerVal, key);
 	return [key, metaVal];
 };
-const rawModelAutoMeta = model => Object.entries(model)
+const autoMeta = model => Object.entries(model)
 	.map(keyValPairToMetaRecursive)
 	.reduce((res, [key, val]) => addProp(res, key, val), {});
 
-
-// If value is complex, should we make name & meta, not name & value,
-// as form fields can't generally express complex values
-
 // ---------------------------------------------------------------------------------------------------------------------
 // Combine automatically generated metadata with overrides
 
 const combineMetaForField = (metaF, metaFextra) => Object.assign(metaF, metaFextra);
 const combineExtraMeta = (metaG, metaExtra) => {
-	let combinedMeta = {};
+	const combinedMeta = {};
 	Object.entries(metaExtra).forEach(([key, val]) => {
 		if (typeof metaG[key] === 'object') {
-			combinedMeta[key] = val.meta ?
-				combineExtraMeta(metaG[key].meta, val.meta) :
+			combinedMeta[key] = (<any>val).meta ?
+				combineExtraMeta(metaG[key].meta, (<any>val).meta) :
 				combineMetaForField(metaG[key], val);
 		}
 	});
@@ -78,7 +80,7 @@ const combineExtraMeta = (metaG, metaExtra) => {
 };
 
 // ---------------------------------------------------------------------------------------------------------------------
-// Form Metadata Factory
+// Build Modelled Metadata - Form Metadata Factory - UNDERWAY
 
 const buildModeledField = metaF => {
 	// TODO: Add validity check
@@ -90,9 +92,9 @@ const buildModeledField = metaF => {
 const buildModeledGroupMember = metaFoG => isGroup(metaFoG) ? _buildModeledGroup(metaFoG.meta) : buildModeledField(metaFoG);
 
 // Build Form Group
-const buildModeledGroupReducerIteree = (res, metaFoG) => Object.assign(res, { [metaFoG.name]: buildFormGroupMember(metaFoG) });
+const buildModeledGroupReducerIteree = (res, metaFoG) => Object.assign(res, { [metaFoG.name]: buildModeledGroupMember(metaFoG) });
 const _buildModeledGroup = metaG => reduce(buildModeledGroupReducerIteree, {}, metaG);
-const buildModeledGroup = metaG => _buildModeledGroup(addMissingNames(metaG));
+const buildModedMeta = metaG => _buildModeledGroup(metaG);
 
 // ---------------------------------------------------------------------------------------------------------------------
 // Functions which build FormControls and FormGroups
@@ -113,9 +115,9 @@ const buildFormGroupMember = metaFoG => isGroup(metaFoG) ? _buildFormGroup(metaF
 // Build Form Group
 const buildFormGroupReducerIteree = (res, metaFoG) => Object.assign(res, { [metaFoG.name]: buildFormGroupMember(metaFoG) });
 const _buildFormGroup = metaG => fb.group(reduce(buildFormGroupReducerIteree, {}, metaG));
-const buildFormGroup = metaG => _buildFormGroup(addMissingNames(metaG));
+const buildFormGroup = metaG => _buildFormGroup(metaG);
 
 // ---------------------------------------------------------------------------------------------------------------------
 // Exports
 
-export { buildFormGroup };
+export { autoMeta, combineExtraMeta, buildModedMeta, buildFormGroup };