simple-squiggle

A restricted subset of Squiggle
Log | Files | Refs | README

CompatSource.js (1394B)


      1 /*
      2 	MIT License http://www.opensource.org/licenses/mit-license.php
      3 	Author Tobias Koppers @sokra
      4 */
      5 "use strict";
      6 
      7 const Source = require("./Source");
      8 
      9 class CompatSource extends Source {
     10 	static from(sourceLike) {
     11 		return sourceLike instanceof Source
     12 			? sourceLike
     13 			: new CompatSource(sourceLike);
     14 	}
     15 
     16 	constructor(sourceLike) {
     17 		super();
     18 		this._sourceLike = sourceLike;
     19 	}
     20 
     21 	source() {
     22 		return this._sourceLike.source();
     23 	}
     24 
     25 	buffer() {
     26 		if (typeof this._sourceLike.buffer === "function") {
     27 			return this._sourceLike.buffer();
     28 		}
     29 		return super.buffer();
     30 	}
     31 
     32 	size() {
     33 		if (typeof this._sourceLike.size === "function") {
     34 			return this._sourceLike.size();
     35 		}
     36 		return super.size();
     37 	}
     38 
     39 	map(options) {
     40 		if (typeof this._sourceLike.map === "function") {
     41 			return this._sourceLike.map(options);
     42 		}
     43 		return super.map(options);
     44 	}
     45 
     46 	sourceAndMap(options) {
     47 		if (typeof this._sourceLike.sourceAndMap === "function") {
     48 			return this._sourceLike.sourceAndMap(options);
     49 		}
     50 		return super.sourceAndMap(options);
     51 	}
     52 
     53 	updateHash(hash) {
     54 		if (typeof this._sourceLike.updateHash === "function") {
     55 			return this._sourceLike.updateHash(hash);
     56 		}
     57 		if (typeof this._sourceLike.map === "function") {
     58 			throw new Error(
     59 				"A Source-like object with a 'map' method must also provide an 'updateHash' method"
     60 			);
     61 		}
     62 		hash.update(this.buffer());
     63 	}
     64 }
     65 
     66 module.exports = CompatSource;