فهرست منبع

EAGLESIX-2651: Deps: add setNestedField to potentially fix DepsTracker inconsistency

Kyle P Davis 11 سال پیش
والد
کامیت
134c381455
1فایلهای تغییر یافته به همراه16 افزوده شده و 0 حذف شده
  1. 16 0
      lib/pipeline/Document.js

+ 16 - 0
lib/pipeline/Document.js

@@ -87,3 +87,19 @@ klass.clone = function(document){
 //	proto.addField = function addField(){ throw new Error("Instead of `Document#addField(key,val)` you should just use `obj[key] = val`"); }
 //	proto.setField = function addField(){ throw new Error("Instead of `Document#setField(key,val)` you should just use `obj[key] = val`"); }
 //  proto.getField = function getField(){ throw new Error("Instead of `Document#getField(key)` you should just use `var val = obj[key];`"); }
+
+// NOTE: from MutableDocument, implemented a bit differently here but should be same basic functionality
+klass.getNestedField = function getNestedField(obj, path) {
+	var paths = Array.isArray(path) ? path : path.split(".");
+	for (var i = 0, l = paths.length, o = obj; i < l && o instanceof Object; i++) {
+		o = o[paths[i]];
+	}
+	return o;
+};
+
+klass.setNestedField = function setNestedField(obj, path, val) {
+	var paths = Array.isArray(path) ? path : path.split("."),
+		key = paths.pop(),
+		parent = klass.getNestedField(obj, paths);
+	if (parent) parent[key] = val;
+};