50 lines
		
	
	
		
			1003 B
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
			
		
		
	
	
			50 lines
		
	
	
		
			1003 B
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
| import { Variable } from "./variable.js";
 | |
| 
 | |
| export class Context {
 | |
|     variables;
 | |
| 
 | |
|     constructor(vars) {
 | |
|         if(vars) {
 | |
|             this.variables = vars.map(v => new Variable(v.key, v.value));
 | |
|         } else {
 | |
|             this.variables = [];
 | |
|         }
 | |
|     }
 | |
| 
 | |
|     copy() {
 | |
|         return new Context(this.variables);
 | |
|     }
 | |
| 
 | |
|     hasVariable(key) {
 | |
|         return this.variables.some(
 | |
|             (v) => v.key === key
 | |
|         );
 | |
|     }
 | |
| 
 | |
|     get(key) {
 | |
|         const match = this.variables.find(
 | |
|             (v) => v.key === key
 | |
|         );
 | |
| 
 | |
|         return match?.value ?? "";
 | |
|     }
 | |
| 
 | |
|     set(key, value) {
 | |
|         const withoutOld = this.variables.filter(
 | |
|             (v) => v.key !== key
 | |
|         );
 | |
| 
 | |
|         this.variables = [
 | |
|             ...withoutOld,
 | |
|             new Variable(key, value)
 | |
|         ];
 | |
|     }
 | |
| 
 | |
|     mergeFrom(other) {
 | |
|         for(let i = 0; i < other.variables.length; i++) {
 | |
|             this.set(other.variables[i].key, other.variables[i].value);
 | |
|         }
 | |
| 
 | |
|         return this;
 | |
|     }
 | |
| } | 
