|
| 1 | +/** |
| 2 | +* Copyright 2012-2017, Plotly, Inc. |
| 3 | +* All rights reserved. |
| 4 | +* |
| 5 | +* This source code is licensed under the MIT license found in the |
| 6 | +* LICENSE file in the root directory of this source tree. |
| 7 | +*/ |
| 8 | + |
| 9 | +'use strict'; |
| 10 | + |
| 11 | +var nestedProperty = require('./nested_property'); |
| 12 | + |
| 13 | +var SIMPLE_PROPERTY_REGEX = /^\w*$/; |
| 14 | + |
| 15 | +// bitmask for deciding what's updated. Sometimes the name needs to be updated, |
| 16 | +// sometimes the value needs to be updated, and sometimes both do. This is just |
| 17 | +// a simple way to track what's updated such that it's a simple OR operation to |
| 18 | +// assimilate new updates. |
| 19 | +// |
| 20 | +// The only exception is the UNSET bit that tracks when we need to explicitly |
| 21 | +// unset and remove the property. This concrn arises because of the special |
| 22 | +// way in which nestedProperty handles null/undefined. When you specify `null`, |
| 23 | +// it prunes any unused items in the tree. I ran into some issues with it getting |
| 24 | +// null vs undefined confused, so UNSET is just a bit that forces the property |
| 25 | +// update to send `null`, removing the property explicitly rather than setting |
| 26 | +// it to undefined. |
| 27 | +var NONE = 0; |
| 28 | +var NAME = 1; |
| 29 | +var VALUE = 2; |
| 30 | +var BOTH = 3; |
| 31 | +var UNSET = 4; |
| 32 | + |
| 33 | +module.exports = function keyedContainer(baseObj, path, keyName, valueName) { |
| 34 | + keyName = keyName || 'name'; |
| 35 | + valueName = valueName || 'value'; |
| 36 | + var i, arr; |
| 37 | + var changeTypes = {}; |
| 38 | + |
| 39 | + if(path && path.length) { arr = nestedProperty(baseObj, path).get(); |
| 40 | + } else { |
| 41 | + arr = baseObj; |
| 42 | + } |
| 43 | + |
| 44 | + path = path || ''; |
| 45 | + arr = arr || []; |
| 46 | + |
| 47 | + // Construct an index: |
| 48 | + var indexLookup = {}; |
| 49 | + for(i = 0; i < arr.length; i++) { |
| 50 | + indexLookup[arr[i][keyName]] = i; |
| 51 | + } |
| 52 | + |
| 53 | + var isSimpleValueProp = SIMPLE_PROPERTY_REGEX.test(valueName); |
| 54 | + |
| 55 | + var obj = { |
| 56 | + // NB: this does not actually modify the baseObj |
| 57 | + set: function(name, value) { |
| 58 | + var changeType = value === null ? UNSET : NONE; |
| 59 | + |
| 60 | + var idx = indexLookup[name]; |
| 61 | + if(idx === undefined) { |
| 62 | + changeType = changeType | BOTH; |
| 63 | + idx = arr.length; |
| 64 | + indexLookup[name] = idx; |
| 65 | + } else if(value !== (isSimpleValueProp ? arr[idx][valueName] : nestedProperty(arr[idx], valueName).get())) { |
| 66 | + changeType = changeType | VALUE; |
| 67 | + } |
| 68 | + |
| 69 | + var newValue = arr[idx] = arr[idx] || {}; |
| 70 | + newValue[keyName] = name; |
| 71 | + |
| 72 | + if(isSimpleValueProp) { |
| 73 | + newValue[valueName] = value; |
| 74 | + } else { |
| 75 | + nestedProperty(newValue, valueName).set(value); |
| 76 | + } |
| 77 | + |
| 78 | + // If it's not an unset, force that bit to be unset. This is all related to the fact |
| 79 | + // that undefined and null are a bit specially implemented in nestedProperties. |
| 80 | + if(value !== null) { |
| 81 | + changeType = changeType & ~UNSET; |
| 82 | + } |
| 83 | + |
| 84 | + changeTypes[idx] = changeTypes[idx] | changeType; |
| 85 | + |
| 86 | + return obj; |
| 87 | + }, |
| 88 | + get: function(name) { |
| 89 | + var idx = indexLookup[name]; |
| 90 | + |
| 91 | + if(idx === undefined) { |
| 92 | + return undefined; |
| 93 | + } else if(isSimpleValueProp) { |
| 94 | + return arr[idx][valueName]; |
| 95 | + } else { |
| 96 | + return nestedProperty(arr[idx], valueName).get(); |
| 97 | + } |
| 98 | + }, |
| 99 | + rename: function(name, newName) { |
| 100 | + var idx = indexLookup[name]; |
| 101 | + |
| 102 | + if(idx === undefined) return obj; |
| 103 | + changeTypes[idx] = changeTypes[idx] | NAME; |
| 104 | + |
| 105 | + indexLookup[newName] = idx; |
| 106 | + delete indexLookup[name]; |
| 107 | + |
| 108 | + arr[idx][keyName] = newName; |
| 109 | + |
| 110 | + return obj; |
| 111 | + }, |
| 112 | + remove: function(name) { |
| 113 | + var idx = indexLookup[name]; |
| 114 | + |
| 115 | + if(idx === undefined) return obj; |
| 116 | + |
| 117 | + var object = arr[idx]; |
| 118 | + if(Object.keys(object).length > 2) { |
| 119 | + // This object contains more than just the key/value, so unset |
| 120 | + // the value without modifying the entry otherwise: |
| 121 | + changeTypes[idx] = changeTypes[idx] | VALUE; |
| 122 | + return obj.set(name, null); |
| 123 | + } |
| 124 | + |
| 125 | + if(isSimpleValueProp) { |
| 126 | + for(i = idx; i < arr.length; i++) { |
| 127 | + changeTypes[i] = changeTypes[i] | BOTH; |
| 128 | + } |
| 129 | + for(i = idx; i < arr.length; i++) { |
| 130 | + indexLookup[arr[i][keyName]]--; |
| 131 | + } |
| 132 | + arr.splice(idx, 1); |
| 133 | + delete(indexLookup[name]); |
| 134 | + } else { |
| 135 | + // Perform this update *strictly* so we can check whether the result's |
| 136 | + // been pruned. If so, it's a removal. If not, it's a value unset only. |
| 137 | + nestedProperty(object, valueName).set(null); |
| 138 | + |
| 139 | + // Now check if the top level nested property has any keys left. If so, |
| 140 | + // the object still has values so we only want to unset the key. If not, |
| 141 | + // the entire object can be removed since there's no other data. |
| 142 | + // var topLevelKeys = Object.keys(object[valueName.split('.')[0]] || []); |
| 143 | + |
| 144 | + changeTypes[idx] = changeTypes[idx] | VALUE | UNSET; |
| 145 | + } |
| 146 | + |
| 147 | + return obj; |
| 148 | + }, |
| 149 | + constructUpdate: function() { |
| 150 | + var astr, idx; |
| 151 | + var update = {}; |
| 152 | + var changed = Object.keys(changeTypes); |
| 153 | + for(var i = 0; i < changed.length; i++) { |
| 154 | + idx = changed[i]; |
| 155 | + astr = path + '[' + idx + ']'; |
| 156 | + if(arr[idx]) { |
| 157 | + if(changeTypes[idx] & NAME) { |
| 158 | + update[astr + '.' + keyName] = arr[idx][keyName]; |
| 159 | + } |
| 160 | + if(changeTypes[idx] & VALUE) { |
| 161 | + if(isSimpleValueProp) { |
| 162 | + update[astr + '.' + valueName] = (changeTypes[idx] & UNSET) ? null : arr[idx][valueName]; |
| 163 | + } else { |
| 164 | + update[astr + '.' + valueName] = (changeTypes[idx] & UNSET) ? null : nestedProperty(arr[idx], valueName).get(); |
| 165 | + } |
| 166 | + } |
| 167 | + } else { |
| 168 | + update[astr] = null; |
| 169 | + } |
| 170 | + } |
| 171 | + |
| 172 | + return update; |
| 173 | + } |
| 174 | + }; |
| 175 | + |
| 176 | + return obj; |
| 177 | +}; |
0 commit comments