2019-02-15 16:20:05 +01:00
|
|
|
/**
|
|
|
|
* @author d98762625 [d98762625@gmail.com]
|
|
|
|
* @copyright Crown Copyright 2018
|
|
|
|
* @license Apache-2.0
|
|
|
|
*/
|
|
|
|
|
|
|
|
import util from "util";
|
2019-07-09 13:23:59 +02:00
|
|
|
import Dish from "../core/Dish.mjs";
|
2019-02-15 16:20:05 +01:00
|
|
|
|
|
|
|
/**
|
2019-07-05 11:22:14 +02:00
|
|
|
* Subclass of Dish for use in the Node.js environment.
|
|
|
|
*
|
|
|
|
* Adds some helper functions and improves coercion for Node.js logging.
|
2019-02-15 16:20:05 +01:00
|
|
|
*/
|
|
|
|
class NodeDish extends Dish {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a Dish
|
|
|
|
* @param {any} inputOrDish - The dish input
|
|
|
|
* @param {String|Number} - The dish type, as enum or string
|
|
|
|
*/
|
|
|
|
constructor(inputOrDish=null, type=null) {
|
|
|
|
|
|
|
|
// Allow `fs` file input:
|
|
|
|
// Any node fs Buffers transformed to array buffer
|
2019-03-22 10:39:43 +01:00
|
|
|
// Use Array.from as Uint8Array doesnt pass instanceof Array test
|
2019-02-15 16:20:05 +01:00
|
|
|
if (Buffer.isBuffer(inputOrDish)) {
|
2019-03-22 10:39:43 +01:00
|
|
|
inputOrDish = Array.from(inputOrDish);
|
2019-03-20 18:46:01 +01:00
|
|
|
type = Dish.BYTE_ARRAY;
|
2019-02-15 16:20:05 +01:00
|
|
|
}
|
|
|
|
super(inputOrDish, type);
|
|
|
|
}
|
|
|
|
|
2019-03-01 17:56:14 +01:00
|
|
|
/**
|
|
|
|
* Apply the inputted operation to the dish.
|
|
|
|
*
|
|
|
|
* @param {WrappedOperation} operation the operation to perform
|
|
|
|
* @param {*} args - any arguments for the operation
|
|
|
|
* @returns {Dish} a new dish with the result of the operation.
|
|
|
|
*/
|
|
|
|
apply(operation, args=null) {
|
2019-03-20 18:46:01 +01:00
|
|
|
return operation(this, args);
|
2019-03-01 17:56:14 +01:00
|
|
|
}
|
|
|
|
|
2019-02-15 16:20:05 +01:00
|
|
|
/**
|
|
|
|
* alias for get
|
|
|
|
* @param args see get args
|
|
|
|
*/
|
|
|
|
to(...args) {
|
|
|
|
return this.get(...args);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Avoid coercion to a String primitive.
|
|
|
|
*/
|
|
|
|
toString() {
|
2019-03-20 12:57:47 +01:00
|
|
|
return this.presentAs(Dish.typeEnum("string"));
|
2019-02-15 16:20:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* What we want to log to the console.
|
|
|
|
*/
|
|
|
|
[util.inspect.custom](depth, options) {
|
2019-03-20 12:57:47 +01:00
|
|
|
return this.presentAs(Dish.typeEnum("string"));
|
2019-02-15 16:20:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Backwards compatibility for node v6
|
|
|
|
* Log only the value to the console in node.
|
|
|
|
*/
|
|
|
|
inspect() {
|
2019-03-20 12:57:47 +01:00
|
|
|
return this.presentAs(Dish.typeEnum("string"));
|
2019-02-15 16:20:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Avoid coercion to a Number primitive.
|
|
|
|
*/
|
|
|
|
valueOf() {
|
2019-03-20 12:57:47 +01:00
|
|
|
return this.presentAs(Dish.typeEnum("number"));
|
2019-02-15 16:20:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
export default NodeDish;
|