MCPcopy
hub / github.com/tensorflow/playground / deserializeState

Method deserializeState

src/state.ts:170–239  ·  view source on GitHub ↗

* Deserializes the state from the url hash.

()

Source from the content-addressed store, hash-verified

168 * Deserializes the state from the url hash.
169 */
170 static deserializeState(): State {
171 let map: {[key: string]: string} = {};
172 for (let keyvalue of window.location.hash.slice(1).split("&")) {
173 let [name, value] = keyvalue.split("=");
174 map[name] = value;
175 }
176 let state = new State();
177
178 function hasKey(name: string): boolean {
179 return name in map && map[name] != null && map[name].trim() !== "";
180 }
181
182 function parseArray(value: string): string[] {
183 return value.trim() === "" ? [] : value.split(",");
184 }
185
186 // Deserialize regular properties.
187 State.PROPS.forEach(({name, type, keyMap}) => {
188 switch (type) {
189 case Type.OBJECT:
190 if (keyMap == null) {
191 throw Error("A key-value map must be provided for state " +
192 "variables of type Object");
193 }
194 if (hasKey(name) && map[name] in keyMap) {
195 state[name] = keyMap[map[name]];
196 }
197 break;
198 case Type.NUMBER:
199 if (hasKey(name)) {
200 // The + operator is for converting a string to a number.
201 state[name] = +map[name];
202 }
203 break;
204 case Type.STRING:
205 if (hasKey(name)) {
206 state[name] = map[name];
207 }
208 break;
209 case Type.BOOLEAN:
210 if (hasKey(name)) {
211 state[name] = (map[name] === "false" ? false : true);
212 }
213 break;
214 case Type.ARRAY_NUMBER:
215 if (name in map) {
216 state[name] = parseArray(map[name]).map(Number);
217 }
218 break;
219 case Type.ARRAY_STRING:
220 if (name in map) {
221 state[name] = parseArray(map[name]);
222 }
223 break;
224 default:
225 throw Error("Encountered an unknown type for a state variable");
226 }
227 });

Callers 1

playground.tsFile · 0.80

Calls 1

getHidePropsFunction · 0.85

Tested by

no test coverage detected